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 in one statement. The query supplies 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.

Brief change log

  • Infer column names, types and nullability from the analyzed query. Reuse that plan for population and apply table options, keys and partitioning through Paimon schema validation.
  • Preserve quoted identifiers, session-scoped SQL functions, and snapshot qualifiers in joins and nested field access. Keep time-travel prepared statements in the caller's session.
  • Check target metadata before source planning. IF NOT EXISTS skips existing targets and definite create conflicts. An uncertain create outcome returns an error.
  • Bind writes to the table identity returned by exclusive creation. Recheck that identity before writing and at snapshot publication. The bundled REST server also validates the base snapshot UUID and next snapshot ID.
  • Retain the created table if population fails. Discard owned, safely uncommitted files, let in-flight publication finish after cancellation, and retain files when the publication outcome is uncertain.

Tests

Verified on macOS with Rust 1.98.0. Tested commit

  • 231 integration tests passed across CTAS, time travel, read_files, the REST catalog and the bundled REST server. Regression cases cover destination replacement before and during population, competing REST creates, stale snapshot commits, and writes after schema evolution.
  • 437 DataFusion unit tests passed.
  • 5,204 core unit tests passed with storage-s3 enabled, with 8 ignored. Coverage includes ordinary CREATE fallback, conditional publication and lost S3 responses.
  • cargo fmt --all -- --check and git diff --check passed.

API and Format

  • Catalog::check_table_exists defaults to a table load. Filesystem and REST catalogs use metadata-only lookups.
  • Catalog::create_table_exclusive returns the created table handle or an unsupported-operation error. Filesystem CTAS requires atomic, no-overwrite publication of the initial schema. Ordinary CREATE retains its plain-write fallback on storage where conditional publication is definitively unavailable.
  • REST CTAS requires POST /v1/databases/{db}/exclusive-tables under the configured catalog prefix. The endpoint must enforce exclusive creation and return the initial metadata and identity of the managed native Paimon table. A server without the endpoint can still serve ordinary CREATE.
  • Exclusively created filesystem tables record an optional creationUuid in the initial schema metadata. CTAS uses the existing Paimon snapshot and data-file formats.

Documentation

The SQL reference covers supported syntax and types, storage requirements, 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

@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 clear end-to-end value. GitHub CI is green on this head. I reran the filesystem publication, S3 and REST suites locally, including the earlier fallback cases. A separate filesystem CTAS and read-back comparison reproduced the wrong-result case below.

@SteNicholas

Copy link
Copy Markdown
Member Author

Thanks for rerunning the publication suites and reproducing the CTAS read-back issue, @JingsongLi. Unaliased snapshot scans now use distinct statement-local qualifiers. The planner rewrites exact visible snapshot references, so x.t.id resolves to the nested field in the derived table. Original review · Commit 39e4a496

The regression reads both CTAS destinations back through a fresh SQL context. Both t VERSION AS OF 1 and t VERSION AS OF 1 AS s persist [999, 999, 999]. All 137 tests passed across the five related DataFusion suites. CTAS regression · Inline follow-up

@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 a real materialization workflow. The 42 local DataFusion CTAS, snapshot-schema, and read_files integration tests passed on 39e4a496.

Integration is currently blocked. Merging this head with main at d557fb13 conflicts in crates/paimon/src/catalog/filesystem.rs and crates/paimon/src/catalog/rest/rest_catalog.rs. Please rebase and rerun the filesystem and REST publication tests on the resolved result.

@SteNicholas

Copy link
Copy Markdown
Member Author

Thanks for checking this against main, @JingsongLi. I rebased PAIMON-1425 onto d557fb13, resolved both catalog conflicts, and force-pushed the updated head. Review · Commit 1b0f369f

Both ordinary and exclusive table creation now apply upstream’s reserved-name checks. REST creation still maps missing databases to DatabaseNotExist and preserves error details for other missing resources. The filesystem catalog keeps atomic schema publication for CTAS and the ordinary CREATE TABLE fallback on backends without conditional publication.

On the rebased head, all 90 catalog unit tests, 174 REST catalog integration tests, and 103 DataFusion integration tests passed. That includes the filesystem create race, the fallback for ordinary creation, and REST exclusive creation.

@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. On 851fb39a, I ran 197 DataFusion integration tests, 174 REST catalog integration tests, and 26 S3 unit tests. A deterministic filesystem-catalog test reproduced the destination identity race below.

Materializing a DataFusion query in Paimon required separate CREATE
and INSERT statements. Issue apache#1425 calls for a single statement that
infers the destination schema and populates the table.

- Infer column names, types and nullability from a bounded query and
  reuse the analyzed plan. Apply table options, keys and partitioning.
- Resolve sources and SQL functions in the session namespace. Preserve
  quoted names, snapshot bindings and nested field access.
- Check target metadata before planning. Skip existing targets and
  definite create conflicts with IF NOT EXISTS.
- Require atomic creation for CTAS. Preserve ordinary CREATE on storage
  without conditional publication.
- Bind writes to the created table and share lifecycle locks across
  warehouse aliases. Use a dedicated REST create endpoint and validate
  the table identity and base snapshot before publication.
- Discard owned uncommitted files on failure or cancellation. Retain
  files when publication is uncertain.
- Cover persisted results, competing creates, table replacement and
  stale REST commits. Document CTAS syntax and recovery behavior.
@SteNicholas

Copy link
Copy Markdown
Member Author

Thanks for running those suites and for the deterministic reproducer, @JingsongLi. Both findings from your review are addressed in f50867c5, based on main at e4b84b47.

  • CTAS carries the identity returned by exclusive creation through writing and snapshot publication. Replacing the destination before population or during it now fails and leaves the replacement's [77] rows intact. Identity regression
  • REST CTAS uses a dedicated endpoint that calls the catalog's atomic creation path. The bundled server accepts one winner in a concurrent create and rejects stale table or base-snapshot identities at publication. REST regression

On macOS with Rust 1.98.0, 231 integration tests, 437 DataFusion unit tests, and 5,204 core unit tests with storage-s3 enabled passed. The core suite had 8 ignored tests. Formatting and git diff --check passed.

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