From 3766134bae36a4b2568c655af21599c0fc1fa744 Mon Sep 17 00:00:00 2001 From: Jon Tirsen Date: Mon, 28 Aug 2017 14:57:50 +0200 Subject: [PATCH] Reset shard targetting using USE with no argument --- go/vt/sqlparser/ast.go | 6 +++++- go/vt/sqlparser/sql.y | 4 ++++ go/vt/vtgate/executor_test.go | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/go/vt/sqlparser/ast.go b/go/vt/sqlparser/ast.go index 7d180512fb8..9f79c9967fb 100644 --- a/go/vt/sqlparser/ast.go +++ b/go/vt/sqlparser/ast.go @@ -1024,7 +1024,11 @@ type Use struct { // Format formats the node. func (node *Use) Format(buf *TrackedBuffer) { - buf.Myprintf("use %v", node.DBName) + if node.DBName.v != "" { + buf.Myprintf("use %v", node.DBName) + } else { + buf.Myprintf("use") + } } // WalkSubtree walks the nodes of the subtree. diff --git a/go/vt/sqlparser/sql.y b/go/vt/sqlparser/sql.y index f19648f80f5..1c0642d273a 100644 --- a/go/vt/sqlparser/sql.y +++ b/go/vt/sqlparser/sql.y @@ -1017,6 +1017,10 @@ use_statement: { $$ = &Use{DBName: $2} } +| USE + { + $$ = &Use{DBName:TableIdent{v:""}} + } other_statement: DESC force_eof diff --git a/go/vt/vtgate/executor_test.go b/go/vt/vtgate/executor_test.go index aa30ad2bfae..5e46fd66f85 100644 --- a/go/vt/vtgate/executor_test.go +++ b/go/vt/vtgate/executor_test.go @@ -941,3 +941,42 @@ func TestPassthroughDDL(t *testing.T) { sbc2.Queries = nil masterSession.TargetString = "" } + +func TestParseEmptyTargetSingleKeyspace(t *testing.T) { + r, _, _, _ := createExecutorEnv() + altVSchema := &vindexes.VSchema{ + Keyspaces: map[string]*vindexes.KeyspaceSchema{ + KsTestUnsharded: r.vschema.Keyspaces[KsTestUnsharded], + }, + } + r.vschema = altVSchema + + got := r.ParseTarget("") + want := querypb.Target{ + Keyspace: KsTestUnsharded, + TabletType: topodatapb.TabletType_MASTER, + } + if !proto.Equal(&got, &want) { + t.Errorf("ParseTarget(%s): %v, want %v", "@master", got, want) + } +} + +func TestParseEmptyTargetMultiKeyspace(t *testing.T) { + r, _, _, _ := createExecutorEnv() + altVSchema := &vindexes.VSchema{ + Keyspaces: map[string]*vindexes.KeyspaceSchema{ + KsTestUnsharded: r.vschema.Keyspaces[KsTestUnsharded], + KsTestSharded: r.vschema.Keyspaces[KsTestSharded], + }, + } + r.vschema = altVSchema + + got := r.ParseTarget("") + want := querypb.Target{ + Keyspace: "", + TabletType: topodatapb.TabletType_MASTER, + } + if !proto.Equal(&got, &want) { + t.Errorf("ParseTarget(%s): %v, want %v", "@master", got, want) + } +}