Wednesday, 26 August 2026

Arrays in ClickHouse: A Beginner-Friendly Guide with Examples

  

Arrays are a powerful data type in ClickHouse that allow you to store multiple values in a single column. Whether you're working with a list of tags, numeric series, or JSON-like structures, arrays can help you organize data more efficiently. In this guide, I’ll walk through the basics of arrays in ClickHouse, including how to define them, what they can contain, and how to use them effectively.

 

1. What is an Array in ClickHouse?

An array is an ordered collection of elements of the same type. It allows you to store multiple values in a single field.

 

2. How to Define Arrays

 

2.1 Using square brackets:

SELECT [1, 2, 3, 4] AS numbers;

krishna :) SELECT [1, 2, 3, 4] AS numbers;

SELECT [1, 2, 3, 4] AS numbers

Query id: ce01a46b-ae3e-444b-8ad9-bf8e0be17126

Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 25.5.1.

   ┌─numbers───┐
1. │ [1,2,3,4] │
   └───────────┘

1 row in set. Elapsed: 0.002 sec. 

   

2.2 Using the array() function

 

SELECT array(1, 2, 3, 4) AS numbers;

krishna :) SELECT array(1, 2, 3, 4) AS numbers;

SELECT [1, 2, 3, 4] AS numbers

Query id: d5fd5ff5-57ee-4c48-9e75-1820d3a105d4

   ┌─numbers───┐
1. │ [1,2,3,4] │
   └───────────┘

1 row in set. Elapsed: 0.001 sec. 

3. Multi-dimensional Arrays

ClickHouse also supports arrays of arrays.

SELECT [[1, 2], [3, 4]] AS matrix;

krishna :) SELECT [[1, 2], [3, 4]] AS matrix;

SELECT [[1, 2], [3, 4]] AS matrix

Query id: ec39ab09-89c2-44c3-9ca8-05d71d5f966d

   ┌─matrix────────┐
1. │ [[1,2],[3,4]] │
   └───────────────┘

1 row in set. Elapsed: 0.002 sec.

   

Multi Dimensional arrays are useful for representing tabular or grid-like data.

 

4. Type Consistency

All elements in an array must be of the compatible type. Mixing types will result in an error.

 

For example, following statement results in an error.

 

SELECT ['apple', 1] AS invalid_array;

krishna :) SELECT ['apple', 1] AS invalid_array;

SELECT ['apple', 1] AS invalid_array

Query id: 761150cf-622b-4557-a36c-17e97575e736


Elapsed: 0.028 sec. 

Received exception from server (version 25.5.1):
Code: 386. DB::Exception: Received from localhost:9000. DB::Exception: There is no supertype for types String, UInt8 because some of them are String/FixedString/Enum and some of them are not. (NO_COMMON_TYPE)

   

5. NULL Values in Arrays

ClickHouse supports NULL values in the array.

 

SELECT [1, NULL, 3] AS nums;

krishna :) SELECT [1, NULL, 3] AS nums;

SELECT [1, NULL, 3] AS nums

Query id: 09d7aaaa-ec5a-4dbb-a5d3-7ea417196104

   ┌─nums───────┐
1. │ [1,NULL,3] │
   └────────────┘

1 row in set. Elapsed: 0.001 sec. 

   

6. Example: Tracking User Interests in a Blog Platform

 

CREATE DATABASE IF NOT EXISTS demo_db;

CREATE TABLE demo_db.user_profiles (
    user_id UInt32,
    user_name String,
    interests Array(String)
) ENGINE = MergeTree()
ORDER BY user_id;

INSERT INTO demo_db.user_profiles VALUES
(1, 'Ram', ['tech', 'ai', 'space']),
(2, 'Krishna', ['cooking', 'travel']),
(3, 'Chamu', ['tech', 'finance']),
(4, 'Hari', ['art', 'travel', 'fashion']),
(5, 'Ravi', ['ai', 'robotics', 'space']);

SELECT * FROM demo_db.user_profiles;

krishna :) SELECT * FROM demo_db.user_profiles;

SELECT *
FROM demo_db.user_profiles

Query id: cb4c0817-86f6-43ac-b11c-b71b642cdb0b

   ┌─user_id─┬─user_name─┬─interests──────────────────┐
