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

Gen4: fail more unsupported queries #8451

Merged
merged 7 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion go/vt/vtgate/planbuilder/abstract/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ func getOperatorFromTableExpr(tableExpr sqlparser.TableExpr, semTable *semantics
case *sqlparser.AliasedTableExpr:
qg := newQueryGraph()
tableName := tableExpr.Expr.(sqlparser.TableName)
qt := &QueryTable{Alias: tableExpr, Table: tableName, TableID: semTable.TableSetFor(tableExpr)}
tableID := semTable.TableSetFor(tableExpr)
tableInfo, err := semTable.TableInfoFor(tableID)
if err != nil {
return nil, err
}
isInfSchema := tableInfo.IsInfSchema()
qt := &QueryTable{Alias: tableExpr, Table: tableName, TableID: tableID, IsInfSchema: isInfSchema}
qg.Tables = append(qg.Tables, qt)
return qg, nil
case *sqlparser.JoinTableExpr:
Expand Down
49 changes: 5 additions & 44 deletions go/vt/vtgate/planbuilder/abstract/querygraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ type (

// QueryTable is a single FROM table, including all predicates particular to this table
QueryTable struct {
TableID semantics.TableSet
Alias *sqlparser.AliasedTableExpr
Table sqlparser.TableName
Predicates []sqlparser.Expr
TableID semantics.TableSet
Alias *sqlparser.AliasedTableExpr
Table sqlparser.TableName
Predicates []sqlparser.Expr
IsInfSchema bool
}
)

Expand Down Expand Up @@ -90,46 +91,6 @@ func newQueryGraph() *QueryGraph {
}
}

func (qg *QueryGraph) collectTable(t sqlparser.TableExpr, semTable *semantics.SemTable) error {
switch table := t.(type) {
case *sqlparser.AliasedTableExpr:
tableName := table.Expr.(sqlparser.TableName)
qt := &QueryTable{Alias: table, Table: tableName, TableID: semTable.TableSetFor(table)}
qg.Tables = append(qg.Tables, qt)
case *sqlparser.JoinTableExpr:
if err := qg.collectTable(table.LeftExpr, semTable); err != nil {
return err
}
if err := qg.collectTable(table.RightExpr, semTable); err != nil {
return err
}
if table.Condition.On != nil {
for _, predicate := range sqlparser.SplitAndExpression(nil, table.Condition.On) {
err := qg.collectPredicateTable(t, predicate, semTable)
if err != nil {
return err
}
}
}
case *sqlparser.ParenTableExpr:
for _, expr := range table.Exprs {
if err := qg.collectTable(expr, semTable); err != nil {
return err
}
}
}
return nil
}

func (qg *QueryGraph) collectTables(t sqlparser.TableExprs, semTable *semantics.SemTable) error {
for _, expr := range t {
if err := qg.collectTable(expr, semTable); err != nil {
return err
}
}
return nil
}

