Skip to content

[spark] Incrementally refresh grouped Data Evolution materialized views - #11568

Merged
JingsongLi merged 6 commits into
apache:masterfrom
JingsongLi:codex/paimon-spark-mv-group-by
Nov 30, 2026
Merged

JingsongLi merged 6 commits into
apache:masterfrom
JingsongLi:codex/paimon-spark-mv-group-by

Conversation

@JingsongLi

Copy link
Copy Markdown
Contributor

Purpose

Support single-source GROUP BY materialized views on Data Evolution tables in Spark 4.1. Share the snapshot scan and projected-row delta execution with join refreshes.

Read keys from changed row-ID ranges at both snapshots, then evaluate only the affected groups, including every row in an affected NULL group. Apply the resulting changes in projected-row multiplicity and commit source progress in the same target snapshot.

Support one physical grouping column, COUNT, SUM, AVG, MIN, MAX, and COUNT(DISTINCT ...), with optional WHERE, HAVING, and aggregate FILTER. The SELECT list can omit the grouping key. Refresh can rebuild the view when the affected scope exceeds its budget or the aggregate cannot reproduce the previous result exactly.

Tests

  • Spark 4.1 Maven package with the grouped, join, and projection materialized-view SQL suites, plus the Spark view, Data Evolution MERGE, and composite partition-key suites.
  • Java regressions for refresh state and conflict detection, Data Evolution changes and deletion vectors, and ROW file-format reads and writes.
  • Spark 3.5 shared-module compilation.
  • Documentation site build.

@leaves12138 leaves12138 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 base master at head 880f0de. Full file list: docs/docs/spark/materialized-views.md; paimon-core main: new DataEvolutionMaterializedGroupRefresh, DataEvolutionMaterializedJoinRefresh, MaterializedSource; Spark: new DataEvolutionMaterializedGroupViewTest.scala, new SparkMaterializedGroup.scala, SparkMaterializedJoin.scala, new SparkMaterializedQuery.scala, SparkMaterializedView.scala.

Core change notice: this PR adds a third generic refresh strategy to paimon-core and reshapes shared code. New @experimental DataEvolutionMaterializedGroupRefresh recomputes affected groups of a single source with a maxReplayRows contract that bounds when recomputed before-images can reproduce previous aggregates exactly. The key-collection loop (budgets 128 keys / 4MiB, NULL handling, previously-empty bootstrap) moves from DataEvolutionMaterializedJoinRefresh into MaterializedSource.collectChangedKeys / keyChangeRanges, now shared by join and group refreshes. On the Spark side the pinned-scan, signed-delta, and write execution is extracted verbatim into SparkMaterializedQuery and reused by both join and group paths. The single-source projection refresh and commit/conflict semantics are untouched.

Analysis:

  • Group completeness: affected keys are collected from changed row-ID ranges at both snapshots, then each affected group is evaluated in full (the key IN/isNull filters carry no row-range restriction), so partial-group drift is impossible. NULL groups include every NULL-key row; the unavailable-file test proves unrelated non-NULL files are skipped while complete NULL groups are still read.
  • Exact-reproduction bounds are sound and layered: per-aggregate maxReplayRows is the minimum across the SELECT (COUNT unbounded; MIN/MAX only for order-stable types; integral SUM bounded by accumulator headroom; Long SUM effectively rebuild-only; small-precision decimal AVG bounded by the 2^53 double-sum exactness limit matching Spark DecimalAggregates; floating-point SUM/AVG rebuild on any candidate change). Even if a before-image failed to reproduce, findProjectedDeletes matches removed values against the actual target and falls back to a full rebuild before any commit, so the bounds are an optimization over an already-safe value-matching contract.
  • The over-budget path is still correct: when replay is disallowed, keyChangeRanges plus metadata-only split pruning decides between a progress-only empty delta and a rebuild; a missing nextRowId or unavailable history rebuilds. The progress-only test with a WHERE that prunes all changes verifies the cursor advances without rewriting rows.
  • Binding is conservative where it matters: exactly one physical AttributeReference grouping key of an order-stable scalar type, only COUNT/SUM/AVG/MIN/MAX with DISTINCT restricted to COUNT, aggregate FILTER and HAVING left to Spark on the pinned plan, and alwaysInline alias resolution so expression grouping (g+1), multi-key GROUP BY, global aggregates, and FIRST are rejected with tests. Read-side CHAR padding projections are tracked through requiredProjects so references still resolve to physical columns.
  • The refactor preserves join behavior: collectChangedKeys is the previous loop moved with identical budgets and NULL semantics, and SparkMaterializedQuery is the previous join execution code extracted unchanged; the join now composes nonNullKeyFilters and scan from the shared helper with the same file-level superset plus Catalyst InSet exactness.
  • Bootstrap from a saved empty position reuses the reviewed previouslyEmpty path, and an empty source head still publishes progress through the non-null RefreshState commit.

No findings. Ten new SQL regressions cover partial updates, key moves, DV deletes, hidden HAVING dependencies, duplicate outputs without the key, NULL groups with file skipping, empty-source bootstrap, decimal sums/averages, CHAR projections, lateral references, floating-point rebuild, WHERE-pruned progress-only refresh, and the rejection matrix; the shared join suite still runs.

CI note: no checks reported on the head branch yet. The PR description records the Spark 4.1 Maven package with grouped/join/projection suites plus view, MERGE, and composite partition-key suites, the Java refresh-state/conflict/DV/ROW regressions, Spark 3.5 shared-module compilation, and the docs build. Approving without waiting for CI.

LGTM.

@JingsongLi
JingsongLi merged commit 09be7d9 into apache:master Nov 30, 2026
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