Sunday, 6 September 2026

Understanding the Polygon Type in ClickHouse

  

When working with geospatial data in ClickHouse, shapes like points and lines are easy to grasp. But how do you model complex areas, like a city boundary that has lakes inside it?

That’s where the Polygon type shines, it allows you to define areas with or without holes, such as:

 

·      A park with a pond in the middle

·      A city excluding restricted zones

·      A farmland area with structures in the center

 

ClickHouse makes this possible with the Polygon type, which is built from rings.

 

1. What is a Polygon?

In ClickHouse, a Polygon is an Array(Ring). A Ring is an Array(Point) (a closed shape with no holes)

 

The Polygon has:

·      The first Ring is the outer boundary

·      The subsequent Rings are the holes

 


2. Real World Example:

Imagine you're modeling a city park that includes:

 

·      The outer boundary of the park

·      A few buildings inside the park

·      One or more ponds in the park

 

In this case:

·      The outer ring defines the full area of the park.

·      The holes (inner rings) represent the buildings and ponds, which are excluded areas (no trees, no lawns, etc.,)

 

This is a perfect use case for a Polygon with holes, because:

·      You want to model the space accurately.

·      You need to subtract internal non-park areas from the overall polygon area.

 

CREATE DATABASE IF NOT EXISTS demo_db;

CREATE TABLE demo_db.city_parks (
    park_id UInt32,
    name String,
    area Polygon
) ENGINE = MergeTree ORDER BY park_id;

-- Outer ring is the park boundary, inner rings are building and pond exclusions
INSERT INTO demo_db.city_parks VALUES (
    1, 'Central Park',
    [[(0,0), (100,0), (100,100), (0,100)],       -- outer boundary
     [(20,20), (30,20), (30,30), (20,30)],       -- building
     [(60,60), (70,60), (70,70), (60,70)]]       -- pond
);

SELECT name, area FROM demo_db.city_parks WHERE park_id = 1;

krishna :) SELECT name, area FROM demo_db.city_parks WHERE park_id = 1;

SELECT
    name,
    area
FROM demo_db.city_parks
WHERE park_id = 1

Query id: 8db4a7b3-aa87-4e23-8bba-7fa55cf0543f

   ┌─name─────────┬─area────────────────────────────────────────────────────────────────────────────────────────────────────┐
1. │ Central Park │ [[(0,0),(100,0),(100,100),(0,100)],[(20,20),(30,20),(30,30),(20,30)],[(60,60),(70,60),(70,70),(60,70)]] │
   └──────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────┘

1 row in set. Elapsed: 0.006 sec.

  

Polygons with holes make spatial data far more precise, supporting use cases like urban planning, disaster management, and autonomous navigation.

 

Previous                                                    Next                                                    Home

MultiLineString: Representing Multiple Paths with Arrays of LineStrings

  

Imagine you’re trying to model a bus route, river, or road. A simple LineString works fine only if the path is continuous, meaning it’s one long, unbroken line of connected points.

 

Example

-----------------------------------------
Case 1: LineString (One Connected Line)
-----------------------------------------
Bus Route A:
      ●─────●─────●─────●

Data: [(0,0), (5,0), (10,0), (15,0)]


-----------------------------------------
Case 2: MultiLineString (Multiple Lines)
-----------------------------------------
Bus Route B:
      ●─────●     (Gap here)     ●─────●─────●

Data: [
         [(0,0), (5,0)],          -- first segment
         [(10,0), (15,0), (20,0)] -- second segment
      ]

   

Another example is each metro line can be a separate LineString. You can group all the lines into a MultiLineString for the entire map.

 

    


Image credits goes to https://commons.wikimedia.org/wiki/File:Metro_Map_2025_-_Bengaluru_City.pdf.

 

Find the below working application.

CREATE DATABASE IF NOT EXISTS demo_db;

CREATE TABLE demo_db.transit_routes (
    route_name String,
    route MultiLineString
) ENGINE = Memory();

   

Insert some data into transit_routes table.

 

INSERT INTO demo_db.transit_routes VALUES
(
    'Blue Line',
    [
        [(0, 0), (10, 0), (10, 10), (0, 10)],
        [(1, 1), (2, 2), (3, 3)]
    ]
);

   

Select or view the route.

 

SELECT
    route_name,
    route,
    toTypeName(route) AS type
FROM demo_db.transit_routes;

krishna :) SELECT
    route_name,
    route,
    toTypeName(route) AS type
FROM demo_db.transit_routes;

SELECT
    route_name,
    route,
    toTypeName(route) AS type
FROM demo_db.transit_routes

Query id: b3347d00-9761-4d16-8e21-3665bbe3ddb7

   ┌─route_name─┬─route───────────────────────────────────────────────┬─type────────────┐
1. │ Blue Line  │ [[(0,0),(10,0),(10,10),(0,10)],[(1,1),(2,2),(3,3)]] │ MultiLineString │
   └────────────┴─────────────────────────────────────────────────────┴─────────────────┘

