Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,13 @@ func (node TableIdent) String() string {
return node.v
}

// String returns the unescaped table name with qualifier. It must
// not be used for SQL generation. Use sqlparser.String
// instead.
func (node TableName) String() string {
return strings.ToLower(node.Qualifier.String() + "." + node.Name.String())
}

Comment thread
GuptaManan100 marked this conversation as resolved.
Outdated
// CompliantName returns a compliant id name
// that can be used for a bind var.
func (node TableIdent) CompliantName() string {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/cached_size.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/vt/vtgate/engine/cached_size.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions go/vt/vtgate/planbuilder/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,28 @@ func (rb *route) JoinCanMerge(pb *primitiveBuilder, rrb *route, ajoin *sqlparser
if where == nil {
return true
}
hasRuntimeRoutingPredicates := false
var tableWithRoutingPredicates sqlparser.TableNames
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
col, ok := node.(*sqlparser.ColName)
if ok {
hasRuntimeRoutingPredicates = hasRuntimeRoutingPredicates || isTableNameCol(col) || isDbNameCol(col)
hasRuntimeRoutingPredicates := isTableNameCol(col) || isDbNameCol(col)
if hasRuntimeRoutingPredicates {
for _, table := range tableWithRoutingPredicates {
if table.String() == col.Qualifier.String() {
return true, nil
}
}
tableWithRoutingPredicates = append(tableWithRoutingPredicates, col.Qualifier)
}
}
return !hasRuntimeRoutingPredicates, nil
return true, nil
}, where)
return !hasRuntimeRoutingPredicates
// Routes can be merged if only tables from information schema are accessed and only 1 table is used in the predicates that are used for routing
// TODO :- Even if more table are present in the routing, we can merge if they agree
if rb.ContainsOnlyInformationSchema() && rrb.ContainsOnlyInformationSchema() && len(tableWithRoutingPredicates) <= 1 {
return true
}
Comment thread
GuptaManan100 marked this conversation as resolved.
Outdated
return len(tableWithRoutingPredicates) == 0
}
if ajoin == nil {
return false
Expand Down Expand Up @@ -820,3 +833,8 @@ func queryTimeout(d sqlparser.CommentDirectives) int {
}
return 0
}

// ContainsOnlyInformationSchema returns if the route only contains information_schema tables
func (rb *route) ContainsOnlyInformationSchema() bool {
return rb.eroute.Opcode == engine.SelectDBA
}
19 changes: 19 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/select_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1696,3 +1696,22 @@ Gen4 plan same as above
]
}
}

# Select from information schema query with two tables that route should be merged
"SELECT DELETE_RULE, UPDATE_RULE FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC ON KCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME WHERE KCU.TABLE_SCHEMA = 'test' AND KCU.TABLE_NAME = 'data_type_table' AND KCU.COLUMN_NAME = 'id' AND KCU.REFERENCED_TABLE_SCHEMA = 'test' AND KCU.CONSTRAINT_NAME = 'data_type_table_id_fkey' ORDER BY KCU.CONSTRAINT_NAME, KCU.COLUMN_NAME"
{
"QueryType": "SELECT",
"Original": "SELECT DELETE_RULE, UPDATE_RULE FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC ON KCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME WHERE KCU.TABLE_SCHEMA = 'test' AND KCU.TABLE_NAME = 'data_type_table' AND KCU.COLUMN_NAME = 'id' AND KCU.REFERENCED_TABLE_SCHEMA = 'test' AND KCU.CONSTRAINT_NAME = 'data_type_table_id_fkey' ORDER BY KCU.CONSTRAINT_NAME, KCU.COLUMN_NAME",
"Instructions": {
"OperatorType": "Route",
"Variant": "SelectDBA",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select DELETE_RULE, UPDATE_RULE from INFORMATION_SCHEMA.KEY_COLUMN_USAGE as KCU join INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS as RC on KCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME where 1 != 1",
"Query": "select DELETE_RULE, UPDATE_RULE from INFORMATION_SCHEMA.KEY_COLUMN_USAGE as KCU join INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS as RC on KCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME where KCU.TABLE_SCHEMA = :__vtschemaname and KCU.TABLE_NAME = :__vttablename and KCU.COLUMN_NAME = 'id' and KCU.REFERENCED_TABLE_SCHEMA = 'test' and KCU.CONSTRAINT_NAME = 'data_type_table_id_fkey' order by KCU.CONSTRAINT_NAME asc, KCU.COLUMN_NAME asc",
"SysTableTableName": "VARBINARY(\"data_type_table\")",
"SysTableTableSchema": "VARBINARY(\"test\")"
}
}