From 78ac47e4a5c2823b6945b64934382067eb864de0 Mon Sep 17 00:00:00 2001 From: "vitess-bot[bot]" <108069721+vitess-bot[bot]@users.noreply.github.com> Date: Fri, 16 Feb 2024 15:54:36 +0530 Subject: [PATCH] [release-18.0] Fix routing rule query rewrite (#15253) (#15258) Signed-off-by: Harshit Gangal Signed-off-by: Florent Poinsard <35779988+frouioui@users.noreply.github.com> Signed-off-by: Florent Poinsard Co-authored-by: Harshit Gangal Co-authored-by: Florent Poinsard <35779988+frouioui@users.noreply.github.com> Co-authored-by: Florent Poinsard --- go/vt/vtgate/planbuilder/dml_planner.go | 1 + 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 ++- 5 files changed, 104 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/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..1bf982e2b47 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": "INT64(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": {