Iceberg table engine

### Complex types[​](#complex-types "Direct link to Complex types")

|  |  |  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Iceberg type ClickHouse type|  |  |  |  |  |  | | --- | --- | --- | --- | --- | --- | | `list` `Array`| `map` `Map`| `struct` `Tuple` | | | | | | | |

## Schema evolution[​](#schema-evolution "Direct link to Schema evolution")

ClickHouse supports reading Iceberg tables whose schema has evolved over time. This includes tables where columns have been added, removed, or reordered, as well as columns changed from required to nullable. Additionally, the following type casts are supported:

- int -> long
- float -> double
- decimal(P, S) -> decimal(P', S) where P' > P.

Currently, it is not possible to change nested structures or the types of elements within arrays and maps.

To read a table where the schema has changed after its creation with dynamic schema inference, set allow\_dynamic\_metadata\_for\_data\_lakes = true when creating the table.

## Partition pruning[​](#partition-pruning "Direct link to Partition pruning")

ClickHouse supports partition pruning during SELECT queries for Iceberg tables, which helps optimize query performance by skipping irrelevant data files. To enable partition pruning, set `use_iceberg_partition_pruning = 1`. For more information about iceberg partition pruning address <https://iceberg.apache.org/spec/#partitioning>

## Time travel[​](#time-travel "Direct link to Time travel")

ClickHouse supports time travel for Iceberg tables, allowing you to query historical data with a specific timestamp or snapshot ID.

## Processing of tables with deleted rows[​](#deleted-rows "Direct link to Processing of tables with deleted rows")

ClickHouse supports reading Iceberg tables that use the following deletion methods:

- [Position deletes](https://iceberg.apache.org/spec/#position-delete-files)
- [Equality deletes](https://iceberg.apache.org/spec/#equality-delete-files) (supported from version 25.8+)

The following deletion method is **not supported**:

- [Deletion vectors](https://iceberg.apache.org/spec/#deletion-vectors) (introduced in v3)

### Basic usage[​](#basic-usage "Direct link to Basic usage")

```
SELECT * FROM example_table ORDER BY 1 
SETTINGS iceberg_timestamp_ms = 1714636800000
```

```
SELECT * FROM example_table ORDER BY 1 
SETTINGS iceberg_snapshot_id = 3547395809148285433
```

Note: You cannot specify both `iceberg_timestamp_ms` and `iceberg_snapshot_id` parameters in the same query.

### Important considerations[​](#important-considerations "Direct link to Important considerations")

- **Snapshots** are typically created when:

  - New data is written to the table
  - Some kind of data compaction is performed
- **Schema changes typically don't create snapshots** - This leads to important behaviors when using time travel with tables that have undergone schema evolution.

### Example scenarios[​](#example-scenarios "Direct link to Example scenarios")

All scenarios are written in Spark because CH doesn't support writing to Iceberg tables yet.

#### Scenario 1: Schema changes without new snapshots[​](#scenario-1 "Direct link to Scenario 1: Schema changes without new snapshots")

Consider this sequence of operations:

```
-- Create a table with two columns
 CREATE TABLE IF NOT EXISTS spark_catalog.db.time_travel_example (
 order_number int, 
 product_code string
 ) 
 USING iceberg 
 OPTIONS ('format-version'='2')

-- Insert data into the table
 INSERT INTO spark_catalog.db.time_travel_example VALUES 
   (1, 'Mars')

 ts1 = now() // A piece of pseudo code

-- Alter table to add a new column
 ALTER TABLE spark_catalog.db.time_travel_example ADD COLUMN (price double)

 ts2 = now()

-- Insert data into the table
 INSERT INTO spark_catalog.db.time_travel_example VALUES (2, 'Venus', 100)

  ts3 = now()

-- Query the table at each timestamp
 SELECT * FROM spark_catalog.db.time_travel_example TIMESTAMP AS OF ts1;

+------------+------------+
|order_number|product_code|
+------------+------------+
|           1|        Mars|
+------------+------------+
 SELECT * FROM spark_catalog.db.time_travel_example TIMESTAMP AS OF ts2;

+------------+------------+
|order_number|product_code|
+------------+------------+
|           1|        Mars|
+------------+------------+

 SELECT * FROM spark_catalog.db.time_travel_example TIMESTAMP AS OF ts3;

+------------+------------+-----+
|order_number|product_code|price|
+------------+------------+-----+
|           1|        Mars| NULL|
|           2|       Venus|100.0|
+------------+------------+-----+
```

Query results at different timestamps:

- At ts1 & ts2: Only the original two columns appear
- At ts3: All three columns appear, with NULL for the price of the first row

#### Scenario 2: Historical vs. current schema differences[​](#scenario-2 "Direct link to Scenario 2: Historical vs. current schema differences")

A time travel query at a current moment might show a different schema than the current table:

```
-- Create a table
  CREATE TABLE IF NOT EXISTS spark_catalog.db.time_travel_example_2 (
  order_number int, 
  product_code string
  ) 
  USING iceberg 
  OPTIONS ('format-version'='2')

-- Insert initial data into the table
  INSERT INTO spark_catalog.db.time_travel_example_2 VALUES (2, 'Venus');

-- Alter table to add a new column
  ALTER TABLE spark_catalog.db.time_travel_example_2 ADD COLUMN (price double);

  ts = now();

-- Query the table at a current moment but using timestamp syntax

  SELECT * FROM spark_catalog.db.time_travel_example_2 TIMESTAMP AS OF ts;

    +------------+------------+
    |order_number|product_code|
    +------------+------------+
    |           2|       Venus|
    +------------+------------+

-- Query the table at a current moment
  SELECT * FROM spark_catalog.db.time_travel_example_2;
    +------------+------------+-----+
    |order_number|product_code|price|
    +------------+------------+-----+
    |           2|       Venus| NULL|
    +------------+------------+-----+
```

This happens because `ALTER TABLE` doesn't create a new snapshot but for the current table Spark takes value of `schema_id` from the latest metadata file, not a snapshot.

#### Scenario 3: Historical vs. current schema differences[​](#scenario-3 "Direct link to Scenario 3: Historical vs. current schema differences")

The second one is that while doing time travel you can't get state of table before any data was written to it:

```
-- Create a table
  CREATE TABLE IF NOT EXISTS spark_catalog.db.time_travel_example_3 (
  order_number int, 
  product_code string
  ) 
  USING iceberg 
  OPTIONS ('format-version'='2');

  ts = now();

-- Query the table at a specific timestamp
  SELECT * FROM spark_catalog.db.time_travel_example_3 TIMESTAMP AS OF ts; -- Finises with error: Cannot find a snapshot older than ts.
```

In Clickhouse the behavior is consistent with Spark. You can mentally replace Spark Select queries with Clickhouse Select queries and it will work the same way.

## Metadata file resolution[​](#metadata-file-resolution "Direct link to Metadata file resolution")

When using the `Iceberg` table engine in ClickHouse, the system needs to locate the correct metadata.json file that describes the Iceberg table structure. Here's how this resolution process works:

### Candidates search[​](#candidate-search "Direct link to Candidates search")

1. **Direct Path Specification**:

- If you set `iceberg_metadata_file_path`, the system will use this exact path by combining it with the Iceberg table directory path.
- When this setting is provided, all other resolution settings are ignored.

2. **Table UUID Matching**:

- If `iceberg_metadata_table_uuid` is specified, the system will:
  - Look only at `.metadata.json` files in the `metadata` directory
  - Filter for files containing a `table-uuid` field matching your specified UUID (case-insensitive)

3. **Default Search**:

- If neither of the above settings are provided, all `.metadata.json` files in the `metadata` directory become candidates

### Selecting the most recent file[​](#most-recent-file "Direct link to Selecting the most recent file")