[Enhancement] IVM: re-derive the maintenance query at refresh instead of freezing it (backport #74881)#75129
Merged
Merged
Conversation
Contributor
Author
|
Cherry-pick of 14bf004 has failed: To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally |
23 tasks
Contributor
Author
|
@mergify[bot]: Backport conflict, please reslove the conflict and resubmit the pr |
Mergify left conflict markers in IVMBasedMvRefreshProcessorIcebergTest. #74881's hunk that retargets the getIVMTaskDefinition mock signature sits inside testImvSourceRangesNotRecordedOnPctFallback -- part of a TVR/imvSource feature not present on branch-4.1 (no getImvSourceVersionRange), so the hunk could not apply and dragged the surrounding main-only methods into the conflict. Resolve by taking branch-4.1's side: the three main-only testImvSource* methods are dropped (they would not compile here). #74881's own addition, testFirstFullRefreshOfAggregateIvmMvRebuildsRewrittenInsert, stays -- its helpers exist on branch-4.1. The resolved file equals branch-4.1 + that one method. Signed-off-by: Youngwb <yangwenbo_mailbox@163.com>
Youngwb
approved these changes
Jun 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why I'm doing:
IVM (incremental materialized view) maintenance used to freeze the rewritten maintenance query into a persisted
ivmDefineSqlat CREATE time and execute that frozen text on every refresh. The rewritten query is the user query plus the hidden group-identity column__ROW_ID__(from_binary(encode_row_id(keys))) and, for aggregates, the mergeable__AGG_STATE_*columns.Because the rewrite was frozen, bugfixes to the rewriter could not reach already-created MVs. For example the positional-
GROUP BY/SELECT DISTINCT__ROW_ID__fix in #74493 only helped newly created MVs; existing MVs kept executing their stale, buggy frozen text and produced wrong results until dropped and re-created. The encode scheme was also baked into the SQL literal with no room to evolve.What I'm doing:
Re-derive the IVM maintenance query from the MV's original
viewDefineSqlon every refresh (re-running the current, fixed rewriter), instead of executing a frozen string. So rewriter fixes apply retroactively to existing MVs on their next refresh.Fixes #issue: N/A — internal IVM refresh-mechanism change (no separate tracking issue); makes rewriter fixes such as #74493 apply retroactively to existing MVs.
Mechanism
IvmRefreshDefinition.derive(ctx, mv)/deriveRewrittenQuery(ctx, mv): parseviewDefineSql→ analyze → re-run the IVM rewrite with the MV's pinnedencodeRowIdVersion(no trial) → analyze again → validate →buildSimple.ivmDefineSql(MaterializedViewAnalyzer/LocalMetastore); both refresh paths re-derive instead. The field is kept Gson-nullable for old metadata.RowIdStrategymust equal the stored one;IvmSchemaCompat): the re-derived output columns (incl. hidden__ROW_ID__/__AGG_STATE) must match the stored MV schema, reusing the CREATE-time column derivation so type equivalence stays identical.MVIVMRefreshProcessor) and the PCT full-rebuild path (MVPCTRefreshProcessor, used for an MV's first refresh / when no delta baseline exists). The IVM-MV predicate isisIncrementalOrAuto()so legacy AUTO-mode IVM MVs are handled too.MVRefreshSchemaChecker(external-base drift) re-derives the same way so its column comparison matches the stored hidden-column schema.ivmDefineSql, a one-time WARN is logged if the re-derived definition differs (advisory only; refresh always uses the re-derived result).Behavior change — existing IVM MVs created before this change carry a persisted
ivmDefineSql; their refresh now executes the re-derived query instead of that frozen text (this is exactly how rewriter fixes become retroactive). New IVM MVs persist no frozen text. PCT (non-IVM) refresh andSHOW CREATE MATERIALIZED VIEWare unchanged. The persistedTask.definitionof an IVM MV now displays the user query rather than the rewritten INSERT (display only — the executed plan is always re-derived). On downgrade to a pre-change binary, a new IVM MV (emptyivmDefineSql) falls back to the un-rewritten user query: for aggregate /DISTINCTMVs the old binary's own schema check rejects it with a clear error (base table schema check failed … column count N vs M, refresh fails — verified on a dev cluster), and a scan MV's user query equals its maintenance query so it refreshes correctly. Downgrade therefore fails loudly or stays correct — it does not silently produce wrong data. (Upgrade is smooth: an existing MV with a frozenivmDefineSqlre-derives on its next refresh — also verified on a dev cluster.)Verification
GROUP BYcol/ordinal,DISTINCT, all-constant, sum/max); retroactivity (frozen buggy text is ignored); strategy/shape gates; schema-checker no-false-drift for INCREMENTAL and AUTO MVs; the PCT first-refresh full-rebuild of an aggregate MV (validated to fail without the fix). Full IVM + MV suites green (239 tests), checkstyle clean.GROUP BY 1,2andSELECT DISTINCTMV both refresh to data that matches the direct query exactly (0 mismatched rows), through both the PCT full-rebuild and the IVM-delta path.What type of PR is this:
Does this PR entail a change in behavior?
If yes, please specify the type of change:
Checklist:
Bugfix cherry-pick branch check:
This is an automatic backport of pull request #74881 done by [Mergify](https://mergify.com).