SEPURI-SAI-KRISHNA opened a new pull request, #60175:
URL: https://github.com/apache/spark/pull/60175

   ### What changes were proposed in this pull request?
   
   For a `RANGE` window frame with an offset, ordered by a `TINYINT`, 
`SMALLINT`, `INT` or `BIGINT` expression, this PR makes the frame bounds exact:
   
   - `WindowFrameTypeCoercion` no longer narrows an integral offset to the 
integral order type. Both offsets take the widest of the order type and their 
own types, and `WindowSpecDefinition` accepts an integral offset that is wider 
than the integral order type. An offset that fits in the order type is coerced 
as before.
   - `WindowEvaluatorFactoryBase.createBoundOrdering` uses a new 
`IntegralRangeBoundOrdering` for these frames. Instead of computing the bound 
(`value + offset`, or `value - offset` in descending order) in the order type, 
it compares a value with the bound exactly in `Long`, with an overflow check: a 
bound beyond the range of `Long` is greater or smaller than every value. NULL 
values compare as before.
   
   This is the same problem that SPARK-41793 fixed for `DECIMAL` order types.
   
   ### Why are the changes needed?
   
   The frame bounds of these frames are computed in the order type, so they 
overflow:
   
   1. The offset is cast to the order type. An offset that does not fit wraps 
around with ANSI mode off, and fails the query with ANSI mode on:
   
   ```sql
   CREATE TABLE t USING parquet AS
     SELECT CAST(id AS INT) AS id, CAST(id * 10 AS TINYINT) AS b FROM range(0, 
6);
   
   SELECT id, b, count(*) OVER (ORDER BY b RANGE BETWEEN 200 PRECEDING AND 
CURRENT ROW) FROM t;
   -- ANSI off: 0 for every row (expected 1, 2, ..., 6)
   -- ANSI on:  [CAST_OVERFLOW] The value -200 of the type "INT" cannot be cast 
to "TINYINT"
   ```
   
   2. The bound itself overflows near the limits of the type, even when the 
offset fits. For `TINYINT` scores 0, 5, ..., 100, `count(*) OVER (ORDER BY 
score DESC RANGE BETWEEN 30 PRECEDING AND CURRENT ROW)` is wrong for every 
score of 70 or more with ANSI mode off (the score 100 counts 0 rows, although 
the frame always contains the current row), and fails with 
`BINARY_ARITHMETIC_OVERFLOW` with ANSI mode on. The same happens for `INT` and 
`BIGINT` values near their limits.
   
   The frames are well defined, so they should return the rows they define. The 
ported PostgreSQL test `postgreSQL/window_part2.sql` ("Check overflow behavior 
for various integer sizes") recorded `ARITHMETIC_OVERFLOW` for its `BIGINT` 
cases, where PostgreSQL returns a result; it now returns the PostgreSQL results.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, these `RANGE` frames return the correct rows, instead of wrong rows 
(ANSI mode off) or an error (ANSI mode on). Frames whose bounds do not overflow 
return the same rows as before.
   
   The generated name of a window expression whose integral offset is wider 
than the integral order type, such as `200 PRECEDING` on a `TINYINT` column, 
now shows the frame as written (`200 PRECEDING`) instead of the cast offset 
(`(- 200) FOLLOWING`). Other frames keep their names and plans.
   
   ### How was this patch tested?
   
   - New tests in `DataFrameWindowFramesSuite`: the examples above, and a test 
that checks `count`, `min` and `max` over 34 frames (offsets up to 
`Long.MaxValue`, combined with `CURRENT ROW`, another offset, or `UNBOUNDED`) 
for `TINYINT`, `SMALLINT`, `INT` and `BIGINT` values at the limits of each type 
and NULL, for all four directions / null orderings, with ANSI mode on and off, 
against the frames computed from their definition. It also checks the segment 
tree frames (`count`) and the monotonic deque frames (`min` and `max`), and 
that they are used. Both tests fail without this change.
   - New tests in `TypeCoercionSuite` and `AnsiTypeCoercionSuite` for the 
coercion of the offsets.
   - Regenerated golden files: `postgreSQL/window_part2.sql` (the overflow 
cases above) and the analyzer results of 
`typeCoercion/native/windowFrameCoercion.sql` (an `INT` offset of a `TINYINT` / 
`SMALLINT` order is no longer cast).
   - Ran `TypeCoercionSuite`, `AnsiTypeCoercionSuite`, `AnalysisSuite`, 
`AnalysisErrorSuite`, `ExpressionTypeCheckingSuite`, 
`OptimizeWindowFunctionsSuite`, `CollapseWindowSuite`, 
`DataFrameWindowFramesSuite`, `DataFrameWindowFunctionsSuite`, 
`SQLWindowFunctionSuite`, the suites in 
`org.apache.spark.sql.execution.window`, and `SQLQueryTestSuite` for the 
window, pipe operator and counter-diff inputs.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   


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