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


##########
spark/src/main/scala/org/apache/spark/sql/comet/CometWindowExec.scala:
##########
@@ -545,6 +598,31 @@ object CometWindowExec extends 
CometOperatorSerde[WindowExec] {
       case _ =>
     }
 
+    // Spark accepts a ROWS 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. For
+    // an offset of 2 or more DataFusion computes a range whose start is past 
its end, and its
+    // sliding accumulator fails on that range; an offset of 1 gives an empty 
range, as in Spark.
+    // RANGE frames in this order are rejected by rangeBoundLiteral. A NaN 
RANGE offset, or an
+    // infinite one applied to an infinite key, gives a NaN bound that 
DataFusion's range search
+    // does not compare the way Spark does, in either sort direction.
+    f match {

Review Comment:
   Could we fold the finiteness check into `rangeBoundLiteral` and check 
`RowOffset` in `lBoundProto` and `uBoundProto`? That reuses `boundOffset` 
alongside the `MIN_VALUE` guards and lets us drop `rowsOffset`, 
`isNonFiniteBound`, and `boundDescription`.



##########
spark/src/test/resources/sql-tests/windows/reversed_frame.sql:
##########
@@ -0,0 +1,152 @@
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements.  See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership.  The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License.  You may obtain a copy of the License at
+--
+--   http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing,
+-- software distributed under the License is distributed on an
+-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+-- KIND, either express or implied.  See the License for the
+-- specific language governing permissions and limitations
+-- under the License.
+
+-- Window frames whose lower bound lies after their upper bound, and RANGE 
frames with a
+-- non-finite offset. Spark accepts a reversed frame when one bound is CURRENT 
ROW and evaluates
+-- every frame as empty. For a ROWS offset of 2 or more DataFusion computes a 
range whose start is
+-- past its end and its sliding accumulator failed the task or returned wrong 
values, so these fall
+-- back; an offset of 1 gives an empty range natively, as in Spark. A NaN 
RANGE offset, or an
+-- infinite one applied to an infinite key, gives a NaN bound that the native 
range search does
+-- not compare the way Spark does.
+
+-- Spark only takes a folded literal as a ROWS offset
+-- ConstantFolding: enabled
+-- Config: spark.sql.session.timeZone=UTC
+
+statement
+CREATE TABLE reversed_frame(id INT, p INT, i INT, d DATE, ts TIMESTAMP) USING 
parquet
+
+statement
+INSERT INTO reversed_frame VALUES
+  (1, 1, 1, DATE '2025-01-01', TIMESTAMP '2025-01-01 00:00:00'),
+  (2, 1, 2, DATE '2025-01-02', TIMESTAMP '2025-01-02 00:00:00'),
+  (3, 1, 2, DATE '2025-01-02', TIMESTAMP '2025-01-02 00:00:00'),
+  (4, 1, 4, DATE '2025-01-04', TIMESTAMP '2025-01-04 00:00:00'),
+  (5, 1, NULL, NULL, NULL),
+  (6, 2, 1, DATE '2025-01-01', TIMESTAMP '2025-01-01 00:00:00'),
+  (7, 2, 3, DATE '2025-01-03', TIMESTAMP '2025-01-03 00:00:00'),
+  (8, 2, NULL, NULL, NULL)
+
+statement
+CREATE TABLE non_finite_frame(id INT, x DOUBLE) USING parquet
+
+statement
+INSERT INTO non_finite_frame VALUES
+  (1, CAST('-Infinity' AS DOUBLE)), (2, -1.0), (3, 0.0), (4, 1.0), (5, 1.0),
+  (6, CAST('Infinity' AS DOUBLE)), (7, NULL)
+
+-- ROWS, offset of 2 or more: count 0 and NULL for the rest in Spark
+query expect_fallback(after upper bound CURRENT ROW is not supported)
+SELECT id,
+  COUNT(*) OVER w AS c, SUM(id) OVER w AS s, AVG(id) OVER w AS a, MIN(id) OVER 
w AS mn,
+  MAX(id) OVER w AS mx, FIRST_VALUE(id) OVER w AS fv, LAST_VALUE(id) OVER w AS 
lv
+FROM reversed_frame
+WINDOW w AS (PARTITION BY p ORDER BY i ROWS BETWEEN 2 FOLLOWING AND CURRENT 
ROW)
+
+query expect_fallback(after upper bound 2 PRECEDING is not supported)
+SELECT id,
+  COUNT(*) OVER w AS c, SUM(id) OVER w AS s, AVG(id) OVER w AS a, MIN(id) OVER 
w AS mn,
+  MAX(id) OVER w AS mx, FIRST_VALUE(id) OVER w AS fv, LAST_VALUE(id) OVER w AS 
lv
+FROM reversed_frame
+WINDOW w AS (PARTITION BY p ORDER BY i ROWS BETWEEN CURRENT ROW AND 2 
PRECEDING)
+
+query expect_fallback(after upper bound CURRENT ROW is not supported)
+SELECT id, COUNT(*) OVER (ORDER BY i DESC ROWS BETWEEN 3 FOLLOWING AND CURRENT 
ROW) AS c,
+  SUM(id) OVER (ORDER BY i DESC ROWS BETWEEN 3 FOLLOWING AND CURRENT ROW) AS s
+FROM reversed_frame
+
+query expect_fallback(after upper bound 2 PRECEDING is not supported)
+SELECT id, COUNT(*) OVER (ORDER BY i DESC ROWS BETWEEN CURRENT ROW AND 2 
PRECEDING) AS c,
+  SUM(id) OVER (ORDER BY i DESC ROWS BETWEEN CURRENT ROW AND 2 PRECEDING) AS s
+FROM reversed_frame
+
+-- ROWS, offset of 1 or 0: empty or one-row frames, native
+query

Review Comment:
   Maybe include `AVG`, `FIRST_VALUE`, and `LAST_VALUE` in the offset 1 `ROWS` 
queries too? `FIRST_VALUE` and `LAST_VALUE` hit `NthValueEvaluator` rather than 
`SlidingAggregateWindowExpr`, so that covers both paths in the Comet pipeline.



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