From ec19d20fc5622eb28149b4996b3f42b070218896 Mon Sep 17 00:00:00 2001 From: Harshit Gangal Date: Thu, 15 Feb 2024 22:23:52 +0530 Subject: [PATCH] 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 396c27bd559..915b5e753cd 100644 --- a/go/vt/vtgate/planbuilder/rewrite.go +++ b/go/vt/vtgate/planbuilder/rewrite.go @@ -70,6 +70,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 } diff --git a/go/vt/vtgate/planbuilder/testdata/dml_cases.json b/go/vt/vtgate/planbuilder/testdata/dml_cases.json index d7a6b32adf3..b71848517c9 100644 --- a/go/vt/vtgate/planbuilder/testdata/dml_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/dml_cases.json @@ -5550,5 +5550,78 @@ "user.user" ] } + }, + { + "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 a1ea952ff7d..c05641d04ed 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 70f55c62f1f..7aaa2648388 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": [ @@ -508,8 +514,7 @@ "sharded": true, "vindexes": { "hash_dup": { - "type": "hash_test", - "owner": "user" + "type": "hash_test" } }, "tables": {