Skip to content

Commit

Permalink
Fix vtexplain not handling UNION queries with weight_string res…
Browse files Browse the repository at this point in the history
…ults correctly.

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
  • Loading branch information
arthurschreiber committed Jun 12, 2024
1 parent fd7c546 commit cd3d6f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,9 @@ SELECT id FROM orders WHERE id IN (1, "1", 1)
2 ks_sharded/40-80: select id from orders where id in (1, '1', 1) limit 10001

----------------------------------------------------------------------
(SELECT user.id, user.name FROM user WHERE user.id = 1) UNION (SELECT user.id, user.name FROM user WHERE user.id = 2)

2 ks_sharded/-40: select dt.c0 as id, dt.c1 as `name`, weight_string(dt.c0), weight_string(dt.c1) from (select distinct `user`.id, `user`.`name` from `user` where `user`.id = 2) as dt(c0, c1) limit 10001
2 ks_sharded/-40: select dt.c0 as id, dt.c1 as `name`, weight_string(dt.c0), weight_string(dt.c1) from (select distinct `user`.id, `user`.`name` from `user` where `user`.id = 1) as dt(c0, c1) limit 10001

----------------------------------------------------------------------
4 changes: 3 additions & 1 deletion go/vt/vtexplain/testdata/selectsharded-queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ select id from user where not id in (select col from music where music.user_id =

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);

SELECT id FROM orders WHERE id IN (1, "1", 1)
SELECT id FROM orders WHERE id IN (1, "1", 1);

(SELECT user.id, user.name FROM user WHERE user.id = 1) UNION (SELECT user.id, user.name FROM user WHERE user.id = 2);
12 changes: 9 additions & 3 deletions go/vt/vtexplain/vtexplain_vttablet.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,15 @@ func inferColTypeFromExpr(node sqlparser.Expr, tableColumnMap map[sqlparser.Iden
colTypes = append(colTypes, colType)
}
case sqlparser.Callable:
// As a shortcut, functions are integral types
colNames = append(colNames, sqlparser.String(node))
colTypes = append(colTypes, querypb.Type_INT32)
switch node := node.(type) {
case *sqlparser.WeightStringFuncExpr:
colNames = append(colNames, sqlparser.String(node))
colTypes = append(colTypes, querypb.Type_BINARY)
default:
// As a shortcut, functions are integral types
colNames = append(colNames, sqlparser.String(node))
colTypes = append(colTypes, querypb.Type_INT32)
}
case *sqlparser.Literal:
colNames = append(colNames, sqlparser.String(node))
switch node.Type {
Expand Down

0 comments on commit cd3d6f8

Please sign in to comment.