Skip to content

Commit a2b0c6e

Browse files
committed
fix: remap constraints to the correct output column when projecting equivalence properties
1 parent c6c1cc3 commit a2b0c6e

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

  • datafusion
    • physical-expr/src/equivalence/properties
    • sqllogictest/test_files

‎datafusion/physical-expr/src/equivalence/properties/mod.rs‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,15 +1577,24 @@ impl EquivalenceProperties {
15771577
/// Returns an optional `Constraints` object containing only the constraints
15781578
/// that are valid for the projected columns (if any exists).
15791579
fn projected_constraints(&self, mapping: &ProjectionMapping) -> Option<Constraints> {
1580-
let indices = mapping
1581-
.iter()
1582-
.flat_map(|(_, targets)| {
1583-
targets.iter().filter_map(|(target, _)| {
1584-
target.downcast_ref::<Column>().map(|c| c.index())
1585-
})
1586-
})
1587-
.collect::<Vec<_>>();
1588-
self.constraints.project(&indices)
1580+
// Source column index of each output column, `usize::MAX` if the
1581+
// output is not a plain column
1582+
let mut source_indices = vec![];
1583+
for (source, targets) in mapping.iter() {
1584+
let Some(source) = source.downcast_ref::<Column>() else {
1585+
continue;
1586+
};
1587+
for (target, _) in targets.iter() {
1588+
if let Some(target) = target.downcast_ref::<Column>() {
1589+
let index = target.index();
1590+
if source_indices.len() <= index {
1591+
source_indices.resize(index + 1, usize::MAX);
1592+
}
1593+
source_indices[index] = source.index();
1594+
}
1595+
}
1596+
}
1597+
self.constraints.project(&source_indices)
15891598
}
15901599

15911600
/// Projects these equivalence properties onto `output_schema` according to

‎datafusion/sqllogictest/test_files/order.slt‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,43 @@ physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/
19261926
statement ok
19271927
drop table table_with_ordered_pk;
19281928

1929+
# A PRIMARY KEY must stay on its own column when the scan drops columns
1930+
# before it, otherwise the sort on (d, a) is wrongly removed
1931+
statement ok
1932+
COPY (VALUES (0, 3, 0, 10, 1), (0, 1, 0, 11, 1), (0, 2, 0, 12, 1), (0, 9, 0, 13, 2), (0, 5, 0, 14, 2))
1933+
TO 'test_files/scratch/order/pk_dropped_columns.csv'
1934+
STORED AS CSV
1935+
OPTIONS ('format.has_header' 'false');
1936+
1937+
statement ok
1938+
CREATE EXTERNAL TABLE pk_dropped_columns (a0 INT, a INT, b INT, c INT, d INT, PRIMARY KEY (c))
1939+
STORED AS CSV
1940+
WITH ORDER (d ASC)
1941+
OPTIONS ('format.has_header' 'false')
1942+
LOCATION 'test_files/scratch/order/pk_dropped_columns.csv';
1943+
1944+
query IIII
1945+
SELECT a, b, c, d FROM pk_dropped_columns ORDER BY d, a;
1946+
----
1947+
1 0 11 1
1948+
2 0 12 1
1949+
3 0 10 1
1950+
5 0 14 2
1951+
9 0 13 2
1952+
1953+
query TT
1954+
EXPLAIN SELECT a, b, c, d FROM pk_dropped_columns ORDER BY d, a;
1955+
----
1956+
logical_plan
1957+
01)Sort: pk_dropped_columns.d ASC NULLS LAST, pk_dropped_columns.a ASC NULLS LAST
1958+
02)--TableScan: pk_dropped_columns projection=[a, b, c, d]
1959+
physical_plan
1960+
01)SortExec: expr=[d@3 ASC NULLS LAST, a@0 ASC NULLS LAST], preserve_partitioning=[false]
1961+
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/order/pk_dropped_columns.csv]]}, projection=[a, b, c, d], output_ordering=[d@3 ASC NULLS LAST], constraints=[PrimaryKey([3])], file_type=csv, has_header=false
1962+
1963+
statement ok
1964+
drop table pk_dropped_columns;
1965+
19291966
statement ok
19301967
set datafusion.execution.target_partitions = 1;
19311968

0 commit comments

Comments
 (0)