Skip to content

[datafusion] Support creating materialized views for Spark refresh - #1581

Merged
JingsongLi merged 2 commits into
apache:mainfrom
JingsongLi:codex/datafusion-create-materialized-view
Dec 2, 2026
Merged

JingsongLi merged 2 commits into
apache:mainfrom
JingsongLi:codex/datafusion-create-materialized-view

Conversation

@JingsongLi

Copy link
Copy Markdown
Contributor

Purpose

Allow DataFusion to register a Paimon materialized view that Spark can populate and refresh against the same warehouse.

Brief change log

  • Add CREATE MATERIALIZED VIEW [IF NOT EXISTS] name AS query to Paimon SQLContext.
  • Resolve persistent Data Evolution sources in the view's catalog and database. Infer the output schema from their physical columns so wildcard projections match Spark.
  • Persist an empty Parquet Data Evolution backing table with the Java materialized table type, query definition, row tracking, and deletion vectors.
  • Cover the empty backing table, inferred schema, repeated creation, invalid source types, and the reserved output column name.

Tests

  • RUST_MIN_STACK=16777216 cargo test --locked -p paimon-datafusion --lib --test sql_context_tests
  • cargo fmt --all -- --check
  • Local Filesystem Catalog smoke test with DataFusion 55.2.0 and Spark 4.1.4. Verified creation, first Spark refresh, incremental refresh after appending source rows, and DataFusion readback for a projection and a filtered CTE.

API and Format

The new SQL statement uses the existing Java Paimon materialized table format. Spark performs the first refresh and records the source snapshot progress. The definition and output schema must be compatible with both engines. Spark validates them when it refreshes.

Documentation

The SQL guide and DataFusion integration README describe registration and first refresh.

@leaves12138 leaves12138 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed base main at head eaedb0ea854776d46d4d556ad1b413b87a425fee (+392/-15, 3 files): crates/integrations/datafusion/src/sql_context.rs, crates/integrations/datafusion/README.md, docs/src/sql.md. No change to the core paimon crate; this is purely the DataFusion integration SQL surface plus docs, and it reuses the existing Java materialized-table format (type=materialized-table, materialized-table.definition-query) without extending it.

Analysis:

  • CREATE MATERIALIZED VIEW [IF NOT EXISTS] name AS query registers an empty backing table only: bucket=-1, row-tracking + data-evolution + deletion-vectors enabled, Parquet, no snapshot until Spark performs the first refresh. This matches the Java/Spark contract where the first refresh records source snapshot progress; the test asserts no snapshot exists after creation.
  • Existence handling checks both tables and views before planning, so IF NOT EXISTS short-circuits without validating the query (test covers a stale definition against a missing source), and a plain re-create fails with TableAlreadyExist. Branch/system-table suffixes on the view name are rejected. validate_persistent_create_view still blocks OR REPLACE/ALTER, options, comments, and column lists are explicitly rejected for the materialized form.
  • Schema inference plans against a private MemoryCatalogProviderList of EmptyTables built from build_target_arrow_schema (physical columns, no exposed row ID), so wildcard expansion matches Spark and temporary tables cannot shadow persistent sources. Sources must be persistent TableType::Table with data evolution enabled, at branch head, without time-travel selectors or query authorization; unqualified names resolve in the view catalog/database. Rejecting SELECT 1 without a source is consistent with the refresh model.
  • The stored definition is the original sqlparser AST display, not the DataFusion-rewritten plan; the binary-cast/regex/lateral/expression-compat rewrites are applied only for local schema inference. Spark re-parses and validates the stored SQL at refresh time, and the docs call out explicit aliases/casts where the engines infer differently. The double-quote identifier rejection is a sensible guard since Spark may treat double quotes as string literals; the tokenizer check correctly distinguishes quoted identifiers from single-quoted string literals containing double quotes (covered by the '"tag"' test).
  • source_row_id output names are rejected case-insensitively, matching the reserved refresh column. Arrow-to-Paimon type mapping preserves nullability (NOT NULL id vs nullable label asserted).
  • Docs are updated consistently: the SQL guide gains a CREATE MATERIALIZED VIEW section with the Spark first-refresh flow, and the previous "materialized views not supported" statements are removed from both README and guide.

No findings. Tests cover the empty backing table and options, inferred schema and wildcard expansion with a shadowing temporary view, IF NOT EXISTS and duplicate creation, quote rules, invalid source types, and the reserved output column. Author also ran a local Filesystem Catalog smoke test with DataFusion 55.2.0 and Spark 4.1.4 covering creation, first refresh, incremental refresh after appends, and DataFusion readback.

CI note: 1 check pass, 14 pending. Approving without waiting for CI per maintainer request; author ran the datafusion lib + sql_context tests and fmt locally.

LGTM.

@JingsongLi
JingsongLi merged commit c2d3036 into apache:main Dec 2, 2026
15 checks 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.

2 participants