Wednesday, 2 September 2026

Getting Started with the Geometric Point Type in ClickHouse

  

ClickHouse, a high-performance columnar OLAP database, supports various data types to handle different kinds of data efficiently. One of the lesser-known but powerful features is its support for geometric data types, such as the Point type, which is especially useful for geospatial analysis and location-based queries. In this post, I am going to explain the Point type, show you how it’s represented, and demonstrate how to use it in practice.

 

What is the Point Type in ClickHouse?

The Point type in ClickHouse is used to represent a two-dimensional point in space. Internally, it is stored as a Tuple(Float64, Float64), where the two floating-point numbers represent the X and Y coordinates respectively.

 

This structure makes it easy to store, query, and manipulate spatial data using familiar SQL syntax and ClickHouse functions.

 

Since Point is just a wrapper over Tuple(Float64, Float64), it's worth understanding how tuples behave in ClickHouse:

 

·      A Tuple can contain multiple elements of different types.

·      Tuples can be used for  column grouping, especially useful with IN expressions and lambda functions.

·      When used in SELECT queries, tuple values are shown in parentheses, comma-separated.

·      In JSON-based formats, tuples are output as arrays in square brackets.

 

So, a Point is functionally equivalent to a tuple with two Float64 values, but it gains semantic meaning as a spatial data type.

 

Let's take a real-world example using the Point type in ClickHouse, with DDLs (table creation), sample inserts, and selects. These scenarios show how you might use Point for things like store locations, user check-ins, vehicle tracking, or IoT devices.

 

Example 1: Track the store locations.

CREATE DATABASE IF NOT EXISTS demo_db;

CREATE TABLE demo_db.store_locations (
    store_id UInt32,
    store_name String,
    location Point
) ENGINE = MergeTree()
ORDER BY store_id;

INSERT INTO demo_db.store_locations VALUES
(1, 'Downtown', (40.7128, -74.0060)),
(2, 'Target Uptown', (34.0522, -118.2437)),
(3, 'Best Buy Central', (41.8781, -87.6298));

   

Let’s print the latitude and longitude locations of the stores.

 

krishna :) SELECT store_name, location, location.1 AS latitude, location.2 AS longitude
FROM demo_db.store_locations;

SELECT
    store_name,
    location,
    location.1 AS latitude,
    location.2 AS longitude
FROM demo_db.store_locations

Query id: 3046d365-669d-4cc3-a742-df414fa61f87

   ┌─store_name───────┬─location────────────┬─latitude─┬─longitude─┐
1. │ Downtown         │ (40.7128,-74.006)   │  40.7128 │   -74.006 │
2. │ Target Uptown    │ (34.0522,-118.2437) │  34.0522 │ -118.2437 │
3. │ Best Buy Central │ (41.8781,-87.6298)  │  41.8781 │  -87.6298 │
   └──────────────────┴─────────────────────┴──────────┴───────────┘

3 rows in set. Elapsed: 0.004 sec.

   

Example 2: User Check-ins (Social App)

 

CREATE TABLE demo_db.user_checkins (
    user_id UInt64,
    username String,
    checkin_time DateTime,
    location Point
) ENGINE = MergeTree()
ORDER BY (user_id, checkin_time);

INSERT INTO demo_db.user_checkins VALUES
(101, 'ram', now(), (37.7749, -122.4194)),
(102, 'krishna', now(), (47.6062, -122.3321)),
(103, 'chamu', now(), (29.7604, -95.3698));

   

This query filters users who checked in somewhere between given latitude and longitude.

 

SELECT username, checkin_time, location.1 AS lat, location.2 AS lon
FROM demo_db.user_checkins
WHERE location.1 > 30 AND location.2 < -90;

krishna :) SELECT username, checkin_time, location.1 AS lat, location.2 AS lon
FROM demo_db.user_checkins
WHERE location.1 > 30 AND location.2 < -90;

SELECT
    username,
    checkin_time,
    location.1 AS lat,
    location.2 AS lon
FROM demo_db.user_checkins
WHERE ((location.1) > 30) AND ((location.2) < -90)

Query id: 2e2e73f4-1b27-462d-916c-1f0d5388680d

   ┌─username─┬────────checkin_time─┬─────lat─┬───────lon─┐
1. │ ram      │ 2025-05-18 21:37:12 │ 37.7749 │ -122.4194 │
2. │ krishna  │ 2025-05-18 21:37:12 │ 47.6062 │ -122.3321 │
   └──────────┴─────────────────────┴─────────┴───────────┘

2 rows in set. Elapsed: 0.007 sec.

 

 

 

Previous                                                    Next                                                    Home

Boost ClickHouse Query Performance Using LowCardinality Columns

  

