From 3d238cf54f67e6c63ca88d681fbd4d5d05361aab Mon Sep 17 00:00:00 2001 From: Harshit Gangal Date: Thu, 15 Feb 2024 22:23:52 +0530 Subject: [PATCH 1/2] Fix routing rule query rewrite (#15253) Signed-off-by: Harshit Gangal Signed-off-by: Florent Poinsard <35779988+frouioui@users.noreply.github.com> Signed-off-by: Florent Poinsard Co-authored-by: Florent Poinsard <35779988+frouioui@users.noreply.github.com> Co-authored-by: Florent Poinsard Signed-off-by: Florent Poinsard --- go/vt/vtgate/planbuilder/rewrite.go | 1 + .../planbuilder/testdata/dml_cases.json | 73 +++++++++++++++++++ .../planbuilder/testdata/select_cases.json | 22 ++++++ .../planbuilder/testdata/vschemas/schema.json | 9 ++- 4 files changed, 103 insertions(+), 2 deletions(-) diff --git a/go/vt/vtgate/planbuilder/rewrite.go b/go/vt/vtgate/planbuilder/rewrite.go index f59441c77ac..30ba06f25fa 100644 --- a/go/vt/vtgate/planbuilder/rewrite.go +++ b/go/vt/vtgate/planbuilder/rewrite.go @@ -82,6 +82,7 @@ func (r *rewriter) rewriteDown(cursor *sqlparser.Cursor) bool { node.As = tableName.Name } // replace the table name with the original table + tableName.Qualifier = sqlparser.IdentifierCS{} tableName.Name = vindexTable.Name node.Expr = tableName case *sqlparser.Subquery: diff --git a/go/vt/vtgate/planbuilder/testdata/dml_cases.json b/go/vt/vtgate/planbuilder/testdata/dml_cases.json index dfaea96cb9e..5db88df2ad7 100644 --- a/go/vt/vtgate/planbuilder/testdata/dml_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/dml_cases.json @@ -4901,5 +4901,78 @@ "user.music" ] } + }, + { + "comment": "Delete with routed table on music", + "query": "delete from second_user.bar", + "plan": { + "QueryType": "DELETE", + "Original": "delete from second_user.bar", + "Instructions": { + "OperatorType": "Delete", + "Variant": "Scatter", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "TargetTabletType": "PRIMARY", + "KsidLength": 1, + "KsidVindex": "user_index", + "OwnedVindexQuery": "select user_id, id from music as bar for update", + "Query": "delete from music as bar", + "Table": "music" + }, + "TablesUsed": [ + "user.music" + ] + } + }, + { + "comment": "Update with routed table on music", + "query": "update second_user.bar set col = 23", + "plan": { + "QueryType": "UPDATE", + "Original": "update second_user.bar set col = 23", + "Instructions": { + "OperatorType": "Update", + "Variant": "Scatter", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "TargetTabletType": "PRIMARY", + "Query": "update music as bar set col = 23", + "Table": "music" + }, + "TablesUsed": [ + "user.music" + ] + } + }, + { + "comment": "Insert with routed table on music", + "query": "insert into second_user.bar(id) values (2)", + "plan": { + "QueryType": "INSERT", + "Original": "insert into second_user.bar(id) values (2)", + "Instructions": { + "OperatorType": "Insert", + "Variant": "Sharded", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "TargetTabletType": "PRIMARY", + "Query": "insert into music(id, user_id) values (:_id_0, :_user_id_0)", + "TableName": "music", + "VindexValues": { + "music_user_map": "2", + "user_index": "null" + } + }, + "TablesUsed": [ + "user.music" + ] + } } ] diff --git a/go/vt/vtgate/planbuilder/testdata/select_cases.json b/go/vt/vtgate/planbuilder/testdata/select_cases.json index 3798b0752cb..9d62d887ee5 100644 --- a/go/vt/vtgate/planbuilder/testdata/select_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/select_cases.json @@ -1573,6 +1573,28 @@ ] } }, + { + "comment": "routing table on music", + "query": "select * from second_user.bar where id > 2", + "plan": { + "QueryType": "SELECT", + "Original": "select * from second_user.bar where id > 2", + "Instructions": { + "OperatorType": "Route", + "Variant": "Scatter", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select * from music as bar where 1 != 1", + "Query": "select * from music as bar where id > 2", + "Table": "music" + }, + "TablesUsed": [ + "user.music" + ] + } + }, { "comment": "testing SingleRow Projection", "query": "select 42", diff --git a/go/vt/vtgate/planbuilder/testdata/vschemas/schema.json b/go/vt/vtgate/planbuilder/testdata/vschemas/schema.json index ac88fea498d..863b708725d 100644 --- a/go/vt/vtgate/planbuilder/testdata/vschemas/schema.json +++ b/go/vt/vtgate/planbuilder/testdata/vschemas/schema.json @@ -25,6 +25,12 @@ "user.user" ] }, + { + "from_table": "second_user.bar", + "to_tables": [ + "user.music" + ] + }, { "from_table": "primary_redirect@primary", "to_tables": [ @@ -489,8 +495,7 @@ "sharded": true, "vindexes": { "hash_dup": { - "type": "hash_test", - "owner": "user" + "type": "hash_test" } }, "tables": { From 3fee6cc4af89910290c53c61edc77ae2b1978d74 Mon Sep 17 00:00:00 2001 From: Florent Poinsard Date: Thu, 15 Feb 2024 11:08:29 -0600 Subject: [PATCH 2/2] fix DML routed queries Signed-off-by: Florent Poinsard --- go/vt/vtgate/planbuilder/dml_planner.go | 1 + go/vt/vtgate/planbuilder/testdata/dml_cases.json | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/go/vt/vtgate/planbuilder/dml_planner.go b/go/vt/vtgate/planbuilder/dml_planner.go index a85d10b742a..675df1b91cb 100644 --- a/go/vt/vtgate/planbuilder/dml_planner.go +++ b/go/vt/vtgate/planbuilder/dml_planner.go @@ -48,6 +48,7 @@ func rewriteRoutedTables(stmt sqlparser.Statement, vschema plancontext.VSchema) // if the user hasn't specified an alias, we'll insert one here so the old table name still works aliasTbl.As = sqlparser.NewIdentifierCS(name.String()) } + tableName.Qualifier = sqlparser.IdentifierCS{} tableName.Name = sqlparser.NewIdentifierCS(vschemaTable.Name.String()) aliasTbl.Expr = tableName } diff --git a/go/vt/vtgate/planbuilder/testdata/dml_cases.json b/go/vt/vtgate/planbuilder/testdata/dml_cases.json index 5db88df2ad7..1bf982e2b47 100644 --- a/go/vt/vtgate/planbuilder/testdata/dml_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/dml_cases.json @@ -4966,8 +4966,8 @@ "Query": "insert into music(id, user_id) values (:_id_0, :_user_id_0)", "TableName": "music", "VindexValues": { - "music_user_map": "2", - "user_index": "null" + "music_user_map": "INT64(2)", + "user_index": "NULL" } }, "TablesUsed": [