Iceberg DDL Syntax
CREATE TABLE
Spark 3 can create tables in any Iceberg catalog. Iceberg will convert the column type in Spark to corresponding Iceberg type. The command is as follows:
SET spark.sql.catalog.prod = org.apache.iceberg.spark.SparkCatalog;
SET spark.sql.catalog.prod.type = hadoop;
SET spark.sql.catalog.prod.warehouse = file:///path/to/local/warehouse;
CREATE DATABASE IF NOT EXISTS prod.db;
CREATE TABLE prod.db.sample (id bigint NOT NULL COMMENT 'unique id', data string) USING iceberg;
You need to set spark.sql.catalog.prod.warehouse to the local directory path.
Table creation commands support the full range of Spark create clauses, including:
- PARTITIONED BY to configure partitioning
- LOCATION '(fully-qualified-uri)' to set the table location
- COMMENT 'table documentation' to set a table description
- TBLPROPERTIES ('key'='value', ...) to set table configuration
PARTITIONED BY
To create a partitioned table, use the following command:
CREATE TABLE prod.db.sample (id bigint, data string, category string) USING iceberg PARTITIONED BY (category);
The PARTITIONED BY clause supports transform expressions to create hidden partitions.
CREATE TABLE prod.db.sample (id bigint, data string, category string, ts timestamp) USING iceberg PARTITIONED BY (bucket(16, id), days(ts), category);
Supported transformations are:
- year(ts): partition by year
- month(ts): partition by month
- day(ts) or date(ts): equivalent to dateint partitioning
- hour(ts) or date_hour(ts): equivalent to dateint and hour partitioning
- bucket(N, col): partition by hashed value mod N buckets
- truncate(L, col): partition by value truncated to L
- Strings are truncated to the given length (L).
- Integers and longs truncate to bins: truncate(10, i) produces partitions 0, 10, 20, 30, ...
CTAS Syntax
Iceberg supports CTAS (CREATE TABLE ... AS SELECT) as an atomic operation when using a SparkCatalog. CTAS is supported, but is not atomic when using SparkSessionCatalog.
CREATE TABLE prod.db.sample USING iceberg AS SELECT ...;
The newly created table will not inherit the partition spec and table properties from the source table in SELECT, you can use PARTITIONED BY and TBLPROPERTIES in CTAS to declare partition spec and table properties for the new table. The command is as follows:
CREATE TABLE prod.db.sample USING iceberg PARTITIONED BY (part) TBLPROPERTIES ('key'='value') AS SELECT ...; RTAS Syntax
Iceberg supports RTAS (REPLACE TABLE ... AS SELECT) as an atomic operation when using a SparkCatalog. RTAS is supported, but is not atomic when using SparkSessionCatalog.
Atomic table replacement creates a new snapshot with the results of the SELECT query, but keeps table history. Methods are as follows:
- Method 1:
REPLACE TABLE prod.db.sample USING iceberg AS SELECT ...;
- Method 2:
REPLACE TABLE prod.db.sample USING iceberg PARTITIONED BY (part) TBLPROPERTIES ('key'='value') AS SELECT ...; - Method 3:
CREATE OR REPLACE TABLE prod.db.sample USING iceberg AS SELECT ...;
DROP TABLE
The DROP TABLE behavior in Iceberg changed in 0.14. Detailed changes are as follows:
- Prior to 0.14, running DROP TABLE would remove the table from the catalog and delete the table contents as well.
- From 0.14 onwards, DROP TABLE would only remove the table from the catalog. In order to delete the table contents, DROP TABLE PURGE should be used.
- DROP TABLE (removes metadata only)
DROP TABLE prod.db.sample;
- DROP TABLE PURGE (removes metadata and deletes data)
DROP TABLE prod.db.sample PURGE;
- DROP TABLE (removes metadata only)
ALTER TABLE
Iceberg has full ALTER TABLE support in Spark 3, including:
- Renaming a table (ALTER TABLE ... RENAME TO)
ALTER TABLE prod.db.sample RENAME TO prod.db.new_name;
Note that the Hadoop catalog of Iceberg does not support table renaming.
- Setting or removing table properties (ALTER TABLE ... SET/UNSET TBLPROPERTIES)
- Setting table properties
ALTER TABLE prod.db.sample SET TBLPROPERTIES ('read.split.target-size'='268435456');SET TBLPROPERTIES can also be used to set the table comment (description).
ALTER TABLE prod.db.sample SET TBLPROPERTIES ( 'comment' = 'A table comment.');
- UNSET is used to remove properties.
ALTER TABLE prod.db.sample UNSET TBLPROPERTIES ('read.split.target-size');
- Setting table properties
- Adding, deleting, and renaming columns
- Adding columns
- To add a column to Iceberg, use the ADD COLUMNS clause with ALTER TABLE. Altering a map 'key' column by adding columns is not allowed. Only map values can be updated. Example:
- Add struct-type point columns to the prod.db.sample table.
ALTER TABLE prod.db.sample ADD COLUMN point struct<x: double, y: double>;
- Add double-type point.z columns to the prod.db.sample table.
ALTER TABLE prod.db.sample ADD COLUMN point.z double;
- Add array-type points columns to the prod.db.sample table.
ALTER TABLE prod.db.sample ADD COLUMN points array<struct<x: double, y: double>>;
- Add struct-type point columns to the prod.db.sample table.
- Add columns in any position by adding FIRST or AFTER clauses. Examples are as follows:
- Example 1:
ALTER TABLE prod.db.sample ADD COLUMN new_column bigint AFTER {Existing field names in the table (for example, ID and data)}; - Example 2:
ALTER TABLE prod.db.sample ADD COLUMN new_column bigint FIRST;
- Example 1:
- To add a column to Iceberg, use the ADD COLUMNS clause with ALTER TABLE. Altering a map 'key' column by adding columns is not allowed. Only map values can be updated. Example:
- Renaming columns (ALTER TABLE ... RENAME COLUMN)
Iceberg allows any field to be renamed. To rename a field, use RENAME COLUMN.
ALTER TABLE prod.db.sample RENAME COLUMN data TO payload;
- You can use ALTER TABLE ... DROP COLUMN to drop columns.
ALTER TABLE prod.db.sample DROP COLUMN id;
- Adding columns
- Adding, deleting, and renaming nested fields
To add or remove columns from a struct, use ADD COLUMN or DROP COLUMN with a nested column name.
- Reordering top-level columns and nested struct fields.
Iceberg allows reordering top-level columns or columns in a struct using FIRST and AFTER clauses.
ALTER TABLE prod.db.sample ALTER COLUMN id FIRST;
- Widening the type of int, float, and decimal fields (ALTER TABLE ... ALTER COLUMN)
ALTER COLUMN is used to widen types, make a field optional, set comments, and reorder fields.
Iceberg allows updating column types if the update is safe. Safe updates are:
- int to bigint
- float to double
- decimal(P,S) to decimal(P2,S) when P2 > P (Precision can be increased, but scale must remain unchanged.)
For example, run the following command to change the data type of the measurement column to double:
ALTER TABLE prod.db.sample ALTER COLUMN measurement TYPE double;
Column comments can also be updated using ALTER COLUMN:ALTER TABLE prod.db.sample ALTER COLUMN {Double-type field} COMMENT 'unit is kilobytes per second'; - Making required columns optional
Nullability for a non-nullable column can be changed using DROP NOT NULL.
ALTER TABLE prod.db.sample ALTER COLUMN id DROP NOT NULL;
- SQL extensions can be used to add support for partition evolution and setting a table's write order.
DDL Syntax of Iceberg SQL Extensions
These commands are available in Spark 3 when you use Iceberg SQL extensions.
ALTER TABLE ... ADD PARTITION FIELD;
- ADD PARTITION FIELD
Iceberg supports adding new partition fields to a spec using ADD PARTITION FIELD.
ALTER TABLE prod.db.sample ADD PARTITION FIELD id;
Partition transforms are also supported.
- Example 1:
ALTER TABLE prod.db.sample ADD PARTITION FIELD bucket(16, id);
- Example 2:
ALTER TABLE prod.db.sample ADD PARTITION FIELD truncate(4, data);
- Example 3:
ALTER TABLE prod.db.sample ADD PARTITION FIELD year(ts);
- Example 4
ALTER TABLE prod.db.sample ADD PARTITION FIELD day(ts);
Adding a partition field is a metadata operation and does not change any of the existing table data. New data will be written with the new partitioning, but existing data will remain in the old partition layout. Old data files will have null values for the new partition fields in metadata tables.
Dynamic partition overwrite behavior will change when the table's partitioning changes because dynamic overwrite replaces partitions implicitly. To overwrite explicitly, use the new DataFrameWriterV2 API.
- Example 1:
- DROP PARTITION FIELD
Partition fields can be removed using DROP PARTITION FIELD. Note that although the partition is removed, the column will still exist in the table schema.
Dropping a partition field is a metadata operation and does not change any of the existing table data. New data will be written with the new partitioning, but existing data will remain in the old partition layout.
- Create an Iceberg table. For example:
CREATE TABLE prod.db.sample (id bigint, data string, category string, ts timestamp, shard int) USING iceberg PARTITIONED BY (category);
- Remove partition fields.
- Example 1:
ALTER TABLE prod.db.sample DROP PARTITION FIELD catalog;
- Example 2:
ALTER TABLE prod.db.sample DROP PARTITION FIELD bucket(16, id);
- Example 3:
ALTER TABLE prod.db.sample DROP PARTITION FIELD truncate(4, data);
- Example 4
ALTER TABLE prod.db.sample DROP PARTITION FIELD year(ts);
- Example 5:
ALTER TABLE prod.db.sample DROP PARTITION FIELD shard;
- Example 1:
- Create an Iceberg table. For example:
- REPLACE PARTITION FIELD
A partition field can be replaced by a new partition field in a single metadata update by using REPLACE PARTITION FIELD.
- Example 1:
ALTER TABLE prod.db.sample REPLACE PARTITION FIELD ts_day WITH day(ts);
- Example 2:
ALTER TABLE prod.db.sample REPLACE PARTITION FIELD ts_day WITH day(ts) AS day_of_ts;
- Example 1:
- WRITE ORDERED BY
Iceberg tables can be configured with a sort order that is used to automatically sort data that is written to the table in some engines. For example, MERGE INTO in Spark will use the table ordering.
To set the write order for a table, use WRITE ORDERED BY. Examples are as follows:
- Example 1:
ALTER TABLE prod.db.sample WRITE ORDERED BY category, id;
- Example 2:
ALTER TABLE prod.db.sample WRITE ORDERED BY category ASC, id DESC;
- Example 3:
ALTER TABLE prod.db.sample WRITE ORDERED BY category ASC NULLS LAST, id DESC NULLS FIRST;
WRITE ORDERED BY sets a global ordering where rows are ordered across tasks, like using ORDER BY in an INSERT command.INSERT INTO prod.db.sample SELECT id, data, category, ts FROM {Another able} ORDER BY ts, category;To order within each task, not across tasks, use LOCALLY ORDERED BY.
ALTER TABLE prod.db.sample WRITE LOCALLY ORDERED BY category, id;
To cancel the sort order of the table, use UNORDERED.
ALTER TABLE prod.db.sample WRITE UNORDERED;
- Example 1:
- WRITE DISTRIBUTED BY PARTITION
WRITE DISTRIBUTED BY PARTITION will request that each partition is handled by one writer, the default implementation is hash distribution.
ALTER TABLE prod.db.sample WRITE DISTRIBUTED BY PARTITION;
DISTRIBUTED BY PARTITION and LOCALLY ORDERED BY may be used together, to distribute by partition and locally order rows within each task.
ALTER TABLE prod.db.sample WRITE DISTRIBUTED BY PARTITION LOCALLY ORDERED BY category, id;
- SET IDENTIFIER FIELDS
Iceberg supports setting identifier fields to a spec using SET IDENTIFIER FIELDS. A Spark table can support Flink SQL upsert operations if the table has identifier fields.
- Example 1: Ensure that all fields are set.
ALTER TABLE prod.db.sample SET IDENTIFIER FIELDS id;
- Example 2:
ALTER TABLE prod.db.sample SET IDENTIFIER FIELDS id, data;
Identifier fields must be NOT NULL columns when they are created or added. The later ALTER TABLE ... SET IDENTIFIER FIELDS statement will overwrite the previous setting.
- Example 1: Ensure that all fields are set.
- DROP IDENTIFIER FIELDS
When removing identifier fields using DROP IDENTIFIER FIELDS, although the identifier is removed, the column will still exist in the table schema.
- Example 1: Ensure that all fields are set.
ALTER TABLE prod.db.sample DROP IDENTIFIER FIELDS id;
- Example 2:
ALTER TABLE prod.db.sample DROP IDENTIFIER FIELDS id, data;
- Example 1: Ensure that all fields are set.
Branching and Tagging DDL
- CREATE BRANCH
Branches can be created via the CREATE BRANCH statement with the following options:
- IF NOT EXISTS: Do not fail if the branch already exists.
- CREATE OR REPLACE: Update the branch if it already exists.
- Create a branch at a specific snapshot.
- Create a branch with a specified retention period.
- Example 1:
ALTER TABLE prod.db.sample CREATE BRANCH `audit-branch`;
- Example 2:
ALTER TABLE prod.db.sample CREATE BRANCH IF NOT EXISTS `audit-branch`;
- Example 3:
ALTER TABLE prod.db.sample CREATE OR REPLACE BRANCH `audit-branch`;
- Example 1:
- CREATE TAG
Tags can be created via the CREATE TAG statement with the following options:
- IF NOT EXISTS: Do not fail if the tag already exists.
- CREATE OR REPLACE: Update the tag if it already exists
- Create a tag at a specific snapshot.
- Create a tag with a specified retention period.
- Example 1:
ALTER TABLE prod.db.sample CREATE TAG `historical-tag`;
- Example 2:
ALTER TABLE prod.db.sample CREATE TAG IF NOT EXISTS `historical-tag`;
- Example 3:
ALTER TABLE prod.db.sample CREATE OR REPLACE TAG `historical-tag`;
- Example 1:
- REPLACE BRANCH
The snapshot which a branch references can be updated via the REPLACE BRANCH statement. Retention can also be updated in this statement.
ALTER TABLE prod.db.sample REPLACE BRANCH `audit-branch` AS OF VERSION {current-snapshot-id} RETAIN 60 DAYS; - REPLACE TAG
The snapshot which a tag references can be updated via the REPLACE TAG statement. Retention can also be updated in this statement.
ALTER TABLE prod.db.sample REPLACE TAG `historical-tag` AS OF {current-snapshot-id} RETAIN 60 DAYS; - DROP BRANCH
Branches can be removed via the DROP BRANCH statement.
ALTER TABLE prod.db.sample DROP BRANCH `audit-branch`;
- DROP TAG
Tags can be removed via the DROP TAG statement.
ALTER TABLE prod.db.sample DROP TAG `historical-tag`;
DDL Syntax of Iceberg Views
Iceberg views are designed to be engine-agnostic, allowing the same view definition to yield consistent behavior and results across different query engines. This enhances flexibility and compatibility in data querying.
Iceberg views are a common representation of SQL views and aim to be interpreted across multiple query engines. This section covers how to create and manage views in Spark 3.4 and later versions.
- Creating a view
Create a simple view without any comments or properties.
CREATE VIEW <viewName> AS SELECT * FROM <tableName>;
Using IF NOT EXISTS prevents the SQL statement from failing in case the view already exists.
CREATE VIEW IF NOT EXISTS <viewName> AS SELECT * FROM <tableName>;
Create a view with a comment, including aliased and commented columns that are different from the source table.
CREATE VIEW <viewName> (ID COMMENT 'Unique ID', ZIP COMMENT 'Zipcode') COMMENT 'View Comment' AS SELECT id, zip FROM <tableName>;
- Creating a view with properties
Create a view with properties using TBLPROPERTIES.
CREATE VIEW <viewName> TBLPROPERTIES ('key1' = 'val1', 'key2' = 'val2') AS SELECT * FROM <tableName>;Display view properties.
SHOW TBLPROPERTIES <viewName>;
- Dropping a view
- Drop an existing view.
DROP VIEW <viewName>;
- Using IF EXISTS prevents the SQL statement from failing if the view does not exist.
DROP VIEW IF EXISTS <viewName>;
- Drop an existing view.
- Replacing a view
Update a view's schema, its properties, or the underlying SQL statement using CREATE OR REPLACE:
CREATE OR REPLACE <viewName> (updated_id COMMENT 'updated ID') TBLPROPERTIES ('key1' = 'new_val1') AS SELECT id FROM <tableName>; - Setting and removing view properties
- Set the properties of an existing view using ALTER VIEW ... SET TBLPROPERTIES.
ALTER VIEW <viewName> SET TBLPROPERTIES ('key1' = 'val1', 'key2' = 'val2'); - Remove the properties from an existing view using ALTER VIEW ... UNSET TBLPROPERTIES.
ALTER VIEW <viewName> UNSET TBLPROPERTIES ('key1', 'key2');
- Set the properties of an existing view using ALTER VIEW ... SET TBLPROPERTIES.
- Showing available views
List all views in the currently set namespace (set using USE <namespace>).
SHOW VIEWS;
List all available views in the defined catalog and/or namespace using one of the below variations:
- List all available views in the defined catalog.
SHOW VIEWS IN <catalog>;
- List all available views in the defined namespace.
SHOW VIEWS IN <namespace>;
- List all available views in the defined catalog and namespace.
SHOW VIEWS IN <catalog>.<namespace>;
- List all available views in the defined catalog.
- Showing the CREATE statement of a view
SHOW CREATE TABLE <viewName>;
- Displaying view details using DESCRIBE
DESCRIBE [EXTENDED] <viewName>;
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot