-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add support for AVG on sharded queries #14419
Conversation
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
} | ||
|
||
if avg.Distinct { | ||
panic(vterrors.VT12001("AVG(distinct <>)")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my understanding, supporting this would require loading all the distinct values from the shard, and then perform the avg calculation on the vtgate layer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. Have not spent enough time thinking about it to come up with a solution, which is why I just fail here.
calcExpr := &sqlparser.BinaryExpr{ | ||
Operator: sqlparser.DivOp, | ||
Left: sumExpr, | ||
Right: countExpr, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
"QueryType": "SELECT", | ||
"Original": "select avg(id)+count(foo)+bar from user group by bar", | ||
"Instructions": { | ||
"OperatorType": "Projection", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add logic to compact two projections into a single one. Not part of this PR, just commenting for future reference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to create an issue for it.
{ | ||
"OperatorType": "Aggregate", | ||
"Variant": "Scalar", | ||
"Aggregates": "sum(0) AS avg(foo), sum_count(1) AS count(foo), sum_count(2) AS count(foo)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to have count twice here. could/should optimise this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty :gucci: my friend 👌
Signed-off-by: Andres Taylor <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
{ | ||
"OperatorType": "Aggregate", | ||
"Variant": "Scalar", | ||
"Aggregates": "sum(0) AS avg(foo), sum_count(1) AS count(foo), sum_count(2) AS count(foo)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't that be sum(0) as sum(foo)
? Maybe its just a display thing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, it's just a display thing. that alias is only seen in the plans
Nice to add |
Description
Adds support for AVG on queries that need to be sharded. We can't just send down
AVG
to each shard and average these together. We need to turnAVG
intoSUM/COUNT
, which is easy to shard.Paired with @arthurschreiber on this
Related Issue(s)
Fixes #9397
Checklist