Skip to content

branch-4.1: [fix](window) Respect frames when simplifying window functions #67706 - #67743

Open
github-actions[bot] wants to merge 1 commit into
branch-4.1from
auto-pick-67706-branch-4.1
Open

branch-4.1: [fix](window) Respect frames when simplifying window functions #67706#67743
github-actions[bot] wants to merge 1 commit into
branch-4.1from
auto-pick-67706-branch-4.1

Conversation

@github-actions

@github-actions github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Cherry-picked from #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.
@github-actions
github-actions Bot requested a review from yiguolei as a code owner September 9, 2026 12:50
@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@hello-stephen

Copy link
Copy Markdown
Contributor

run buildall

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants