Repository navigation
[datafusion] Support creating materialized views for Spark refresh - #1581
JingsongLi merged 2 commits into
Conversation
leaves12138
left a comment
There was a problem hiding this comment.
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 queryregisters 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 EXISTSshort-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_viewstill blocks OR REPLACE/ALTER, options, comments, and column lists are explicitly rejected for the materialized form. - Schema inference plans against a private
MemoryCatalogProviderListofEmptyTables built frombuild_target_arrow_schema(physical columns, no exposed row ID), so wildcard expansion matches Spark and temporary tables cannot shadow persistent sources. Sources must be persistentTableType::Tablewith data evolution enabled, at branch head, without time-travel selectors or query authorization; unqualified names resolve in the view catalog/database. RejectingSELECT 1without 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_idoutput 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.
Purpose
Allow DataFusion to register a Paimon materialized view that Spark can populate and refresh against the same warehouse.
Brief change log
CREATE MATERIALIZED VIEW [IF NOT EXISTS] name AS queryto PaimonSQLContext.Tests
RUST_MIN_STACK=16777216 cargo test --locked -p paimon-datafusion --lib --test sql_context_testscargo fmt --all -- --checkAPI 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.