Skip to content

Commit

Permalink
Merge pull request cockroachdb#57814 from ajwerner/backport20.2-57749
Browse files Browse the repository at this point in the history
release-20.2: sql: fix bug in SHOW TABLES FROM <db> hiding non-public tables
  • Loading branch information
ajwerner authored Dec 17, 2020
2 parents 4a2d371 + e344819 commit 7f140b6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/sql/delegate/show_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <db>.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 <db>. 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())
Expand Down
42 changes: 42 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/schema
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7f140b6

Please sign in to comment.