In large-scale analytics, repeated string values (like country names, user types, or statuses) appear in millions of rows. Storing and querying such repeated values as raw strings is inefficient. ClickHouse offers the LowCardinality type to optimize performance in such cases through dictionary encoding.

 

What is LowCardinality(T)?

LowCardinality is a wrapper around existing data types like String, Int, Date, etc. It stores distinct values once in a dictionary and replaces actual data with integer references (indexes to the dictionary). This can result in:

 

·      Smaller disk usage

·      Faster filtering, joins, and aggregations

 

Syntax

LowCardinality(data_type)

   

Following are the supported types for LowCardinality.

·      String, FixedString

·      Date, DateTime

·      All numeric types except Decimal

 

Let’s take a real world example and try to understand this. Suppose you are storing 1 million user records in ClickHouse, each with a country field. There are only 200 distinct countries worldwide, and your data includes country values like "USA", "India", "Germany", etc.

 

CREATE TABLE demo_db.users (
    user_id UInt32,
    country LowCardinality(String)
) ENGINE = MergeTree()
ORDER BY user_id;

   

Above snippet tells ClickHouse to:

·      Store user_id as-is.

·      Store country using dictionary encoding via LowCardinality(String).

 

Without LowCardinality: Raw String Storage

·      Each country string (say "India") appears repeatedly across rows.

·      "India" = 5 characters = ~5 bytes per row.

·      With 1 million users, 5 bytes × 1,000,000 = ~5 MB just for "India" (even more with variable length).

·      Multiply that across other countries, storage cost grows linearly.

 

With LowCardinality: Dictionary Encoding

ClickHouse builds a dictionary like below.

 

·      0 for "USA"

·      1 for "India"

·      2 for "Germany"

·      ...

·      199 for "Iceland"

 

Instead of storing full strings in each row, it stores an integer index (1 byte or 2 bytes per row depending on size of dictionary). With this the storage is

 

1M rows × 1 byte per encoded index = ~1 MB + (Some internal overhead).

 

Follow below step by step procedure to understand this better with real time example.

 

CREATE DATABASE IF NOT EXISTS demo_db;

CREATE TABLE demo_db.users (
    user_id UInt32,
    country LowCardinality(String)
) ENGINE = MergeTree()
ORDER BY user_id;

INSERT INTO demo_db.users VALUES
    (1, 'USA'),
    (2, 'India'),
    (3, 'USA'),
    (4, 'Germany'),
    (5, 'India');

krishna :) SELECT * FROM demo_db.users;

SELECT *
FROM demo_db.users

Query id: 77f37210-a58d-4c62-a38c-6579b86ab049

   ┌─user_id─┬─country─┐
1. │       1 │ USA     │
2. │       2 │ India   │
3. │       3 │ USA     │
4. │       4 │ Germany │
5. │       5 │ India   │
   └─────────┴─────────┘

5 rows in set. Elapsed: 0.003 sec.

   

Use LowCardinality with filters, joins, or groupings on string columns to drastically cut query time, but only if the number of unique values is reasonably low.


  

Previous                                                    Next                                                    Home

Understanding Enum8 and Enum16 in ClickHouse with Real-World Examples

  

ClickHouse offers a compact and efficient way to store categorical string data using the Enum type. Instead of storing strings directly, ClickHouse maps them to integer values using Enum8 or Enum16 for optimizing storage and speeding up operations such as GROUP BY, ORDER BY, and comparisons.

 

1. What is Enum in ClickHouse?

An Enum is an enumerated type that allows you to store a set of predefined string values as compact integers, which helps in optimizing both storage and query performance.

 

ClickHouse supports:

·      Enum8: Up to 256 values (range: -128 to 127)

·      Enum16: Up to 65,536 values (range: -32,768 to 32,767)

 

Behind the scenes, Enums are stored as their corresponding integers (Int8 or Int16), but ClickHouse lets you interact with them using their string names.

 

2. Rules and Behavior

·      Each string is mapped to a unique integer in the allowed range.

·      The string and number pairs must be unique.

·      NULL is not allowed directly but can be used via Nullable(Enum8) or Nullable(Enum16) type.

·      Default value is the smallest numeric value.

 

·      During:

·      Text Reads: Input must match a defined Enum string. Invalid strings throw an error.

·      Text Writes: Output shows the string name.

·      When reading and writing in binary form, it works the same way as for Int8 and Int16 data types.

·      Enums compare and sort numerically.

·      Cannot perform arithmetic or string concatenation directly on Enums.

·      You can convert Enum to a string using toString() and to numbers using toInt8() or similar functions.

·      ALTER TABLE supports:

o   Adding/removing values (with care).

o   Changing between Enum8 <--> Enum16.

 

3. Example of Enum8

Imagine a typical e-commerce system. Every time someone places an order you need to store order_id, customer_id and status.

 

