From dd81005fccab410fc35a482356470fa7d2f95af1 Mon Sep 17 00:00:00 2001 From: "vitess-bot[bot]" <108069721+vitess-bot[bot]@users.noreply.github.com> Date: Thu, 13 Jun 2024 12:40:55 +0200 Subject: [PATCH 1/2] Fix `vtexplain` not handling `UNION` queries with `weight_string` results correctly. (#16129) Signed-off-by: Arthur Schreiber --- .../testdata/multi-output/selectsharded-output.txt | 6 ++++++ go/vt/vtexplain/testdata/selectsharded-queries.sql | 4 +++- go/vt/vtexplain/vtexplain_vttablet.go | 12 +++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/go/vt/vtexplain/testdata/multi-output/selectsharded-output.txt b/go/vt/vtexplain/testdata/multi-output/selectsharded-output.txt index b7d0c359002..1016fcc97cc 100644 --- a/go/vt/vtexplain/testdata/multi-output/selectsharded-output.txt +++ b/go/vt/vtexplain/testdata/multi-output/selectsharded-output.txt @@ -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 distinct `user`.id, `user`.`name`, weight_string(`user`.id), weight_string(`user`.`name`) from `user` where `user`.id = 2 limit 10001 +2 ks_sharded/-40: select distinct `user`.id, `user`.`name`, weight_string(`user`.id), weight_string(`user`.`name`) from `user` where `user`.id = 1 limit 10001 + +---------------------------------------------------------------------- diff --git a/go/vt/vtexplain/testdata/selectsharded-queries.sql b/go/vt/vtexplain/testdata/selectsharded-queries.sql index ad003d1c457..a68b5c49073 100644 --- a/go/vt/vtexplain/testdata/selectsharded-queries.sql +++ b/go/vt/vtexplain/testdata/selectsharded-queries.sql @@ -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); diff --git a/go/vt/vtexplain/vtexplain_vttablet.go b/go/vt/vtexplain/vtexplain_vttablet.go index 9a82cc915bf..5c0b854bc1e 100644 --- a/go/vt/vtexplain/vtexplain_vttablet.go +++ b/go/vt/vtexplain/vtexplain_vttablet.go @@ -794,9 +794,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 { From f6087bf910649d94511ff8b67be8eeebbbedea1c Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Thu, 13 Jun 2024 12:39:42 +0000 Subject: [PATCH 2/2] Fix flakiness in `vtexplain` unit test case. Signed-off-by: Arthur Schreiber --- .../testdata/multi-output/selectsharded-output.txt | 6 +++--- go/vt/vtexplain/testdata/selectsharded-queries.sql | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go/vt/vtexplain/testdata/multi-output/selectsharded-output.txt b/go/vt/vtexplain/testdata/multi-output/selectsharded-output.txt index 1016fcc97cc..f8cb799a942 100644 --- a/go/vt/vtexplain/testdata/multi-output/selectsharded-output.txt +++ b/go/vt/vtexplain/testdata/multi-output/selectsharded-output.txt @@ -207,9 +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) +(SELECT user.id, user.name FROM user WHERE user.id = 1) UNION (SELECT user.id, user.name FROM user WHERE user.id = 3) -2 ks_sharded/-40: select distinct `user`.id, `user`.`name`, weight_string(`user`.id), weight_string(`user`.`name`) from `user` where `user`.id = 2 limit 10001 -2 ks_sharded/-40: select distinct `user`.id, `user`.`name`, weight_string(`user`.id), weight_string(`user`.`name`) from `user` where `user`.id = 1 limit 10001 +1 ks_sharded/-40: select distinct `user`.id, `user`.`name`, weight_string(`user`.id), weight_string(`user`.`name`) from `user` where `user`.id = 1 limit 10001 +1 ks_sharded/40-80: select distinct `user`.id, `user`.`name`, weight_string(`user`.id), weight_string(`user`.`name`) from `user` where `user`.id = 3 limit 10001 ---------------------------------------------------------------------- diff --git a/go/vt/vtexplain/testdata/selectsharded-queries.sql b/go/vt/vtexplain/testdata/selectsharded-queries.sql index a68b5c49073..067f53df4e6 100644 --- a/go/vt/vtexplain/testdata/selectsharded-queries.sql +++ b/go/vt/vtexplain/testdata/selectsharded-queries.sql @@ -40,4 +40,4 @@ SELECT user.id, user.name, name_info.info FROM user INNER JOIN music ON (user.id 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); +(SELECT user.id, user.name FROM user WHERE user.id = 1) UNION (SELECT user.id, user.name FROM user WHERE user.id = 3);