DDL Syntax
Creating a Table
You can create Paimon tables in a Paimon catalog. Tables created within the Paimon catalog are managed by the catalog. When a table is deleted from the catalog, its table files are also deleted.
CREATE TABLE my_table (
user_id BIGINT,
item_id BIGINT,
behavior STRING,
dt STRING,
hh STRING
)
PARTITIONED BY (dt, hh)
TBLPROPERTIES (
'primary-key' = 'dt,hh,user_id',
'bucket' = '2'
)
LOCATION 'obs://{bucket}/path/to/table'; - PARTITIONED BY: Configures table partitioning.
- LOCATION '(fully-qualified-uri)': Sets the table location. If LOCATION is specified, a foreign table is created. Deleting the table only removes the metadata, not the data files. If data already exists in the specified location, you can omit column definitions, partitions, and properties. The new table will inherit all information from the metadata of the existing table.
- bucket: Configures bucketing. A bucket is the smallest storage unit for reads and writes. Unpartitioned tables, or partitions in partitioned tables, are sub-divided into buckets. This distribution enables concurrent read and write operations, significantly improving overall data read and write performance. You can specify bucketing columns by providing the bucket-key option. If no bucket-key option is specified, the primary key (if defined) will be used as the bucket key.
- COMMENT 'table documentation': Specifies the table description.
- TBLPROPERTIES ('key'='value', ...): Specifies the table configuration.
- primary key: Specifies the primary key field.
- You can specify a primary key during Paimon table creation to create a primary key table. Primary keys consist of a set of columns that contain unique values for each record. If you define a table with a primary key, you can insert, update, or delete records in the table. Paimon enforces data ordering by sorting the primary key within each bucket, allowing you to achieve high performance by applying filtering conditions on the primary key. A primary key table adopts the LSM tree structure to store data, enabling efficient read and write operations.
- If no primary key is specified, an append-only table is created. An append-only tables supports only INSERT operations. They do not generate changelogs and are suitable for scenarios that do not require streaming updates.
- The partition key must be selected from the primary key.
- Not all primary keys can be set as partition keys.
CTAS Syntax
Paimon supports executing CTAS (CREATE TABLE ... AS SELECT) as an atomic operation.
A new table generated by a CTAS operation does not inherit the partitioning scheme or table properties from the source table specified in the SELECT statement. To configure partitioning or table properties for the new table, you can explicitly declare them using the PARTITIONED BY clause and TBLPROPERTIES clause in the CTAS statement.
-- Basic CTAS
CREATE TABLE my_table_as USING paimon AS SELECT * FROM my_table;
-- CTAS with partitioning and primary key:
CREATE TABLE my_table_all_as USING paimon PARTITIONED BY (dt) TBLPROPERTIES ('primary-key' = 'dt,hh') AS SELECT * FROM my_table_all; Notes
For append-only tables (tables without a primary key), if the bucket property is specified, the bucket-key property must also be specified.
RTAS Syntax
Paimon supports executing RTAS (REPLACE TABLE ... AS SELECT) as an atomic operation. Atomic table replacement creates a new snapshot based on the SELECT query results while preserving the table's history.
-- Basic RTAS
REPLACE TABLE paimon.db.sample USING paimon AS SELECT ...;
CREATE OR REPLACE TABLE paimon.db.sample USING paimon AS SELECT ...;
-- RTAS with partitioning and bucketing:
REPLACE TABLE paimon.db.sample USING paimon PARTITIONED BY (pt) TBLPROPERTIES ('bucket' = '2', 'bucket-key' = 'id') AS SELECT ...; Deleting a Table
DROP TABLE my_table;
- MANAGED: Deletes both metadata and data files in OBS.
- EXTERNAL: Deletes only metadata. Data files in OBS are retained.
ALTER TABLE
Paimon supports the following ALTER TABLE operations:
Setting/Removing table properties
ALTER TABLE my_table SET TBLPROPERTIES ('write-buffer-size' = '256 MB'); ALTER TABLE my_table UNSET TBLPROPERTIES ('write-buffer-size'); Setting/Removing table comments
ALTER TABLE my_table SET TBLPROPERTIES ('comment' = 'table comment'); ALTER TABLE my_table UNSET TBLPROPERTIES ('comment'); Renaming a table
ALTER TABLE my_table RENAME TO my_table_new;
Note: Exercise caution when using this syntax in object storage (such as OBS), as the renaming operations in object storage are not atomic.
Adding a column
ALTER TABLE my_table ADD COLUMNS (c1 INT, c2 STRING); -- Add a nested column to the struct type. ALTER TABLE my_table ADD COLUMN v.f3 STRING; -- Add a nested column to a struct element in an array. ALTER TABLE my_table ADD COLUMN v.element.f3 STRING; -- Add a nested column to the value struct in a map. ALTER TABLE my_table ADD COLUMN v.value.f3 STRING;
Specifying the column position
ALTER TABLE my_table ADD COLUMN c INT FIRST; ALTER TABLE my_table ADD COLUMN c INT AFTER b;
Renaming a column
ALTER TABLE my_table RENAME COLUMN c0 TO c1; -- Rename a nested column. ALTER TABLE my_table RENAME COLUMN v.f1 to f100; ALTER TABLE my_table RENAME COLUMN v.element.f1 to f100; ALTER TABLE my_table RENAME COLUMN v.value.f1 to f100;
Dropping a column
ALTER TABLE my_table DROP COLUMNS (c1, c2); -- Drop a nested column. ALTER TABLE my_table DROP COLUMN v.f2; ALTER TABLE my_table DROP COLUMN v.element.f2; ALTER TABLE my_table DROP COLUMN v.value.f2;
Dropping a partition
ALTER TABLE my_table DROP PARTITION (`id` = 1, `name` = 'paimon');
Modifying column comments
ALTER TABLE my_table ALTER COLUMN buy_count COMMENT 'buy count';
Modifying the column position
ALTER TABLE my_table ALTER COLUMN col_a FIRST; ALTER TABLE my_table ALTER COLUMN col_a AFTER col_b;
Modifying the column type (widening)
ALTER TABLE my_table ALTER COLUMN col_a TYPE DOUBLE; -- Modify the nested column type. ALTER TABLE my_table ALTER COLUMN v.f2 TYPE BIGINT; ALTER TABLE my_table ALTER COLUMN v.element.f2 TYPE BIGINT; ALTER TABLE my_table ALTER COLUMN v.value.f2 TYPE BIGINT;
Tag Management
Creating a Tag
-- Create a tag based on the latest snapshot. ALTER TABLE T CREATE TAG `TAG-1`; -- Create the tag if it does not exist. ALTER TABLE T CREATE TAG IF NOT EXISTS `TAG-1`; -- Create a tag that can be retained for seven days. ALTER TABLE T CREATE TAG `TAG-2` RETAIN 7 DAYS; -- Create a tag based on a specified snapshot. ALTER TABLE T CREATE TAG `TAG-3` AS OF VERSION 1; -- Create or replace a tag. ALTER TABLE T CREATE OR REPLACE TAG `TAG-5` AS OF VERSION 2 RETAIN 24 HOURS;
Deleting a tag
ALTER TABLE T DELETE TAG `TAG-1`; ALTER TABLE T DELETE TAG IF EXISTS `TAG-1`; ALTER TABLE T DELETE TAG `TAG-1,TAG-2`;
Renaming a tag
ALTER TABLE T RENAME TAG `TAG-1` TO `TAG-2`;
Viewing tags
SHOW TAGS T;
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