[FLINK-40496][table-planner] CREATE OR ALTER MATERIALIZED TABLE honors query column position - #29034
Open
AHeise wants to merge 1 commit into
Open
[FLINK-40496][table-planner] CREATE OR ALTER MATERIALIZED TABLE honors query column position#29034AHeise wants to merge 1 commit into
AHeise wants to merge 1 commit into
Conversation
Collaborator
AHeise
force-pushed
the
FLINK-40496-coa-mt-column-position
branch
2 times, most recently
from
August 28, 2026 13:00
f49fb94 to
bdac042
Compare
AHeise
marked this pull request as ready for review
August 28, 2026 13:01
AHeise
marked this pull request as draft
August 28, 2026 13:02
AHeise
force-pushed
the
FLINK-40496-coa-mt-column-position
branch
2 times, most recently
from
August 28, 2026 14:14
c47794e to
446af6c
Compare
… ALTER MATERIALIZED TABLE CREATE OR ALTER derives its schema delta via validateAndExtractColumnChanges, which appends new columns but never diffs column position, so inserting a column anywhere but the end of the query projection stores a schema whose order disagrees with the query and later miscompiles the positional refresh INSERT. ALTER ... AS already diffs position via buildSchemaTableChanges. Diff column position on the CREATE OR ALTER path as well, positioning each old column by its rank among the columns that survive into the new schema so retained non-persisted columns do not skew the comparison. CREATE OR ALTER now matches ALTER ... AS: a mid-projection insertion stores the query order, and an insertion that would reorder existing physical columns is rejected up front instead of miscompiling at refresh time.
AHeise
force-pushed
the
FLINK-40496-coa-mt-column-position
branch
from
August 28, 2026 14:44
446af6c to
0831111
Compare
AHeise
marked this pull request as ready for review
August 28, 2026 16:06
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.
What is the purpose of the change
CREATE OR ALTER MATERIALIZED TABLEderives its schema delta viaMaterializedTableUtils.validateAndExtractColumnChanges, which appends new columns but never diffs column position. Adding a column anywhere but the end of the query projection therefore stores a schema whose column order disagrees with the query. Nothing rejects it at DDL time (validateChangespasses); the mismatch only surfaces later, when the refresh runs a positionalINSERT INTO <table> <expandedQuery>and the wrong query column binds to the wrong sink column.ALTER MATERIALIZED TABLE ... ASalready diffs position viabuildSchemaTableChanges, so the two query-evolution paths disagree. This PR makesCREATE OR ALTERhonor query column position the same way.Example —
mt = (a, b, c, d):Expected stored schema:
(a, b, c, mid, d)(the query projection order, asALTER ... ASproduces).Before this change:
(a, b, c, d, mid)—midappended at the end.Brief change log
validateAndExtractColumnChangesemitsmodifyColumnPositionfor name-matched columns whose index changed, mirroringbuildSchemaTableChanges.CREATE OR ALTERbut absent from the query projection) do not skew the position comparison.Verifying this change
This change added tests and can be verified as follows:
MaterializedTableUtilsTest#testCreateOrAlter— a physical mid-projection insert and a case with a retained virtual-metadata column, asserting the position-awareTableChangelist.SqlNodeToOperationSqlCreateOrAlterMaterializedTableConverterTest— end-to-end over realCREATE OR ALTERSQL, exercising schema resolution, the position diff, andvalidateChanges: inserting before the last column stores the query order and is accepted; inserting before multiple trailing columns is rejected up front with a clear error instead of miscompiling at refresh time.AlterMaterializedTableAsQueryOperationValidationTest— guard boundary: a reposition anchored after a newly-added column is accepted, one anchored after an existing column is rejected.Does this pull request potentially affect one of the following parts:
@Public(Evolving): noDocumentation
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Claude Opus 4.8)