Iceberg functions in DuckDB
Use Iceberg functions to inspect the metadata of Apache Iceberg tables, such as snapshots, history, schemas, manifests, data files, column statistics, partitions, branches and tags, and properties. You can also read an Iceberg table directly from its location in Amazon S3, and you can set and remove the properties of an Iceberg table. Other functions compute the values of the Iceberg bucket and truncate partition transforms.
Call the table functions in the FROM clause of a query. Most metadata functions take the table name as a string literal in the form '[catalog.database.]table', for example 'sales_db.orders_iceberg'. Write the name as a literal; you cannot compute it with a subquery.
Most examples in this topic use sales_db.orders_iceberg, an unpartitioned Iceberg table in format version 2 with the columns order_id, customer, region, amount, and order_date. The table has two snapshots: the first adds two rows in one data file, and the second adds one row in another data file. Snapshot IDs, file paths, and timestamps differ for every table, so most examples select counts or a few narrow columns. The examples that set and remove properties use a second table, sales_db.props_iceberg, so that orders_iceberg stays unchanged.
The following table lists the Iceberg functions that this topic describes.
| Function | Description |
|---|---|
Returns the bucket number that the Iceberg |
|
Returns the column statistics that an Iceberg table stores for each data file, one row per file and column. |
|
Returns one row for each data file and delete file of an Iceberg table, with the file's statistics. |
|
Returns the history of an Iceberg table: when each snapshot became the current snapshot. |
|
Returns one row for each manifest file of an Iceberg table. |
|
Returns one row for each manifest entry of an Iceberg table, with the file that the entry tracks. |
|
Returns the partition specs of an Iceberg table, one row per partition field. |
|
Returns the bounds of each partition field in each manifest of an Iceberg table. |
|
Returns the record, file, and delete counts for each partition of an Iceberg table. |
|
Returns the branches and tags of an Iceberg table. |
|
Reads the rows of an Iceberg table from its location in Amazon S3. |
|
Returns the data files that a scan of an Iceberg table reads, with an optional row filter. |
|
Returns the properties of a database in the AWS Glue Data Catalog as key and value pairs. |
|
Returns the fields of every schema of an Iceberg table, one row per top-level field. |
|
Returns one row for each snapshot of an Iceberg table. |
|
Returns the properties of an Iceberg table as key and value pairs. |
|
Returns a value truncated as the Iceberg |
|
Removes one or more properties from an Iceberg table. |
|
Sets one or more properties of an Iceberg table. |
iceberg_bucket function
Returns the bucket that value falls into when a table is partitioned with the Iceberg bucket transform into num_buckets buckets. The function hashes the value with the 32-bit Murmur3 hash that the Apache Iceberg specification defines for the value's type. Then it maps the hash to a bucket number from 0 to num_buckets - 1.
Use it to find the bucket partition that holds a value, or to check how evenly values spread across buckets. If value is NULL, the function returns NULL.
Syntax
iceberg_bucket(num_buckets, value)
Arguments
The iceberg_bucket function takes the following arguments:
num_buckets-
The number of buckets, as a positive
INTEGER. value-
The value to hash: an
INTEGER,BIGINT,DECIMAL,DATE,TIME,TIMESTAMP,TIMESTAMP WITH TIME ZONE,TIMESTAMP_NS,TIMESTAMPTZ_NS,VARCHAR,UUID, orBLOBvalue.
Return type
INTEGER
Examples
The following SQL query returns the bucket of the integer 34 in a table with 16 buckets.
SELECT iceberg_bucket(16, 34) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the bucket depends on the type of the value. The integer 34 and the string '34' land in different buckets.
SELECT iceberg_bucket(16, 34) AS from_integer, iceberg_bucket(16, '34') AS from_string
The query returns the following result.
| from_integer | from_string |
|---|---|
|
|
The following SQL query returns NULL for a NULL value.
SELECT iceberg_bucket(16, CAST(NULL AS INTEGER)) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The bucket depends on the type of value, so pass the value with the type of the partition source column. The number 34 and the string '34' land in different buckets. INTEGER and BIGINT values of the same number land in the same bucket.
num_buckets must be greater than 0. Otherwise the query fails.
There is no form for REAL or DOUBLE values, because the Iceberg specification does not define the bucket transform for floating-point types.
iceberg_column_stats function
Returns the statistics that the table's manifests store for each column of each data file in the current snapshot, or in the snapshot that you select. Each row names the file, its status and content type, and the column and its type. It also gives the column's lower and upper bounds, its size in bytes, and its counts of values, NULL values, and NaN values in that file.
The lower_bound and upper_bound columns hold the bounds as text, and column_type gives the column's type, so one result can hold bounds of every type.
Syntax
iceberg_column_stats(table_name [,
{ snapshot_from_id := snapshot_id
| snapshot_from_timestamp := timestamp } ])
Arguments
The iceberg_column_stats function takes the following arguments:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'. snapshot_from_id-
The ID of the snapshot to read instead of the current snapshot, as a
UBIGINT. If no snapshot has this ID, the query fails. This argument is optional. snapshot_from_timestamp-
A point in time, as a
TIMESTAMP. The function reads the snapshot that was current at this time instead of the current snapshot. You cannot combine it withsnapshot_from_id. If the time is in the future, the query fails. This argument is optional.
Return type
A table with one row per data file and column, and the columns status, content, file_path, partition, column_name, column_type, lower_bound, and upper_bound (all VARCHAR). The other columns are column_size, value_count, null_value_count, nan_value_count, and file_sequence_number (all BIGINT).
Examples
The following SQL query returns the bounds and the value count of the order_id column in each data file, with the file's sequence number.
SELECT file_sequence_number, lower_bound, upper_bound, value_count FROM iceberg_column_stats('sales_db.orders_iceberg') WHERE column_name = 'order_id' ORDER BY file_sequence_number
The query returns the following results.
| file_sequence_number | lower_bound | upper_bound | value_count |
|---|---|---|---|
|
|
|
|
|
|
|
|
The following SQL query lists the columns and their types.
SELECT DISTINCT column_name, column_type FROM iceberg_column_stats('sales_db.orders_iceberg') ORDER BY column_name
The query returns the following results.
| column_name | column_type |
|---|---|
|
|
|
|
|
|
|
|
|
|
Considerations
lower_bound and upper_bound are VARCHAR values. Cast them to the type in column_type before you compare them as numbers or dates. As text, '10' sorts before '9'.
iceberg_files function
Returns the data files and delete files that the current snapshot, or the snapshot that you select, uses. Each row gives the file's content type, path, format, partition spec, record count, and size in bytes. It also gives the per-column statistics that the manifest stores for the file, and the other file fields that the Apache Iceberg specification defines. The content column is 0 for a data file.
In column_sizes, value_counts, null_value_counts, nan_value_counts, lower_bounds, and upper_bounds, the keys are Iceberg field IDs, and the bounds are in the binary encoding of the Iceberg specification. The readable_metrics column holds the same statistics as a STRUCT with one field for each column name, and gives the bounds in the column's own type.
Syntax
iceberg_files(table_name [,
{ snapshot_from_id := snapshot_id
| snapshot_from_timestamp := timestamp } ])
Arguments
The iceberg_files function takes the following arguments:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'. snapshot_from_id-
The ID of the snapshot to read instead of the current snapshot, as a
UBIGINT. If no snapshot has this ID, the query fails. This argument is optional. snapshot_from_timestamp-
A point in time, as a
TIMESTAMP. The function reads the snapshot that was current at this time instead of the current snapshot. You cannot combine it withsnapshot_from_id. If the time is in the future, the query fails. This argument is optional.
Return type
A table with one row per file and the columns content (INTEGER), file_path (VARCHAR), file_format (VARCHAR), spec_id (INTEGER), record_count (BIGINT), and file_size_in_bytes (BIGINT). The next columns are column_sizes, value_counts, null_value_counts, and nan_value_counts (MAP(INTEGER, BIGINT)), lower_bounds and upper_bounds (MAP(INTEGER, BLOB)), key_metadata (BLOB), and split_offsets (BIGINT[]). The last columns are equality_ids (INTEGER[]), sort_order_id (INTEGER), first_row_id (BIGINT), referenced_data_file (VARCHAR), content_offset (BIGINT), content_size_in_bytes (BIGINT), and readable_metrics (STRUCT).
Examples
The following SQL query returns the content type, format, and record count of each file.
SELECT content, file_format, record_count FROM iceberg_files('sales_db.orders_iceberg') ORDER BY record_count
The query returns the following results.
| content | file_format | record_count |
|---|---|---|
|
|
|
|
|
|
The following SQL query reads the lower and upper bounds of the order_id column in each file from readable_metrics.
SELECT readable_metrics.order_id.lower_bound AS min_order_id, readable_metrics.order_id.upper_bound AS max_order_id FROM iceberg_files('sales_db.orders_iceberg') ORDER BY min_order_id
The query returns the following results.
| min_order_id | max_order_id |
|---|---|
|
|
|
|
The following SQL query counts the files of the table's first snapshot. The snapshot ID comes from iceberg_snapshots. The first snapshot has one data file.
SELECT count(*) AS data_files FROM iceberg_files( 'sales_db.orders_iceberg', snapshot_from_id := 1294122691170353594 )
The query returns the following result.
| data_files |
|---|
|
Considerations
To compare a file's bounds with values, use readable_metrics, for example readable_metrics.order_id.lower_bound. The values in lower_bounds and upper_bounds are binary.
iceberg_history function
Returns one row for each entry in the table's snapshot log. Each row gives the time that the snapshot became the current snapshot, the snapshot ID, and the ID of its parent snapshot. It also shows whether the snapshot is an ancestor of the current snapshot.
parent_id is NULL for the table's first snapshot. A snapshot that is no longer an ancestor of the current snapshot, for example after a rollback, has is_current_ancestor set to false.
Syntax
iceberg_history(table_name)
Arguments
The iceberg_history function takes the following argument:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'.
Return type
A table with the columns made_current_at (TIMESTAMP_MS), snapshot_id (BIGINT), parent_id (BIGINT), and is_current_ancestor (BOOLEAN).
Examples
The following SQL query lists the history in order and shows, for each entry, whether it is the first snapshot and whether it is an ancestor of the current snapshot.
SELECT parent_id IS NULL AS is_first_snapshot, is_current_ancestor FROM iceberg_history('sales_db.orders_iceberg') ORDER BY made_current_at
The query returns the following results.
| is_first_snapshot | is_current_ancestor |
|---|---|
|
|
|
|
The following SQL query counts the history entries that are ancestors of the current snapshot.
SELECT count(*) AS ancestors FROM iceberg_history('sales_db.orders_iceberg') WHERE is_current_ancestor
The query returns the following result.
| ancestors |
|---|
|
iceberg_manifests function
Returns the manifest files that the current snapshot, or the snapshot that you select, lists in its manifest list. Each row gives the manifest's path and length in bytes, its partition spec, and the snapshot that added it. It also gives the numbers of data files and delete files that the manifest adds, keeps, and deletes.
The content column is 0 for a manifest of data files and 1 for a manifest of delete files. The partition_summaries column holds one summary for each partition field, and is an empty list for an unpartitioned table.
Syntax
iceberg_manifests(table_name [,
{ snapshot_from_id := snapshot_id
| snapshot_from_timestamp := timestamp } ])
Arguments
The iceberg_manifests function takes the following arguments:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'. snapshot_from_id-
The ID of the snapshot to read instead of the current snapshot, as a
UBIGINT. If no snapshot has this ID, the query fails. This argument is optional. snapshot_from_timestamp-
A point in time, as a
TIMESTAMP. The function reads the snapshot that was current at this time instead of the current snapshot. You cannot combine it withsnapshot_from_id. If the time is in the future, the query fails. This argument is optional.
Return type
A table with one row per manifest and the columns content (INTEGER), path (VARCHAR), length (BIGINT), partition_spec_id (INTEGER), and added_snapshot_id (BIGINT). The next columns are added_data_files_count, existing_data_files_count, deleted_data_files_count, added_delete_files_count, existing_delete_files_count, and deleted_delete_files_count (all INTEGER). The last column is partition_summaries (STRUCT(contains_null BOOLEAN, contains_nan BOOLEAN, lower_bound VARCHAR, upper_bound VARCHAR)[]).
Examples
The following SQL query counts the manifests of each content type and the data files that they add. Each of the two snapshots added one manifest with one data file.
SELECT content, count(*) AS manifests, sum(added_data_files_count) AS added_data_files FROM iceberg_manifests('sales_db.orders_iceberg') GROUP BY content ORDER BY content
The query returns the following result.
| content | manifests | added_data_files |
|---|---|---|
|
|
|
iceberg_metadata function
Returns the entries of the manifests in the current snapshot, or in the snapshot that you select. Each row describes one data file or delete file. It gives the manifest that lists the file, and the manifest's sequence number and content type.
The status column shows whether the snapshot that wrote the manifest added, kept, or deleted the file: ADDED, EXISTING, or DELETED. Because the status refers to the snapshot that wrote the manifest, an entry in an older manifest can still be ADDED. The content column shows whether the file holds data or deletes. The row also gives the file's path, format, and record count, and its data and file sequence numbers.
To count the files that a snapshot still uses, leave out the entries whose status is DELETED.
Syntax
iceberg_metadata(table_name [,
{ snapshot_from_id := snapshot_id
| snapshot_from_timestamp := timestamp } ])
Arguments
The iceberg_metadata function takes the following arguments:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'. snapshot_from_id-
The ID of the snapshot to read instead of the current snapshot, as a
UBIGINT. If no snapshot has this ID, the query fails. This argument is optional. snapshot_from_timestamp-
A point in time, as a
TIMESTAMP. The function reads the snapshot that was current at this time instead of the current snapshot. You cannot combine it withsnapshot_from_id. If the time is in the future, the query fails. This argument is optional.
Return type
A table with one row per manifest entry and the columns manifest_path (VARCHAR), manifest_sequence_number (BIGINT), manifest_content (VARCHAR), status (VARCHAR), and content (VARCHAR). The other columns are file_path (VARCHAR), file_format (VARCHAR), record_count (BIGINT), data_sequence_number (BIGINT), and file_sequence_number (BIGINT).
Examples
The following SQL query returns the manifest sequence number, status, content type, format, and record count of each entry.
SELECT manifest_sequence_number, status, content, file_format, record_count FROM iceberg_metadata('sales_db.orders_iceberg') ORDER BY manifest_sequence_number
The query returns the following results.
| manifest_sequence_number | status | content | file_format | record_count |
|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
The following SQL query counts the files that the current snapshot uses and the records in them.
SELECT count(*) AS live_files, sum(record_count) AS records FROM iceberg_metadata('sales_db.orders_iceberg') WHERE status <> 'DELETED'
The query returns the following result.
| live_files | records |
|---|---|
|
|
iceberg_partition_specs function
Returns every partition spec in the table metadata, with one row for each partition field. Each row gives the spec ID, whether the spec is the table's default spec, and the field's ID, name, transform, and source column.
A spec with no fields, such as the spec of an unpartitioned table, returns one row whose field columns are NULL.
Syntax
iceberg_partition_specs(table_name)
Arguments
The iceberg_partition_specs function takes the following argument:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'.
Return type
A table with the columns spec_id (INTEGER), is_default (BOOLEAN), field_id (BIGINT), name (VARCHAR), transform (VARCHAR), source_id (BIGINT), and source_column (VARCHAR).
Examples
The following SQL query returns the spec ID, whether the spec is the default, and whether the row has no partition field. The example table is not partitioned, so its only spec has no fields.
SELECT spec_id, is_default, name IS NULL AS no_partition_field FROM iceberg_partition_specs('sales_db.orders_iceberg')
The query returns the following result.
| spec_id | is_default | no_partition_field |
|---|---|---|
|
|
|
iceberg_partition_stats function
Returns one row for each partition field of each manifest in the current snapshot, or in the snapshot that you select. Each row gives the manifest and the snapshot that added it, the partition spec, and the partition field's ID, name, source columns, transform, and type. It also gives the lower and upper bounds of the field's values in that manifest, and whether any value is NULL or NaN.
For an unpartitioned table, the function returns no rows.
Syntax
iceberg_partition_stats(table_name [,
{ snapshot_from_id := snapshot_id
| snapshot_from_timestamp := timestamp } ])
Arguments
The iceberg_partition_stats function takes the following arguments:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'. snapshot_from_id-
The ID of the snapshot to read instead of the current snapshot, as a
UBIGINT. If no snapshot has this ID, the query fails. This argument is optional. snapshot_from_timestamp-
A point in time, as a
TIMESTAMP. The function reads the snapshot that was current at this time instead of the current snapshot. You cannot combine it withsnapshot_from_id. If the time is in the future, the query fails. This argument is optional.
Return type
A table with the columns manifest_path (VARCHAR), added_snapshot_id (BIGINT), partition_spec_id (INTEGER), partition_field_id (UBIGINT), partition_field_name (VARCHAR), partition_source_columns (VARCHAR[]), partition_field_transform (VARCHAR), partition_field_type (VARCHAR), lower_bound (VARCHAR), upper_bound (VARCHAR), contains_null (BOOLEAN), and contains_nan (BOOLEAN).
Examples
The following SQL query counts the rows that the function returns. The example table is not partitioned, so the count is 0.
SELECT count(*) AS partition_field_rows FROM iceberg_partition_stats('sales_db.orders_iceberg')
The query returns the following result.
| partition_field_rows |
|---|
|
iceberg_partitions function
Returns one row for each partition in the current snapshot, or in the snapshot that you select. Each row gives the number of records, the number of data files, and the total size of the data files in bytes. It also gives the numbers of position delete and equality delete records and files, and the time and the snapshot of the partition's last change.
For an unpartitioned table, the function returns one row for the whole table.
Syntax
iceberg_partitions(table_name [,
{ snapshot_from_id := snapshot_id
| snapshot_from_timestamp := timestamp } ])
Arguments
The iceberg_partitions function takes the following arguments:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'. snapshot_from_id-
The ID of the snapshot to read instead of the current snapshot, as a
UBIGINT. If no snapshot has this ID, the query fails. This argument is optional. snapshot_from_timestamp-
A point in time, as a
TIMESTAMP. The function reads the snapshot that was current at this time instead of the current snapshot. You cannot combine it withsnapshot_from_id. If the time is in the future, the query fails. This argument is optional.
Return type
A table. For an unpartitioned table, the columns are record_count (BIGINT), file_count (INTEGER), total_data_file_size_in_bytes (BIGINT), position_delete_record_count (BIGINT), position_delete_file_count (INTEGER), equality_delete_record_count (BIGINT), equality_delete_file_count (INTEGER), last_updated_at (TIMESTAMP_MS), and last_updated_snapshot_id (BIGINT).
Examples
The following SQL query returns the record count, the data file count, and the position delete record count. The example table is not partitioned, so the function returns one row for the whole table.
SELECT record_count, file_count, position_delete_record_count FROM iceberg_partitions('sales_db.orders_iceberg')
The query returns the following result.
| record_count | file_count | position_delete_record_count |
|---|---|---|
|
|
|
iceberg_refs function
Returns one row for each named reference in the table metadata, that is, each branch, such as main, and each tag. Each row gives the reference's name, its type (BRANCH or TAG), the ID of the snapshot that it points to, and its retention settings. A retention column is NULL when the reference does not set it.
Syntax
iceberg_refs(table_name)
Arguments
The iceberg_refs function takes the following argument:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'.
Return type
A table with the columns name (VARCHAR), type (VARCHAR), snapshot_id (BIGINT), max_reference_age_in_ms (BIGINT), min_snapshots_to_keep (INTEGER), and max_snapshot_age_in_ms (BIGINT).
Examples
The following SQL query returns the name and type of each reference.
SELECT name, type FROM iceberg_refs('sales_db.orders_iceberg') ORDER BY name
The query returns the following result.
| name | type |
|---|---|
|
|
The following SQL query checks whether each reference points to the table's newest snapshot.
SELECT r.name, r.snapshot_id = ( SELECT s.snapshot_id FROM iceberg_snapshots( 'sales_db.orders_iceberg' ) AS s ORDER BY s.sequence_number DESC LIMIT 1 ) AS points_to_newest FROM iceberg_refs('sales_db.orders_iceberg') AS r ORDER BY r.name
The query returns the following result.
| name | points_to_newest |
|---|---|
|
|
Considerations
When you query an Iceberg table by name, DuckDB reads the table's current snapshot. You cannot read a branch or a tag by its name, and AT (VERSION => ' fails. To read the snapshot that a reference points to, pass its name')snapshot_id in AT (VERSION => .snapshot_id)
iceberg_scan function
Reads the rows of an Apache Iceberg table from its location in Amazon S3, without going through a table in the AWS Glue Data Catalog. Pass the location of a table metadata file, or the table directory together with version. The function returns the table's columns from the current snapshot of that metadata, or from the snapshot that you select with snapshot_from_id or snapshot_from_timestamp.
To read an Iceberg table in the AWS Glue Data Catalog, query the table by name instead, for example SELECT * FROM sales_db.orders_iceberg. For time travel on a catalog table, write AT (VERSION => or snapshot_id)AT (TIMESTAMP => after the table name.timestamp)
iceberg_scan also accepts the named parameters of read_parquet that control how the engine reads the Parquet data files, except schema.
Syntax
iceberg_scan(location [, version := version ]
[, version_name_format := format ]
[, metadata_compression_codec := codec ]
[, allow_moved_paths := boolean ] [,
{ snapshot_from_id := snapshot_id
| snapshot_from_timestamp := timestamp } ])
Arguments
The iceberg_scan function takes the following arguments:
location-
The Amazon S3 location of a table metadata file, a file whose name ends in
.metadata.json, as aVARCHARliteral. You can also pass the table directory together withversion. version-
The metadata version to read when
locationis the table directory, as aVARCHAR. The engine looks forv, thenversion.metadata.json, in the table'sversion.metadata.jsonmetadatadirectory. This argument is optional. version_name_format-
A file name pattern, as a
VARCHARvalue, that replaces the default patterns forversion. The pattern must contain%sexactly twice: the first stands for the version and the second for the compression suffix. You can pass only one pattern. This argument is optional. metadata_compression_codec-
The compression codec of the metadata files, as a
VARCHARvalue. Set it to'gzip'when the metadata files are compressed with gzip. The default is'none'. This argument is optional. allow_moved_paths-
The option to look for the table's files under
location, as aBOOLEAN. Whentrue, the engine looks for them there even when the metadata records a different table location, for example after the files were copied. The default isfalse. This argument is optional. snapshot_from_id-
The ID of the snapshot to read instead of the current snapshot, as a
UBIGINT. If no snapshot has this ID, the query fails. This argument is optional. snapshot_from_timestamp-
A point in time, as a
TIMESTAMP. The function reads the snapshot that was current at this time instead of the current snapshot. You cannot combine it withsnapshot_from_id. If the time is in the future, the query fails. This argument is optional.
Return type
A table with the columns of the Iceberg table, in the schema of the snapshot that the function reads.
Examples
The following SQL query counts the rows of the table from its current metadata file.
SELECT count(*) AS order_count FROM iceberg_scan( 's3://amzn-s3-demo-bucket/tables/orders_iceberg/metadata/00003-7f27ce45-a215-4b96-aa29-cfc276a81182.metadata.json' )
The query returns the following result.
| order_count |
|---|
|
The following SQL query reads the table directory with version and returns the order_id and customer columns.
SELECT order_id, customer FROM iceberg_scan( 's3://amzn-s3-demo-bucket/tables/orders_iceberg', version := '00003-7f27ce45-a215-4b96-aa29-cfc276a81182' ) ORDER BY order_id
The query returns the following results.
| order_id | customer |
|---|---|
|
|
|
|
|
|
The following SQL query reads the table's first snapshot, which holds two rows. The snapshot ID comes from iceberg_snapshots.
SELECT order_id, customer FROM iceberg_scan( 's3://amzn-s3-demo-bucket/tables/orders_iceberg/metadata/00003-7f27ce45-a215-4b96-aa29-cfc276a81182.metadata.json', snapshot_from_id := 1294122691170353594 ) ORDER BY order_id
The query returns the following results.
| order_id | customer |
|---|---|
|
|
|
|
The following SQL query looks up the current metadata file of a table in the AWS Glue Data Catalog, which you can then pass to iceberg_scan.
SELECT parameters['metadata_location'] AS metadata_location FROM glue_get_table_response('sales_db.orders_iceberg')
The query returns the following result.
| metadata_location |
|---|
|
Considerations
Pass an Amazon S3 location, not a table name. A table name in iceberg_scan makes the query fail.
If the table directory has no version-hint.text file, a call with the directory and no version fails. You cannot change engine settings to let the engine choose a metadata file on its own. The table in the examples has no such file. Pass the metadata file, or pass the directory with version.
To find the current metadata file of a table in the AWS Glue Data Catalog, read the table's metadata_location parameter with glue_get_table_response in a separate query.
iceberg_scan_plan function
Plans a scan of an Iceberg table from its location in Amazon S3 and returns one row for each data file that the scan reads. Each row gives the file's path, format, size, record count, partition values, and delete files. It also gives the snapshot and schema that the plan uses.
With row_filter, the plan leaves out the files whose column statistics show that no row in the file can match the filter. Like iceberg_scan, the function takes the location of a table metadata file, or the table directory together with version, not a table name.
Syntax
iceberg_scan_plan(location
[, row_filter := predicate ]
[, produce_sequence_number := boolean ] [,
{ snapshot_from_id := snapshot_id
| snapshot_from_timestamp := timestamp } ]
[, version := version ]
[, version_name_format := format ]
[, metadata_compression_codec := codec ]
[, allow_moved_paths := boolean ])
Arguments
The iceberg_scan_plan function takes the following arguments:
location-
The Amazon S3 location of a table metadata file, a file whose name ends in
.metadata.json, as aVARCHARliteral. You can also pass the table directory together withversion. row_filter-
A predicate on the table's columns, as a
VARCHAR, for example'order_id > 2'. It cannot beNULL. This argument is optional. produce_sequence_number-
The option to fill the
sequence_numbercolumn, as aBOOLEAN. Whentrue, the function fills the column. The default isfalse, which leaves itNULL. This argument is optional. snapshot_from_id-
The ID of the snapshot to plan instead of the current snapshot, as a
UBIGINT. If no snapshot has this ID, the query fails. This argument is optional. snapshot_from_timestamp-
A point in time, as a
TIMESTAMP. The function plans the snapshot that was current at this time instead of the current snapshot. You cannot combine it withsnapshot_from_id. If the time is in the future, the query fails. This argument is optional. version-
The metadata version to read when
locationis the table directory, as aVARCHAR. The engine looks forv, thenversion.metadata.json, in the table'sversion.metadata.jsonmetadatadirectory. This argument is optional. version_name_format-
A file name pattern, as a
VARCHARvalue, that replaces the default patterns forversion. The pattern must contain%sexactly twice: the first stands for the version and the second for the compression suffix. You can pass only one pattern. This argument is optional. metadata_compression_codec-
The compression codec of the metadata files, as a
VARCHARvalue. Set it to'gzip'when the metadata files are compressed with gzip. The default is'none'. This argument is optional. allow_moved_paths-
The option to look for the table's files under
location, as aBOOLEAN. Whentrue, the engine looks for them there even when the metadata records a different table location. The default isfalse. This argument is optional.
Return type
A table with one row per data file and the columns file_path (VARCHAR), file_format (VARCHAR), file_size_in_bytes (BIGINT), record_count (BIGINT), sequence_number (BIGINT), and first_row_id (BIGINT). The next columns are partition_spec_id (INTEGER), partition_constants (STRUCT), delete_files (a list of STRUCT values), snapshot_id (BIGINT), and schema_id (INTEGER). The last columns are metadata (VARIANT), schema (STRUCT), and row_filter (VARCHAR).
Examples
The following SQL query counts the data files that a full scan reads and the records in them.
SELECT count(*) AS data_files, sum(record_count) AS records FROM iceberg_scan_plan( 's3://amzn-s3-demo-bucket/tables/orders_iceberg/metadata/00003-7f27ce45-a215-4b96-aa29-cfc276a81182.metadata.json' )
The query returns the following result.
| data_files | records |
|---|---|
|
|
The following SQL query plans a scan with a row filter. The column statistics show that only one file can hold rows with order_id greater than 2, so the plan has one file.
SELECT count(*) AS data_files, sum(record_count) AS records FROM iceberg_scan_plan( 's3://amzn-s3-demo-bucket/tables/orders_iceberg/metadata/00003-7f27ce45-a215-4b96-aa29-cfc276a81182.metadata.json', row_filter := 'order_id > 2' )
The query returns the following result.
| data_files | records |
|---|---|
|
|
iceberg_schema_properties function
Returns one row for each property of a database, which Iceberg calls a namespace, with the columns key and value. Pass the database name, not a table name.
Syntax
iceberg_schema_properties(database_name)
Arguments
The iceberg_schema_properties function takes the following argument:
database_name-
The database, as a
VARCHARliteral in the form'[catalog.]database'.
Return type
A table with the columns key (VARCHAR) and value (VARCHAR).
Examples
The following SQL query counts the properties of the sales_db database. The database has no properties, so the count is 0.
SELECT count(*) AS properties FROM iceberg_schema_properties('sales_db')
The query returns the following result.
| properties |
|---|
|
iceberg_schemas function
Returns one row for each top-level field of each schema in the table metadata. Each row gives the schema ID and the field's name, Iceberg field ID, type, doc string, and whether the field is required. The is_current column is true for the fields of the table's current schema.
An Iceberg table gets a new schema each time its columns change, for example when a writer adds, drops, or renames a column. The earlier schemas stay in the table metadata. Use iceberg_schemas to see how the columns of a table changed, or to find the field ID of a column.
The type column holds the DuckDB type that the field is read as, such as INTEGER or DECIMAL(10,2), not the Iceberg type name, such as int. A field inside a STRUCT, LIST, or MAP column has no row of its own, and the type of the column shows the full nested type. The doc column is NULL when the field has no doc string.
Syntax
iceberg_schemas(table_name)
Arguments
The iceberg_schemas function takes the following argument:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'.
Return type
A table with one row per field and the columns schema_id (INTEGER), name (VARCHAR), field_id (INTEGER), type (VARCHAR), required (BOOLEAN), doc (VARCHAR), and is_current (BOOLEAN).
Examples
The following SQL query lists the fields of the table's current schema, with their field IDs and types, and whether each field is required.
SELECT field_id, name, type, required FROM iceberg_schemas('sales_db.orders_iceberg') WHERE is_current ORDER BY field_id
The query returns the following results.
| field_id | name | type | required |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The following SQL query counts the fields in each schema of the table. The columns of the example table never changed, so the table has one schema, with the ID 0.
SELECT schema_id, is_current, count(*) AS fields FROM iceberg_schemas('sales_db.orders_iceberg') GROUP BY schema_id, is_current ORDER BY schema_id
The query returns the following result.
| schema_id | is_current | fields |
|---|---|---|
|
|
|
The following SQL query uses the field ID of order_id to read its value count from iceberg_files, whose statistics are keyed by field ID. The two data files hold three values in total.
SELECT s.name, sum(f.value_counts[s.field_id]) AS value_count FROM iceberg_schemas('sales_db.orders_iceberg') AS s CROSS JOIN iceberg_files('sales_db.orders_iceberg') AS f WHERE s.is_current AND s.name = 'order_id' GROUP BY s.name
The query returns the following result.
| name | value_count |
|---|---|
|
|
iceberg_snapshots function
Returns one row for each snapshot in the table metadata, with the snapshot's sequence number, ID, parent snapshot ID, commit time, manifest list location, operation, and summary. Use it to see when and how a table changed, for example to check whether a write committed before you retry it. You can also use it to find a snapshot ID for time travel.
summary is a MAP of the counters that the writer recorded for the snapshot, such as added-records and total-records. To read one counter, write summary['.key']
Syntax
iceberg_snapshots(table_name)
Arguments
The iceberg_snapshots function takes the following argument:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'.
Return type
A table with the columns sequence_number (UBIGINT), snapshot_id (UBIGINT), parent_id (BIGINT), timestamp_ms (TIMESTAMP_MS), manifest_list (VARCHAR), operation (VARCHAR), and summary (MAP(VARCHAR, VARCHAR)).
Examples
The following SQL query lists the snapshots in order, with the operation and the number of rows that each snapshot added.
SELECT sequence_number, operation, summary['added-records'] AS added_records FROM iceberg_snapshots('sales_db.orders_iceberg') ORDER BY sequence_number
The query returns the following results.
| sequence_number | operation | added_records |
|---|---|---|
|
|
|
|
|
|
The following SQL query returns the sequence number and the operation of the newest snapshot.
SELECT sequence_number, operation FROM iceberg_snapshots('sales_db.orders_iceberg') ORDER BY sequence_number DESC LIMIT 1
The query returns the following result.
| sequence_number | operation |
|---|---|
|
|
Considerations
The function returns snapshots in no fixed order. Add ORDER BY sequence_number or ORDER BY timestamp_ms when the order matters.
snapshot_id is a UBIGINT here, while iceberg_history and iceberg_refs return snapshot IDs as BIGINT.
Differences from Athena engine version 3
In Athena engine version 3, you query the $snapshots metadata table, for example SELECT * FROM "orders_iceberg$snapshots". In DuckDB, call iceberg_snapshots('sales_db.orders_iceberg') instead.
The columns differ from the Athena engine version 3 $snapshots table. For example, the commit time is in timestamp_ms, not committed_at.
iceberg_table_properties function
Returns one row for each property in the table's Iceberg metadata, with the columns key and value. Use it in place of SHOW TBLPROPERTIES for an Iceberg table.
Syntax
iceberg_table_properties(table_name)
Arguments
The iceberg_table_properties function takes the following argument:
table_name-
The Iceberg table, as a
VARCHARliteral in the form'[catalog.database.]table'.
Return type
A table with the columns key (VARCHAR) and value (VARCHAR).
Examples
The following SQL query lists the properties of the example table.
SELECT key, value FROM iceberg_table_properties('sales_db.orders_iceberg') ORDER BY key
The query returns the following results.
| key | value |
|---|---|
|
|
|
|
The following SQL query looks for a property that the table does not set, so the count is 0.
SELECT count(*) AS matches FROM iceberg_table_properties('sales_db.orders_iceberg') WHERE key = 'write.format.default'
The query returns the following result.
| matches |
|---|
|
Considerations
The function returns the properties in the Iceberg metadata, not the AWS Glue table parameters such as table_type and metadata_location. To read those, use the glue_get_table_response function.
A property that the table does not set returns no row.
Differences from Athena engine version 3
In Athena engine version 3, you list table properties with SHOW TBLPROPERTIES. In DuckDB, use iceberg_table_properties for an Iceberg table.
iceberg_truncate function
Returns value truncated to width, as the Iceberg truncate partition transform computes it. For an integer, the result is the largest multiple of width that is less than or equal to the value, so a negative value rounds down, away from zero. For a VARCHAR value, the result is the first width characters, and for a BLOB value, the first width bytes.
For a DECIMAL value, width applies to the unscaled value, the digits without the decimal point. For example, with a scale of 2, a width of 50 truncates to a multiple of 0.50. If value is NULL, the function returns NULL.
Syntax
iceberg_truncate(width, value)
Arguments
The iceberg_truncate function takes the following arguments:
width-
The truncation width, as a positive
INTEGER. value-
The value to truncate: an
INTEGER,BIGINT,DECIMAL,VARCHAR, orBLOBvalue.
Return type
The type of value: INTEGER, BIGINT, DECIMAL with the same precision and scale, VARCHAR, or BLOB.
Examples
The following SQL query truncates an integer to a multiple of 10.
SELECT iceberg_truncate(10, 42) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query truncates a negative integer. The result rounds down to -10, not toward zero.
SELECT iceberg_truncate(10, -1) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query keeps the first three characters of a string.
SELECT iceberg_truncate(3, 'iceberg') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query truncates a decimal with a scale of 2. The width 50 applies to the unscaled value 1065, so the result is 10.50.
SELECT iceberg_truncate(50, 10.65) AS result
The query returns the following result.
| result |
|---|
|
Considerations
width must be greater than 0. Otherwise the query fails.
There is no form for REAL, DOUBLE, date, or time values, because the Iceberg specification does not define the truncate transform for them.
set_iceberg_table_properties function
Adds or changes properties in the table's Iceberg metadata, and commits the change to the catalog. Pass the properties as a STRUCT or MAP literal of property names and values, such as {'owner': 'analytics'}.
Run the function as its own statement with CALL. It returns one row with a Success column that holds the number of properties in the call. You can also set properties with ALTER TABLE .table_name SET ('key' = 'value')
Syntax
CALL set_iceberg_table_properties(table_name,
properties)
Arguments
The set_iceberg_table_properties function takes the following arguments:
table_name-
The Iceberg table to change, as a
VARCHARliteral in the form'[catalog.database.]table'. properties-
The properties to set, as a
STRUCTorMAPliteral whose keys are property names and whose values areVARCHARproperty values.
Return type
A table with one row and one column, Success (BIGINT), the number of properties in the call.
Examples
The following SQL statement sets two properties on an Iceberg table.
CALL set_iceberg_table_properties( 'sales_db.props_iceberg', {'owner': 'analytics', 'review_status': 'pending'} )
The statement returns the following result.
| Success |
|---|
|
The following SQL query reads the two properties back. The WHERE clause leaves out any other properties of the table.
SELECT key, value FROM iceberg_table_properties('sales_db.props_iceberg') WHERE key IN ('owner', 'review_status') ORDER BY key
The query returns the following results.
| key | value |
|---|---|
|
|
|
|
Considerations
You cannot set the reserved properties format-version, uuid, snapshot-count, current-snapshot-id, current-snapshot-summary, current-snapshot-timestamp-ms, current-schema, default-partition-spec, and default-sort-order. A call that names one fails.
Differences from Athena engine version 3
In Athena engine version 3, you set table properties with ALTER TABLE . In DuckDB, write table_name SET TBLPROPERTIESALTER TABLE , or call table_name SET ('key' = 'value')set_iceberg_table_properties.
remove_iceberg_table_properties function
Removes the named properties from the table's Iceberg metadata, and commits the change to the catalog. Pass the property names as a list of VARCHAR values, such as ['review_status'].
Run the function as its own statement with CALL. It returns one row with a Success column that holds the number of properties that the table has after the call, not the number of properties that the call removed. To check the result, read the properties with iceberg_table_properties.
Syntax
CALL remove_iceberg_table_properties(table_name,
property_names)
Arguments
The remove_iceberg_table_properties function takes the following arguments:
table_name-
The Iceberg table to change, as a
VARCHARliteral in the form'[catalog.database.]table'. property_names-
The names of the properties to remove, as a
VARCHAR[]list literal.
Return type
A table with one row and one column, Success (BIGINT), the number of properties that the table has after the call.
Examples
The following SQL statement removes one of the two properties that the set_iceberg_table_properties example sets. The table also has two default properties, so Success is 3: owner and the two defaults.
CALL remove_iceberg_table_properties( 'sales_db.props_iceberg', ['review_status'] )
The statement returns the following result.
| Success |
|---|
|
The following SQL query reads back the owner and review_status properties. Only owner is left.
SELECT key, value FROM iceberg_table_properties('sales_db.props_iceberg') WHERE key IN ('owner', 'review_status') ORDER BY key
The query returns the following result.
| key | value |
|---|---|
|
|
Considerations
You cannot remove the reserved properties format-version, uuid, snapshot-count, current-snapshot-id, current-snapshot-summary, current-snapshot-timestamp-ms, current-schema, default-partition-spec, and default-sort-order. A call that names one fails.
Differences from Athena engine version 3
In Athena engine version 3, you remove table properties with ALTER TABLE , which DuckDB does not support. For an Iceberg table, call table_name UNSET TBLPROPERTIESremove_iceberg_table_properties instead.