Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for more queries #14369

Merged
merged 9 commits into from
Nov 1, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,39 @@ func TestInfrSchemaAndUnionAll(t *testing.T) {
})
}
}

func TestTypeORMQuery(t *testing.T) {
// This test checks that we can run queries similar to the ones that the TypeORM framework uses

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

mcmp, closer := start(t)
defer closer()
query := `SELECT *
FROM (SELECT *
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
WHERE kcu.TABLE_SCHEMA = 'ks'
AND kcu.TABLE_NAME = 't1'
UNION
SELECT *
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
WHERE kcu.TABLE_SCHEMA = 'ks'
AND kcu.TABLE_NAME = 't7_xxhash') kcu
INNER JOIN
(SELECT *
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = 'ks'
AND TABLE_NAME = 't1'
UNION
SELECT *
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = 'ks'
AND TABLE_NAME = 't7_xxhash') rc
ON rc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA
AND
rc.TABLE_NAME = kcu.TABLE_NAME
AND
rc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME`
utils.AssertMatches(t, mcmp.VtConn, query, "[]")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to modify the DDL such that the query results in some rows.

}
4 changes: 0 additions & 4 deletions go/vt/vtgate/planbuilder/operators/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,6 @@ func (p *Projection) ShortDescription() string {
}

func (p *Projection) Compact(ctx *plancontext.PlanningContext) (ops.Operator, *rewrite.ApplyResult, error) {
if p.isDerived() {
return p, rewrite.SameTree, nil
}

ap, err := p.GetAliasedProjections()
if err != nil {
return p, rewrite.SameTree, nil
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/route_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func requiresSwitchingSides(ctx *plancontext.PlanningContext, op ops.Operator) b
_ = rewrite.Visit(op, func(current ops.Operator) error {
horizon, isHorizon := current.(*Horizon)

if isHorizon && horizon.IsDerived() && !horizon.IsMergeable(ctx) {
if isHorizon && !horizon.IsMergeable(ctx) {
required = true
return io.EOF
}
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/subquery_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func isMergeable(ctx *plancontext.PlanningContext, query sqlparser.SelectStateme

sel, ok := query.(*sqlparser.Select)
if !ok {
return false
return true
}

systay marked this conversation as resolved.
Show resolved Hide resolved
if len(sel.GroupBy) > 0 {
Expand Down
218 changes: 129 additions & 89 deletions go/vt/vtgate/planbuilder/testdata/union_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -749,29 +749,22 @@
"QueryType": "SELECT",
"Original": "select * from ((select id from user union select id+1 from user) union select user_id from user_extra) as t",
"Instructions": {
"OperatorType": "SimpleProjection",
"Columns": [
0
"OperatorType": "Distinct",
"Collations": [
"(0:1)"
],
"ResultColumns": 1,
"Inputs": [
{
"OperatorType": "Distinct",
"Collations": [
"(0:1)"
],
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from (select id from `user` where 1 != 1 union select id + 1 from `user` where 1 != 1 union select user_id from user_extra where 1 != 1) as dt where 1 != 1",
"Query": "select id, weight_string(id) from (select id from `user` union select id + 1 from `user` union select user_id from user_extra) as dt",
"Table": "`user`, user_extra"
}
]
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from (select id from `user` where 1 != 1 union select id + 1 from `user` where 1 != 1 union select user_id from user_extra where 1 != 1) as dt where 1 != 1",
"Query": "select id, weight_string(id) from (select id from `user` union select id + 1 from `user` union select user_id from user_extra) as dt",
"Table": "`user`, user_extra"
}
]
},
Expand Down Expand Up @@ -955,49 +948,41 @@
"TableName": "`user`_`user`",
"Inputs": [
{
"OperatorType": "SimpleProjection",
"Columns": [
0
],
"OperatorType": "Concatenate",
"Inputs": [
{
"OperatorType": "Concatenate",
"OperatorType": "Limit",
"Count": "INT64(5)",
"Inputs": [
{
"OperatorType": "Limit",
"Count": "INT64(5)",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from `user` where 1 != 1",
"OrderBy": "(0|1) ASC",
"Query": "select id, weight_string(id) from `user` order by id asc limit :__upper_limit",
"Table": "`user`"
}
]
},
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from `user` where 1 != 1",
"OrderBy": "(0|1) ASC",
"Query": "select id, weight_string(id) from `user` order by id asc limit :__upper_limit",
"Table": "`user`"
}
]
},
{
"OperatorType": "Limit",
"Count": "INT64(5)",
"Inputs": [
{
"OperatorType": "Limit",
"Count": "INT64(5)",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from `user` where 1 != 1",
"OrderBy": "(0|1) DESC",
"Query": "select id, weight_string(id) from `user` order by id desc limit :__upper_limit",
"Table": "`user`"
}
]
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, weight_string(id) from `user` where 1 != 1",
"OrderBy": "(0|1) DESC",
"Query": "select id, weight_string(id) from `user` order by id desc limit :__upper_limit",
"Table": "`user`"
}
]
}
Expand Down Expand Up @@ -1098,41 +1083,35 @@
],
"Inputs": [
{
"OperatorType": "Projection",
"Expressions": null,
"OperatorType": "Distinct",
"Collations": [
"(0:1)"
],
"Inputs": [
{
"OperatorType": "Distinct",
"Collations": [
"(0:1)"
],
"OperatorType": "Concatenate",
"Inputs": [
{
"OperatorType": "Concatenate",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id + 42 as foo, weight_string(id + 42) from `user` where 1 != 1",
"Query": "select distinct id + 42 as foo, weight_string(id + 42) from `user`",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Unsharded",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select 1 + id as foo, weight_string(1 + id) from unsharded where 1 != 1",
"Query": "select distinct 1 + id as foo, weight_string(1 + id) from unsharded",
"Table": "unsharded"
}
]
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id + 42 as foo, weight_string(id + 42) from `user` where 1 != 1",
"Query": "select distinct id + 42 as foo, weight_string(id + 42) from `user`",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Unsharded",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select 1 + id as foo, weight_string(1 + id) from unsharded where 1 != 1",
"Query": "select distinct 1 + id as foo, weight_string(1 + id) from unsharded",
"Table": "unsharded"
}
]
}
Expand Down Expand Up @@ -1495,5 +1474,66 @@
"user.user"
]
}
},
{
"comment": "join between two derived tables containing UNION",
"query": "select * from (select foo from user where bar = 12 union select foo from user where bar = 134) as t1 join (select bar from music where foo = 12 union select bar from music where foo = 1234) as t2 on t1.foo = t2.bar",
"plan": {
"QueryType": "SELECT",
"Original": "select * from (select foo from user where bar = 12 union select foo from user where bar = 134) as t1 join (select bar from music where foo = 12 union select bar from music where foo = 1234) as t2 on t1.foo = t2.bar",
"Instructions": {
"OperatorType": "Join",
"Variant": "Join",
"JoinColumnIndexes": "L:0,R:0",
"JoinVars": {
"t1_foo": 0
},
"TableName": "`user`_music",
"Inputs": [
{
"OperatorType": "Distinct",
"Collations": [
"(0:1)"
],
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select foo, weight_string(foo) from (select foo from `user` where 1 != 1 union select foo from `user` where 1 != 1) as dt where 1 != 1",
"Query": "select foo, weight_string(foo) from (select foo from `user` where bar = 12 union select foo from `user` where bar = 134) as dt",
"Table": "`user`"
}
]
},
{
"OperatorType": "Distinct",
"Collations": [
"(0:1)"
],
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select bar, weight_string(bar) from (select bar from music where 1 != 1 union select bar from music where 1 != 1) as dt where 1 != 1",
"Query": "select bar, weight_string(bar) from (select bar from music where foo = 12 and bar = :t1_foo union select bar from music where foo = 1234 and bar = :t1_foo) as dt",
"Table": "music"
}
]
}
]
},
"TablesUsed": [
"user.music",
"user.user"
]
}
}
]
Loading