Snowflake: parse ORDER BY ALL - #2502
Conversation
## Which issue does this PR close? Closes a planner gap where `ORDER BY ALL` only accepted raw column projections and was silently ignored for set-operation outputs. ## Rationale for this change `ORDER BY ALL` means sorting by every output column in select-list order. Expanding it to the equivalent 1-based ordinal keys supports computed expressions, aliases, aggregate outputs, wildcard-expanded projections, and set-operation outputs through one planner path. The ordinal expansion also sorts already-projected columns rather than evaluating computed expressions again. Physical execution remains on DataFusion's existing vectorized `SortExec`; this adds no row-wise conversion or custom physical operator. ## What changes are included? - expand `OrderByKind::All` to ordinal `OrderByExpr`s from the output width - provide output width for non-`SELECT` set expressions instead of dropping `ORDER BY ALL` - add focused execution coverage for computed expressions, null-order options, set operations, and aggregate output ## Are these changes tested? - `cargo +1.95.0 test -p datafusion-sqllogictest --test sqllogictests -- order_by_all.slt --test-threads 1` - `cargo +1.95.0 test -p datafusion-sql --test sql_integration -- --test-threads 8` (591 passed) - `cargo +1.95.0 clippy -p datafusion-sql --all-targets -- -D warnings` - `cargo +1.95.0 fmt --all -- --check` Snowflake dialect parsing support is proposed independently in apache/datafusion-sqlparser-rs#2502; this planner change is generic to every dialect that already emits `OrderByKind::All`.
66dce8d to
9aa8b64
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2502 +/- ##
=======================================
Coverage 80.96% 80.96%
=======================================
Files 42 42
Lines 33386 33389 +3
Branches 33386 33389 +3
=======================================
+ Hits 27032 27035 +3
Misses 2789 2789
Partials 3565 3565 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
45bd8db to
77081de
Compare
| #[test] | ||
| fn parse_order_by_all() { | ||
| let query = snowflake() | ||
| .verified_query("SELECT value + 1 AS computed FROM source ORDER BY ALL DESC NULLS FIRST"); | ||
| assert_eq!( | ||
| query.order_by.expect("ORDER BY expected").kind, | ||
| OrderByKind::All(OrderByOptions { | ||
| sort: Some(OrderBySort::Desc), | ||
| nulls_first: Some(true), | ||
| }) | ||
| ); | ||
| } | ||
|
|
There was a problem hiding this comment.
You could drop this test. parse_select_order_by_all in tests/sqlparser_common.rs runs every dialect where supports_order_by_all() holds, so Snowflake now gets the same ORDER BY ALL DESC NULLS FIRST case plus the other eight option combinations.
| #[test] | |
| fn parse_order_by_all() { | |
| let query = snowflake() | |
| .verified_query("SELECT value + 1 AS computed FROM source ORDER BY ALL DESC NULLS FIRST"); | |
| assert_eq!( | |
| query.order_by.expect("ORDER BY expected").kind, | |
| OrderByKind::All(OrderByOptions { | |
| sort: Some(OrderBySort::Desc), | |
| nulls_first: Some(true), | |
| }) | |
| ); | |
| } |
There was a problem hiding this comment.
Agreed. I removed the duplicate Snowflake-specific test in 712cb91. The shared parse_select_order_by_all test covers Snowflake and all nine option combinations; it passes, as does the full Snowflake test suite (167/167).
712cb91 to
e3680ce
Compare
ORDER BY ALLis supported by Snowflake and already represented by the sharedOrderByKind::Allparser path. This PR enables that capability forSnowflakeDialect.The shared
parse_select_order_by_alltest now exercises Snowflake for all nine supported option combinations, includingDESC NULLS FIRST, so no dialect-specific duplicate test is needed.Reference: https://docs.snowflake.com/en/sql-reference/constructs/order-by
Validation:
cargo +1.98.1 fmt --all -- --checkcargo +1.98.1 test --test sqlparser_common parse_select_order_by_allcargo +1.98.1 test --test sqlparser_snowflake(167 passed)