Skip to content

Commit

Permalink
evalengine: fix setting proper Field flags
Browse files Browse the repository at this point in the history
Signed-off-by: Vicent Marti <[email protected]>
  • Loading branch information
vmg committed Mar 7, 2024
1 parent d94d945 commit 21e4558
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
8 changes: 7 additions & 1 deletion go/test/endtoend/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ func AssertMatchesAny(t testing.TB, conn *mysql.Conn, query string, expected ...
return
}
}
t.Errorf("Query: %s (-want +got):\n%v\nGot:%s", query, expected, got)

var err strings.Builder
_, _ = fmt.Fprintf(&err, "Query did not match:\n%s\n", query)
for i, e := range expected {
_, _ = fmt.Fprintf(&err, "Expected query %d does not match.\nwant: %v\ngot: %v\n\n", i, e, got)
}
t.Error(err.String())
}

// AssertMatchesCompareMySQL executes the given query on both Vitess and MySQL and make sure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,26 @@ func TestInfrSchemaAndUnionAll(t *testing.T) {
}
}

func TestInfoschemaTypes(t *testing.T) {
utils.SkipIfBinaryIsBelowVersion(t, 19, "vtgate")

require.NoError(t,
utils.WaitForAuthoritative(t, "ks", "t1", clusterInstance.VtgateProcess.ReadVSchema))

mcmp, closer := start(t)
defer closer()

mcmp.Exec(`
SELECT ORDINAL_POSITION
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'ks' AND TABLE_NAME = 't1'
UNION
SELECT ORDINAL_POSITION
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'ks' AND TABLE_NAME = 't2';
`)
}

