morrySnow opened a new pull request, #67706: URL: https://github.com/apache/doris/pull/67706
## Problem The optimizer simplifies window functions when the partition keys are unique because each partition contains at most one row. It replaces functions such as `SUM(value)` with `value` and a non-null `COUNT` with `1`. That replacement produces incorrect results when the window frame excludes the current row. For a one-row partition, a preceding-only or following-only frame is empty, so `SUM` must return `NULL` and `COUNT` must return `0`. ## Root cause The simplification rule used partition cardinality alone and did not check whether the normalized frame actually contains the partition's only row. ## How to reproduce Create a merge-on-write unique-key table with one row and run `SUM` and `COUNT(*)` over a window partitioned by the unique key with `ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING` (or the equivalent following-only frame). Before this change, the optimizer removed the window and returned the current value and `1`; the correct result is `NULL` and `0`. ## Fix Check the normalized frame boundaries before simplifying frame-dependent functions. `COUNT`, `SUM`, `MIN`, `MAX`, `AVG`, `FIRST_VALUE`, and `LAST_VALUE` are simplified only when the frame contains the current row. Ranking functions retain their existing simplification because their result does not depend on frame membership. ## Tests - Added a regression suite covering preceding-only and following-only frames that must retain `PhysicalWindow` and return `NULL`/`0`. - Added current-row and centered-frame cases that continue to eliminate `PhysicalWindow` and return the simplified values. - Ran the new regression suite in verification mode: 1 suite passed, 0 failed. - Built the FE successfully with all reactor modules passing and no checkstyle violations. -- 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]
