Skip to content

[datafusion] Support persistent CREATE TABLE AS SELECT - #1448

Open
SteNicholas wants to merge 1 commit into
apache:mainfrom
SteNicholas:PAIMON-1425
Open

SteNicholas wants to merge 1 commit into
apache:mainfrom
SteNicholas:PAIMON-1425

Conversation

@SteNicholas

@SteNicholas SteNicholas commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Purpose

Closes #1425.

Materialize a bounded, read-only DataFusion query as a persistent native Paimon table. The query defines the destination schema even when it returns no rows.

CREATE TABLE paimon.reporting.results
PARTITIONED BY (region)
WITH ('bucket' = '-1')
AS SELECT id, region, score FROM scores WHERE score > 10;

scores resolves in the session's current catalog and database, independently of the destination namespace.

Brief change log

  • Infer column names, types and nullability from the analyzed query and reuse that plan to populate the table. Apply table options, comments and keys through existing schema validation.
  • Capture the session namespace for source tables and catalog SQL functions. Preserve quoted identifiers, snapshot aliases and live-table qualifiers in joins. Register time-travel prepared statements in the caller's session.
  • Check target metadata before planning the source and require exclusive creation before writing. IF NOT EXISTS skips existing targets and definite create conflicts. Uncertain publication outcomes propagate as errors.
  • Retain the created table after population failure and clean up its owned, uncommitted files. Let an in-flight commit finish after cancellation and keep files that a published snapshot may reference.

Tests

On Rust 1.98.0, 400 focused tests passed on macOS for the code in c0e1c689.

  • 41 DataFusion tests covering CTAS, time travel and read_files, including the fully qualified snapshot/live join and its persisted result.
  • 185 core tests with storage-s3 enabled, including native mount fallback, direct S3 HTTP 501 fallback, lost responses, redirects and mismatched storage error responses.
  • 174 REST catalog tests, including metadata-only target checks and exclusive create responses.

The standalone storage-gcs compilation check, formatting and git diff --check also passed.

API and Format

  • Catalog::check_table_exists defaults to load_table. Filesystem and REST catalogs check metadata without opening the table's data backend.
  • Catalog::create_table_exclusive defaults to Error::Unsupported. Filesystem CTAS requires atomic, no-overwrite publication of schema-0. REST CTAS requires server-enforced exclusive creation and a direct completion response.
  • Ordinary filesystem CREATE TABLE can fall back to a plain write when conditional publication is definitively unsupported, including on writable mounts or after a first-attempt S3 HTTP 501. A conditional object upload counts as a definite conflict only when the HTTP status and storage error agree and there was no replay.

CTAS writes the existing Paimon schema, snapshot and data-file formats. It rejects explicit column declarations, OR REPLACE and TIME outputs finer than milliseconds.

Documentation

The SQL reference describes syntax, supported types, concurrency and recovery after an uncertain create or commit. The DataFusion README includes a session-namespace example.

SQL reference · DataFusion README

🤖 Assisted with Codex.

Comment thread crates/paimon/src/catalog/mod.rs Resolved

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Persistent CTAS has end-to-end value. All 19 CTAS cases and 103 related DataFusion cases passed locally. The filesystem catalog change below blocks ordinary table creation on existing backends.

Comment thread crates/paimon/src/catalog/filesystem.rs Resolved
@SteNicholas

SteNicholas commented Nov 22, 2026 •

Copy link
Copy Markdown
Member Author

@JingsongLi, thanks for running the CTAS and DataFusion suites. I fixed the ordinary CREATE TABLE regression in 178d008. Filesystem CTAS requires atomic publication of the initial schema. Ordinary table creation can use a plain write on HDFS-native and other backends that lack conditional publication. Original review

All 19 CTAS tests passed on this revision. The catalog regression test disables conditional write and rename on a local filesystem operator, registers an existing Parquet file as a Format Table, and reads its rows back. Regression test

Rebased PAIMON-1425 onto upstream/main at fc931574 and force-pushed 690b10ea. git range-diff matched the pre-rebase patch, and git diff --check passed. Rebased commit

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Persistent CTAS has end-to-end value. I reran the ordinary CREATE regression with conditional writes and renames disabled, along with the strict creation race and conditional rename error tests. All 48 filesystem catalog tests, the REST create case, 19 existing CTAS tests and 92 related DataFusion tests passed. The additional existing-target probe found the no-op issue below.

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed f78635d4 for CTAS correctness and SQL compatibility.

@SteNicholas

Copy link
Copy Markdown
Member Author

