Skip to content

Commit e3d4142

Browse files
committed
Testing v18 backport of #17503
Signed-off-by: Matt Lord <[email protected]>
1 parent 7bc0ef7 commit e3d4142

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

go/vt/sqlparser/ast_funcs.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,14 @@ func RemoveKeyspace(in SQLNode) {
21352135
}, in)
21362136
}
21372137

2138+
// RemoveSpecificKeyspace removes the keyspace qualifier from all ColName and TableName
2139+
// when it matches the keyspace provided
2140+
func RemoveSpecificKeyspace(in SQLNode, keyspace string) {
2141+
removeKeyspace(in, func(qualifier string) bool {
2142+
return qualifier == keyspace // Remove only if it matches the provided keyspace
2143+
})
2144+
}
2145+
21382146
// RemoveKeyspaceInTables removes the database qualifier for all table names in the AST
21392147
func RemoveKeyspaceInTables(in SQLNode) {
21402148
Rewrite(in, nil, func(cursor *Cursor) bool {
@@ -2147,6 +2155,23 @@ func RemoveKeyspaceInTables(in SQLNode) {
21472155
})
21482156
}
21492157

2158+
func removeKeyspace(in SQLNode, shouldRemove func(qualifier string) bool) {
2159+
Rewrite(in, nil, func(cursor *Cursor) bool {
2160+
switch expr := cursor.Node().(type) {
2161+
case *ColName:
2162+
if shouldRemove(expr.Qualifier.Qualifier.String()) {
2163+
expr.Qualifier.Qualifier = NewIdentifierCS("")
2164+
}
2165+
case TableName:
2166+
if shouldRemove(expr.Qualifier.String()) {
2167+
expr.Qualifier = NewIdentifierCS("")
2168+
cursor.Replace(expr)
2169+
}
2170+
}
2171+
return true
2172+
})
2173+
}
2174+
21502175
func convertStringToInt(integer string) int {
21512176
val, _ := strconv.Atoi(integer)
21522177
return val

go/vt/sqlparser/ast_funcs_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,18 @@ func TestColumns_Indexes(t *testing.T) {
172172
})
173173
}
174174
}
175+
176+
// TestRemoveSpecificKeyspace tests the RemoveSpecificKeyspace function.
177+
// It removes the specific keyspace from the database qualifier.
178+
func TestRemoveSpecificKeyspace(t *testing.T) {
179+
stmt, err := Parse("select 1 from uks.unsharded")
180+
require.NoError(t, err)
181+
182+
// does not match
183+
RemoveSpecificKeyspace(stmt, "ks2")
184+
require.Equal(t, "select 1 from uks.unsharded", String(stmt))
185+
186+
// match
187+
RemoveSpecificKeyspace(stmt, "uks")
188+
require.Equal(t, "select 1 from unsharded", String(stmt))
189+
}

go/vt/vtgate/planbuilder/bypass.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ func buildPlanForBypass(stmt sqlparser.Statement, _ *sqlparser.ReservedVars, vsc
4949
}
5050
}
5151

52+
sqlparser.RemoveSpecificKeyspace(stmt, keyspace.Name)
53+
5254
send := &engine.Send{
5355
Keyspace: keyspace,
5456
TargetDestination: vschema.Destination(),

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,22 @@
164164
"Query": "create /* test */ table t1(id bigint, primary key(id)) /* comments */"
165165
}
166166
}
167+
},
168+
{
169+
"comment": "remove the matching keyspace from shard targeted query",
170+
"query": "select count(*), col from `main`.unsharded join vt_main.t1 where exists (select 1 from main.t2 join information_schema.tables where table_name = 't3')",
171+
"plan": {
172+
"QueryType": "SELECT",
173+
"Original": "select count(*), col from `main`.unsharded join vt_main.t1 where exists (select 1 from main.t2 join information_schema.tables where table_name = 't3')",
174+
"Instructions": {
175+
"OperatorType": "Send",
176+
"Keyspace": {
177+
"Name": "main",
178+
"Sharded": false
179+
},
180+
"TargetDestination": "ExactKeyRange(-)",
181+
"Query": "select count(*), col from unsharded join vt_main.t1 where exists (select 1 from t2 join information_schema.`tables` where table_name = 't3')"
182+
}
183+
}
167184
}
168185
]

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,22 @@
174174
"Query": "create /* test */ table t1(id bigint, primary key(id)) /* comments */"
175175
}
176176
}
177+
},
178+
{
179+
"comment": "remove the matching keyspace from shard targeted query",
180+
"query": "select count(*), col from `main`.unsharded join vt_main.t1 where exists (select 1 from main.t2 join information_schema.tables where table_name = 't3')",
181+
"plan": {
182+
"QueryType": "SELECT",
183+
"Original": "select count(*), col from `main`.unsharded join vt_main.t1 where exists (select 1 from main.t2 join information_schema.tables where table_name = 't3')",
184+
"Instructions": {
185+
"OperatorType": "Send",
186+
"Keyspace": {
187+
"Name": "main",
188+
"Sharded": false
189+
},
190+
"TargetDestination": "Shard(-80)",
191+
"Query": "select count(*), col from unsharded join vt_main.t1 where exists (select 1 from t2 join information_schema.`tables` where table_name = 't3')"
192+
}
193+
}
177194
}
178195
]

0 commit comments

Comments
 (0)