Column

What it holds

Why it’s that type

order_id

A unique, sequential ID for each order

UInt32 is plenty (up to 4 billion) and tiny on disk.

customer_id

Which customer placed the order

Also a compact UInt32; matches a primary-key in a customers table.

status

Where the order sits in your fulfilment pipeline

Enum8 keeps 5 human-readable states ('Pending', 'Processing', etc.) but stores them as one-byte integers (1-5).

 

CREATE TABLE demo_db.orders (
    order_id UInt32,
    customer_id UInt32,
    status Enum8(
        'Pending' = 1,
        'Processing' = 2,
        'Shipped' = 3,
        'Delivered' = 4,
        'Cancelled' = 5
    )
) ENGINE = MergeTree()
ORDER BY order_id;

INSERT INTO demo_db.orders VALUES
    (1001, 501, 'Processing'),
    (1001, 501, 'Shipped'),
    (1002, 502, 'Cancelled'),
    (1003, 503, 'Processing'),
    (1003, 503, 'Shipped'),
    (1003, 503, 'Delivered'),
    (1004, 501, 'Pending'),
    (1005, 502, 'Pending'),
    (1006, 503, 'Pending');

krishna :) SELECT * FROM demo_db.orders;

SELECT *
FROM demo_db.orders

Query id: 48f51cb1-8cbe-4380-acb5-7c3956b0d470

   ┌─order_id─┬─customer_id─┬─status─────┐
1. │     1001 │         501 │ Processing │
2. │     1001 │         501 │ Shipped    │
3. │     1002 │         502 │ Cancelled  │
4. │     1003 │         503 │ Processing │
5. │     1003 │         503 │ Shipped    │
6. │     1003 │         503 │ Delivered  │
7. │     1004 │         501 │ Pending    │
8. │     1005 │         502 │ Pending    │
9. │     1006 │         503 │ Pending    │
   └──────────┴─────────────┴────────────┘

9 rows in set. Elapsed: 0.017 sec.

   

3.1 Querying with enum string

 

SELECT *
FROM demo_db.orders
WHERE status = 'Shipped';

krishna :) SELECT *
FROM demo_db.orders
WHERE status = 'Shipped';

SELECT *
FROM demo_db.orders
WHERE status = 'Shipped'

Query id: ee004925-2165-4cb7-b2ec-2a6dad758f98

   ┌─order_id─┬─customer_id─┬─status──┐
1. │     1001 │         501 │ Shipped │
2. │     1003 │         503 │ Shipped │
   └──────────┴─────────────┴─────────┘

2 rows in set. Elapsed: 0.008 sec.

   

3.2 Querying with the enum’s numeric value

Since Enum8 is stored as an Int8, ClickHouse accepts either the name or the numeric code assigned to that name. Because of this ClickHouse lets you compare an Enum8 column directly to its integer codes, but for readability most teams stick to the string form in production SQL.

 

SELECT *
FROM demo_db.orders
WHERE status = 3;

krishna :) SELECT *
FROM demo_db.orders
WHERE status = 3;

SELECT *
FROM demo_db.orders
WHERE status = 3

Query id: 6c0a453c-9106-476e-82c7-c141d82ff950

   ┌─order_id─┬─customer_id─┬─status──┐
1. │     1001 │         501 │ Shipped │
2. │     1003 │         503 │ Shipped │
   └──────────┴─────────────┴─────────┘

2 rows in set. Elapsed: 0.003 sec.

   

We can even insert data into the table using their enum codes.

 

INSERT INTO demo_db.orders VALUES
    (1008, 504, 1), 
    (1009, 504, 2),
    (1009, 504, 3),
    (1009, 504, 4),
    (1010, 504, 5);

krishna :) SELECT * FROM demo_db.orders WHERE customer_id=504;

SELECT *
FROM demo_db.orders
WHERE customer_id = 504

Query id: 73cc3555-089a-47a6-aa88-49542b4f774e

   ┌─order_id─┬─customer_id─┬─status─────┐
1. │     1008 │         504 │ Pending    │
2. │     1009 │         504 │ Processing │
3. │     1009 │         504 │ Shipped    │
4. │     1009 │         504 │ Delivered  │
5. │     1010 │         504 │ Cancelled  │
   └──────────┴─────────────┴────────────┘

5 rows in set. Elapsed: 0.007 sec.

   

In summary,

·      Use Enum8 when your list is small and fixed (e.g., statuses, flags, roles).

·      Use Enum16 when your list is larger or expected to grow (e.g., country codes, languages).

·      Always define explicit mappings for clarity and maintainability.

·      Prefer Enums over strings for frequent grouping or filtering on categorical fields.

 


 

Previous                                                    Next                                                    Home