Skip to content

Commit

Permalink
fix: derived table join column expression to be part of add join pred…
Browse files Browse the repository at this point in the history
…icate on rewrite (#15956)

Signed-off-by: Harshit Gangal <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
Co-authored-by: Andres Taylor <[email protected]>
  • Loading branch information
harshit-gangal and systay authored May 16, 2024
1 parent 6ff462c commit 7997d1e
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, 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))
}
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"
]
}
}
]

0 comments on commit 7997d1e

Please sign in to comment.