1. │       1 │ Ram       │ ['tech','ai','space']      │
2. │       2 │ Krishna   │ ['cooking','travel']       │
3. │       3 │ Chamu     │ ['tech','finance']         │
4. │       4 │ Hari      │ ['art','travel','fashion'] │
5. │       5 │ Ravi      │ ['ai','robotics','space']  │
   └─────────┴───────────┴────────────────────────────┘

5 rows in set. Elapsed: 0.004 sec.

   

6.1 Find users interested in tech.

 

SELECT user_name
FROM demo_db.user_profiles
WHERE has(interests, 'tech');

krishna :) SELECT user_name
FROM demo_db.user_profiles
WHERE has(interests, 'tech');

SELECT user_name
FROM demo_db.user_profiles
WHERE has(interests, 'tech')

Query id: 09582d25-dd3b-4a3f-9fef-c92404f52989

   ┌─user_name─┐
1. │ Ram       │
2. │ Chamu     │
   └───────────┘

2 rows in set. Elapsed: 0.008 sec.

   

6.2 Flatten interests across all users (arrayJoin)

 

SELECT user_name, arrayJoin(interests) AS interest
FROM demo_db.user_profiles;

krishna :) SELECT user_name, arrayJoin(interests) AS interest
FROM demo_db.user_profiles;

SELECT
    user_name,
    arrayJoin(interests) AS interest
FROM demo_db.user_profiles

Query id: 54f41a7d-6474-42ea-b87f-26b23cdbdc97

    ┌─user_name─┬─interest─┐
 1. │ Ram       │ tech     │
 2. │ Ram       │ ai       │
 3. │ Ram       │ space    │
 4. │ Krishna   │ cooking  │
 5. │ Krishna   │ travel   │
 6. │ Chamu     │ tech     │
 7. │ Chamu     │ finance  │
 8. │ Hari      │ art      │
 9. │ Hari      │ travel   │
10. │ Hari      │ fashion  │
11. │ Ravi      │ ai       │
12. │ Ravi      │ robotics │
13. │ Ravi      │ space    │
    └───────────┴──────────┘

13 rows in set. Elapsed: 0.004 sec.

   

6.3 Filter interests that contain the letter 'a'

 

SELECT
    user_name,
    arrayFilter(x -> position(x, 'a') > 0, interests) AS filtered_interests
FROM demo_db.user_profiles;

krishna :) SELECT
    user_name,
    arrayFilter(x -> position(x, 'a') > 0, interests) AS filtered_interests
FROM demo_db.user_profiles;

SELECT
    user_name,
    arrayFilter(x -> (position(x, 'a') > 0), interests) AS filtered_interests
FROM demo_db.user_profiles

Query id: 30a6a257-b886-46ca-b19b-bab0cbc5d91e

   ┌─user_name─┬─filtered_interests─────────┐
1. │ Ram       │ ['ai','space']             │
2. │ Krishna   │ ['travel']                 │
3. │ Chamu     │ ['finance']                │
4. │ Hari      │ ['art','travel','fashion'] │
5. │ Ravi      │ ['ai','space']             │
   └───────────┴────────────────────────────┘

5 rows in set. Elapsed: 0.005 sec.

   

6.4 Count how many interests each user has.

 

SELECT
    user_name,
    length(interests) AS interest_count
FROM demo_db.user_profiles;

krishna :) SELECT
    user_name,
    length(interests) AS interest_count
FROM demo_db.user_profiles;

SELECT
    user_name,
    length(interests) AS interest_count
FROM demo_db.user_profiles

Query id: 691a5e9c-cb64-47e9-a41d-0872caed076a

   ┌─user_name─┬─interest_count─┐
1. │ Ram       │              3 │
2. │ Krishna   │              2 │
3. │ Chamu     │              2 │
4. │ Hari      │              3 │
5. │ Ravi      │              3 │
   └───────────┴────────────────┘

5 rows in set. Elapsed: 0.004 sec. 

 

  

Previous                                                    Next                                                    Home

Understanding DateTime and DateTime64 Types in ClickHouse

  

ClickHouse provides two main data types to store timestamps with both date and time components: DateTime and DateTime64. These types allow you to represent specific instants in time with different levels of precision and features.

 

