CREATE TABLE ... FLOW (pipelines)

Important

This feature is in Beta.

Use the CREATE TABLE ... FLOW statement to create a managed table in a pipeline, written by one or more flows.

To create a managed table with a single flow from Databricks SQL, outside of a pipeline definition, see CREATE TABLE ... FLOW.

Syntax

CREATE TABLE
  table_name
  [ table_specification ]
  [ table_clauses ]
  [ flow_clause ]

table_specification
  ( { column_identifier column_type [column_properties] } [, ...]
    [ CONSTRAINT expectation_name EXPECT (expectation_expr)
        [ ON VIOLATION { FAIL UPDATE | DROP ROW } ] ] [, ...] )

table_clauses
  { PARTITIONED BY (col [, ...]) |
    CLUSTER BY clause |
    LOCATION path |
    COMMENT table_comment |
    TBLPROPERTIES clause |
    WITH { ROW FILTER clause } |
    copy_clause } [ ... ]

copy_clause
  { COPY | NOCOPY } { * | copy_option | ( copy_option [, ...] ) }

copy_option
  { TBLPROPERTIES | TBLPROPERTY property_key |
    COMMENT | COLUMN COMMENTS |
    CONSTRAINTS | CHECK CONSTRAINTS | CONSTRAINT constraint_name |
    DELETE AFTER |
    CLUSTER BY }

flow_clause
  FLOW [ IMPORT CHECKPOINT checkpoint_path ] INSERT BY NAME query

To fan multiple sources into one managed table, declare several flows that target it with CREATE FLOW (pipelines):

CREATE FLOW flow_name AS INSERT INTO table_name BY NAME query

Parameters

  • table_name

    The name of the managed table to create. If the name is not qualified, the table is created in the pipeline's target schema. The name must not already belong to a streaming table.

  • table_specification

    Optionally defines the columns, their types, properties, and descriptions. If omitted, the schema is inferred from the flow query.

  • CONSTRAINT expectation_name EXPECT (expectation_expr) [ ON VIOLATION { FAIL UPDATE | DROP ROW } ]

    Adds data quality expectations to the managed table. These data quality expectations can be tracked over time and accessed through the pipeline's event log. A FAIL UPDATE expectation causes the processing to fail when both creating the table as well as refreshing the table. A DROP ROW expectation causes the entire row to be dropped if the expectation is not met. See Manage data quality with pipeline expectations.

    expectation_expr can be composed of literals, column identifiers within the table, and deterministic, built-in SQL functions or operators except:

    Also expectation_expr must not contain any subquery.

  • PARTITIONED BY (col [, ...])

    Optionally partitions the table by a subset of columns.

  • CLUSTER BY clause

    Optionally enables liquid clustering on the table. You cannot combine PARTITIONED BY and CLUSTER BY.

  • LOCATION path

    An optional storage location for the table data.

  • COMMENT table_comment

    A STRING literal describing the table.

  • TBLPROPERTIES clause

    Optionally sets one or more user-defined table properties.

  • WITH ROW FILTER clause

    Adds a row filter function to the table. Future queries for that table receive a subset of the rows for which the function evaluates to TRUE.

  • COPY or NOCOPY copy_option

    (Beta) Controls whether a refresh keeps metadata that exists on the table but isn't in the current definition, such as a property or comment added with ALTER TABLE. COPY keeps the named metadata. NOCOPY drops it. You can specify more than one clause, in any order. See Preserve table metadata across refreshes.

  • FLOW INSERT BY NAME query

    Defines an append flow that inserts the result of query into the table, matching result columns to table columns by name. query can reference batch or streaming sources. Each named flow processes its input exactly one time per pipeline update, identical to FLOW INSERT BY NAME on a streaming table.

  • IMPORT CHECKPOINT checkpoint_path

    A STRING literal with the path to an existing Structured Streaming checkpoint to import into the flow, so a migrated stream resumes from its last committed offset instead of reprocessing the source. Importing a checkpoint is in Beta. See Migrate a Structured Streaming checkpoint.

Migrate a Structured Streaming checkpoint

Important

Importing a checkpoint is in Beta.

Use IMPORT CHECKPOINT to migrate an existing Structured Streaming workload to a pipeline without reprocessing the source. Set checkpoint_path to the checkpointLocation your Structured Streaming query used, which can be a cloud storage, Unity Catalog volume, or DBFS path. On the first pipeline update, the flow clones that checkpoint into the pipeline's managed storage. The flow then resumes from the last committed offset with its state (such as aggregations, deduplication keys, and watermarks) intact. Subsequent pipeline updates use the flow's cloned checkpoint; the original checkpoint is not modified.

Warning

