Repository navigation
fix: remap constraints to the correct output column when projecting equivalence properties - #27597
Conversation
…quivalence properties
c8a69e3 to
a2b0c6e
Compare
2010YOUY01
left a comment
There was a problem hiding this comment.
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.
|
I reproduced an
1,,0,2
1,,0,1CREATE 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 |
docs: clarify projected constraint propagation
Thanks @shihchengwei-lab, I reproduced it. The cause is not in this PR though: the ordering check accepts a nullable I filed #27619 and opened #27620 with the fix you suggested: a cc: @2010YOUY01 as it might be quick for you to review #27620 too, since you know the context and the PR is small |
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.