1. DateTime

Used to store a specific moment in time, represented by a calendar date and a time of day. The supported range of values is from 1970-01-01 00:00:00 up to 2106-02-07 06:28:15.

 

Precision: 1 second

 

Syntax

DateTime([timezone])

   

Date vs DateTime

The Date data type is generally faster than DateTime in most scenarios. Date uses 2 bytes of storage, whereas DateTime requires 4 bytes. This difference becomes even more significant when the database applies compression, since the minutes and seconds in DateTime are less compressible. Additionally, filtering and aggregating data using the Date type is typically faster than using DateTime.

 

Remember following points while working with DateTime

·      Internally, DateTime values are stored as Unix timestamps, which are time zone–agnostic.

·      The time zone influences how DateTime values are displayed as text and how string inputs are parsed.

·      Time zone information is stored in column metadata, not in individual rows or result sets.

·      Supported time zones follow the IANA Time Zone Database and can be queried from the system or found on Wikipedia.

·      You can specify a time zone when defining a DateTime column (e.g., DateTime('UTC')). If omitted, the server or OS time zone at startup is used.

·      The default time zone used by clickhouse-client is the server time zone unless overridden with the --use_client_time_zone option.

·      Output format of DateTime values defaults to YYYY-MM-DD hh:mm:ss but can be customized using date_time_output_format or the formatDateTime function.

·      When inserting data, various date/time string formats are accepted depending on the date_time_input_format setting.

 

2. DateTime64

This type stores a precise moment in time, expressed as a calendar date and time of day, with configurable sub-second precision.

 

Precision (tick size): Defined by a value from 0 to 9, representing 10^(-precision) seconds. Commonly used precisions are 3 (milliseconds), 6 (microseconds), and 9 (nanoseconds).

 

Syntax:

DateTime64(precision, [timezone])

   

Internally, DateTime64 stores the timestamp as the number of ticks since the Unix epoch (1970-01-01 00:00:00 UTC) in an Int64 format. The tick size depends on the specified precision.

 

The time zone can be set for the entire column, influencing how values are displayed and parsed (e.g., '2020-01-01 05:00:01.000'). Like DateTime, the time zone is saved in the column metadata but not within individual rows or result sets.

 

Supported value range: From 1900-01-01 00:00:00 up to 2299-12-31 23:59:59.99999999.

 

Note: When using maximum precision (9 digits, nanoseconds), the maximum supported timestamp is 2262-04-11 23:47:16 UTC. For lower precisions, the maximum date extends up to the year 2299.

 

Following snippet demonstrate the examples.

 

CREATE DATABASE IF NOT EXISTS demo_db;

CREATE TABLE demo_db.events_datetime_demo (
    event_name String,
    event_time DateTime,                -- Stores timestamp with 1-second precision
    event_time_high_precision DateTime64(6)  -- Stores timestamp with microseconds precision
) ENGINE = MergeTree()
ORDER BY event_time;

-- Insert some sample data
INSERT INTO demo_db.events_datetime_demo VALUES
('Standard Precision Event', '2025-05-16 14:30:00', '2025-05-16 14:30:00.123456'),
('High Precision Event', '2025-05-16 14:31:00', '2025-05-16 14:31:00.654321');

-- Query the table
SELECT
    event_name,
    event_time,
    event_time_high_precision
FROM demo_db.events_datetime_demo;

krishna :) SELECT
    event_name,
    event_time,
    event_time_high_precision
FROM demo_db.events_datetime_demo;

SELECT
    event_name,
    event_time,
    event_time_high_precision
FROM demo_db.events_datetime_demo

Query id: b4a047ce-dc2f-49bf-91a4-2343e0af1a6e

   ┌─event_name───────────────┬──────────event_time─┬──event_time_high_precision─┐
1. │ Standard Precision Event │ 2025-05-16 14:30:00 │ 2025-05-16 14:30:00.123456 │
2. │ High Precision Event     │ 2025-05-16 14:31:00 │ 2025-05-16 14:31:00.654321 │
   └──────────────────────────┴─────────────────────┴────────────────────────────┘

2 rows in set. Elapsed: 0.004 sec.

 


Previous                                                    Next                                                    Home