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: derived table join column expression to be part of add join predicate on rewrite #15956

Merged
merged 5 commits into from
May 16, 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
12 changes: 12 additions & 0 deletions go/test/endtoend/vtgate/queries/derived/derived_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,15 @@ func TestDerivedTablesWithLimit(t *testing.T) {
(SELECT id, user_id FROM music LIMIT 10) as m on u.id = m.user_id`,
`[[INT64(1) INT64(1)] [INT64(5) INT64(2)] [INT64(1) INT64(3)] [INT64(2) INT64(4)] [INT64(3) INT64(5)] [INT64(5) INT64(7)] [INT64(4) INT64(6)] [INT64(6) NULL]]`)
}

// TestDerivedTableColumnAliasWithJoin tests the derived table having alias column and using it in the join condition
func TestDerivedTableColumnAliasWithJoin(t *testing.T) {
utils.SkipIfBinaryIsBelowVersion(t, 20, "vtgate")
mcmp, closer := start(t)
defer closer()

mcmp.Exec(`SELECT user.id FROM user join (SELECT id as uid FROM user) t on t.uid = user.id`)
mcmp.Exec(`SELECT user.id FROM user left join (SELECT id as uid FROM user) t on t.uid = user.id`)
mcmp.Exec(`SELECT user.id FROM user join (SELECT id FROM user) t(uid) on t.uid = user.id`)
mcmp.Exec(`SELECT user.id FROM user left join (SELECT id FROM user) t(uid) on t.uid = user.id`)
}
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/horizon.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (h *Horizon) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.
panic(err)
}

newExpr := semantics.RewriteDerivedTableExpression(expr, tableInfo)
newExpr := ctx.RewriteDerivedTableExpression(expr, tableInfo)
if ContainsAggr(ctx, newExpr) {
return newFilter(h, expr)
}
Expand Down
3 changes: 3 additions & 0 deletions go/vt/vtgate/planbuilder/operators/rewriters.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ func bottomUp(
childID = childID.Merge(resolveID(oldInputs[0]))
}
in, changed := bottomUp(operator, childID, resolveID, rewriter, shouldVisit, false)
if DebugOperatorTree && changed.Changed() {
fmt.Println(ToTree(in))
dbussink marked this conversation as resolved.
Show resolved Hide resolved
}
anythingChanged = anythingChanged.Merge(changed)
newInputs[i] = in
}
Expand Down
13 changes: 13 additions & 0 deletions go/vt/vtgate/planbuilder/plancontext/planning_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,16 @@ func (ctx *PlanningContext) execOnJoinPredicateEqual(joinPred sqlparser.Expr, fn
}
return false
}

func (ctx *PlanningContext) RewriteDerivedTableExpression(expr sqlparser.Expr, tableInfo semantics.TableInfo) sqlparser.Expr {
modifiedExpr := semantics.RewriteDerivedTableExpression(expr, tableInfo)
for key, exprs := range ctx.joinPredicates {
for _, rhsExpr := range exprs {
if ctx.SemTable.EqualsExpr(expr, rhsExpr) {
ctx.joinPredicates[key] = append(ctx.joinPredicates[key], modifiedExpr)
return modifiedExpr
}
}
}
return modifiedExpr
}
22 changes: 22 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/select_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -5221,5 +5221,27 @@
"main.unsharded_a"
]
}
},
{
"comment": "join with derived table with alias and join condition - merge into route",
"query": "select 1 from user join (select id as uid from user) as t where t.uid = user.id",
"plan": {
"QueryType": "SELECT",
"Original": "select 1 from user join (select id as uid from user) as t where t.uid = user.id",
"Instructions": {
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select 1 from (select id as uid from `user` where 1 != 1) as t, `user` where 1 != 1",
"Query": "select 1 from (select id as uid from `user`) as t, `user` where t.uid = `user`.id",
"Table": "`user`"
},
"TablesUsed": [
"user.user"
]
}
}
]
Loading