fix: declare the input order of a native window over a sorted cache #7256
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
rich7420
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
ScanExecthey arrive through declares no order to DataFusion. The native window needs its input to declare the partition keys' order.BoundedWindowAggExec, which runs inInputOrderMode::Sorted, fails the task withExpects PARTITION BY expression to be ordered.WindowAggExecfails when its stream starts, withAll 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_exprsraises the error on DataFusionmainas 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?
Windowgetsrepeated Expr input_sort_orders = 5.CometWindowExecsends 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 whenmayReportFalseOrderdoubts that order. A newmayReportFalseInputOrderin theCometHashJointrait exposes that check. A window whose partition or order key holds a collated string falls back to Spark, asWindowGroupLimitdoes, since the native window compares keys byte by byte. So does a window withORDER BYbut noPARTITION BYover an ordermayReportFalseOrderdoubts, since the native window checks no order there.WindowBuilderdeclares the order on the window's input withwith_declared_order, nowpub(super). It is the same walk the hash join uses, so the order is declared only above aScanExec,ShuffleScanExecor 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 throughget_ordered_partition_by_indiceson that shared input, so the declaration covers both.keeps_source_orderalso accepts anEmptyExec, 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.CometExecRulesorts the input of a window that runs in Spark whenmayReportFalseOrderdoubts 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, soCometExecRulesorts 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'sInputOrderMode::Linearneeds no declared order, but it hashes every partition andWindowAggExechas no such mode, so Comet keepsSortedand declares the order instead.How are these changes tested?
planner.rs:window_declares_input_order_on_a_scancoversrow_number(BoundedWindowAggExec) andpercent_rank(WindowAggExec). It checks that the window's input is aSortedInputExecwhose 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_declareschecks the declaration over anEmptyExec, 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_inputalso checks that a right outer join over a declared probe keeps the source order. The window expression moved into a helper thatwindow_cast_projection_reports_window_timenow uses as well.CometInMemoryCacheSuite:a native window over a sorted cache declares the cache's orderruns 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 runsdense_rank, which lands onBoundedWindowAggExec, and asumwith a frame up toUNBOUNDED FOLLOWING, which lands onWindowAggExec. Each checks the rows, that the serde sentk ASC NULLS FIRSTand not the order key, and that no sort is planned.CometInMemoryCacheSuite:a native window declares its order next to the order its input declaresruns a window bykunder one by(k, x)over a cache sorted by(k, x), a window bykunder one bya, an alias ofk, and a window bykover a left outer join whose streamed order Spark reports as the aliasa, 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 againruns 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 nativelywrites a table with eight buckets sorted by(k, v)and two keys, reads it withspark.sql.legacy.bucketedTableScan.outputOrderingon, and checks the window against Spark and that it sits directly on the bucketed native scan.SortExecdirectly under a native window does not come up. Comet converts only leaf operators into a native operator's JVM input, and withspark.comet.exec.sort.enabled=falsethe window stays in Spark.The window snippet in the debugging guide now builds both operators over
input. TheSortedInputExecdoc and "Reporting an Output Ordering" in the operator guide now cover the window, andcompatibility/operators.mdlists collated keys among the window's fallbacks.Run locally with JDK 17 on Spark 4.1, merged with
mainat3439d48583: the cache suite's window tests,CometWindowExecSuiteand the collation SQL file tests pass, and the nested loop join test fails without theCometExecRulepass. Spark 3.4 does not build offline here, so it runs through the label. With only the native change inwindow.rsreverted, the cache test fails withCometNativeException: Expects PARTITION BY expression to be ordered. The Spark SQL suites run through therun-all-spark-profilesandrun-spark-4.1-testslabels.