From 0e6915575507801adb1c83c162aa1da011003ac2 Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Fri, 13 Apr 2018 09:59:22 +0200 Subject: [PATCH] sql: clarify the error message upon access to vtable with no db Prior to this patch: ``` root@:26257/> select * from pg_catalog.pg_class; pq: no database specified ``` After: ``` root@:26257/> select * from pg_catalog.pg_class; pq: cannot access virtual schema in anonymous database HINT: verify that the current database is set ``` Release note (sql change): the error message produced upon accessing a virtual schema with no database prefix (e.g. when `database` is not set) is now clarified. --- pkg/sql/logictest/testdata/logic_test/namespace | 2 +- pkg/sql/logictest/testdata/logic_test/pg_catalog | 14 +++++++------- pkg/sql/virtual_schema.go | 7 ++++++- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/pkg/sql/logictest/testdata/logic_test/namespace b/pkg/sql/logictest/testdata/logic_test/namespace index 9a236bd79f17..0247c1c988a7 100644 --- a/pkg/sql/logictest/testdata/logic_test/namespace +++ b/pkg/sql/logictest/testdata/logic_test/namespace @@ -52,7 +52,7 @@ SELECT x FROM pg_type # Leave database, check name resolves to default. # The expected error can only occur on the virtual pg_type, not the physical one. -query error no database specified +query error cannot access virtual schema in anonymous database SET database = ''; SELECT * FROM pg_type # Go to different database, check name still resolves to default. diff --git a/pkg/sql/logictest/testdata/logic_test/pg_catalog b/pkg/sql/logictest/testdata/logic_test/pg_catalog index d290b6419450..0013e1b3cd11 100644 --- a/pkg/sql/logictest/testdata/logic_test/pg_catalog +++ b/pkg/sql/logictest/testdata/logic_test/pg_catalog @@ -1321,29 +1321,29 @@ user root statement ok SET DATABASE = '' -query error no database specified +query error cannot access virtual schema in anonymous database SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname='public' ORDER BY 1 -query error no database specified +query error cannot access virtual schema in anonymous database SELECT viewname FROM pg_catalog.pg_views WHERE schemaname='public' ORDER BY 1 -query error no database specified +query error cannot access virtual schema in anonymous database SELECT relname FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid WHERE nspname='public' -query error no database specified +query error cannot access virtual schema in anonymous database SELECT conname FROM pg_catalog.pg_constraint con JOIN pg_catalog.pg_namespace n ON con.connamespace = n.oid WHERE n.nspname = 'public' -query error no database specified +query error cannot access virtual schema in anonymous database SELECT COUNT(*) FROM pg_catalog.pg_depend -query error no database specified +query error cannot access virtual schema in anonymous database select 'upper'::REGPROC; -query error no database specified +query error cannot access virtual schema in anonymous database select 'system.namespace'::regclass statement ok diff --git a/pkg/sql/virtual_schema.go b/pkg/sql/virtual_schema.go index f4190d0ad0d6..7cfd83953c3f 100644 --- a/pkg/sql/virtual_schema.go +++ b/pkg/sql/virtual_schema.go @@ -25,6 +25,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/sql/parser" + "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" ) @@ -93,6 +94,10 @@ type virtualTableEntry struct { type virtualTableConstructor func(context.Context, *planner, string) (planNode, error) +var errInvalidDbPrefix = pgerror.NewError(pgerror.CodeUndefinedObjectError, + "cannot access virtual schema in anonymous database", +).SetHintf("verify that the current database is set") + // getPlanInfo returns the column metadata and a constructor for a new // valuesNode for the virtual table. We use deferred construction here // so as to avoid populating a RowContainer during query preparation, @@ -119,7 +124,7 @@ func (e virtualTableEntry) getPlanInfo( } } else { if !e.validWithNoDatabaseContext { - return nil, errNoDatabase + return nil, errInvalidDbPrefix } }