func (qg *QueryGraph) collectPredicates(sel *sqlparser.Select, semTable *semantics.SemTable) error {
predicates := sqlparser.SplitAndExpression(nil, sel.Where.Expr)

Expand Down
6 changes: 6 additions & 0 deletions go/vt/vtgate/planbuilder/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package planbuilder
import (
"errors"
"fmt"
"strings"

vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
Expand Down Expand Up @@ -186,6 +187,7 @@ func (pb *primitiveBuilder) buildTablePrimitive(tableExpr *sqlparser.AliasedTabl
}
rb, st := newRoute(sel)
rb.eroute = engine.NewSimpleRoute(engine.SelectDBA, ks)
rb.eroute.TableName = sqlparser.String(tableName)
pb.plan, pb.st = rb, st
// Add the table to symtab
return st.AddTable(&table{
Expand Down Expand Up @@ -343,6 +345,10 @@ func (pb *primitiveBuilder) join(rpb *primitiveBuilder, ajoin *sqlparser.JoinTab
} else {
sel.From = sqlparser.TableExprs{ajoin}
}
// join table name
if lRoute.eroute.TableName != rRoute.eroute.TableName {
lRoute.eroute.TableName = strings.Join([]string{lRoute.eroute.TableName, rRoute.eroute.TableName}, ", ")
}

// Since the routes have merged, set st.singleRoute to point at
// the merged route.
Expand Down
8 changes: 7 additions & 1 deletion go/vt/vtgate/planbuilder/jointree.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ func (rp *leJoin) tableNames() []string {
}

func (rp *routeTable) tableNames() []string {
return []string{sqlparser.String(rp.qtable.Table.Name)}
var name string
if rp.qtable.IsInfSchema {
name = sqlparser.String(rp.qtable.Table)
} else {
name = sqlparser.String(rp.qtable.Table.Name)
}
return []string{name}
}

func (p parenTables) tableNames() []string {
Expand Down
19 changes: 13 additions & 6 deletions go/vt/vtgate/planbuilder/jointree_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ func transformRoutePlan(n *routePlan) (*route, error) {
LeftExpr: lft,
}
tablesForSelect = sqlparser.TableExprs{joinExpr}
// todo: add table
// tableNameMap[sqlparser.String()] = nil
for _, tblNames := range leftJoin.right.tableNames() {
tableNameMap[tblNames] = nil
}
Comment on lines -115 to +117
Copy link
Member Author

Choose a reason for hiding this comment

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

Awesome 👏

}

predicates := n.Predicates()
Expand Down Expand Up @@ -161,13 +162,19 @@ func transformRoutePlan(n *routePlan) (*route, error) {
func relToTableExpr(t relation) (sqlparser.TableExpr, error) {
switch t := t.(type) {
case *routeTable:
return &sqlparser.AliasedTableExpr{
Expr: sqlparser.TableName{
var expr sqlparser.SimpleTableExpr
if t.qtable.IsInfSchema {
expr = t.qtable.Table
} else {
expr = sqlparser.TableName{
Name: t.vtable.Name,
},
}
}
return &sqlparser.AliasedTableExpr{
Expr: expr,
Partitions: nil,
As: t.qtable.Alias.As,
Hints: nil,
Hints: t.qtable.Alias.Hints,
}, nil
case parenTables:
tables := sqlparser.TableExprs{}
Expand Down
27 changes: 27 additions & 0 deletions go/vt/vtgate/planbuilder/route_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,26 @@ func removeAt(plans []joinTree, idx int) []joinTree {
}

func createRoutePlan(table *abstract.QueryTable, solves semantics.TableSet, vschema ContextVSchema) (*routePlan, error) {
if table.IsInfSchema {
defaultKeyspace, err := vschema.DefaultKeyspace()
if err != nil {
return nil, err
}
return &routePlan{
routeOpCode: engine.SelectDBA,
solved: solves,
// TODO: find keyspace to route using the predicates as in v3
keyspace: defaultKeyspace,
tables: []relation{&routeTable{
qtable: table,
vtable: &vindexes.Table{
Name: table.Table.Name,
Keyspace: defaultKeyspace,
},
}},
predicates: table.Predicates,
}, nil
}
vschemaTable, _, _, _, _, err := vschema.FindTableOrVindex(table.Table)
if err != nil {
return nil, err
Expand Down Expand Up @@ -603,6 +623,9 @@ func tryMerge(a, b joinTree, joinPredicates []sqlparser.Expr, semTable *semantic
return nil
}
case engine.SelectScatter, engine.SelectEqualUnique:
if bRoute.routeOpCode == engine.SelectReference {
return r
}
if len(joinPredicates) == 0 {
// If we are doing two Scatters, we have to make sure that the
// joins are on the correct vindex to allow them to be merged
Expand All @@ -615,6 +638,10 @@ func tryMerge(a, b joinTree, joinPredicates []sqlparser.Expr, semTable *semantic
return nil
}
r.pickBestAvailableVindex()
case engine.SelectReference:
if bRoute.routeOpCode != engine.SelectReference {
return nil
}
}
return r
}
Expand Down
51 changes: 20 additions & 31 deletions go/vt/vtgate/planbuilder/testdata/filter_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ Gen4 plan same as above
},
"FieldQuery": "select user_extra.id from `user` join user_extra on `user`.id = user_extra.user_id where 1 != 1",
"Query": "select user_extra.id from `user` join user_extra on `user`.id = user_extra.user_id where `user`.id = 5",
"Table": "`user`",
"Table": "`user`, user_extra",
"Values": [
5
],
Expand Down Expand Up @@ -598,7 +598,7 @@ Gen4 plan same as above
},
"FieldQuery": "select user_extra.id from `user` join user_extra on `user`.id = user_extra.user_id where 1 != 1",
"Query": "select user_extra.id from `user` join user_extra on `user`.id = user_extra.user_id where user_extra.user_id = 5",
"Table": "`user`",
"Table": "`user`, user_extra",
"Values": [
5
],
Expand Down Expand Up @@ -639,7 +639,7 @@ Gen4 plan same as above
},
"FieldQuery": "select user_extra.id from `user` left join user_extra on `user`.id = user_extra.user_id where 1 != 1",
"Query": "select user_extra.id from `user` left join user_extra on `user`.id = user_extra.user_id where `user`.id = 5",
"Table": "`user`",
"Table": "`user`, user_extra",
"Values": [
5
],
Expand All @@ -662,7 +662,7 @@ Gen4 plan same as above
},
"FieldQuery": "select user_extra.id from `user` left join user_extra on `user`.id = user_extra.user_id where 1 != 1",
"Query": "select user_extra.id from `user` left join user_extra on `user`.id = user_extra.user_id where user_extra.user_id = 5",
"Table": "`user`",
"Table": "`user`, user_extra",
"Values": [
5
],
Expand Down Expand Up @@ -1185,7 +1185,8 @@ Gen4 plan same as above
"Sharded": false
},
"FieldQuery": "select * from information_schema.a where 1 != 1",
"Query": "select * from information_schema.a where id in (select * from information_schema.b)"
"Query": "select * from information_schema.a where id in (select * from information_schema.b)",
"Table": "information_schema.a"
}
}

Expand Down Expand Up @@ -1700,7 +1701,7 @@ Gen4 plan same as above
},
"FieldQuery": "select user_extra.Id from `user` join user_extra on `user`.iD = user_extra.User_Id where 1 != 1",
"Query": "select user_extra.Id from `user` join user_extra on `user`.iD = user_extra.User_Id where `user`.Id = 5",
"Table": "`user`",
"Table": "`user`, user_extra",
"Values": [
5
],
Expand Down Expand Up @@ -1912,7 +1913,6 @@ Gen4 plan same as above
}
Gen4 plan same as above


