Skip to content

Commit

Permalink
[release-19.0] fix: derived table join column expression to be part o…
Browse files Browse the repository at this point in the history
…f add join predicate on rewrite (#15956) (#15960)

Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Co-authored-by: Harshit Gangal <harshit@planetscale.com>
Co-authored-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
3 people authored May 16, 2024
1 parent 61ece6f commit 31c5a7d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
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, 19, "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 sqlparser.ContainsAggregation(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))
}
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 @@ -4943,5 +4943,27 @@
"user.user"
]
}
},
{
"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"
]
}
}
]

0 comments on commit 31c5a7d

Please sign in to comment.