Skip to content

Snowflake: parse ORDER BY ALL - #2502

Merged
LucaCappelletti94 merged 2 commits into
apache:mainfrom
Embucket:upstream-snowflake-order-by-all
Sep 24, 2026
Merged

LucaCappelletti94 merged 2 commits into
apache:mainfrom
Embucket:upstream-snowflake-order-by-all

Conversation

@osipovartem

@osipovartem osipovartem commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

ORDER BY ALL is supported by Snowflake and already represented by the shared OrderByKind::All parser path. This PR enables that capability for SnowflakeDialect.

The shared parse_select_order_by_all test now exercises Snowflake for all nine supported option combinations, including DESC 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 -- --check
  • cargo +1.98.1 test --test sqlparser_common parse_select_order_by_all
  • cargo +1.98.1 test --test sqlparser_snowflake (167 passed)
  • Independent read-only review: approved; no blockers.

neilconway pushed a commit to neilconway/datafusion that referenced this pull request Sep 14, 2026
## 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`.
@osipovartem
osipovartem force-pushed the upstream-snowflake-order-by-all branch from 66dce8d to 9aa8b64 Compare September 22, 2026 22:25
@codecov-commenter

codecov-commenter commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.96%. Comparing base (6862e71) to head (e3680ce).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@osipovartem
osipovartem force-pushed the upstream-snowflake-order-by-all branch from 45bd8db to 77081de Compare September 24, 2026 11:37
Comment thread tests/sqlparser_snowflake.rs Outdated
Comment on lines +47 to +59
#[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),
})
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
#[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),
})
);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@LucaCappelletti94 LucaCappelletti94 added the waiting on contributor The review needs further refinements by its author label Sep 24, 2026
@osipovartem
osipovartem force-pushed the upstream-snowflake-order-by-all branch from 712cb91 to e3680ce Compare September 24, 2026 15:24
@LucaCappelletti94 LucaCappelletti94 added waiting on reviewer PR needs to be reviewed and removed waiting on contributor The review needs further refinements by its author labels Sep 24, 2026
@LucaCappelletti94 LucaCappelletti94 removed the waiting on reviewer PR needs to be reviewed label Sep 24, 2026
@LucaCappelletti94

Copy link
Copy Markdown
Contributor

LGTM!
cc @alamb @iffyio

@LucaCappelletti94
LucaCappelletti94 added this pull request to the merge queue Sep 24, 2026
Merged via the queue into apache:main with commit f45d254 Sep 24, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants