Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in SQL planner with GROUP BY scalar function and alias #2457

Merged
merged 5 commits into from
May 6, 2022

Conversation

andygrove
Copy link
Member

@andygrove andygrove commented May 5, 2022

Which issue does this PR close?

Closes #2430

Rationale for this change

Fix bugs that prevent us from running some valid aggregate SQL queries.

What changes are included in this PR?

  • Fixes a bug with aliasing and GROUP BY where an alias from the projection could have the same name as an input to the aggregate plan
  • Allow projection expressions to be resolved from the aggregate plan even if they are not directly part of the grouping expressions. For example we can now resolve c0 when the grouping expression is substr(c0, 1, 2)

Are there any user-facing changes?

No

@andygrove andygrove self-assigned this May 5, 2022
@github-actions github-actions bot added the datafusion Changes in the datafusion crate label May 5, 2022
@andygrove andygrove changed the title [WIP] Fix bugs in SQL planner with GROUP BY scalar function Fix bugs in SQL planner with GROUP BY scalar function May 6, 2022
@andygrove andygrove marked this pull request as ready for review May 6, 2022 14:43
// now attempt to resolve columns and replace with fully-qualified columns
let aggr_projection_exprs = aggr_projection_exprs
.iter()
.map(|expr| resolve_columns(expr, &input))
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the fix .. we were comparing qualified and unqualified names before this, leading to failures in some cases

@andygrove
Copy link
Member Author

@comphead @alamb @tustvold @yjshen @jdye64 This PR is now ready for review

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.

I also tried out some more exotic queries and they seem to work as expected 👍

select substr(substr(location, 4), 1) a from andrew group by substr(substr(location, 4), 1);
+-----------+
| a         |
+-----------+
| ta_monica |
| ote_creek |
| et_sound  |
+-----------+select substr(location, 4) a from andrew group by substr(location, 5);
Plan("Projection references non-aggregate values: Expression #andrew.location could not be resolved from available columns: #substr(andrew.location,Int64(5))")

I did find an issue with a query which technically probably shouldn't be run:

select substr(location, 4) a from andrew group by a;
+-----------+
| a         |
+-----------+
| ote_creek |
| et_sound  |
| ta_monica |
+-----------+

As the a is defined logically after the grouping

However, that query also works on the master branch prior to this PR

.aggregate(group_by_exprs.clone(), aggr_exprs.clone())?
.build()?;

// in this next section of code we are re-writing the projection to refer to columns
Copy link
Contributor

Choose a reason for hiding this comment

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

I am a huge fan of the comments ❤️ thank you

@alamb alamb changed the title Fix bugs in SQL planner with GROUP BY scalar function Fix bugs in SQL planner with GROUP BY scalar function and alias May 6, 2022
@andygrove
Copy link
Member Author

Thanks for the review @alamb !

@andygrove andygrove merged commit 675eb82 into apache:master May 6, 2022
@andygrove andygrove deleted the issue-2430 branch May 6, 2022 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datafusion Changes in the datafusion crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Group by incorrectly works with alias.
2 participants