Repository navigation
Conversation
|
Hi @jayzhan211! Could you review this PR when/if you have a chance? Thank you!!! |
jayzhan211
left a comment
There was a problem hiding this comment.
@TinyMurky thanks! One small ask inline: a test for the Expr::Column skip, which keeps swapped alias names correct.
| if matches!(sort.expr, Expr::Column(_)) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
@TinyMurky thanks for the fix! This Expr::Column skip is what keeps a bare output name from matching an input column with the same name, but no test fails if it's removed. Without it, SELECT DISTINCT column1 AS column2, column2 AS column1 FROM (VALUES (1, 20), (2, 10)) ORDER BY column1 returns (1, 20), (2, 10) instead of (2, 10), (1, 20). Could you add a comment saying why, and this case to order.slt?
| if matches!(sort.expr, Expr::Column(_)) { | |
| continue; | |
| } | |
| // 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; | |
| } |
# ORDER BY column1 names the output column1 (input column2), not the input
# column1 that the first SELECT expression reads
query II
SELECT DISTINCT column1 AS column2, column2 AS column1
FROM (VALUES (1, 20), (2, 10))
ORDER BY column1;
----
2 10
1 20
There was a problem hiding this comment.
Hello !@jayzhan211
I had added the test and the comment in the new commit!
jayzhan211
left a comment
There was a problem hiding this comment.
@TinyMurky thanks for the update! One more case inline: a computed sort key can still hit the same name collision, so the result depends on whether the subquery has an alias.
| 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; | ||
| } |
There was a problem hiding this 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:
| 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; | |
| } | |
| // 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; | |
| } |
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")
# 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.
There was a problem hiding this comment.
@jayzhan211 I have changed the codes and added the tests
Could you review the changesif you have a chance?
…iases Follow the review on apache#27773 by checking for renamed output columns throughout the sort expression.
189f7c0 to
392300b
Compare
Which issue does this PR close?
Rationale for this change
DataFusion can reject a valid
SELECT DISTINCTquery whenORDER BYrepeats an expression from the SELECT list. This happens with or without an alias.Both queries should return
2and3in that order. Before the fix, both fail with an error saying thatt.amust appear in the SELECT list.This example selects two columns and sorts by both expressions.
The result is
(2, 6),(2, 2),(3, 4), and(3, 2). It sorts byvin ascending order and bywin descending order within eachv.What changes are included in this PR?
The planner now matches computed
ORDER BYexpressions to expressions in the SELECT list, ignoring aliases during the comparison. It sorts by the matching output column. For example, the second query above sorts byv.What is the testing strategy for this PR?
Added cases to
datafusion/sqllogictest/test_files/order.sltfor expressions with and without aliases, descending order withNULLS LAST, and multiple sort keys. A query that sorts by an unselected expression,a + 2, still returns a planning error.Are there any user-facing changes?
The queries above now return the expected rows.