Repository navigation
fix: remap constraints to the correct output column when projecting equivalence properties - #27597
Open
asolimando wants to merge 1 commit into
Open
asolimando wants to merge 1 commit into
asolimando wants to merge 1 commit into
Conversation
…quivalence properties
asolimando
force-pushed
the
asolimando/fix-projected-constraints-index
branch
from
November 19, 2026 20:55
c8a69e3 to
a2b0c6e
Compare
asolimando
marked this pull request as ready for review
November 19, 2026 21:02
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.
Which issue does this PR close?
Rationale for this change
A table with a PRIMARY KEY and a declared sort order (
WITH ORDER) can lose a required sort when the scan reads only some of the table columns, so the query returns rows in the wrong order. See the issue for the reproducer.The table in the issue has the columns
a0, a, b, c, d(indices 0 to 4), so the keychas table index 3. The scan readsa, b, c, d, so in the scan outputchas index 2 anddhas index 3.EquivalenceProperties::projected_constraintspasses the output column indices of the projection ([0, 1, 2, 3]) toConstraints::project.Constraints::projectexpects, at each output position, the index of the source column (here[1, 2, 3, 4]). It looks up the key index 3 in[0, 1, 2, 3], finds it at position 3, and remaps the key to output column 3, which isd.satisfied_by_constraintsthen treatsdas unique: the input is sorted ond, so the requirementd, ais considered satisfied and the sort is removed.With other projections the same error can also drop a valid key (a missed optimization) instead of moving it to the wrong column.
What changes are included in this PR?
EquivalenceProperties::projected_constraintsnow passes toConstraints::project, at each output column index, the index of its source column. Output columns that are not a plain source column getusize::MAX, which matches no constraint column.What is the testing strategy for this PR?
A new
sqllogictestcase inorder.slt(tablepk_dropped_columns) checks the query result and that theSortExecstays in the plan. Without the fix, the case fails: the rows are not sorted and the plan has noSortExec.Are there any user-facing changes?
Queries on tables with constraints and a declared sort order return correctly sorted results. No public API changes.
Disclaimer: I used AI to assist in the code generation, I have manually reviewed the output and it matches my intention and understanding.