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: make sure to split predicates before planning them #17099

Merged
merged 1 commit into from
Oct 29, 2024
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
22 changes: 14 additions & 8 deletions go/vt/vtgate/planbuilder/operators/apply_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,21 @@ func (aj *ApplyJoin) AddJoinPredicate(ctx *plancontext.PlanningContext, expr sql
if expr == nil {
return nil
}
col, err := BreakExpressionInLHSandRHS(ctx, expr, TableID(aj.LHS))
if err != nil {
return err
}
aj.JoinPredicates = append(aj.JoinPredicates, col)
rhs, err := aj.RHS.AddPredicate(ctx, col.RHSExpr)
if err != nil {
return err
rhs := aj.RHS
predicates := sqlparser.SplitAndExpression(nil, expr)
for _, pred := range predicates {
col, err := BreakExpressionInLHSandRHS(ctx, pred, TableID(aj.LHS))
if err != nil {
return err
}
aj.JoinPredicates = append(aj.JoinPredicates, col)
ctx.AddJoinPredicates(pred, col.RHSExpr)
rhs, err = rhs.AddPredicate(ctx, col.RHSExpr)
if err != nil {
return err
}
}

aj.RHS = rhs

return nil
Expand Down
24 changes: 23 additions & 1 deletion go/vt/vtgate/planbuilder/testdata/from_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@
"Sharded": true
},
"FieldQuery": "select 42 from `user` as u, user_extra as ue, music as m where 1 != 1",
"Query": "select 42 from `user` as u, user_extra as ue, music as m where u.id = ue.user_id and u.id = :m_user_id and (u.foo or :m_foo or ue.foo) and m.user_id = u.id and (u.foo or m.foo or ue.foo)",
"Query": "select 42 from `user` as u, user_extra as ue, music as m where u.id = ue.user_id and m.user_id = u.id and (u.foo or m.foo or ue.foo)",
"Table": "`user`, music, user_extra"
},
"TablesUsed": [
Expand Down Expand Up @@ -4193,5 +4193,27 @@
"zlookup_unique.t1"
]
}
},
{
"comment": "lots of joins that can be merged should not confuse the planner",
"query": "SELECT `music`.id FROM `music` INNER JOIN music `music2` ON `music2`.`id` = `music`.`id` INNER JOIN music `music3` ON `music3`.`id` = `music2`.`id` INNER JOIN music `music4` ON `music4`.`id` = `music3`.`id` WHERE music.user_index = music4.user_index",
"plan": {
"QueryType": "SELECT",
"Original": "SELECT `music`.id FROM `music` INNER JOIN music `music2` ON `music2`.`id` = `music`.`id` INNER JOIN music `music3` ON `music3`.`id` = `music2`.`id` INNER JOIN music `music4` ON `music4`.`id` = `music3`.`id` WHERE music.user_index = music4.user_index",
"Instructions": {
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select music.id from music, music as music2, music as music3, music as music4 where 1 != 1",
"Query": "select music.id from music, music as music2, music as music3, music as music4 where music2.id = music.id and music4.id = music3.id and music3.id = music2.id and music.user_index = music4.user_index",
"Table": "music"
},
"TablesUsed": [
"user.music"
]
}
}
]
Loading