Skip to content

Commit 79ddc2f

Browse files
wip
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
1 parent 45192d2 commit 79ddc2f

File tree

11 files changed

+61
-23
lines changed

11 files changed

+61
-23
lines changed

go/vt/vtgate/planbuilder/operators/cte_merging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func tryMergeRecurse(ctx *plancontext.PlanningContext, in *RecurseCTE) (Operator
3131
}
3232

3333
func tryMergeCTE(ctx *plancontext.PlanningContext, seed, term Operator, in *RecurseCTE) *Route {
34-
seedRoute, termRoute, routingA, routingB, a, b, sameKeyspace := prepareInputRoutes(seed, term)
34+
seedRoute, termRoute, routingA, routingB, a, b, sameKeyspace := prepareInputRoutes(ctx, seed, term)
3535
if seedRoute == nil {
3636
return nil
3737
}

go/vt/vtgate/planbuilder/operators/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func createDeleteWithInputOp(ctx *plancontext.PlanningContext, del *sqlparser.De
124124
}
125125

126126
var delOps []dmlOp
127-
for _, target := range ctx.SemTable.Targets.Constituents() {
127+
for _, target := range ctx.SemTable.DMLTargets.Constituents() {
128128
op := createDeleteOpWithTarget(ctx, target, del.Ignore)
129129
delOps = append(delOps, op)
130130
}

go/vt/vtgate/planbuilder/operators/join_merging.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
// If they can be merged, a new operator with the merged routing is returned
2929
// If they cannot be merged, nil is returned.
3030
func (jm *joinMerger) mergeJoinInputs(ctx *plancontext.PlanningContext, lhs, rhs Operator, joinPredicates []sqlparser.Expr) *Route {
31-
lhsRoute, rhsRoute, routingA, routingB, a, b, sameKeyspace := prepareInputRoutes(lhs, rhs)
31+
lhsRoute, rhsRoute, routingA, routingB, a, b, sameKeyspace := prepareInputRoutes(ctx, lhs, rhs)
3232
if lhsRoute == nil {
3333
return nil
3434
}
@@ -102,13 +102,13 @@ func mergeAnyShardRoutings(ctx *plancontext.PlanningContext, a, b *AnyShardRouti
102102
}
103103
}
104104

105-
func prepareInputRoutes(lhs Operator, rhs Operator) (*Route, *Route, Routing, Routing, routingType, routingType, bool) {
105+
func prepareInputRoutes(ctx *plancontext.PlanningContext, lhs Operator, rhs Operator) (*Route, *Route, Routing, Routing, routingType, routingType, bool) {
106106
lhsRoute, rhsRoute := operatorsToRoutes(lhs, rhs)
107107
if lhsRoute == nil || rhsRoute == nil {
108108
return nil, nil, nil, nil, 0, 0, false
109109
}
110110

111-
lhsRoute, rhsRoute, routingA, routingB, sameKeyspace := getRoutesOrAlternates(lhsRoute, rhsRoute)
111+
lhsRoute, rhsRoute, routingA, routingB, sameKeyspace := getRoutesOrAlternates(ctx, lhsRoute, rhsRoute)
112112

113113
a, b := getRoutingType(routingA), getRoutingType(routingB)
114114
return lhsRoute, rhsRoute, routingA, routingB, a, b, sameKeyspace
@@ -159,7 +159,7 @@ func (rt routingType) String() string {
159159

160160
// getRoutesOrAlternates gets the Routings from each Route. If they are from different keyspaces,
161161
// we check if this is a table with alternates in other keyspaces that we can use
162-
func getRoutesOrAlternates(lhsRoute, rhsRoute *Route) (*Route, *Route, Routing, Routing, bool) {
162+
func getRoutesOrAlternates(ctx *plancontext.PlanningContext, lhsRoute, rhsRoute *Route) (*Route, *Route, Routing, Routing, bool) {
163163
routingA := lhsRoute.Routing
164164
routingB := rhsRoute.Routing
165165
sameKeyspace := routingA.Keyspace() == routingB.Keyspace()
@@ -171,13 +171,15 @@ func getRoutesOrAlternates(lhsRoute, rhsRoute *Route) (*Route, *Route, Routing,
171171
return lhsRoute, rhsRoute, routingA, routingB, sameKeyspace
172172
}
173173

174-
if refA, ok := routingA.(*AnyShardRouting); ok {
174+
if refA, ok := routingA.(*AnyShardRouting); ok &&
175+
!TableID(lhsRoute).IsOverlapping(ctx.SemTable.DMLTargets) {
175176
if altARoute := refA.AlternateInKeyspace(routingB.Keyspace()); altARoute != nil {
176177
return altARoute, rhsRoute, altARoute.Routing, routingB, true
177178
}
178179
}
179180

180-
if refB, ok := routingB.(*AnyShardRouting); ok {
181+
if refB, ok := routingB.(*AnyShardRouting); ok &&
182+
!TableID(rhsRoute).IsOverlapping(ctx.SemTable.DMLTargets) {
181183
if altBRoute := refB.AlternateInKeyspace(routingA.Keyspace()); altBRoute != nil {
182184
return lhsRoute, altBRoute, routingA, altBRoute.Routing, true
183185
}

go/vt/vtgate/planbuilder/operators/subquery_planning.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ func rewriteOriginalPushedToRHS(ctx *plancontext.PlanningContext, expression sql
377377
// this is used when we push an operator from above the subquery into the outer side of the subquery
378378
func rewriteColNameToArgument(
379379
ctx *plancontext.PlanningContext,
380-
in sqlparser.Expr, // the expression to rewrite
381-
se SubQueryExpression, // the subquery expression we are rewriting
380+
in sqlparser.Expr, // the expression to rewrite
381+
se SubQueryExpression, // the subquery expression we are rewriting
382382
subqueries ...*SubQuery, // the inner subquery operators
383383
) sqlparser.Expr {
384384
// the visitor function that will rewrite the expression tree
@@ -730,7 +730,7 @@ func mergeSubqueryInputs(ctx *plancontext.PlanningContext, in, out Operator, joi
730730
return nil
731731
}
732732

733-
inRoute, outRoute, inRouting, outRouting, sameKeyspace := getRoutesOrAlternates(inRoute, outRoute)
733+
inRoute, outRoute, inRouting, outRouting, sameKeyspace := getRoutesOrAlternates(ctx, inRoute, outRoute)
734734
inner, outer := getRoutingType(inRouting), getRoutingType(outRouting)
735735

736736
switch {

go/vt/vtgate/planbuilder/operators/union_merging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func mergeUnionInputs(
108108
lhsExprs, rhsExprs sqlparser.SelectExprs,
109109
distinct bool,
110110
) (Operator, sqlparser.SelectExprs) {
111-
lhsRoute, rhsRoute, routingA, routingB, a, b, sameKeyspace := prepareInputRoutes(lhs, rhs)
111+
lhsRoute, rhsRoute, routingA, routingB, a, b, sameKeyspace := prepareInputRoutes(ctx, lhs, rhs)
112112
if lhsRoute == nil {
113113
return nil, nil
114114
}

go/vt/vtgate/planbuilder/operators/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func createUpdateWithInputOp(ctx *plancontext.PlanningContext, upd *sqlparser.Up
164164
ueMap := prepareUpdateExpressionList(ctx, upd)
165165

166166
var updOps []dmlOp
167-
for _, target := range ctx.SemTable.Targets.Constituents() {
167+
for _, target := range ctx.SemTable.DMLTargets.Constituents() {
168168
op := createUpdateOpWithTarget(ctx, upd, target, ueMap[target])
169169
updOps = append(updOps, op)
170170
}
@@ -308,7 +308,7 @@ func errIfUpdateNotSupported(ctx *plancontext.PlanningContext, stmt *sqlparser.U
308308
}
309309
}
310310

311-
// Now we check if any of the foreign key columns that are being udpated have dependencies on other updated columns.
311+
// Now we check if any of the foreign key columns that are being updated have dependencies on other updated columns.
312312
// This is unsafe, and we currently don't support this in Vitess.
313313
if err := ctx.SemTable.ErrIfFkDependentColumnUpdated(stmt.Exprs); err != nil {
314314
panic(err)

go/vt/vtgate/planbuilder/plan_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ func (s *planTestSuite) TestOne() {
305305
s.addPKsProvided(vschema, "user", []string{"user_extra"}, []string{"id", "user_id"})
306306
s.addPKsProvided(vschema, "ordering", []string{"order"}, []string{"oid", "region_id"})
307307
s.addPKsProvided(vschema, "ordering", []string{"order_event"}, []string{"oid", "ename"})
308+
s.addPKsProvided(vschema, "legacy", []string{"users"}, []string{"idUser"})
308309

309310
s.testFile("onecase.json", vw, false)
310311
}

go/vt/vtgate/planbuilder/testdata/onecase.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"comment": "Add your test case here for debugging and run go test -run=One.",
4-
"query": "",
4+
"query": "update legacy.users as u join legacy.users2sites as u2s on u2s.idUser = u.idUser inner join sites2024.sites as s on s.idSite = u2s.idSite set u.email = 'asdf@example.com' where s.idSite = 234234234;",
55
"plan": {
66
}
77
}

go/vt/vtgate/planbuilder/testdata/vschemas/schema.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,41 @@
10071007
]
10081008
}
10091009
}
1010+
},
1011+
"sites2024": {
1012+
"sharded": true,
1013+
"require_explicit_routing": false,
1014+
"vindexes": {
1015+
"a_standard_hash": {
1016+
"type": "xxhash"
1017+
}
1018+
},
1019+
"tables": {
1020+
"users": {
1021+
"type": "reference",
1022+
"source": "legacy.users"
1023+
},
1024+
"users2sites": {
1025+
"type": "reference",
1026+
"source": "legacy.users2sites"
1027+
},
1028+
"sites": {
1029+
"column_vindexes": [
1030+
{
1031+
"column": "idSite",
1032+
"name": "a_standard_hash"
1033+
}
1034+
]
1035+
}
1036+
}
1037+
},
1038+
"legacy": {
1039+
"require_explicit_routing": false,
1040+
"sharded": false,
1041+
"tables": {
1042+
"users": {},
1043+
"users2sites": {}
1044+
}
10101045
}
10111046
}
10121047
}

go/vt/vtgate/semantics/analyzer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (a *analyzer) newSemTable(
174174
Direct: a.binder.direct,
175175
ExprTypes: a.typer.m,
176176
Tables: a.tables.Tables,
177-
Targets: a.binder.targets,
177+
DMLTargets: a.binder.targets,
178178
NotSingleRouteErr: a.notSingleRouteErr,
179179
NotUnshardedErr: a.unshardedErr,
180180
Warning: a.warning,

go/vt/vtgate/semantics/semantic_table.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ type (
129129
// It doesn't recurse inside derived tables to find the original dependencies.
130130
Direct ExprDependencies
131131

132-
// Targets contains the TableSet of each table getting modified by the update/delete statement.
133-
Targets TableSet
132+
// DMLTargets contains the TableSet of each table getting modified by the update/delete statement.
133+
DMLTargets TableSet
134134

135135
// ColumnEqualities is used for transitive closures (e.g., if a == b and b == c, then a == c).
136136
ColumnEqualities map[columnName][]sqlparser.Expr
@@ -202,15 +202,15 @@ func (st *SemTable) CopyDependencies(from, to sqlparser.Expr) {
202202

203203
// GetChildForeignKeysForTargets gets the child foreign keys as a list for all the target tables.
204204
func (st *SemTable) GetChildForeignKeysForTargets() (fks []vindexes.ChildFKInfo) {
205-
for _, ts := range st.Targets.Constituents() {
205+
for _, ts := range st.DMLTargets.Constituents() {
206206
fks = append(fks, st.childForeignKeysInvolved[ts]...)
207207
}
208208
return fks
209209
}
210210

211211
// GetChildForeignKeysForTableSet gets the child foreign keys as a listfor the TableSet.
212212
func (st *SemTable) GetChildForeignKeysForTableSet(target TableSet) (fks []vindexes.ChildFKInfo) {
213-
for _, ts := range st.Targets.Constituents() {
213+
for _, ts := range st.DMLTargets.Constituents() {
214214
if target.IsSolvedBy(ts) {
215215
fks = append(fks, st.childForeignKeysInvolved[ts]...)
216216
}
@@ -238,15 +238,15 @@ func (st *SemTable) GetChildForeignKeysList() []vindexes.ChildFKInfo {
238238

239239
// GetParentForeignKeysForTargets gets the parent foreign keys as a list for all the target tables.
240240
func (st *SemTable) GetParentForeignKeysForTargets() (fks []vindexes.ParentFKInfo) {
241-
for _, ts := range st.Targets.Constituents() {
241+
for _, ts := range st.DMLTargets.Constituents() {
242242
fks = append(fks, st.parentForeignKeysInvolved[ts]...)
243243
}
244244
return fks
245245
}
246246

247247
// GetParentForeignKeysForTableSet gets the parent foreign keys as a list for the TableSet.
248248
func (st *SemTable) GetParentForeignKeysForTableSet(target TableSet) (fks []vindexes.ParentFKInfo) {
249-
for _, ts := range st.Targets.Constituents() {
249+
for _, ts := range st.DMLTargets.Constituents() {
250250
if target.IsSolvedBy(ts) {
251251
fks = append(fks, st.parentForeignKeysInvolved[ts]...)
252252
}
@@ -970,7 +970,7 @@ func (st *SemTable) UpdateChildFKExpr(origUpdExpr *sqlparser.UpdateExpr, newExpr
970970

971971
// GetTargetTableSetForTableName returns the TableSet for the given table name from the target tables.
972972
func (st *SemTable) GetTargetTableSetForTableName(name sqlparser.TableName) (TableSet, error) {
973-
for _, target := range st.Targets.Constituents() {
973+
for _, target := range st.DMLTargets.Constituents() {
974974
tbl, err := st.Tables[target.TableOffset()].Name()
975975
if err != nil {
976976
return "", err

0 commit comments

Comments
 (0)