From e34481915cf26cd0aa130269b651196c324c5317 Mon Sep 17 00:00:00 2001 From: Andrew Werner Date: Wed, 9 Dec 2020 13:49:56 -0500 Subject: [PATCH] sql: fix bug in SHOW TABLES FROM hiding non-public tables The name resolution logic was hiding tables from schemas other than `public` when using an explicit database name in `SHOW TABLES`. Release note (bug fix): Fixed a bug whereby tables in schemas other than "public" would not be displayed when running `SHOW TABLES FROM `. --- pkg/sql/delegate/show_tables.go | 12 +++++- pkg/sql/logictest/testdata/logic_test/schema | 42 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/pkg/sql/delegate/show_tables.go b/pkg/sql/delegate/show_tables.go index a5a085c16c4e..2d9cd58a826c 100644 --- a/pkg/sql/delegate/show_tables.go +++ b/pkg/sql/delegate/show_tables.go @@ -29,9 +29,17 @@ func (d *delegator) delegateShowTables(n *tree.ShowTables) (tree.Statement, erro if err != nil { return nil, err } - + // If we're resolved a one-part name into .public (which is the behavior + // of ResolveSchema, not for any obviously good reason), rework the resolved + // name to have an explicit catalog but no explicit schema. This would arise + // when doing SHOW TABLES FROM . Without this logic, we would not show the + // tables from other schemas than public. + if name.ExplicitSchema && name.ExplicitCatalog && name.SchemaName == tree.PublicSchemaName && + n.ExplicitSchema && !n.ExplicitCatalog && n.SchemaName == name.CatalogName { + name.SchemaName, name.ExplicitSchema = "", false + } var schemaClause string - if n.ExplicitSchema { + if name.ExplicitSchema { schema := lex.EscapeSQLString(name.Schema()) if name.Schema() == sessiondata.PgTempSchemaName { schema = lex.EscapeSQLString(d.evalCtx.SessionData.SearchPath.GetTemporarySchemaName()) diff --git a/pkg/sql/logictest/testdata/logic_test/schema b/pkg/sql/logictest/testdata/logic_test/schema index 223d94562906..f7860d9f1af9 100644 --- a/pkg/sql/logictest/testdata/logic_test/schema +++ b/pkg/sql/logictest/testdata/logic_test/schema @@ -569,3 +569,45 @@ SELECT privilege_type FROM [SHOW GRANTS ON schema s FOR testuser] ---- CREATE USAGE + +subtest show_tables + +statement ok +CREATE DATABASE for_show; + +statement ok; +USE for_show; + +statement ok; +CREATE TABLE t1 (i INT PRIMARY KEY); + +statement ok; +CREATE SCHEMA sc1; + +statement ok; +CREATE TABLE sc1.t1 (i INT PRIMARY KEY); + +query TT +SELECT schema_name, table_name FROM [SHOW TABLES] +---- +public t1 +sc1 t1 + +query TT +SELECT schema_name, table_name FROM [SHOW TABLES FROM sc1] +---- +sc1 t1 + +statement ok +USE test + +query TT +SELECT schema_name, table_name FROM [SHOW TABLES FROM for_show] +---- +public t1 +sc1 t1 + +query TT +SELECT schema_name, table_name FROM [SHOW TABLES FROM for_show.sc1] +---- +sc1 t1