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

bugfix: use the original expression and not the alias #14704

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions go/vt/vtgate/planbuilder/operators/aggregation_pushing.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,19 +433,22 @@ func splitGroupingToLeftAndRight(ctx *plancontext.PlanningContext, rootAggr *Agg
var groupingJCs []JoinColumn

for _, groupBy := range rootAggr.Grouping {
deps := ctx.SemTable.RecursiveDeps(groupBy.Inner)
expr := groupBy.Inner
expr, err := rootAggr.QP.GetSimplifiedExpr(ctx, groupBy.Inner)
if err != nil {
panic(err)
}
deps := ctx.SemTable.RecursiveDeps(expr)
switch {
case deps.IsSolvedBy(lhs.tableID):
lhs.addGrouping(ctx, groupBy)
groupingJCs = append(groupingJCs, JoinColumn{
Original: groupBy.Inner,
Original: expr,
LHSExprs: []BindVarExpr{{Expr: expr}},
})
case deps.IsSolvedBy(rhs.tableID):
rhs.addGrouping(ctx, groupBy)
groupingJCs = append(groupingJCs, JoinColumn{
Original: groupBy.Inner,
Original: expr,
RHSExpr: expr,
})
case deps.IsSolvedBy(lhs.tableID.Merge(rhs.tableID)):
Expand Down
70 changes: 70 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/aggr_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -6372,5 +6372,75 @@
"user.user"
]
}
},
{
"comment": "Group by aliases on both sides of a join",
"query": "select count(*), cast(user.foo as datetime) as f1, cast(music.foo as datetime) as f2 from user join music group by f1, f2",
"plan": {
"QueryType": "SELECT",
"Original": "select count(*), cast(user.foo as datetime) as f1, cast(music.foo as datetime) as f2 from user join music group by f1, f2",
"Instructions": {
"OperatorType": "Aggregate",
"Variant": "Ordered",
"Aggregates": "sum_count_star(0) AS count(*)",
"GroupBy": "(1|3), (2|4)",
"ResultColumns": 3,
"Inputs": [
{
"OperatorType": "Sort",
"Variant": "Memory",
"OrderBy": "(1|3) ASC, (2|4) ASC",
"Inputs": [
{
"OperatorType": "Projection",
"Expressions": [
"count(*) * count(*) as count(*)",
":2 as f1",
":3 as f2",
":4 as weight_string(cast(`user`.foo as datetime))",
":5 as weight_string(cast(music.foo as datetime))"
],
"Inputs": [
{
"OperatorType": "Join",
"Variant": "Join",
"JoinColumnIndexes": "L:0,R:0,L:1,R:1,L:2,R:2",
"TableName": "`user`_music",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select count(*), cast(`user`.foo as datetime) as f1, weight_string(cast(`user`.foo as datetime)) from `user` where 1 != 1 group by f1, weight_string(cast(`user`.foo as datetime))",
"Query": "select count(*), cast(`user`.foo as datetime) as f1, weight_string(cast(`user`.foo as datetime)) from `user` group by f1, weight_string(cast(`user`.foo as datetime))",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select count(*), cast(music.foo as datetime) as f2, weight_string(cast(music.foo as datetime)) from music where 1 != 1 group by f2, weight_string(cast(music.foo as datetime))",
"Query": "select count(*), cast(music.foo as datetime) as f2, weight_string(cast(music.foo as datetime)) from music group by f2, weight_string(cast(music.foo as datetime))",
"Table": "music"
}
]
}
]
}
]
}
]
},
"TablesUsed": [
"user.music",
"user.user"
]
}
}
]
Loading