Repository navigation
Commit 6edc624
committed
fix: plan ORDER BY on a source column hidden by a select alias
- Closes apache#26462.
`SELECT lower(e.email) AS email FROM employees e ORDER BY e.email` failed to
plan:
Schema error: Schema contains qualified field name e.email and unqualified
field name email which would be ambiguous
The sort key is not in the select list, so the planner carries `e.email` into
the projection to sort on it. That projection then holds the qualified field
`e.email` and the unqualified field `email` in one schema, which
`DFSchema::check_names` rejects, even though the carried column is internal and
is projected away above the sort. PostgreSQL plans the same query and sorts on
the input column.
`LogicalPlanBuilder::sort` (`datafusion/expr/src/logical_plan/builder.rs`) now
carries a missing sort column under an alias when the projection it is added to
already outputs an unqualified field with the same name. The alias is the
column's flat name (`e.email`), so the carried column cannot collide with the
projection's other fields, and the sorts are rebound to it. The projection
above the sort still strips the extra column, so the output schema of the query
is unchanged.
`datafusion/sqllogictest/test_files/order.slt` gets the query from the issue and
its `ORDER BY email` counterpart, which pins the two resolutions apart:
`ORDER BY e.email` sorts on the input column (`b@x`, `a@y`), and
`ORDER BY email` sorts on the output column (`a@y`, `b@x`), as in PostgreSQL.
A unit test in `datafusion/expr/src/logical_plan/builder.rs` covers the same
path through `LogicalPlanBuilder` and checks the resulting plan.
Validation, on 61c3472:
- `cargo test -p datafusion-expr` passed: 285 unit tests, 63 documentation tests.
- `cargo test -p datafusion-sql` passed: 95, 604 and 12 tests.
- `cargo test -p datafusion-optimizer` passed: 941, 29 and 5 tests.
- `cargo test -p datafusion --lib` passed: 468 tests.
- `cargo test --profile ci -p datafusion-sqllogictest --test sqllogictests`
passed: 534 of 534 files.
- `cargo clippy -p datafusion-expr --all-targets --all-features -- -D warnings`
and `cargo fmt --all -- --check` passed.
The `order.slt` case was also run against the unpatched code, where it fails
with the schema error quoted above.
None beyond the query now planning. The result and the schema of the query are
the ones PostgreSQL returns; plans that already planned are unchanged.1 parent cd571e5 commit 6edc624
2 files changed
Lines changed: 92 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
823 | 823 | | |
824 | 824 | | |
825 | 825 | | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
826 | 832 | | |
827 | 833 | | |
828 | 834 | | |
829 | 835 | | |
| 836 | + | |
830 | 837 | | |
831 | 838 | | |
832 | 839 | | |
833 | 840 | | |
834 | 841 | | |
835 | | - | |
| 842 | + | |
836 | 843 | | |
837 | 844 | | |
838 | 845 | | |
| |||
846 | 853 | | |
847 | 854 | | |
848 | 855 | | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
849 | 876 | | |
850 | 877 | | |
851 | 878 | | |
| |||
860 | 887 | | |
861 | 888 | | |
862 | 889 | | |
| 890 | + | |
863 | 891 | | |
864 | 892 | | |
865 | 893 | | |
| |||
963 | 991 | | |
964 | 992 | | |
965 | 993 | | |
| 994 | + | |
966 | 995 | | |
967 | 996 | | |
968 | 997 | | |
969 | 998 | | |
| 999 | + | |
970 | 1000 | | |
971 | 1001 | | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
972 | 1016 | | |
973 | 1017 | | |
974 | 1018 | | |
| |||
2638 | 2682 | | |
2639 | 2683 | | |
2640 | 2684 | | |
| 2685 | + | |
| 2686 | + | |
| 2687 | + | |
| 2688 | + | |
| 2689 | + | |
| 2690 | + | |
| 2691 | + | |
| 2692 | + | |
| 2693 | + | |
| 2694 | + | |
| 2695 | + | |
| 2696 | + | |
| 2697 | + | |
| 2698 | + | |
| 2699 | + | |
| 2700 | + | |
| 2701 | + | |
| 2702 | + | |
| 2703 | + | |
| 2704 | + | |
| 2705 | + | |
| 2706 | + | |
| 2707 | + | |
| 2708 | + | |
2641 | 2709 | | |
2642 | 2710 | | |
2643 | 2711 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2466 | 2466 | | |
2467 | 2467 | | |
2468 | 2468 | | |
| 2469 | + | |
| 2470 | + | |
| 2471 | + | |
| 2472 | + | |
| 2473 | + | |
| 2474 | + | |
| 2475 | + | |
| 2476 | + | |
| 2477 | + | |
| 2478 | + | |
| 2479 | + | |
| 2480 | + | |
| 2481 | + | |
| 2482 | + | |
| 2483 | + | |
| 2484 | + | |
| 2485 | + | |
| 2486 | + | |
| 2487 | + | |
| 2488 | + | |
| 2489 | + | |
| 2490 | + | |
0 commit comments