jayzhan211 commented on code in PR #27773:
URL: https://github.com/apache/datafusion/pull/27773#discussion_r4755030903
##########
datafusion/sql/src/select.rs:
##########
@@ -564,6 +564,31 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
}
Some(Distinct::Distinct) => {
let plan = self.try_process_unnest(plan,
select_exprs_post_aggr)?;
+ if let LogicalPlan::Projection(projection) = &plan {
+ // DISTINCT exposes only the projection's output columns.
+ //
+ // SELECT DISTINCT a + 1 AS v FROM t ORDER BY a + 1;
+ //
+ // The sort input has v, so trying to resolve t.a would
fail.
+ // Match a + 1 against the aliased projection expression
and
+ // use v as the sort key, as in ORDER BY v.
+ for sort in &mut order_by_rex {
+ // A bare column may name an output column whose name
an input
+ // column in another expression shares, e.g. ORDER BY
column1 in
+ // SELECT DISTINCT column1 AS column2, column2 AS
column1.
+ if matches!(sort.expr, Expr::Column(_)) {
+ continue;
+ }
Review Comment:
@TinyMurky thanks for adding the test! One more case: a computed key can
still hit the same name collision, and then the result depends on whether the
subquery has an alias. `SELECT DISTINCT price AS old_price, 10 - price AS price
FROM (SELECT 1 AS price UNION ALL SELECT 5) ORDER BY 10 - price` returns `(5,
5), (1, 9)`. With `AS s` after the subquery it returns `(1, 9), (5, 5)`. Main
returns `(1, 9), (5, 5)` for both.
`price` in the key resolves to the output `price`, but it compares equal to
the unqualified input `price` inside `10 - price AS price`. Skipping any key
that reads a renamed output fixes this, and it also covers the bare-column
case, so the `Expr::Column` check can go:
````suggestion
// ORDER BY names resolve to SELECT outputs first, but
// projection.expr reads input columns. With an
unqualified input
// (VALUES or a subquery without an alias), the output
column1 of
// column2 AS column1 equals the input column1, so a key
that
// reads a renamed output is not matched.
let renamed_outputs: Vec<Column> = projection
.expr
.iter()
.zip(projection.schema.columns())
.filter(|(expr, output)| {
!matches!(expr, Expr::Column(input) if input ==
output)
})
.map(|(_, output)| output)
.collect();
for sort in &mut order_by_rex {
if sort
.expr
.column_refs()
.iter()
.any(|col| renamed_outputs.contains(col))
{
continue;
}
````
<details>
<summary>Tests for order.slt, after the swapped-alias test (the first two
pin the alias case; the third separates this rule from "skip any key that reads
an output column")</summary>
```
# price in ORDER BY names the output price, with or without the subquery
alias
query II
SELECT DISTINCT price AS old_price, 10 - price AS price
FROM (SELECT 1 AS price UNION ALL SELECT 5)
ORDER BY 10 - price;
----
1 9
5 5
query II
SELECT DISTINCT price AS old_price, 10 - price AS price
FROM (SELECT 1 AS price UNION ALL SELECT 5) AS s
ORDER BY 10 - price;
----
1 9
5 5
# a key that reads a selected column and an unselected one still matches a +
b
query II
SELECT DISTINCT a, a + b AS v
FROM (VALUES (2, 1), (1, 5), (2, 1)) AS input(a, b)
ORDER BY a + b;
----
2 3
1 6
```
With this change, the full sqllogictest suite, `cargo test -p
datafusion-sql` and clippy pass locally.
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]