From 623e82075fc6f5cb866abf3ad93439498ad72150 Mon Sep 17 00:00:00 2001 From: Harshit Gangal Date: Tue, 6 Aug 2024 11:28:16 +0530 Subject: [PATCH] vindex function: error when keyspace not selected (#16534) Signed-off-by: Harshit Gangal --- go/vt/vtgate/engine/fake_vcursor_test.go | 2 +- go/vt/vtgate/engine/vindex_func.go | 3 +++ go/vt/vtgate/executor_select_test.go | 13 +++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/go/vt/vtgate/engine/fake_vcursor_test.go b/go/vt/vtgate/engine/fake_vcursor_test.go index adc425c0632..653bcf64576 100644 --- a/go/vt/vtgate/engine/fake_vcursor_test.go +++ b/go/vt/vtgate/engine/fake_vcursor_test.go @@ -342,7 +342,7 @@ func (t *noopVCursor) ExceedsMaxMemoryRows(numRows int) bool { } func (t *noopVCursor) GetKeyspace() string { - return "" + return "test_ks" } func (t *noopVCursor) RecordWarning(warning *querypb.QueryWarning) { diff --git a/go/vt/vtgate/engine/vindex_func.go b/go/vt/vtgate/engine/vindex_func.go index ecd83baeaad..13507631716 100644 --- a/go/vt/vtgate/engine/vindex_func.go +++ b/go/vt/vtgate/engine/vindex_func.go @@ -153,6 +153,9 @@ func (vf *VindexFunc) mapVindex(ctx context.Context, vcursor VCursor, bindVars m case key.DestinationKeyspaceID: if len(d) > 0 { if vcursor != nil { + if vcursor.GetKeyspace() == "" { + return nil, vterrors.VT09005() + } resolvedShards, _, err := vcursor.ResolveDestinations(ctx, vcursor.GetKeyspace(), nil, []key.Destination{d}) if err != nil { return nil, err diff --git a/go/vt/vtgate/executor_select_test.go b/go/vt/vtgate/executor_select_test.go index bd24907af9b..fa448092550 100644 --- a/go/vt/vtgate/executor_select_test.go +++ b/go/vt/vtgate/executor_select_test.go @@ -427,6 +427,19 @@ func TestSetSystemVariablesWithReservedConnection(t *testing.T) { sbc1.Queries = nil } +func TestSelectVindexFunc(t *testing.T) { + executor, _, _, _, _ := createExecutorEnv(t) + + query := "select * from hash_index where id = 1" + session := NewAutocommitSession(&vtgatepb.Session{}) + _, err := executor.Execute(context.Background(), nil, "TestSelectVindexFunc", session, query, nil) + require.ErrorContains(t, err, "VT09005: no database selected") + + session.TargetString = KsTestSharded + _, err = executor.Execute(context.Background(), nil, "TestSelectVindexFunc", session, query, nil) + require.NoError(t, err) +} + func TestCreateTableValidTimestamp(t *testing.T) { executor, sbc1, _, _, _ := createExecutorEnv(t) executor.normalize = true