From fb8966beb9452dbbe3ec2c3bd75a492349b379eb Mon Sep 17 00:00:00 2001 From: "vitess-bot[bot]" <108069721+vitess-bot[bot]@users.noreply.github.com> Date: Mon, 15 Jul 2024 18:53:16 +0200 Subject: [PATCH 1/3] Fix Join Predicate Cleanup Bug in Route Merging (#16386) Signed-off-by: Andres Taylor Signed-off-by: Florent Poinsard Co-authored-by: Florent Poinsard --- .../vtgate/vitess_tester/join/join.test | 49 +++++++++++++++++++ .../vtgate/vitess_tester/join/vschema.json | 38 ++++++++++++++ .../planbuilder/operators/SQL_builder.go | 4 +- .../planbuilder/testdata/from_cases.json | 24 +++++++++ 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 go/test/endtoend/vtgate/vitess_tester/join/join.test create mode 100644 go/test/endtoend/vtgate/vitess_tester/join/vschema.json diff --git a/go/test/endtoend/vtgate/vitess_tester/join/join.test b/go/test/endtoend/vtgate/vitess_tester/join/join.test new file mode 100644 index 00000000000..cffd3a1b3aa --- /dev/null +++ b/go/test/endtoend/vtgate/vitess_tester/join/join.test @@ -0,0 +1,49 @@ +CREATE TABLE `t1` +( + `id` int unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(191) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE InnoDB, + CHARSET utf8mb4, + COLLATE utf8mb4_unicode_ci; + +CREATE TABLE `t2` +( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `t1_id` int unsigned NOT NULL, + PRIMARY KEY (`id`) +) ENGINE InnoDB, + CHARSET utf8mb4, + COLLATE utf8mb4_unicode_ci; + +CREATE TABLE `t3` +( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(191) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE InnoDB, + CHARSET utf8mb4, + COLLATE utf8mb4_unicode_ci; + +insert into t1 (id, name) +values (1, 'A'), + (2, 'B'), + (3, 'C'), + (4, 'D'); + +insert into t2 (id, t1_id) +values (1, 1), + (2, 2), + (3, 3); + +insert into t3 (id, name) +values (1, 'A'), + (2, 'B'), + (3, 'B'), + (4, 'B'), + (5, 'B'); + +-- wait_authoritative t1 +-- wait_authoritative t2 +-- wait_authoritative t3 +select 42 from t1 join t2 on t1.id = t2.t1_id join t3 on t1.id = t3.id where t1.name or t2.id or t3.name; diff --git a/go/test/endtoend/vtgate/vitess_tester/join/vschema.json b/go/test/endtoend/vtgate/vitess_tester/join/vschema.json new file mode 100644 index 00000000000..b922d3f760c --- /dev/null +++ b/go/test/endtoend/vtgate/vitess_tester/join/vschema.json @@ -0,0 +1,38 @@ +{ + "keyspaces": { + "joinks": { + "sharded": true, + "vindexes": { + "hash": { + "type": "hash" + } + }, + "tables": { + "t1": { + "column_vindexes": [ + { + "column": "id", + "name": "hash" + } + ] + }, + "t2": { + "column_vindexes": [ + { + "column": "t1_id", + "name": "hash" + } + ] + }, + "t3": { + "column_vindexes": [ + { + "column": "id", + "name": "hash" + } + ] + } + } + } + } +} \ No newline at end of file diff --git a/go/vt/vtgate/planbuilder/operators/SQL_builder.go b/go/vt/vtgate/planbuilder/operators/SQL_builder.go index 0a2e545ea48..10faee5e568 100644 --- a/go/vt/vtgate/planbuilder/operators/SQL_builder.go +++ b/go/vt/vtgate/planbuilder/operators/SQL_builder.go @@ -233,7 +233,9 @@ func (qb *queryBuilder) joinWith(other *queryBuilder, onCondition sqlparser.Expr switch joinType { case sqlparser.NormalJoinType: newFromClause = append(stmt.GetFrom(), otherStmt.GetFrom()...) - qb.addPredicate(onCondition) + for _, pred := range sqlparser.SplitAndExpression(nil, onCondition) { + qb.addPredicate(pred) + } default: newFromClause = []sqlparser.TableExpr{buildJoin(stmt, otherStmt, onCondition, joinType)} } diff --git a/go/vt/vtgate/planbuilder/testdata/from_cases.json b/go/vt/vtgate/planbuilder/testdata/from_cases.json index 81381f3d7d7..88a4ebd7027 100644 --- a/go/vt/vtgate/planbuilder/testdata/from_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/from_cases.json @@ -1783,6 +1783,30 @@ ] } }, + { + "comment": "three table join with join predicate touching all tables", + "query": "select 42 from user u join user_extra ue on u.id = ue.user_id join music m on m.user_id = u.id where u.foo or m.foo or ue.foo", + "plan": { + "QueryType": "SELECT", + "Original": "select 42 from user u join user_extra ue on u.id = ue.user_id join music m on m.user_id = u.id where u.foo or m.foo or ue.foo", + "Instructions": { + "OperatorType": "Route", + "Variant": "Scatter", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select 42 from `user` as u, user_extra as ue, music as m where 1 != 1", + "Query": "select 42 from `user` as u, user_extra as ue, music as m where u.id = ue.user_id and m.user_id = u.id and (u.foo or m.foo or ue.foo)", + "Table": "`user`, music, user_extra" + }, + "TablesUsed": [ + "user.music", + "user.user", + "user.user_extra" + ] + } + }, { "comment": "join of normal table with information_schema", "query": "select unsharded.foo from unsharded join information_schema.CHARACTER_SETS", From 1922f1f7828b4da92d94b563ae54950dc77293da Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 15 Jul 2024 21:33:58 +0200 Subject: [PATCH 2/3] add end2end test Signed-off-by: Andres Taylor Signed-off-by: Florent Poinsard --- go/test/endtoend/vtgate/queries/misc/misc_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/go/test/endtoend/vtgate/queries/misc/misc_test.go b/go/test/endtoend/vtgate/queries/misc/misc_test.go index 3c031687cff..dc789f3d1bb 100644 --- a/go/test/endtoend/vtgate/queries/misc/misc_test.go +++ b/go/test/endtoend/vtgate/queries/misc/misc_test.go @@ -114,6 +114,17 @@ func TestInvalidDateTimeTimestampVals(t *testing.T) { require.Error(t, err) } +func TestJoinWithThreeTables(t *testing.T) { + utils.SkipIfBinaryIsBelowVersion(t, 20, "vtgate") + + mcmp, closer := start(t) + defer closer() + + mcmp.Exec("insert into t1(id1, id2) values (0,0), (1,1), (2,2)") + mcmp.Exec("insert into tbl(id, unq_col, nonunq_col) values (0,0,0), (1,1,1), (2,2,1)") + mcmp.Exec("select 42 from t1 u1, t1 u2, tbl u3 where u1.id1 = u2.id1 and u1.id1 = u3.id and (u1.id2 or u2.id2 or u3.unq_col)") +} + // TestIntervalWithMathFunctions tests that the Interval keyword can be used with math functions. func TestIntervalWithMathFunctions(t *testing.T) { mcmp, closer := start(t) From e2e03900a1deb98ccda37fe5f5b6ba3e4f6c1f77 Mon Sep 17 00:00:00 2001 From: Florent Poinsard Date: Mon, 15 Jul 2024 17:52:14 -0600 Subject: [PATCH 3/3] Remove vitess-tester tests Signed-off-by: Florent Poinsard --- .../vtgate/vitess_tester/join/join.test | 49 ------------------- .../vtgate/vitess_tester/join/vschema.json | 38 -------------- 2 files changed, 87 deletions(-) delete mode 100644 go/test/endtoend/vtgate/vitess_tester/join/join.test delete mode 100644 go/test/endtoend/vtgate/vitess_tester/join/vschema.json diff --git a/go/test/endtoend/vtgate/vitess_tester/join/join.test b/go/test/endtoend/vtgate/vitess_tester/join/join.test deleted file mode 100644 index cffd3a1b3aa..00000000000 --- a/go/test/endtoend/vtgate/vitess_tester/join/join.test +++ /dev/null @@ -1,49 +0,0 @@ -CREATE TABLE `t1` -( - `id` int unsigned NOT NULL AUTO_INCREMENT, - `name` varchar(191) NOT NULL, - PRIMARY KEY (`id`) -) ENGINE InnoDB, - CHARSET utf8mb4, - COLLATE utf8mb4_unicode_ci; - -CREATE TABLE `t2` -( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `t1_id` int unsigned NOT NULL, - PRIMARY KEY (`id`) -) ENGINE InnoDB, - CHARSET utf8mb4, - COLLATE utf8mb4_unicode_ci; - -CREATE TABLE `t3` -( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `name` varchar(191) NOT NULL, - PRIMARY KEY (`id`) -) ENGINE InnoDB, - CHARSET utf8mb4, - COLLATE utf8mb4_unicode_ci; - -insert into t1 (id, name) -values (1, 'A'), - (2, 'B'), - (3, 'C'), - (4, 'D'); - -insert into t2 (id, t1_id) -values (1, 1), - (2, 2), - (3, 3); - -insert into t3 (id, name) -values (1, 'A'), - (2, 'B'), - (3, 'B'), - (4, 'B'), - (5, 'B'); - --- wait_authoritative t1 --- wait_authoritative t2 --- wait_authoritative t3 -select 42 from t1 join t2 on t1.id = t2.t1_id join t3 on t1.id = t3.id where t1.name or t2.id or t3.name; diff --git a/go/test/endtoend/vtgate/vitess_tester/join/vschema.json b/go/test/endtoend/vtgate/vitess_tester/join/vschema.json deleted file mode 100644 index b922d3f760c..00000000000 --- a/go/test/endtoend/vtgate/vitess_tester/join/vschema.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "keyspaces": { - "joinks": { - "sharded": true, - "vindexes": { - "hash": { - "type": "hash" - } - }, - "tables": { - "t1": { - "column_vindexes": [ - { - "column": "id", - "name": "hash" - } - ] - }, - "t2": { - "column_vindexes": [ - { - "column": "t1_id", - "name": "hash" - } - ] - }, - "t3": { - "column_vindexes": [ - { - "column": "id", - "name": "hash" - } - ] - } - } - } - } -} \ No newline at end of file