Skip to content

Commit

Permalink
Merge pull request #7829 from planetscale/vtexplain-lj
Browse files Browse the repository at this point in the history
VTExplain: Add support for multi table join query
  • Loading branch information
deepthi authored Apr 13, 2021
2 parents 3481a8f + a3bf34f commit f14dbce
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
12 changes: 12 additions & 0 deletions go/vt/vtexplain/testdata/multi-output/selectsharded-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,15 @@ select id from user where not id in (select col from music where music.user_id =
2 ks_sharded/40-80: select col from music where music.user_id = 411 limit 10001

----------------------------------------------------------------------
SELECT user.id, user.name, name_info.info FROM user INNER JOIN music ON (user.id = music.user_id) LEFT OUTER JOIN name_info ON (user.name = name_info.name)

1 ks_sharded/-40: select `user`.id, `user`.`name` from `user` join music on `user`.id = music.user_id limit 10001
1 ks_sharded/40-80: select `user`.id, `user`.`name` from `user` join music on `user`.id = music.user_id limit 10001
1 ks_sharded/80-c0: select `user`.id, `user`.`name` from `user` join music on `user`.id = music.user_id limit 10001
1 ks_sharded/c0-: select `user`.id, `user`.`name` from `user` join music on `user`.id = music.user_id limit 10001
2 ks_sharded/40-80: select name_info.info from name_info where name_info.`name` = 'name_val_2' limit 10001
3 ks_sharded/40-80: select name_info.info from name_info where name_info.`name` = 'name_val_2' limit 10001
4 ks_sharded/40-80: select name_info.info from name_info where name_info.`name` = 'name_val_2' limit 10001
5 ks_sharded/40-80: select name_info.info from name_info where name_info.`name` = 'name_val_2' limit 10001

----------------------------------------------------------------------
2 changes: 2 additions & 0 deletions go/vt/vtexplain/testdata/selectsharded-queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ select id, case when substr(name, 1, 5) = 'alice' then 'ALICE' when name = 'bob'
select id, 'abc' as test from user where id = 1 union all select id, 'def' as test from user where id = 1 union all select id, 'ghi' as test from user where id = 1 /* union all */;

select id from user where not id in (select col from music where music.user_id = 42) and id in (select col from music where music.user_id = 411);

SELECT user.id, user.name, name_info.info FROM user INNER JOIN music ON (user.id = music.user_id) LEFT OUTER JOIN name_info ON (user.name = name_info.name);
44 changes: 29 additions & 15 deletions go/vt/vtexplain/vtexplain_vttablet.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,23 +482,25 @@ func (t *explainTablet) HandleQuery(c *mysql.Conn, query string, callback func(*
return fmt.Errorf("unsupported select with multiple from clauses")
}

var table sqlparser.TableIdent
switch node := selStmt.From[0].(type) {
case *sqlparser.AliasedTableExpr:
table = sqlparser.GetTableName(node.Expr)
}
tables := getTables(selStmt.From[0])
colTypeMap := map[string]querypb.Type{}
for _, table := range tables {
tableName := sqlparser.String(table)
columns, exists := tableColumns[tableName]
if !exists && tableName != "" && tableName != "dual" {
return fmt.Errorf("unable to resolve table name %s", tableName)
}

// For complex select queries just return an empty result
// since it's too hard to figure out the real columns
if table.IsEmpty() {
log.V(100).Infof("query %s result {}\n", query)
return callback(&sqltypes.Result{})
}
for k, v := range columns {
if colType, exists := colTypeMap[k]; exists {
if colType != v {
return fmt.Errorf("column type mismatch for column : %s, types: %d vs %d", k, colType, v)
}
continue
}
colTypeMap[k] = v
}

tableName := sqlparser.String(table)
colTypeMap := tableColumns[tableName]
if colTypeMap == nil && tableName != "dual" {
return fmt.Errorf("unable to resolve table name %s", tableName)
}

colNames := make([]string, 0, 4)
Expand Down Expand Up @@ -593,6 +595,18 @@ func (t *explainTablet) HandleQuery(c *mysql.Conn, query string, callback func(*
return callback(result)
}

func getTables(node sqlparser.SQLNode) []sqlparser.TableIdent {
var tables []sqlparser.TableIdent
switch expr := node.(type) {
case *sqlparser.AliasedTableExpr:
tables = append(tables, sqlparser.GetTableName(expr.Expr))
case *sqlparser.JoinTableExpr:
tables = append(tables, getTables(expr.LeftExpr)...)
tables = append(tables, getTables(expr.RightExpr)...)
}
return tables
}

func inferColTypeFromExpr(node sqlparser.Expr, colTypeMap map[string]querypb.Type, colNames []string, colTypes []querypb.Type) ([]string, []querypb.Type) {
switch node := node.(type) {
case *sqlparser.ColName:
Expand Down

0 comments on commit f14dbce

Please sign in to comment.