Skip to content

Conversation

rkrishn7
Copy link
Contributor

Which issue does this PR close?

What changes are included in this PR?

  • Updates select planning to search for aggregate references in QUALIFY and rewrites any aggregates in the QUALIFY clause to use column references (using the expression's schema name).
  • Tests

Are these changes tested?

Yes

Are there any user-facing changes?

No additional features, just fixes.

@github-actions github-actions bot added sql SQL Planner sqllogictest SQL Logic Tests (.slt) labels Aug 25, 2025
@alamb
Copy link
Contributor

alamb commented Aug 26, 2025

@Vedin can you please help review this PR? I see you are starting to do so on the original ticket #17210 (comment)

@Vedin
Copy link

Vedin commented Aug 26, 2025

In general, logic looks correct. The only unsupported thing is what I mentioned under the issue #17210 (comment)

Ok((plan, select_exprs_post_aggr, having_expr_post_aggr))
// Rewrite the QUALIFY expression to use the columns produced by the
// aggregation.
let qualify_expr_post_aggr = if let Some(qualify_expr) = qualify_expr_opt {
Copy link

Choose a reason for hiding this comment

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

nit: this logic is pretty similar to what is used for HAVING. What about creating a helper function for this helper logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely similar, although currently we still require multiple distinct function calls to check_column_satisfy_expr because for each expression (SELECT, HAVING, QUALIFY), we pass in diagnostic information that indicates which clause the error occurs in (CheckColumnsSatisfyExprsPurpose).

Given this, I'm not quite sure a helper function would help readability/redundancy too much

@rkrishn7
Copy link
Contributor Author

@Vedin @alamb What do y'all think about sequencing out the second issue mentioned in the original ticket? For the sake of smaller PRs.

If it makes sense, happy to file a separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql SQL Planner sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support aggregates and constant filters in QUALIFY
3 participants