mrhhsg opened a new pull request, #68113:
URL: https://github.com/apache/doris/pull/68113

   ### What problem does this PR solve?
   
   Issue Number: None
   
   Problem Summary:
   
   Sliding `ROWS` window frames evaluate `sum`/`avg` incrementally: the outgoing
   row is subtracted from the accumulator and the incoming row is added. For
   floating-point accumulators this is not exact. Once `2^54 + 1` rounds to
   `2^54`, subtracting `2^54` again leaves `0` instead of `1`, so the rounding
   lost by a value that already left the frame keeps distorting later results.
   
   ```sql
   WITH t AS (
       SELECT 1 AS id, CAST(18014398509481984 AS DOUBLE) AS v
       UNION ALL SELECT 2, CAST(1 AS DOUBLE)
       UNION ALL SELECT 3, CAST(1 AS DOUBLE)
   )
   SELECT id, v,
          avg(v) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS 
got
   FROM t ORDER BY id;
   ```
   
   Before: the third row returned `0.5` (frame `[1, 1]`, sum `1`, count `2`).
   After: the third row returns `1`.
   
   Fix: `AggregateFunctionSum` and `AggregateFunctionAvg` now report
   `supported_incremental_mode() == false` when the accumulator is a
   floating-point type, so the analytic sink recomputes each sliding frame from
   its rows (the same approach PostgreSQL takes by not providing inverse
   transition functions for `float4`/`float8`). Integer and decimal accumulators
   are exact and keep the incremental path. Frames that only grow
   (`UNBOUNDED PRECEDING`) never removed rows and are unaffected.
   
   Cost: sliding `ROWS` frames over `FLOAT`/`DOUBLE` columns now cost
   O(frame size) per row instead of O(1). This is the trade-off for exact
   results; wide sliding frames over floating-point columns will be slower.
   
   ### Release note
   
   None
   
   ### Check List (For Author)
   
   - Test:
       - Unit Test: 
`AnalyticSinkOperatorTest.SlidingRowsDouble{Avg,Sum}IgnoresRoundingOfOutgoingRow`,
         
`AggregateFunction{Avg,Sum}Test.test_incremental_mode_only_for_exact_sum`
       - Regression test: 
`query_p0/sql_functions/window_functions/test_window_float_sliding_frame`
   - Behavior changed: Yes (sliding window sum/avg over FLOAT/DOUBLE now return 
the exact per-frame result; wide frames are slower)
   - Does this need documentation: No
   
   https://claude.ai/code/session_01KSyhoeWWGWukpHP6MBTbEt
   


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