Skip to content

Commit 92d2af2

Browse files
authored
[bug fix] USING planning on information_schema (vitessio#12542)
* [planner] Schema information on the information_schema views (vitessio#11941) * add info_schema information Signed-off-by: Andres Taylor <andres@planetscale.com> * add SchemaInformation handling for info_schema tables Signed-off-by: Andres Taylor <andres@planetscale.com> * fix bad test query Signed-off-by: Andres Taylor <andres@planetscale.com> * add support for information_schema on mysql 5.7 Signed-off-by: Andres Taylor <andres@planetscale.com> * columns sorted just like mysql, and tests for 5.7 Signed-off-by: Andres Taylor <andres@planetscale.com> * test: skip test that should not run Signed-off-by: Andres Taylor <andres@planetscale.com> Signed-off-by: Andres Taylor <andres@planetscale.com> * Fix for USING when column names not lower cased (vitessio#12379) Signed-off-by: Andres Taylor <andres@planetscale.com> --------- Signed-off-by: Andres Taylor <andres@planetscale.com>
1 parent 7ac5c39 commit 92d2af2

18 files changed

+4449
-246
lines changed

go/vt/sqlparser/ast_funcs.go

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -210,97 +210,101 @@ func (ct *ColumnType) DescribeType() string {
210210

211211
// SQLType returns the sqltypes type code for the given column
212212
func (ct *ColumnType) SQLType() querypb.Type {
213-
switch strings.ToLower(ct.Type) {
214-
case keywordStrings[TINYINT]:
215-
if ct.Unsigned {
213+
return SQLTypeToQueryType(ct.Type, ct.Unsigned)
214+
}
215+
216+
func SQLTypeToQueryType(typeName string, unsigned bool) querypb.Type {
217+
switch keywordVals[strings.ToLower(typeName)] {
218+
case TINYINT:
219+
if unsigned {
216220
return sqltypes.Uint8
217221
}
218222
return sqltypes.Int8
219-
case keywordStrings[SMALLINT]:
220-
if ct.Unsigned {
223+
case SMALLINT:
224+
if unsigned {
221225
return sqltypes.Uint16
222226
}
223227
return sqltypes.Int16
224-
case keywordStrings[MEDIUMINT]:
225-
if ct.Unsigned {
228+
case MEDIUMINT:
229+
if unsigned {
226230
return sqltypes.Uint24
227231
}
228232
return sqltypes.Int24
229-
case keywordStrings[INT], keywordStrings[INTEGER]:
230-
if ct.Unsigned {
233+
case INT, INTEGER:
234+
if unsigned {
231235
return sqltypes.Uint32
232236
}
233237
return sqltypes.Int32
234-
case keywordStrings[BIGINT]:
235-
if ct.Unsigned {
238+
case BIGINT:
239+
if unsigned {
236240
return sqltypes.Uint64
237241
}
238242
return sqltypes.Int64
239-
case keywordStrings[BOOL], keywordStrings[BOOLEAN]:
243+
case BOOL, BOOLEAN:
240244
return sqltypes.Uint8
241-
case keywordStrings[TEXT]:
245+
case TEXT:
242246
return sqltypes.Text
243-
case keywordStrings[TINYTEXT]:
247+
case TINYTEXT:
244248
return sqltypes.Text
245-
case keywordStrings[MEDIUMTEXT]:
249+
case MEDIUMTEXT:
246250
return sqltypes.Text
247-
case keywordStrings[LONGTEXT]:
251+
case LONGTEXT:
248252
return sqltypes.Text
249-
case keywordStrings[BLOB]:
253+
case BLOB:
250254
return sqltypes.Blob
251-
case keywordStrings[TINYBLOB]:
255+
case TINYBLOB:
252256
return sqltypes.Blob
253-
case keywordStrings[MEDIUMBLOB]:
257+
case MEDIUMBLOB:
254258
return sqltypes.Blob
255-
case keywordStrings[LONGBLOB]:
259+
case LONGBLOB:
256260
return sqltypes.Blob
257-
case keywordStrings[CHAR]:
261+
case CHAR:
258262
return sqltypes.Char
259-
case keywordStrings[VARCHAR]:
263+
case VARCHAR:
260264
return sqltypes.VarChar
261-
case keywordStrings[BINARY]:
265+
case BINARY:
262266
return sqltypes.Binary
263-
case keywordStrings[VARBINARY]:
267+
case VARBINARY:
264268
return sqltypes.VarBinary
265-
case keywordStrings[DATE]:
269+
case DATE:
266270
return sqltypes.Date
267-
case keywordStrings[TIME]:
271+
case TIME:
268272
return sqltypes.Time
269-
case keywordStrings[DATETIME]:
273+
case DATETIME:
270274
return sqltypes.Datetime
271-
case keywordStrings[TIMESTAMP]:
275+
case TIMESTAMP:
272276
return sqltypes.Timestamp
273-
case keywordStrings[YEAR]:
277+
case YEAR:
274278
return sqltypes.Year
275-
case keywordStrings[FLOAT_TYPE]:
279+
case FLOAT_TYPE:
276280
return sqltypes.Float32
277-
case keywordStrings[DOUBLE]:
281+
case DOUBLE:
278282
return sqltypes.Float64
279-
case keywordStrings[DECIMAL]:
283+
case DECIMAL, DECIMAL_TYPE:
280284
return sqltypes.Decimal
281-
case keywordStrings[BIT]:
285+
case BIT:
282286
return sqltypes.Bit
283-
case keywordStrings[ENUM]:
287+
case ENUM:
284288
return sqltypes.Enum
285-
case keywordStrings[SET]:
289+
case SET:
286290
return sqltypes.Set
287-
case keywordStrings[JSON]:
291+
case JSON:
288292
return sqltypes.TypeJSON
289-
case keywordStrings[GEOMETRY]:
293+
case GEOMETRY:
290294
return sqltypes.Geometry
291-
case keywordStrings[POINT]:
295+
case POINT:
292296
return sqltypes.Geometry
293-
case keywordStrings[LINESTRING]:
297+
case LINESTRING:
294298
return sqltypes.Geometry
295-
case keywordStrings[POLYGON]:
299+
case POLYGON:
296300
return sqltypes.Geometry
297-
case keywordStrings[GEOMETRYCOLLECTION]:
301+
case GEOMETRYCOLLECTION:
298302
return sqltypes.Geometry
299-
case keywordStrings[MULTIPOINT]:
303+
case MULTIPOINT:
300304
return sqltypes.Geometry
301-
case keywordStrings[MULTILINESTRING]:
305+
case MULTILINESTRING:
302306
return sqltypes.Geometry
303-
case keywordStrings[MULTIPOLYGON]:
307+
case MULTIPOLYGON:
304308
return sqltypes.Geometry
305309
}
306310
return sqltypes.Null

go/vt/sqlparser/keywords.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ var keywords = []keyword{
687687

688688
// keywordStrings contains the reverse mapping of token to keyword strings
689689
var keywordStrings = map[int]string{}
690+
var keywordVals = map[string]int{}
690691

691692
// keywordLookupTable is a perfect hash map that maps **case insensitive** keyword names to their ids
692693
var keywordLookupTable *caseInsensitiveTable
@@ -735,6 +736,7 @@ func init() {
735736
panic(fmt.Sprintf("keyword %q must be lowercase in table", kw.name))
736737
}
737738
keywordStrings[kw.id] = kw.name
739+
keywordVals[kw.name] = kw.id
738740
}
739741

740742
keywordLookupTable = buildCaseInsensitiveTable(keywords)

go/vt/vtgate/planbuilder/plan_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131

3232
"github.com/stretchr/testify/require"
3333

34+
"vitess.io/vitess/go/vt/servenv"
35+
3436
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
3537

3638
"vitess.io/vitess/go/test/utils"
@@ -252,7 +254,16 @@ func TestPlan(t *testing.T) {
252254
testFile(t, "flush_cases_no_default_keyspace.json", testOutputTempDir, vschemaWrapper, false)
253255
testFile(t, "show_cases_no_default_keyspace.json", testOutputTempDir, vschemaWrapper, false)
254256
testFile(t, "stream_cases.json", testOutputTempDir, vschemaWrapper, false)
255-
testFile(t, "systemtables_cases.json", testOutputTempDir, vschemaWrapper, false)
257+
testFile(t, "systemtables_cases80.json", testOutputTempDir, vschemaWrapper, false)
258+
}
259+
260+
func TestSystemTables57(t *testing.T) {
261+
// first we move everything to use 5.7 logic
262+
servenv.SetMySQLServerVersionForTest("5.7")
263+
defer servenv.SetMySQLServerVersionForTest("")
264+
vschemaWrapper := &vschemaWrapper{v: loadSchema(t, "vschemas/schema.json", true)}
265+
testOutputTempDir := makeTestOutput(t)
266+
testFile(t, "systemtables_cases57.json", testOutputTempDir, vschemaWrapper, false)
256267
}
257268

258269
func TestSysVarSetDisabled(t *testing.T) {

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4381,7 +4381,7 @@
43814381
{
43824382
"comment": "SelectDBA with uncorrelated subqueries",
43834383
"query": "select t.table_schema from information_schema.tables as t where t.table_schema in (select c.column_name from information_schema.columns as c)",
4384-
"plan": {
4384+
"v3-plan": {
43854385
"QueryType": "SELECT",
43864386
"Original": "select t.table_schema from information_schema.tables as t where t.table_schema in (select c.column_name from information_schema.columns as c)",
43874387
"Instructions": {
@@ -4395,6 +4395,25 @@
43954395
"Query": "select t.table_schema from information_schema.`tables` as t where t.table_schema in (select c.column_name from information_schema.`columns` as c)",
43964396
"Table": "information_schema.`tables`"
43974397
}
4398+
},
4399+
"gen4-plan": {
4400+
"QueryType": "SELECT",
4401+
"Original": "select t.table_schema from information_schema.tables as t where t.table_schema in (select c.column_name from information_schema.columns as c)",
4402+
"Instructions": {
4403+
"OperatorType": "Route",
4404+
"Variant": "DBA",
4405+
"Keyspace": {
4406+
"Name": "main",
4407+
"Sharded": false
4408+
},
4409+
"FieldQuery": "select t.table_schema from information_schema.`tables` as t where 1 != 1",
4410+
"Query": "select t.table_schema from information_schema.`tables` as t where t.table_schema in (select c.column_name from information_schema.`columns` as c)",
4411+
"Table": "information_schema.`tables`"
4412+
},
4413+
"TablesUsed": [
4414+
"information_schema.columns",
4415+
"information_schema.tables"
4416+
]
43984417
}
43994418
},
44004419
{

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@
587587
"plan": "table disabled has been disabled"
588588
},
589589
{
590+
"comment": "select second_user.foo.col from second_user.foo join user on second_user.foo.id = user.id where second_user.foo.col = 42",
590591
"query": "select second_user.foo.col from second_user.foo join user on second_user.foo.id = user.id where second_user.foo.col = 42",
591592
"v3-plan": {
592593
"QueryType": "SELECT",
@@ -623,6 +624,7 @@
623624
}
624625
},
625626
{
627+
"comment": "select user.music.foo from user.music join user on user.music.id = user.id where user.music.col = 42",
626628
"query": "select user.music.foo from user.music join user on user.music.id = user.id where user.music.col = 42",
627629
"v3-plan": {
628630
"QueryType": "SELECT",
@@ -3001,6 +3003,7 @@
30013003
]
30023004
},
30033005
"TablesUsed": [
3006+
"information_schema.a",
30043007
"main.unsharded"
30053008
]
30063009
}
@@ -3076,6 +3079,7 @@
30763079
]
30773080
},
30783081
"TablesUsed": [
3082+
"information_schema.a",
30793083
"main.unsharded"
30803084
]
30813085
}
@@ -6160,6 +6164,7 @@
61606164
}
61616165
},
61626166
{
6167+
"comment": "select * from (select bar as push_it from (select foo as bar from (select id as foo from user) as t1) as t2) as t3 where push_it = 12",
61636168
"query": "select * from (select bar as push_it from (select foo as bar from (select id as foo from user) as t1) as t2) as t3 where push_it = 12",
61646169
"v3-plan": {
61656170
"QueryType": "SELECT",
@@ -6203,4 +6208,4 @@
62036208
]
62046209
}
62056210
}
6206-
]
6211+
]

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2535,6 +2535,7 @@
25352535
}
25362536
},
25372537
{
2538+
"comment": "(select id from unsharded) union (select id from unsharded_auto) order by id limit 5",
25382539
"query": "(select id from unsharded) union (select id from unsharded_auto) order by id limit 5",
25392540
"v3-plan": {
25402541
"QueryType": "SELECT",
@@ -3295,6 +3296,7 @@
32953296
"plan": "syntax error at position 41 near 'into'"
32963297
},
32973298
{
3299+
"comment": "select (select u.id from user as u where u.id = 1), a.id from user as a where a.id = 1",
32983300
"query": "select (select u.id from user as u where u.id = 1), a.id from user as a where a.id = 1",
32993301
"v3-plan": {
33003302
"QueryType": "SELECT",
@@ -3416,6 +3418,7 @@
34163418
}
34173419
},
34183420
{
3421+
"comment": "((((select 1))))",
34193422
"query": "((((select 1))))",
34203423
"v3-plan": {
34213424
"QueryType": "SELECT",
@@ -3585,6 +3588,7 @@
35853588
}
35863589
},
35873590
{
3591+
"comment": "select (select col from user limit 1) as a from user join user_extra order by a",
35883592
"query": "select (select col from user limit 1) as a from user join user_extra order by a",
35893593
"v3-plan": {
35903594
"QueryType": "SELECT",
@@ -3716,6 +3720,7 @@
37163720
}
37173721
},
37183722
{
3723+
"comment": "select t.a from (select (select col from user limit 1) as a from user join user_extra) t",
37193724
"query": "select t.a from (select (select col from user limit 1) as a from user join user_extra) t",
37203725
"v3-plan": {
37213726
"QueryType": "SELECT",
@@ -3860,6 +3865,7 @@
38603865
}
38613866
},
38623867
{
3868+
"comment": "select (select col from user where user_extra.id = 4 limit 1) as a from user join user_extra",
38633869
"query": "select (select col from user where user_extra.id = 4 limit 1) as a from user join user_extra",
38643870
"plan": "unsupported: cross-shard correlated subquery"
38653871
},
@@ -4462,6 +4468,7 @@
44624468
}
44634469
},
44644470
{
4471+
"comment": "select user.id, trim(leading 'x' from user.name) from user",
44654472
"query": "select user.id, trim(leading 'x' from user.name) from user",
44664473
"v3-plan": {
44674474
"QueryType": "SELECT",
@@ -4571,6 +4578,7 @@
45714578
"Table": "dual"
45724579
},
45734580
"TablesUsed": [
4581+
"information_schema.TABLES",
45744582
"main.dual"
45754583
]
45764584
}
@@ -4613,6 +4621,7 @@
46134621
}
46144622
},
46154623
{
4624+
"comment": "select (select id from user order by id limit 1) from user_extra",
46164625
"query": "select (select id from user order by id limit 1) from user_extra",
46174626
"v3-plan": {
46184627
"QueryType": "SELECT",
@@ -7687,6 +7696,7 @@
76877696
}
76887697
},
76897698
{
7699+
"comment": "select user.a, t.b from user join (select id, count(*) b, req from user_extra group by req, id) as t on user.id = t.id",
76907700
"query": "select user.a, t.b from user join (select id, count(*) b, req from user_extra group by req, id) as t on user.id = t.id",
76917701
"v3-plan": "unsupported: filtering on results of cross-shard subquery",
76927702
"gen4-plan": {
@@ -7765,6 +7775,7 @@
77657775
"gen4-plan": "unsupported: JOIN not supported between derived tables"
77667776
},
77677777
{
7778+
"comment": "SELECT music.id FROM (SELECT MAX(id) as maxt FROM music WHERE music.user_id = 5) other JOIN music ON other.maxt = music.id",
77687779
"query": "SELECT music.id FROM (SELECT MAX(id) as maxt FROM music WHERE music.user_id = 5) other JOIN music ON other.maxt = music.id",
77697780
"v3-plan": {
77707781
"QueryType": "SELECT",
@@ -7909,4 +7920,4 @@
79097920
]
79107921
}
79117922
}
7912-
]
7923+
]

0 commit comments

Comments
 (0)