Stop the original Structured Streaming query before you run the pipeline. The original Structured Streaming query can be reused after the import, but you need to manage its checkpoint state and make sure the pipeline and the query do not write to the same table at the same time, which can produce duplicate data.

Recreate the Structured Streaming query as a pipeline flow that continues writing to the table the query wrote to, and import its checkpoint:

-- Use the existing table your Structured Streaming query writes to; CREATE TABLE adopts it.
CREATE TABLE streaming_table
-- Continue from the imported checkpoint instead of reprocessing the source.
FLOW IMPORT CHECKPOINT '/Volumes/my_catalog/my_schema/checkpoints/my_stream'
INSERT BY NAME
  -- The same source your original query read from.
  SELECT * FROM STREAM(source_table);

The checkpoint is imported only on the first pipeline update. Later updates ignore IMPORT CHECKPOINT. A full refresh does not re-import the checkpoint. It starts from a new, empty checkpoint and reprocesses the source.

Preserve table metadata across refreshes

Important

COPY and NOCOPY for managed tables are in Beta.

Each pipeline update applies the table definition in your source code. By default, a refresh drops table properties and CHECK constraints that you added outside the definition, such as with ALTER TABLE ... SET TBLPROPERTIES or ALTER TABLE ... ADD CONSTRAINT, and keeps table and column comments. Use COPY and NOCOPY to control which existing metadata a refresh keeps. The clauses apply to both regular updates and full refreshes.

A value in the table definition takes precedence over a copied value. For example, if the table has the property owner = 'a' and the definition sets TBLPROPERTIES ('owner' = 'b'), the table ends up with owner = 'b', even with COPY TBLPROPERTIES.

When COPY and NOCOPY both cover some metadata, the more specific option wins: a keyed option such as TBLPROPERTY 'team' or DELETE AFTER beats a category such as TBLPROPERTIES, and a category beats *. Overlapping options at the same level conflict. For example, COPY * NOCOPY COMMENT keeps all supported metadata except the table comment.

Requirements

  • Databricks Runtime 19 and above. On earlier runtimes, SQL definitions fail with a syntax error, and Python definitions fail with UNSUPPORTED_FEATURE.COPY_NOCOPY, or with NotImplementedError from a Spark Connect client.
  • A pipeline that uses Unity Catalog. Managed tables don't support the Hive metastore.
  • If you define a pipeline from a Spark Connect client, the client must support the copy and nocopy parameters. Older clients raise NotImplementedError: copy/nocopy is not yet supported in Spark Connect.

Enable COPY and NOCOPY

To use COPY and NOCOPY, enable them for each pipeline. Add the following key to the pipeline's configuration:

"configuration": {
  "spark.databricks.sql.copyNoCopy.enabled": "true"
}

Without this setting, a managed table definition with a COPY or NOCOPY clause fails. SQL definitions fail with a syntax error (PARSE_SYNTAX_ERROR) at COPY or NOCOPY, and Python definitions fail with UNSUPPORTED_FEATURE.COPY_NOCOPY.

Supported options

Option COPY keeps NOCOPY drops
* All the metadata in the other rows: table properties, comments, CHECK constraints, and auto time-to-live. The clustering rule for CLUSTER BY also applies. Not supported.
TBLPROPERTIES All user-defined table properties. All user-defined table properties.
TBLPROPERTY property_key The named table property. Keys are case-sensitive. The named table property.
COMMENT The table comment. The table comment.
COLUMN COMMENTS Column comments, matched by column name. Column comments that the definition doesn't set.
CONSTRAINTS, CHECK CONSTRAINTS, CONSTRAINT constraint_name All CHECK constraints, or the named CHECK constraint. All CHECK constraints, or the named CHECK constraint.
DELETE AFTER The auto time-to-live setting. TBLPROPERTIES also covers it, but DELETE AFTER takes precedence. The auto time-to-live setting.
CLUSTER BY Nothing. Fails with COPY_NOCOPY_CLUSTER_BY unless the table has no clustering or the definition keeps the same clustering. Nothing. Fails with COPY_NOCOPY_CLUSTER_BY unless the table has no clustering or the definition keeps the same clustering.

Without a COPY or NOCOPY clause, a refresh keeps the table comment and column comments that the definition doesn't set, and drops user-defined table properties and CHECK constraints.

A refresh keeps grants and tags, because it doesn't replace the table in Unity Catalog. COPY and NOCOPY don't control them, and COPY * doesn't include them.

Unsupported options and errors

