Skip to content

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

Merged
2010YOUY01 merged 3 commits into
apache:mainfrom
asolimando:asolimando/fix-projected-constraints-index
Nov 22, 2026
Merged

2010YOUY01 merged 3 commits 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

@2010YOUY01 2010YOUY01 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix and the great explanation.

I've traced the bug from SQL to the change in diff, and I agree this would be the right fix.

@shihchengwei-lab

Copy link
Copy Markdown

I reproduced an ORDER BY regression on a2b0c6e4. With the same two-row CSV and one partition, the base commit (c6c1cc31) keeps SortExec and returns t = 1, 2. The PR removes SortExec and returns t = 2, 1.

reorder.csv has no header. The empty b field reads as NULL.

1,,0,2
1,,0,1
CREATE EXTERNAL TABLE reorder (
  a INT NOT NULL, b INT, c INT NOT NULL, v INT NOT NULL,
  UNIQUE (a, b)
)
STORED AS CSV
WITH ORDER (a ASC, b ASC, c ASC)
OPTIONS ('format.has_header' 'false')
LOCATION 'reorder.csv';

SELECT a AS x, v AS t, b AS y, c AS z
FROM reorder
ORDER BY x, y, z, t;

The patch correctly remaps UNIQUE (a, b) to (x, y). That exposes an existing assumption in the ordering check: it accepts nullable y because y is in the middle of (x, y, z). Both (1, NULL) keys are valid, and the rows tie on all three sort columns, so t must break the tie. Could we require a non-null guarantee for every component of a UNIQUE key before using it to skip the rest of the sort?

docs: clarify projected constraint propagation
@asolimando

Copy link
Copy Markdown
Member Author

I reproduced an ORDER BY regression on a2b0c6e4. With the same two-row CSV and one partition, the base commit (c6c1cc31) keeps SortExec and returns t = 1, 2. The PR removes SortExec and returns t = 2, 1.
[...]

Thanks @shihchengwei-lab, I reproduced it. The cause is not in this PR though: the ordering check accepts a nullable UNIQUE key column when it is neither the first nor the last column of the ordering. main already returns wrong results when the key is not remapped, for example with SELECT a, b, c, v FROM reorder ORDER BY a, b, c, v on your table. This PR only exposes the same check in your query, as a side effect of remapping the key correctly, I'd therefore consider the two bugs as independent.

I filed #27619 and opened #27620 with the fix you suggested: a UNIQUE constraint is used only if none of its key columns is nullable.

cc: @2010YOUY01 as it might be quick for you to review #27620 too, since you know the context and the PR is small

@2010YOUY01
2010YOUY01 added this pull request to the merge queue Nov 22, 2026
Merged via the queue into apache:main with commit 77611de Nov 22, 2026
42 checks passed
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) v57.0.0

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

3 participants