← Back to Pull requests

fix: declare the input order of a native window over a sorted cache #7256

Closed
unknown closed this pull request without merging on Nov 06, 2026 21:46 UTC
View on GitHub
unknown unknown commented on Oct 27, 2026 15:35 UTC
Author

Which issue does this PR close?

Closes #7223.

Rationale for this change

Spark puts no sort below a window when its input reports the order the window needs, a relation sorted before it was cached for instance. When those rows enter the native plan from the JVM, the ScanExec they arrive through declares no order to DataFusion. The native window needs its input to declare the partition keys' order. BoundedWindowAggExec, which runs in InputOrderMode::Sorted, fails the task with Expects PARTITION BY expression to be ordered. WindowAggExec fails when its stream starts, with All partition by columns should have an ordering. The rows are sorted. Only the declaration is missing.

DataFusion has no change pending here. get_partition_by_sort_exprs raises the error on DataFusion main as it does in 55.2.0, and it is right to: it trusts the order its input declares, and Comet's scan declares none.

#6835 fixed the same gap for outer hash joins with streamed_sort_orders. This PR does the same for the window, which is the first option in the issue.

What changes are included in this PR?

  • Window gets repeated Expr input_sort_orders = 5.

  • CometWindowExec sends the order of the window's partition keys, the only order the native window checks, when the child reports an order that satisfies the window's requirement, so Spark dropped the sort. Over a native sort the order is sent too, and the planner leaves the sort's own declaration as it is. Nothing is sent when mayReportFalseOrder doubts that order. A new mayReportFalseInputOrder in the CometHashJoin trait exposes that check. A window whose partition or order key holds a collated string falls back to Spark, as WindowGroupLimit does, since the native window compares keys byte by byte. So does a window with ORDER BY but no PARTITION BY over an order mayReportFalseOrder doubts, since the native window checks no order there.

  • WindowBuilder declares the order on the window's input with with_declared_order, now pub(super). It is the same walk the hash join uses, so the order is declared only above a ScanExec, ShuffleScanExec or a scan of one file group, and an input whose declared order already satisfies the one sent is left as it is. When the input declares only part of it, as a window over another window does, the rest is declared next to it. So a window stacked over another one gets the longer order it needs, including the order of another alias of the same sort key. A hash join's probe input that declares part of its order is extended the same way. Both window operators find the partition keys' order through get_ordered_partition_by_indices on that shared input, so the declaration covers both.

  • keeps_source_order also accepts an EmptyExec, which is how the native scan plans a bucket with no file. Its rows are in any order, and the window checks its input's order before it polls it, so one empty bucket of a sorted bucketed table failed the query. The hash join's probe side gets the same declaration, which changes nothing there. It also accepts a right outer hash join whose probe declares an order, since that is when DataFusion keeps the join's unmatched probe rows in place. A window over an outer join then gets an alias's order declared next to the join's.

  • CometExecRule sorts the input of a window that runs in Spark when mayReportFalseOrder doubts the order its child reports, such as a native nested loop join's (#6898). Spark planned no sort there.
  • When the order the input reports may be false, a partitioned native window gets no order. It fails with the ordering error, as on main, or runs correctly when DataFusion sees the order. A window that runs in Spark over that order would trust it too, so CometExecRule sorts its input again. #6785 covers that case: it keeps a nested loop join over an ordered streamed side in Spark (#6898), and it sorts a window's input natively when the partition keys' order is not declared. DataFusion's InputOrderMode::Linear needs no declared order, but it hashes every partition and WindowAggExec has no such mode, so Comet keeps Sorted and declares the order instead.

    How are these changes tested?

  • planner.rs: window_declares_input_order_on_a_scan covers row_number (BoundedWindowAggExec) and percent_rank (WindowAggExec). It checks that the window's input is a SortedInputExec whose order covers the partition key, and that without the orders it is the bare scan, whose order does not. declared_order_extends_an_order_the_input_declares checks the declaration over an EmptyExec, that an input whose order already satisfies the one sent is left alone, that a trusted input gets the missing order next to its own, and that a sort gets nothing. hash_join_declares_streamed_order_on_probe_input also checks that a right outer join over a declared probe keeps the source order. The window expression moved into a helper that window_cast_projection_reports_window_time now uses as well.

  • CometInMemoryCacheSuite: a native window over a sorted cache declares the cache's order runs the repro from the issue with the native cache scan on and off. The partition key is nullable, so the declared order has to match the cache's null placement, and the order key is descending. It runs dense_rank, which lands on BoundedWindowAggExec, and a sum with a frame up to UNBOUNDED FOLLOWING, which lands on WindowAggExec. Each checks the rows, that the serde sent k ASC NULLS FIRST and not the order key, and that no sort is planned.

  • CometInMemoryCacheSuite: a native window declares its order next to the order its input declares runs a window by k under one by (k, x) over a cache sorted by (k, x), a window by k under one by a, an alias of k, and a window by k over a left outer join whose streamed order Spark reports as the alias a, with a residual condition that leaves unmatched rows among the matched ones of a key. Each checks the rows, the keys each window sends, and that no sort is planned.

  • CometInMemoryCacheSuite: a window over a cache sorted by a collated string key stays in Spark (Spark 4.x) runs a collated partition key and a collated order key, and checks that the window falls back and returns Spark's rows.

  • CometInMemoryCacheSuite: a window that stays in Spark over a nested loop join sorts the join's rows again runs an unpartitioned window and, on Spark 4.x, two windows with a collated key over a native nested loop join that reorders a one-partition cache. Each compares the rows with Spark and checks that every Spark window reads from a sort.

  • CometWindowExecSuite: window over a sorted bucketed table with empty buckets runs natively writes a table with eight buckets sorted by (k, v) and two keys, reads it with spark.sql.legacy.bucketedTableScan.outputOrdering on, and checks the window against Spark and that it sits directly on the bucketed native scan.

  • A Spark SortExec directly under a native window does not come up. Comet converts only leaf operators into a native operator's JVM input, and with spark.comet.exec.sort.enabled=false the window stays in Spark.
  • The window snippet in the debugging guide now builds both operators over input. The SortedInputExec doc and "Reporting an Output Ordering" in the operator guide now cover the window, and compatibility/operators.md lists collated keys among the window's fallbacks.

    Run locally with JDK 17 on Spark 4.1, merged with main at 3439d48583: the cache suite's window tests, CometWindowExecSuite and the collation SQL file tests pass, and the nested loop join test fails without the CometExecRule pass. Spark 3.4 does not build offline here, so it runs through the label. With only the native change in window.rs reverted, the cache test fails with CometNativeException: Expects PARTITION BY expression to be ordered. The Spark SQL suites run through the run-all-spark-profiles and run-spark-4.1-tests labels.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: rich7420 Review duration: 14770m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #7256"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%