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

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we can try it in a follow on PR

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

@rkrishn7
Copy link
Contributor Author

How's this looking? @alamb @Vedin

@rkrishn7
Copy link
Contributor Author

rkrishn7 commented Sep 4, 2025

Hi @alamb friendly bump here. I think this should be good to merge and then we can tackle the second issue in another PR

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Looks good to me -- thank you @rkrishn7 and @Vedin

I apologize for the delay in review

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
Contributor

Choose a reason for hiding this comment

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

Perhaps we can try it in a follow on PR

@@ -184,7 +184,7 @@ fn test_missing_non_aggregate_in_group_by() -> Result<()> {
let diag = do_query(query);
assert_snapshot!(diag.message, @"'person.first_name' must appear in GROUP BY clause because it's not an aggregate expression");
assert_eq!(diag.span, Some(spans["a"]));
assert_snapshot!(diag.helps[0].message, @"Either add 'person.first_name' to GROUP BY clause, or use an aggregare function like ANY_VALUE(person.first_name)");
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

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