From 173b68cfb01c93544e636d7db35fae78b5e1349c Mon Sep 17 00:00:00 2001 From: Harshit Gangal Date: Mon, 5 Aug 2024 13:26:11 +0530 Subject: [PATCH] error when keyspace not selected on vindex function Signed-off-by: Harshit Gangal --- go/vt/vtgate/engine/vindex_func.go | 3 +++ go/vt/vtgate/executor_select_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+) 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