Skip to content

fix: remap constraints to the correct output column when projecting equivalence properties - #27597

Open
asolimando wants to merge 1 commit into
apache:mainfrom
asolimando:asolimando/fix-projected-constraints-index
Open

asolimando wants to merge 1 commit into
apache:mainfrom
asolimando:asolimando/fix-projected-constraints-index

Conversation

@asolimando

Copy link
Copy Markdown
Member

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 key c has table index 3. The scan reads a, b, c, d, so in the scan output c has index 2 and d has index 3.

EquivalenceProperties::projected_constraints passes the output column indices of the projection ([0, 1, 2, 3]) to Constraints::project. Constraints::project expects, 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 is d. satisfied_by_constraints then treats d as unique: the input is sorted on d, so the requirement d, a is 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_constraints now passes to Constraints::project, at each output column index, the index of its source column. Output columns that are not a plain source column get usize::MAX, which matches no constraint column.

What is the testing strategy for this PR?

A new sqllogictest case in order.slt (table pk_dropped_columns) checks the query result and that the SortExec stays in the plan. Without the fix, the case fails: the rows are not sorted and the plan has no SortExec.

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.

@asolimando
asolimando force-pushed the asolimando/fix-projected-constraints-index branch from c8a69e3 to a2b0c6e Compare November 19, 2026 20:55
@asolimando
asolimando marked this pull request as ready for review November 19, 2026 21:02
@github-actions github-actions Bot added physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt) labels Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong results: ORDER BY returns unsorted rows for a table with a PRIMARY KEY and WITH ORDER

1 participant