1 row in set. Elapsed: 0.002 sec.

 


Previous                                                    Next                                                    Home

ClickHouse LineString Type: Representing Paths with Arrays of Points

  

When working with geospatial data, it's important to represent not just areas (polygons) but also paths, routes, or edges. That’s where the LineString type comes in. ClickHouse supports LineString as a simple way to store a line made up of multiple points, very useful for roads, or movement paths.

 

What is a LineString?

A LineString is a series of connected points, forming a line, like a route on a map.

 

LineString Characterstics

·      Internally stored as Array(Point)

·      A Point is a pair of coordinates (x, y)

·      Unlike Ring, it does not close back to the starting point

 

Let’s take a real world example to understand LineString better.

CREATE DATABASE IF NOT EXISTS demo_db;

CREATE TABLE demo_db.road_paths (
    road_name String,
    path LineString
) ENGINE = Memory();

   

Here:

·      road_name: Name of the road or route

·      path: A LineString (series of points forming a path)

 

Let’s define a road that makes 3 turns.

From (0,0) → (10,0) → (10,10) → (0,10)

 

INSERT INTO demo_db.road_paths VALUES
(
    'Main Street',
    [(0, 0), (10, 0), (10, 10), (0, 10)]
);

   

Let's view the line string.

 

SELECT
    road_name,
    path,
    toTypeName(path) AS type
FROM demo_db.road_paths;

krishna :) SELECT
    road_name,
    path,
    toTypeName(path) AS type
FROM demo_db.road_paths;

SELECT
    road_name,
    path,
    toTypeName(path) AS type
FROM demo_db.road_paths

Query id: f57ba1aa-4e68-4d93-a4d3-9718b87f6429

   ┌─road_name───┬─path──────────────────────────┬─type───────┐
1. │ Main Street │ [(0,0),(10,0),(10,10),(0,10)] │ LineString │
   └─────────────┴───────────────────────────────┴────────────┘

1 row in set. Elapsed: 0.001 sec.

 

 

Previous                                                    Next                                                    Home

Wednesday, 2 September 2026

Understanding ClickHouse Ring Type: Storing Simple Polygons as Arrays of Points

  

ClickHouse is a high-performance column-oriented database, often used for analytics. With the increasing demand for handling geospatial data, ClickHouse offers several geometric types. One such type is Ring, which is used to represent simple polygons (without holes). This post introduces the Ring type to beginners using clear examples.

 

What is a Ring in ClickHouse?

A Ring in ClickHouse represents a simple polygon, a closed shape made by connecting a series of straight lines (edges) between points, without any holes.

 

Think of a square or a triangle, a continuous boundary drawn by connecting dots in a specific order. That’s what a Ring represents internally, a list of points (x, y) connected in sequence.

 

What does “without holes” mean in the context of a Ring?

Imagine you're drawing a shape like a square or triangle on paper. If you just draw the outline, that's a simple shape, and that's what a Ring represents: the outer boundary of a shape.

 


Now imagine you draw a donut a circle with a hole in the middle. That’s a polygon with a hole.

 

·      Ring = only the outer boundary.

·      Polygons with holes need a different data structure, like Polygon, which supports both an outer ring and one or more inner rings (holes).

In summary, a Ring is like drawing a simple, solid shape with just one boundary, no cutouts or empty areas inside.

 

Example: Storing Outer Boundary Of a City

 

CREATE DATABASE IF NOT EXISTS demo_db;

CREATE TABLE demo_db.city_boundaries (
    city_name String,
    boundary Ring
) ENGINE = Memory();

   

Here:

·      city_name: Name of the city.

·      boundary: A Ring that defines the outer boundary (polygon without holes).

 

Let's insert a square boundary for the city 'Amaravathi' with corners at

 

(0,0) → (0,10) → (10,10) → (10,0)

 

INSERT INTO demo_db.city_boundaries VALUES
(
    'Amaravathi',
    [(0, 0), (0, 10), (10, 10), (10, 0)]
);

   

This polygon is closed automatically; we don’t need to repeat (0,0).

 

Let's get the city boundary.

 

SELECT
    city_name,
    boundary,
    toTypeName(boundary) AS type
FROM demo_db.city_boundaries;

krishna :) SELECT
    city_name,
    boundary,
    toTypeName(boundary) AS type
FROM demo_db.city_boundaries;

SELECT
    city_name,
    boundary,
    toTypeName(boundary) AS type
FROM demo_db.city_boundaries

Query id: 487043e3-1327-4ecb-9ac4-6cd0f4055b77

   ┌─city_name──┬─boundary──────────────────────┬─type─┐
1. │ Amaravathi │ [(0,0),(0,10),(10,10),(10,0)] │ Ring │
   └────────────┴───────────────────────────────┴──────┘

1 row in set. Elapsed: 0.004 sec.

  

Previous                                                    Next                                                    Home