Skip to main content
Use the CREATE TABLE AS SELECT (CTAS) statement to synchronously or asynchronously query a table and create a new table based on the query result, and then insert the query result into the new table. You can submit an asynchronous CTAS task using SUBMIT TASK.

Syntax

  • Synchronously query a table and create a new table based on the query result, and then insert the query result into the new table.
  • Asynchronously query a table and create a new table based on the query result, and then insert the query result into the new table.

Parameters

Usage notes

  • For Paimon CTAS, use a Paimon catalog for the target table. The database rejects a Unified Catalog target before creating the table. If you omit the catalog or database from the target name, the database uses the session’s current catalog or database. See Paimon CTAS for supported options and failure behavior.
  • The following defaults apply to OLAP targets.
    • The table is a Duplicate Key table by default. You can also specify it as a Primary Key table in key_desc.
    • The sort keys are the first three columns, and the storage space of the data types of these three columns does not exceed 36 bytes.
  • If the CTAS statement fails to execute due to reasons, such as an FE restart, one of the following issues may occur:
    • A new table is created successfully but does not contain data.
    • A new table fails to be created.
  • After a new table is created, if multiple methods (such as INSERT INTO) are used to insert data into the new table, the method that completes the INSERT operation first will insert its data into the new table.
  • After a new table is created, you need to manually grant permissions on this table to users.
  • If you do not specify a name for a Task when you asynchronously query a table and create a new table based on the query result, the database automatically generates a name for the Task.

Examples

Example 1: Synchronously query a table order and create a new table order_new based on the query result, and then insert the query result into the new table.
Example 2: Synchronously query the k1, k2, and k3 columns in the table order and create a new table order_new based on the query result, and then insert the query result into the new table. Additionally, set the column names of the new table to a, b, and c.
or
Example 3: Synchronously query the largest value of the salary column in the table employee and create a new table employee_new based on the query result, and then insert the query result into the new table. Additionally, set the column name of the new table to salary_max.
After data is inserted, query the new table.
Example 4: Synchronously query the customer_id and first_name columns in the table customers and create a new table customers_new based on the query result, and then insert the query result into the new table. Additionally, set the column names of the new table to customer_id_new and first_name_new. Also, build a bitmap index for the column customer_id_new in the new table.
Example 5: Synchronously query a table customers and create a new table customers_new based on the query result, and then insert the query result into the new table. Additionally, specify the new table as a Primary Key table, and specify its sort key as first_name and last_name.
Example 6: Use CTAS to create a Primary Key table. Note that the number of data rows in the Primary Key table may be less than that in the query result. It is because the Primary Key table only stores the most recent data row among a group of rows that have the same primary key.
Example 7: Synchronously query four tables, including lineorder, customer, supplier, and part and create a new table lineorder_flat based on the query result, and then insert the query result to the new table. Additionally, specify the partitioning method and bucketing method for the new table.
Example 8: Asynchronously query the table order_detail and create a new table order_statistics based on the query result, and then insert the query result into the new table.
Check information of the Task.
Check the state of the TaskRun.
Query the new table when the state of the TaskRun is SUCCESS.