comphead commented on code in PR #7813:
URL: https://github.com/apache/datafusion-comet/pull/7813#discussion_r4762658685


##########
native/core/src/execution/planner/window.rs:
##########
@@ -524,6 +555,23 @@ impl PhysicalPlanner {
         };
 
         let window_frame = WindowFrame::new_bounds(units, lower_bound, 
upper_bound);
+        // Spark accepts a frame whose lower bound lies after its upper bound 
when one of them
+        // is CURRENT ROW, such as `2 FOLLOWING AND CURRENT ROW`, and 
evaluates every frame as
+        // empty. DataFusion computes a range whose start is past its end for 
it, and its
+        // sliding accumulator fails on that range, so refuse the frame here; 
the JVM side falls
+        // back to Spark before it gets this far.
+        if bound_is_nan(&window_frame.start_bound) || 
bound_is_nan(&window_frame.end_bound) {
+            return Err(GeneralError(format!(
+                "Window frame with a NaN offset is not supported: {} AND {}",
+                window_frame.start_bound, window_frame.end_bound
+            )));
+        }
+        if lower_bound_after_upper_bound(&window_frame.start_bound, 
&window_frame.end_bound) {
+            return Err(GeneralError(format!(
+                "Window frame lower bound {} lies after its upper bound {}",
+                window_frame.start_bound, window_frame.end_bound
+            )));
+        }

Review Comment:
   This guard, the `bound_is_nan` and `lower_bound_after_upper_bound` helpers 
above, and the 148-line `window_frame_with_lower_bound_after_upper_bound` test 
in `planner.rs` repeat the fallback in `CometWindowExec.scala`, so one 
condition has two guards. The native copy is only reachable for ROWS. The lower 
bound proto only maps to `Preceding` and the upper only to `Following` for 
RANGE (see the RANGE arms above), so `(Following, CurrentRow)` and 
`(CurrentRow, Preceding)` cannot be built from a RANGE frame here. The other 
frame fallbacks (DECIMAL RANGE, nested ORDER BY keys, `FIRST_VALUE` on RANGE) 
have no native twin, and the comment near line 637 says decimal sliding frames 
are rejected by the JVM planner. I would drop the native guard and its test and 
keep the single fallback. I did not run any of this.



##########
spark/src/main/scala/org/apache/spark/sql/comet/CometWindowExec.scala:
##########
@@ -545,6 +581,29 @@ object CometWindowExec extends 
CometOperatorSerde[WindowExec] {
       case _ =>
     }
 
+    // Spark accepts a frame whose lower bound lies after its upper bound when 
one of them is
+    // CURRENT ROW, such as `2 FOLLOWING AND CURRENT ROW`, and evaluates every 
frame as empty.
+    // DataFusion computes a range whose start is past its end for it, and its 
sliding
+    // accumulator fails on that range. A NaN RANGE offset gives a bound Spark 
compares as the
+    // largest value, so a NaN lower bound also makes every frame empty, while 
the native frame
+    // computes a NaN bound and reaches the same reversed range.
+    f match {
+      case SpecifiedWindowFrame(_, lb, ub) if Seq(lb, ub).exists(isNaNBound) =>
+        withFallbackReason(
+          windowExpr,
+          s"Window frame with a NaN offset is not supported: ${lb.sql} AND 
${ub.sql}")
+        return None
+      case SpecifiedWindowFrame(_, lb, ub)
+          if (ub == CurrentRow && boundSignum(lb) > 0) ||
+            (lb == CurrentRow && boundSignum(ub) < 0) =>
+        withFallbackReason(
+          windowExpr,
+          "Window frame with a lower bound after its upper bound is not 
supported: " +
+            s"${lb.sql} AND ${ub.sql}")

Review Comment:
   `lb.sql` and `ub.sql` print the folded bound, not Spark's direction wording. 
Spark's own `boundarySql` is private and only adds PRECEDING or FOLLOWING for 
an unfolded `UnaryMinus`. If the optimizer has folded the bound into a literal 
as `explain` shows (`specifiedwindowframe(RowFrame, -2, currentrow$())`), 
`CURRENT ROW AND 2 PRECEDING` would read `CURRENT ROW AND -2` here. I did not 
run this. The reason is the only thing the user sees in the log, so it would 
help to name the frame type and say which side is after which, for example 
`ROWS frame with lower bound 2 after upper bound CURRENT ROW`.



##########
spark/src/test/scala/org/apache/comet/exec/CometWindowExecSuite.scala:
##########
@@ -492,6 +492,75 @@ class CometWindowExecSuite extends CometTestBase {
     }
   }
 
+  test("window: frame with a lower bound after its upper bound falls back") {

Review Comment:
   This test writes a parquet file and loops over frame strings, which is what 
a SQL file test does with less code. A file under 
`spark/src/test/resources/sql-tests/windows/` can create the table with INT, 
DATE, TIMESTAMP and DOUBLE columns, use `query expect_fallback(lower bound 
after its upper bound)` and `query expect_fallback(NaN offset)` per frame, and 
a plain `query` for the frames that must stay native. `-- Config: 
spark.sql.session.timeZone=UTC` replaces the `withSQLConf`. 
`nested_null_range_frame.sql` in that directory is the same shape. The SQL file 
would also be the place to add the cases the earlier review asked for, a DESC 
ordering and a NULL key, since the loop here only orders ASC over non-null keys.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to