# query trying to query two different keyspaces at the same time
"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'user' AND TABLE_SCHEMA = 'main'"
{
Expand All @@ -1927,7 +1927,8 @@ Gen4 plan same as above
},
"FieldQuery": "select * from INFORMATION_SCHEMA.`TABLES` where 1 != 1",
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname and TABLE_SCHEMA = :__vtschemaname",
"SysTableTableSchema": "[VARBINARY(\"user\"), VARBINARY(\"main\")]"
"SysTableTableSchema": "[VARBINARY(\"user\"), VARBINARY(\"main\")]",
"Table": "INFORMATION_SCHEMA.`TABLES`"
}
}

Expand All @@ -1944,9 +1945,11 @@ Gen4 plan same as above
"Sharded": false
},
"FieldQuery": "select * from INFORMATION_SCHEMA.`TABLES` where 1 != 1",
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = database()"
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = database()",
"Table": "INFORMATION_SCHEMA.`TABLES`"
}
}
Gen4 plan same as above

# table_schema predicate the wrong way around
"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE 'ks' = TABLE_SCHEMA"
Expand All @@ -1962,24 +1965,8 @@ Gen4 plan same as above
},
"FieldQuery": "select * from INFORMATION_SCHEMA.`TABLES` where 1 != 1",
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname",
"SysTableTableSchema": "[VARBINARY(\"ks\")]"
}
}

# table_schema OR predicate
"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'ks' OR TABLE_SCHEMA = 'main'"
{
"QueryType": "SELECT",
"Original": "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'ks' OR TABLE_SCHEMA = 'main'",
"Instructions": {
"OperatorType": "Route",
"Variant": "SelectDBA",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select * from INFORMATION_SCHEMA.`TABLES` where 1 != 1",
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = 'ks' or TABLE_SCHEMA = 'main'"
"SysTableTableSchema": "[VARBINARY(\"ks\")]",
"Table": "INFORMATION_SCHEMA.`TABLES`"
}
}

Expand All @@ -1998,7 +1985,8 @@ Gen4 plan same as above
"FieldQuery": "select * from INFORMATION_SCHEMA.`TABLES` where 1 != 1",
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname and TABLE_NAME = :__vttablename",
"SysTableTableName": "[VARBINARY(\"route1\")]",
"SysTableTableSchema": "[VARBINARY(\"ks\")]"
"SysTableTableSchema": "[VARBINARY(\"ks\")]",
"Table": "INFORMATION_SCHEMA.`TABLES`"
}
}

Expand All @@ -2016,7 +2004,8 @@ Gen4 plan same as above
},
"FieldQuery": "select * from INFORMATION_SCHEMA.`TABLES` where 1 != 1",
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname and other_column = 42",
"SysTableTableSchema": "[VARBINARY(\"ks\")]"
"SysTableTableSchema": "[VARBINARY(\"ks\")]",
"Table": "INFORMATION_SCHEMA.`TABLES`"
}
}

Expand Down Expand Up @@ -2098,7 +2087,8 @@ Gen4 plan same as above
},
"FieldQuery": "select * from INFORMATION_SCHEMA.`TABLES` where 1 != 1",
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname and (other_column = 42 or TABLE_SCHEMA = 'ks') and (other_column = 42 or foobar = 'value')",
"SysTableTableSchema": "[VARBINARY(\"ks\")]"
"SysTableTableSchema": "[VARBINARY(\"ks\")]",
"Table": "INFORMATION_SCHEMA.`TABLES`"
}
}

Expand All @@ -2125,7 +2115,6 @@ Gen4 plan same as above
}
Gen4 plan same as above


"select * from samecolvin where col = :col"
{
"QueryType": "SELECT",
Expand Down
Loading