| description | Provides a read-only table-like interface to Apache Iceberg tables in Amazon S3, Azure, HDFS or locally stored. |
|---|---|
| sidebar_label | iceberg |
| sidebar_position | 90 |
| slug | /sql-reference/table-functions/iceberg |
| title | iceberg |
| doc_type | reference |
Provides a read-only table-like interface to Apache Iceberg tables in Amazon S3, Azure, HDFS or locally stored.
icebergS3(url [, NOSIGN | access_key_id, secret_access_key, [session_token]] [,format] [,compression_method])
icebergS3(named_collection[, option=value [,..]])
icebergAzure(connection_string|storage_account_url, container_name, blobpath, [,account_name], [,account_key] [,format] [,compression_method])
icebergAzure(named_collection[, option=value [,..]])
icebergHDFS(path_to_table, [,format] [,compression_method])
icebergHDFS(named_collection[, option=value [,..]])
icebergLocal(path_to_table, [,format] [,compression_method])
icebergLocal(named_collection[, option=value [,..]])Description of the arguments coincides with description of arguments in table functions s3, azureBlobStorage, HDFS and file correspondingly.
format stands for the format of data files in the Iceberg table.
A table with the specified structure for reading data in the specified Iceberg table.
SELECT * FROM icebergS3('http://test.s3.amazonaws.com/clickhouse-bucket/test_table', 'test', 'test'):::important
ClickHouse currently supports reading v1 and v2 of the Iceberg format via the icebergS3, icebergAzure, icebergHDFS and icebergLocal table functions and IcebergS3, icebergAzure, IcebergHDFS and IcebergLocal table engines.
:::
Here is an example of configuring a named collection for storing the URL and credentials:
<clickhouse>
<named_collections>
<iceberg_conf>
<url>http://test.s3.amazonaws.com/clickhouse-bucket/</url>
<access_key_id>test<access_key_id>
<secret_access_key>test</secret_access_key>
<format>auto</format>
<structure>auto</structure>
</iceberg_conf>
</named_collections>
</clickhouse>SELECT * FROM icebergS3(iceberg_conf, filename = 'test_table')
DESCRIBE icebergS3(iceberg_conf, filename = 'test_table')Iceberg tables can also be used with various data catalogs, such as the REST Catalog, AWS Glue Data Catalog and Unity Catalog.
:::important
When using a catalog, most users will want to use the DataLakeCatalog database engine, which connects ClickHouse to your catalog to discover your tables. You can use this database engine instead of manually creating individual tables with IcebergS3 table engine.
:::
To use them, create a table with the IcebergS3 engine and provide the necessary settings.
For example, using REST Catalog with MinIO storage:
CREATE TABLE `database_name.table_name`
ENGINE = IcebergS3(
'http://minio:9000/warehouse-rest/table_name/',
'minio_access_key',
'minio_secret_key'
)
SETTINGS
storage_catalog_type="rest",
storage_warehouse="demo",
object_storage_endpoint="http://minio:9000/warehouse-rest",
storage_region="us-east-1",
storage_catalog_url="http://rest:8181/v1"Or, using AWS Glue Data Catalog with S3:
CREATE TABLE `my_database.my_table`
ENGINE = IcebergS3(
's3://my-data-bucket/warehouse/my_database/my_table/',
'aws_access_key',
'aws_secret_key'
)
SETTINGS
storage_catalog_type = 'glue',
storage_warehouse = 'my_database',
object_storage_endpoint = 's3://my-data-bucket/',
storage_region = 'us-east-1',
storage_catalog_url = 'https://glue.us-east-1.amazonaws.com/iceberg/v1'At the moment, with the help of CH, you can read iceberg tables, the schema of which has changed over time. We currently support reading tables where columns have been added and removed, and their order has changed. You can also change a column where a value is required to one where NULL is allowed. Additionally, we support permitted type casting for simple types, namely:
- 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.
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
ClickHouse supports time travel for Iceberg tables, allowing you to query historical data with a specific timestamp or snapshot ID.
Currently, only Iceberg tables with position deletes are supported.
The following deletion methods are not supported:
- Equality deletes
- Deletion vectors (introduced in v3)
SELECT * FROM example_table ORDER BY 1
SETTINGS iceberg_timestamp_ms = 1714636800000SELECT * FROM example_table ORDER BY 1
SETTINGS iceberg_snapshot_id = 3547395809148285433Note: You cannot specify both iceberg_timestamp_ms and iceberg_snapshot_id parameters in the same query.
-
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.
All scenarios are written in Spark because CH doesn't support writing to Iceberg tables yet.
Consider this sequence of operations:
-- Create a table with two columns
CREATE TABLE IF NOT EXISTS spark_catalog.db.time_travel_example (
order_number bigint,
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
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 bigint,
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.
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 bigint,
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.
When using the iceberg table function 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:
- 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.
-
Table UUID Matching: *If
iceberg_metadata_table_uuidis specified, the system will: *Look only at.metadata.jsonfiles in themetadatadirectory *Filter for files containing atable-uuidfield matching your specified UUID (case-insensitive) -
Default Search: *If neither of the above settings are provided, all
.metadata.jsonfiles in themetadatadirectory become candidates
After identifying candidate files using the above rules, the system determines which one is the most recent:
-
If
iceberg_recent_metadata_file_by_last_updated_ms_fieldis enabled: -
The file with the largest
last-updated-msvalue is selected -
Otherwise:
-
The file with the highest version number is selected
-
(Version appears as
Vin filenames formatted asV.metadata.jsonorV-uuid.metadata.json)
Note: All mentioned settings are table function settings (not global or query-level settings) and must be specified as shown below:
SELECT * FROM iceberg('s3://bucket/path/to/iceberg_table',
SETTINGS iceberg_metadata_table_uuid = 'a90eed4c-f74b-4e5b-b630-096fb9d09021');Note: While Iceberg Catalogs typically handle metadata resolution, the iceberg table function in ClickHouse directly interprets files stored in S3 as Iceberg tables, which is why understanding these resolution rules is important.
Iceberg table engine and table function support metadata cache storing the information of manifest files, manifest list and metadata json. The cache is stored in memory. This feature is controlled by setting use_iceberg_metadata_files_cache, which is enabled by default.
Table function iceberg is an alias to icebergS3 now.
_path— Path to the file. Type:LowCardinality(String)._file— Name of the file. Type:LowCardinality(String)._size— Size of the file in bytes. Type:Nullable(UInt64). If the file size is unknown, the value isNULL._time— Last modified time of the file. Type:Nullable(DateTime). If the time is unknown, the value isNULL._etag— The etag of the file. Type:LowCardinality(String). If the etag is unknown, the value isNULL.
Starting from version 25.7, ClickHouse supports modifications of user’s Iceberg tables.
Currently, this is an experimental feature, so you first need to enable it:
SET allow_insert_into_iceberg = 1;To create your own empty Iceberg table, use the same commands as for reading, but specify the schema explicitly. Writes supports all data formats from iceberg specification, such as Parquet, Avro, ORC.
CREATE TABLE iceberg_writes_example
(
x Nullable(String),
y Nullable(Int32)
)
ENGINE = IcebergLocal('/home/scanhex12/iceberg_example/')Note: To create a version hint file, enable the iceberg_use_version_hint setting.
If you want to compress the metadata.json file, specify the codec name in the iceberg_metadata_compression_method setting.
After creating a new table, you can insert data using the usual ClickHouse syntax.
INSERT INTO iceberg_writes_example VALUES ('Pavel', 777), ('Ivanov', 993);
SELECT *
FROM iceberg_writes_example
FORMAT VERTICAL;
Row 1:
──────
x: Pavel
y: 777
Row 2:
──────
x: Ivanov
y: 993Deleting extra rows in the merge-on-read format is also supported in ClickHouse. This query will create a new snapshot with position delete files.
NOTE: If you want to read your tables in the future with other Iceberg engines (such as Spark), you need to disable the settings output_format_parquet_use_custom_encoder and output_format_parquet_parallel_encoding.
This is because Spark reads these files by parquet field-ids, while ClickHouse does not currently support writing field-ids when these flags are enabled.
We plan to fix this behavior in the future.
ALTER TABLE iceberg_writes_example DELETE WHERE x != 'Ivanov';
SELECT *
FROM iceberg_writes_example
FORMAT VERTICAL;
Row 1:
──────
x: Ivanov
y: 993ClickHouse allows you to add, drop, modify, or rename columns with simple types (non-tuple, non-array, non-map).
ALTER TABLE iceberg_writes_example MODIFY COLUMN y Nullable(Int64);
SHOW CREATE TABLE iceberg_writes_example;
┌─statement─────────────────────────────────────────────────┐
1. │ CREATE TABLE default.iceberg_writes_example ↴│
│↳( ↴│
│↳ `x` Nullable(String), ↴│
│↳ `y` Nullable(Int64) ↴│
│↳) ↴│
│↳ENGINE = IcebergLocal('/home/scanhex12/iceberg_example/') │
└───────────────────────────────────────────────────────────┘
ALTER TABLE iceberg_writes_example ADD COLUMN z Nullable(Int32);
SHOW CREATE TABLE iceberg_writes_example;
┌─statement─────────────────────────────────────────────────┐
1. │ CREATE TABLE default.iceberg_writes_example ↴│
│↳( ↴│
│↳ `x` Nullable(String), ↴│
│↳ `y` Nullable(Int64), ↴│
│↳ `z` Nullable(Int32) ↴│
│↳) ↴│
│↳ENGINE = IcebergLocal('/home/scanhex12/iceberg_example/') │
└───────────────────────────────────────────────────────────┘
SELECT *
FROM iceberg_writes_example
FORMAT VERTICAL;
Row 1:
──────
x: Ivanov
y: 993
z: ᴺᵁᴸᴸ
ALTER TABLE iceberg_writes_example DROP COLUMN z;
SHOW CREATE TABLE iceberg_writes_example;
┌─statement─────────────────────────────────────────────────┐
1. │ CREATE TABLE default.iceberg_writes_example ↴│
│↳( ↴│
│↳ `x` Nullable(String), ↴│
│↳ `y` Nullable(Int64) ↴│
│↳) ↴│
│↳ENGINE = IcebergLocal('/home/scanhex12/iceberg_example/') │
└───────────────────────────────────────────────────────────┘
SELECT *
FROM iceberg_writes_example
FORMAT VERTICAL;
Row 1:
──────
x: Ivanov
y: 993
ALTER TABLE iceberg_writes_example RENAME COLUMN y TO value;
SHOW CREATE TABLE iceberg_writes_example;
┌─statement─────────────────────────────────────────────────┐
1. │ CREATE TABLE default.iceberg_writes_example ↴│
│↳( ↴│
│↳ `x` Nullable(String), ↴│
│↳ `value` Nullable(Int64) ↴│
│↳) ↴│
│↳ENGINE = IcebergLocal('/home/scanhex12/iceberg_example/') │
└───────────────────────────────────────────────────────────┘
SELECT *
FROM iceberg_writes_example
FORMAT VERTICAL;
Row 1:
──────
x: Ivanov
value: 993ClickHouse supports compaction iceberg table. Currently, it can merge position delete files into data files while updating metadata. Previous snapshot IDs and timestamps remain unchanged, so the time-travel feature can still be used with the same values.
How to use it:
SET allow_experimental_iceberg_compaction = 1
OPTIMIZE TABLE iceberg_writes_example;
SELECT *
FROM iceberg_writes_example
FORMAT VERTICAL;
Row 1:
──────
x: Ivanov
y: 993Iceberg tables accumulate snapshots with each INSERT, DELETE, or UPDATE operation. Over time, this can lead to a large number of snapshots and associated data files. The expire_snapshots command removes old snapshots and cleans up data files that are no longer referenced by any retained snapshot.
Syntax:
ALTER TABLE iceberg_table EXECUTE expire_snapshots(
['timestamp']
[, expire_before = 'timestamp']
[, retention_period = '3d']
[, retain_last = 100]
[, snapshot_ids = [1, 2, 3, 4]]
[, dry_run = 1]
);By default, which snapshots to keep is determined by the retention policy (table properties min-snapshots-to-keep, max-snapshot-age-ms, and per-ref overrides). When snapshot_ids is specified, the retention policy is bypassed and only the listed snapshots are considered for expiration.
Arguments:
'timestamp'(positional) orexpire_before = 'timestamp'— a datetime string (e.g.,'2024-06-01 00:00:00') interpreted in the server's timezone. Acts as a safety fuse: snapshots whosetimestamp-msis at or after this value are protected from expiration, even if the retention policy would otherwise expire them. Can be combined withsnapshot_ids, in which case listed snapshots at or newer than the timestamp are not expired.retention_period = '<duration>'— overrides the table-levelhistory.expire.max-snapshot-age-msfor this invocation only. Snapshots older than this duration (measured from now) become candidates for expiration. The value is a duration string consisting of one or more{number}{unit}pairs concatenated together. Supported units:y(365 days),w(7 days),d(24 hours),h(60 minutes),m(60 seconds),s(1 second),ms(1 millisecond). Units can be combined, e.g.'3d','12h','1d12h30m','500ms'.retain_last = N— overrides the table-levelhistory.expire.min-snapshots-to-keepfor this invocation only. At leastNsnapshots are always retained regardless of age.snapshot_ids = [id1, id2, ...]— expires exactly the listed snapshot IDs (except snapshots referenced by current snapshot, branches, or tags). This mode bypasses the retention policy entirely and cannot be combined withretention_periodorretain_last.dry_run = 1— computes what would be expired and returns metrics without writing new metadata or deleting files.
:::note
retention_period and retain_last override only the table-level retention defaults. Per-ref (branch/tag) retention overrides configured in the Iceberg table properties (e.g., refs.<branch>.min-snapshots-to-keep) are never overridden — they always take effect as specified in the table metadata.
:::
Example:
SET allow_insert_into_iceberg = 1;
-- Create some snapshots by inserting data
INSERT INTO iceberg_table VALUES (1);
INSERT INTO iceberg_table VALUES (2);
INSERT INTO iceberg_table VALUES (3);
-- Expire using retention policy only
ALTER TABLE iceberg_table EXECUTE expire_snapshots();
-- Expire with a safety fuse: protect snapshots newer than the timestamp (positional syntax)
ALTER TABLE iceberg_table EXECUTE expire_snapshots('2025-01-01 00:00:00');
-- Same using the named argument form
ALTER TABLE iceberg_table EXECUTE expire_snapshots(expire_before = '2025-01-01 00:00:00');
-- Override retention parameters for one execution
ALTER TABLE iceberg_table EXECUTE expire_snapshots(retention_period = '3d', retain_last = 10);
-- Expire explicit snapshots
ALTER TABLE iceberg_table EXECUTE expire_snapshots(snapshot_ids = [101, 102, 103]);
-- Dry-run preview (no metadata updates, no file deletes)
ALTER TABLE iceberg_table EXECUTE expire_snapshots(retention_period = '1d', dry_run = 1);Output:
The command returns a table with two columns (metric_name String, metric_value Int64) containing one row per metric. The metric names follow the Iceberg spec:
| metric_name | Description |
|---|---|
deleted_data_files_count |
Number of data files deleted |
deleted_position_delete_files_count |
Number of position delete files deleted |
deleted_equality_delete_files_count |
Number of equality delete files deleted |
deleted_manifest_files_count |
Number of manifest files deleted |
deleted_manifest_lists_count |
Number of manifest list files deleted |
deleted_statistics_files_count |
Number of statistics files deleted (always 0 currently) |
dry_run |
1 for dry-run mode, 0 for normal execution |
The command performs the following steps:
- Evaluates the retention policy (see below) to determine which snapshots must be preserved
- If a timestamp argument was provided, additionally protects all snapshots at or newer than that timestamp
- Expires snapshots that are neither retained by the policy nor protected by the timestamp fuse
- Computes which files are exclusively associated with expired snapshots
- In normal mode: generates new metadata without the expired snapshots
- In normal mode: physically deletes unreachable manifest lists, manifest files, and data files
- In
dry_run = 1mode: skips steps 5 and 6 and only returns the calculated metrics
The expire_snapshots command respects the Iceberg snapshot retention policy. Retention is configured via Iceberg table properties and per-reference overrides:
| Property | Scope | Default | Description |
|---|---|---|---|
history.expire.min-snapshots-to-keep |
Table | iceberg_expire_default_min_snapshots_to_keep (default 1) |
Minimum number of snapshots to keep in each branch's ancestor chain |
history.expire.max-snapshot-age-ms |
Table | iceberg_expire_default_max_snapshot_age_ms (default 432000000, 5 days) |
Maximum age (in ms) of snapshots to retain in a branch |
history.expire.max-ref-age-ms |
Table | iceberg_expire_default_max_ref_age_ms (default ∞) |
Maximum age (in ms) for a snapshot reference (branch or tag) before the reference itself is removed |
Each snapshot reference (refs in the Iceberg metadata) can override these with per-ref fields: min-snapshots-to-keep, max-snapshot-age-ms, and max-ref-age-ms.
Retention evaluation:
- For each branch (including
main): the ancestor chain is walked starting from the branch head. Snapshots are retained while either of these conditions is true:- The snapshot is one of the first
min-snapshots-to-keepin the chain - The snapshot's age is within
max-snapshot-age-ms(i.e.,now - timestamp-ms <= max-snapshot-age-ms)
- The snapshot is one of the first
- For tags: the tagged snapshot is retained unless the tag has exceeded its
max-ref-age-ms, in which case the tag reference is removed - Non-main references whose age exceeds
max-ref-age-msare removed entirely (themainbranch is never removed) - Dangling references that point to non-existent snapshots are removed with a warning
- The current snapshot is always preserved, regardless of retention settings
Required privileges:
The ALTER TABLE EXECUTE privilege is required, which is a child of ALTER TABLE in the ClickHouse access control hierarchy. You can grant it specifically or via the parent:
-- Grant only EXECUTE permission
GRANT ALTER TABLE EXECUTE ON my_iceberg_table TO my_user;
-- Or grant all ALTER TABLE permissions (includes ALTER TABLE EXECUTE)
GRANT ALTER TABLE ON my_iceberg_table TO my_user;:::note
- Only Iceberg format version 2 tables are supported (v1 snapshots do not guarantee
manifest-list, which is required to safely identify files for cleanup) - The current snapshot is always preserved, even if it is older than the specified timestamp
- Requires the
allow_insert_into_icebergsetting to be enabled - Requires the
allow_experimental_expire_snapshotssetting to be enabled - The catalog's own authorization (REST catalog auth, AWS Glue IAM, etc.) is enforced independently when ClickHouse updates the metadata :::
Only in the Altinity Antalya branch does the iceberg table function support all storage types. The storage type can be specified using the named argument storage_type. Supported values are s3, azure, hdfs, and local.
iceberg(storage_type='s3', url [, NOSIGN | access_key_id, secret_access_key, [session_token]] [,format] [,compression_method])
iceberg(storage_type='azure', connection_string|storage_account_url, container_name, blobpath, [,account_name], [,account_key] [,format] [,compression_method])
iceberg(storage_type='hdfs', path_to_table, [,format] [,compression_method])
iceberg(storage_type='local', path_to_table, [,format] [,compression_method])Only in the Altinity Antalya branch can storage_type be included as part of a named collection. This allows for centralized configuration of storage settings.
<clickhouse>
<named_collections>
<iceberg_conf>
<url>http://test.s3.amazonaws.com/clickhouse-bucket/</url>
<access_key_id>test<access_key_id>
<secret_access_key>test</secret_access_key>
<format>auto</format>
<structure>auto</structure>
<storage_type>s3</storage_type>
</iceberg_conf>
</named_collections>
</clickhouse>iceberg(named_collection[, option=value [,..]])The default value for storage_type is s3.