Skip to content

Move filter for HAVING and QUALIFY when volatile aliases are used above projection - #26850

Merged
zhuqi-lucas merged 6 commits into
apache:mainfrom
matthewmturner:fix/correct-plan-for-volatile-aliases
Oct 31, 2026
Merged

zhuqi-lucas merged 6 commits into
apache:mainfrom
matthewmturner:fix/correct-plan-for-volatile-aliases

Conversation

@matthewmturner

@matthewmturner matthewmturner commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

The current behavior returns unexpected / incorrect results since the volatile expression used for evaluating predicate is different from the one used for projection.

What changes are included in this PR?

When a filter on a volatile alias is used with HAVING / QUALIFY it is moved above the projection so it operates on the same value that is projected.

What is the testing strategy for this PR?

SLT tests are added for the triggering cases and for cases that arent allowed with the new plan (primarily cases when there are filters on fields that arent in the projection so they are unavailable for filtering above the projection).

Are there any user-facing changes?

@matthewmturner matthewmturner changed the title Baseline tests to demonstrate the failure mode Plan subquery for HAVING and QUALIFY when volatile aliases are used Oct 28, 2026
@github-actions github-actions Bot added the sqllogictest SQL Logic Tests (.slt) label Oct 28, 2026
@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Oct 28, 2026
@github-actions github-actions Bot added the sql SQL Planner label Oct 30, 2026
@matthewmturner matthewmturner changed the title Plan subquery for HAVING and QUALIFY when volatile aliases are used Move filter for HAVING and QUALIFY when volatile aliases are used above projection Oct 30, 2026
@matthewmturner
matthewmturner marked this pull request as ready for review October 30, 2026 03:59
@matthewmturner

Copy link
Copy Markdown
Contributor Author

@xudong963 @zhuqi-lucas if you get the chance to review

@zhuqi-lucas zhuqi-lucas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @matthewmturner, nice fix — the post-projection filter plus the physical pushdown guard both look right to me, and the resulting plan matches the subquery rewrite suggested in #7976. Possible follow-up: split conjuncts at planning time so HAVING <volatile alias> AND <aggregate> can still plan.

@matthewmturner

Copy link
Copy Markdown
Contributor Author

@zhuqi-lucas ill check how big of a change it would be to add split conjucts on this pr

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

05)--------TableScan: users projection=[id]
physical_plan
01)FilterExec: r@2 > 0.5
02)--ProjectionExec: expr=[id@0 as id, row_number() ORDER BY [users.id ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as rn, random() as r]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to see random still here

----
3

# an aggregate that is not in the SELECT list cannot be combined with a

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯

@zhuqi-lucas

Copy link
Copy Markdown
Contributor

Thanks @matthewmturner and @alamb , LGTM, merged now.

@zhuqi-lucas
zhuqi-lucas added this pull request to the merge queue Oct 31, 2026
Merged via the queue into apache:main with commit 92290cb Oct 31, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate sql SQL Planner sqllogictest SQL Logic Tests (.slt) v56.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect result when use volatile function alias in having clause

3 participants