func TestTypeORMQuery(t *testing.T) {
utils.SkipIfBinaryIsBelowVersion(t, 19, "vtgate")
// This test checks that we can run queries similar to the ones that the TypeORM framework uses
Expand Down
13 changes: 10 additions & 3 deletions go/vt/vtgate/evalengine/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,22 @@ func NewTypeFromField(f *querypb.Field) Type {
}

func (t *Type) ToField(name string) *querypb.Field {
// need to get the proper flags for the type; usually leaving flags
// to 0 is OK, because Vitess' MySQL client will generate the right
// ones for the column's type, but here we're also setting the NotNull
// flag, so it needs to be set with the full flags for the column
_, flags := sqltypes.TypeToMySQL(t.typ)
if !t.nullable {
flags |= int64(querypb.MySqlFlag_NOT_NULL_FLAG)
}

f := &querypb.Field{
Name: name,
Type: t.typ,
Charset: uint32(t.collation),
ColumnLength: uint32(t.size),
Decimals: uint32(t.scale),
}
if !t.nullable {
f.Flags |= uint32(querypb.MySqlFlag_NOT_NULL_FLAG)
Flags: uint32(flags),
}
return f
}
Expand Down
15 changes: 8 additions & 7 deletions go/vt/vtgate/planbuilder/testdata/info_schema80_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@
"0: utf8mb3_general_ci",
"1: utf8mb3_general_ci",
"2: utf8mb3_general_ci",
"3",
"3: binary",
"4: utf8mb3_general_ci",
"5",
"6",
"6: binary",
"7",
"8",
"9",
Expand All @@ -166,8 +166,9 @@
"17: utf8mb3_general_ci",
"18",
"19: utf8mb3_general_ci",
"20: utf8mb3_general_ci"
"(20:21)"
],
"ResultColumns": 21,
"Inputs": [
{
"OperatorType": "Concatenate",
Expand All @@ -179,8 +180,8 @@
"Name": "main",
"Sharded": false
},
"FieldQuery": "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT from information_schema.`tables` where 1 != 1",
"Query": "select distinct TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT from information_schema.`tables` where table_schema = :__vtschemaname /* VARCHAR */",
"FieldQuery": "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT, weight_string(TABLE_COMMENT) from information_schema.`tables` where 1 != 1",
"Query": "select distinct TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT, weight_string(TABLE_COMMENT) from information_schema.`tables` where table_schema = :__vtschemaname /* VARCHAR */",
"SysTableTableSchema": "['user']",
"Table": "information_schema.`tables`"
},
Expand All @@ -191,8 +192,8 @@
"Name": "main",
"Sharded": false
},
"FieldQuery": "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT from information_schema.`tables` where 1 != 1",
"Query": "select distinct TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT from information_schema.`tables` where table_schema = :__vtschemaname /* VARCHAR */",
"FieldQuery": "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT, weight_string(TABLE_COMMENT) from information_schema.`tables` where 1 != 1",
"Query": "select distinct TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT, weight_string(TABLE_COMMENT) from information_schema.`tables` where table_schema = :__vtschemaname /* VARCHAR */",
"SysTableTableSchema": "['main']",
"Table": "information_schema.`tables`"
}
Expand Down
33 changes: 18 additions & 15 deletions go/vt/vtgate/planbuilder/testdata/union_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,9 @@
"Instructions": {
"OperatorType": "Distinct",
"Collations": [
"0: utf8mb3_general_ci"
"(0:1)"
],
"ResultColumns": 1,
"Inputs": [
{
"OperatorType": "Concatenate",
Expand All @@ -335,8 +336,8 @@
"Name": "main",
"Sharded": false
},
"FieldQuery": "select CHARACTER_SET_NAME from information_schema.CHARACTER_SETS where 1 != 1",
"Query": "select distinct CHARACTER_SET_NAME from information_schema.CHARACTER_SETS",
"FieldQuery": "select CHARACTER_SET_NAME, weight_string(CHARACTER_SET_NAME) from information_schema.CHARACTER_SETS where 1 != 1",
"Query": "select distinct CHARACTER_SET_NAME, weight_string(CHARACTER_SET_NAME) from information_schema.CHARACTER_SETS",
"Table": "information_schema.CHARACTER_SETS"
},
{
Expand All @@ -346,8 +347,8 @@
"Name": "main",
"Sharded": false
},
"FieldQuery": "select user_name from unsharded where 1 != 1",
"Query": "select distinct user_name from unsharded",
"FieldQuery": "select user_name, weight_string(user_name) from unsharded where 1 != 1",
"Query": "select distinct user_name, weight_string(user_name) from unsharded",
"Table": "unsharded"
}
]
Expand Down Expand Up @@ -523,8 +524,9 @@
"Instructions": {
"OperatorType": "Distinct",
"Collations": [
"0"
"(0:1)"
],
"ResultColumns": 1,
"Inputs": [
{
"OperatorType": "Route",
Expand All @@ -533,8 +535,8 @@
"Name": "user",
"Sharded": true
},
"FieldQuery": "select 1 from dual where 1 != 1 union select null from dual where 1 != 1 union select 1.0 from dual where 1 != 1 union select '1' from dual where 1 != 1 union select 2 from dual where 1 != 1 union select 2.0 from `user` where 1 != 1",
"Query": "select 1 from dual union select null from dual union select 1.0 from dual union select '1' from dual union select 2 from dual union select 2.0 from `user`",
"FieldQuery": "select dt.`1`, weight_string(dt.`1`) from (select 1 from dual where 1 != 1 union select null from dual where 1 != 1 union select 1.0 from dual where 1 != 1 union select '1' from dual where 1 != 1 union select 2 from dual where 1 != 1 union select 2.0 from `user` where 1 != 1) as dt where 1 != 1",
"Query": "select dt.`1`, weight_string(dt.`1`) from (select 1 from dual union select null from dual union select 1.0 from dual union select '1' from dual union select 2 from dual union select 2.0 from `user`) as dt",
"Table": "`user`, dual"
}
]
Expand Down Expand Up @@ -622,9 +624,10 @@
"Instructions": {
"OperatorType": "Distinct",
"Collations": [
"0: utf8mb4_0900_ai_ci",
"1: utf8mb4_0900_ai_ci"
"(0:2)",
"(1:3)"
],
"ResultColumns": 2,
"Inputs": [
{
"OperatorType": "Concatenate",
Expand All @@ -636,14 +639,14 @@
"Name": "user",
"Sharded": true
},
"FieldQuery": "select 'b', 'c' from `user` where 1 != 1",
"Query": "select distinct 'b', 'c' from `user`",
"FieldQuery": "select 'b', 'c', weight_string('b'), weight_string('c') from `user` where 1 != 1",
"Query": "select distinct 'b', 'c', weight_string('b'), weight_string('c') from `user`",
"Table": "`user`"
},
{
"OperatorType": "Join",
"Variant": "Join",
"JoinColumnIndexes": "L:0,L:1",
"JoinColumnIndexes": "L:0,L:1,L:2,L:3",
"TableName": "`user`_user_extra",
"Inputs": [
{
Expand All @@ -653,8 +656,8 @@
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.id, `user`.`name` from `user` where 1 != 1",
"Query": "select distinct `user`.id, `user`.`name` from `user`",
"FieldQuery": "select `user`.id, `user`.`name`, weight_string(`user`.id), weight_string(`user`.`name`) from `user` where 1 != 1",
"Query": "select distinct `user`.id, `user`.`name`, weight_string(`user`.id), weight_string(`user`.`name`) from `user`",
"Table": "`user`"
},
{
Expand Down

0 comments on commit 21e4558

Please sign in to comment.