Thanks for the review, @JingsongLi. All four fixes are in 690b10ea. I compared it with da3a9470, the commit linked in the inline replies, and git range-diff confirms the rebase preserved the patch. Original review · Current commit

  • On the built-in S3 backend, a first-attempt HTTP 412 on the conditional schema-0 write is a definite conflict. A 412 after a retry returns an unknown publication outcome, which CTAS propagates even with IF NOT EXISTS. S3 regression
  • Time-travel PREPARE plans against the captured state and registers the statement in the caller’s session. The regression runs EXECUTE historical(3) in that session and checks that it returns 6. Prepared-statement regression
  • The parser consumes the full target name before looking for the query’s AS. Ordinary CREATE TABLE mydb.as keeps dt as its partition key, and CTAS into paimon.default.as keeps id even when the query returns no rows. CREATE regression · CTAS regression
  • Unaliased scans of different snapshots get distinct bindings when their qualifiers collide, and explicit aliases retain their meaning. The regression checks that JOIN ... USING (id) finds three shared IDs and a right anti join finds the two IDs added in version 2. Snapshot-join regression

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Persistent CTAS addresses the materialization workflow in #1425. On 8aa3030c, 123 DataFusion tests, 172 REST catalog tests, and targeted conditional-publication tests passed, including lost S3 responses. One ordinary CREATE compatibility issue remains.

Comment thread crates/paimon/src/io/file_io.rs Resolved
@SteNicholas

Copy link
Copy Markdown
Member Author

Thanks for reviewing and running the tests, @JingsongLi. The ordinary CREATE TABLE compatibility issue from your review is fixed in d1314eb3. Ordinary creation on a writable file: mount falls back to a plain write when native no-overwrite publication reports Unsupported. CTAS requires atomic publication, and uncertain outcomes propagate as errors. I covered the mount behavior with fault injection and posted the details in the inline thread.

On the updated head, 1,095 focused tests passed on Rust 1.98.0 (594 DataFusion, 328 core with S3 enabled, and 173 REST catalog), including the lost S3 response and mount fallback regressions. Formatting and git diff --check passed.

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Persistent CTAS covers the durable materialization workflow in #1425. On d1314eb3, I reran 41 existing CTAS, time-travel, and read_files tests, 26 matching core publication tests, and 25 existing S3 tests. The native mount fallback and S3 lost-response cases pass locally. I reproduced a wrong result in an existing snapshot join and a compatibility failure in ordinary filesystem CREATE TABLE against an S3 mock. The Windows unit job also fails both new native publication tests. Windows CI

Comment thread crates/paimon/src/io/file_io.rs Resolved
Comment thread crates/paimon/src/io/file_io.rs Resolved
@SteNicholas
SteNicholas force-pushed the PAIMON-1425 branch 2 times, most recently from 0f32e5a to c0e1c68 Compare November 23, 2026 18:47
@SteNicholas

SteNicholas commented Nov 23, 2026 •

Copy link
Copy Markdown
Member Author

Thanks for reproducing these issues, @JingsongLi. The three fixes are on the updated PR head. Review · Commit b5efd68d

  • The reported snapshot join keeps t.id bound to the fully qualified live table. The CTAS regression reads back a count of 3. Snapshot binding reply
  • Ordinary S3 CREATE TABLE falls back to a plain PUT after a direct first-attempt HTTP 501. Exclusive CTAS requires conditional publication, and uncertain replies remain errors. S3 fallback reply
  • Both native publication tests use the portable path helper to exercise the injected outcomes. Windows path reply

Before the CI dependency fixes, 400 focused tests passed on macOS with Rust 1.98.0 (41 DataFusion, 185 core with S3 enabled, and 174 REST catalog). The standalone GCS compilation check, formatting and git diff --check also passed.

The amended commit also adds the missing uuid dev-dependency for the shared Rust integration-test mock and updates Parquet to 59.3.3 for RUSTSEC-2026-0595. The Rust integration-test targets compiled locally. The dependency policy, dependency reports, release license files, and formatting checks passed. CI run

Materializing query results required separate CREATE and INSERT
statements. Persistent CTAS derives the destination schema and rows
from one analyzed query. Exclusive creation establishes ownership
before population. A lost create response leaves ownership uncertain.

- Infer and populate native Paimon tables from bounded queries. Preserve
  output types and nullability for empty results and apply table
  options, comments and keys, addressing apache#1425.
- Resolve sources and SQL functions in the session namespace. Preserve
  quoted identifiers and snapshot scopes, register time-travel prepared
  statements in the caller's session and reject unsafe LATERAL references.
- Check target metadata and skip existing targets and definite conflicts
  with IF NOT EXISTS. Require exclusive creation, verify REST completion
  and conditional upload responses, and preserve ordinary CREATE on
  storage without conditional publication.
- Retain the table after population failure, discard owned uncommitted
  files, and finish in-flight commits after caller cancellation. Keep
  prepared files when snapshot publication may have succeeded.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Support persistent CREATE TABLE AS SELECT in DataFusion

2 participants