Clause Error
NOCOPY * UNSUPPORTED_FEATURE.COPY_NOCOPY_ALL: NOCOPY * is not supported. Name the categories to drop instead.
ROW FILTER, COLUMN MASKS, PRIMARY KEY, FOREIGN KEYS, UNIQUE CONSTRAINTS, GRANTS, TAGS, TAG, COLUMN TAGS COPY_NO_COPY_UNSUPPORTED_OPTION
CONSTRAINTS, or CONSTRAINT constraint_name naming a primary or foreign key, on a table that has a primary or foreign key COPY_NO_COPY_UNSUPPORTED_OPTION
* or CLUSTER BY on a clustered table whose definition changes or removes the clustering UNSUPPORTED_FEATURE.COPY_NOCOPY_CLUSTER_BY: Cannot modify CLUSTER BY metadata with COPY/NOCOPY. Apply the CLUSTER BY change using a dedicated ALTER TABLE statement.
COPY or NOCOPY on a streaming table or materialized view in a pipeline UNSUPPORTED_FEATURE.COPY_NOCOPY_TABLE. COPY and NOCOPY are supported only on managed tables. On a view, the clause is rejected when the definition is parsed.
The same or overlapping options at the same level in both a COPY and a NOCOPY clause, such as COPY COMMENT NOCOPY COMMENT or COPY CONSTRAINTS NOCOPY CHECK CONSTRAINTS COPY_CONFLICT
The same option twice in one list, such as COPY (TBLPROPERTY 'a', TBLPROPERTY 'a') DUPLICATE_CLAUSES

Example

-- Keep properties and comments added with ALTER TABLE, but drop the auto time-to-live setting.
CREATE TABLE orders
COPY (TBLPROPERTIES, COMMENT, COLUMN COMMENTS)
NOCOPY DELETE AFTER
FLOW INSERT BY NAME
  SELECT * FROM STREAM(samples.tpch.orders);

-- Keep everything that the definition doesn't set, except one property.
CREATE TABLE events
COPY *
NOCOPY TBLPROPERTY 'team'
FLOW INSERT BY NAME
  SELECT * FROM STREAM(source_events);

Limitations

  • Managed tables do not support CDC change flows. AUTO CDC INTO (SQL) or apply_changes / apply_changes_from_snapshot (Python) against a managed table fails with MANAGED_TABLE_DOES_NOT_SUPPORT_CDC. For CDC targets, use a CREATE STREAMING TABLE (pipelines) with a flow defined by CREATE FLOW (pipelines).
  • Managed tables do not support FLOW ... REPLACE WHERE. Only FLOW INSERT BY NAME is supported. To recompute and overwrite a targeted subset of a table, create a streaming table and define a REPLACE WHERE flow with CREATE FLOW (pipelines). See Batch processing with REPLACE WHERE flows.
  • Managed tables are supported only in pipelines with Unity Catalog. The Hive metastore is not supported.
  • You cannot reuse the name of an existing streaming table or materialized view for a managed table, or the reverse. Redefining a name as a different table type fails with CANNOT_CHANGE_TABLE_MODEL_TYPE. Drop the existing table first.
  • IMPORT CHECKPOINT is supported only on FLOW INSERT BY NAME.
  • When a refresh drops a table or column comment, Unity Catalog can return the old comment. DESCRIBE TABLE and DESCRIBE DETAIL show the comment as removed, but information_schema and the Tables API still show the old value. Comments that the definition sets sync correctly.

Examples

-- Create a managed table populated by an inline append flow from a streaming table
CREATE TABLE output
FLOW INSERT BY NAME SELECT * FROM STREAM(samples.tpch.orders);

-- Create a managed table that ingests files with schema inference and evolution
CREATE TABLE raw_data
FLOW INSERT BY NAME
  SELECT * FROM STREAM read_files('/Volumes/catalog/schema/landing/base/path');

-- Create a partitioned managed table from a streaming source
CREATE TABLE events
PARTITIONED BY (bucket)
FLOW INSERT BY NAME
  SELECT id, bucket FROM STREAM read_files('/Volumes/catalog/schema/landing/my_path', format => 'json');

-- Create a managed table with liquid clustering
CREATE TABLE orders_clustered
CLUSTER BY (order_date, customer_id)
FLOW INSERT BY NAME
  SELECT
    o_orderkey   AS order_id,
    o_custkey    AS customer_id,
    o_orderdate  AS order_date,
    o_totalprice AS total_price
  FROM STREAM(samples.tpch.orders);

-- Create a managed table with a data quality expectation that drops violating rows
CREATE TABLE valid_events
  (CONSTRAINT positive_id EXPECT (id > 0) ON VIOLATION DROP ROW)
FLOW INSERT BY NAME
  SELECT id FROM STREAM read_files('/Volumes/catalog/schema/landing/path', format => 'json');

Additional resources