Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: add a SHOW TENANTS command #94734

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/generated/sql/bnf/show_tenant_stmt.bnf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
show_tenant_stmt ::=
'SHOW' 'TENANT' d_expr
'SHOW' 'TENANTS'
| 'SHOW' 'TENANT' d_expr
| 'SHOW' 'TENANT' d_expr 'WITH' 'REPLICATION' 'STATUS'
4 changes: 3 additions & 1 deletion docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,8 @@ show_tables_stmt ::=
| 'SHOW' 'TABLES' with_comment

show_tenant_stmt ::=
'SHOW' 'TENANT' d_expr
'SHOW' 'TENANTS'
| 'SHOW' 'TENANT' d_expr
| 'SHOW' 'TENANT' d_expr 'WITH' 'REPLICATION' 'STATUS'

show_trace_stmt ::=
Expand Down Expand Up @@ -1380,6 +1381,7 @@ unreserved_keyword ::=
| 'TEMPLATE'
| 'TEMPORARY'
| 'TENANT'
| 'TENANTS'
| 'TESTING_RELOCATE'
| 'TEXT'
| 'TIES'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func TestAlterTenantPauseResume(t *testing.T) {

t.Run("pause-resume-tenant-with-no-replication", func(t *testing.T) {
c.DestSysSQL.Exec(t, `CREATE TENANT noreplication`)
c.DestSysSQL.ExpectErr(t, "tenant \"noreplication\" does not have an active replication job",
c.DestSysSQL.ExpectErr(t, `tenant "noreplication" does not have an active replication job`,
`ALTER TENANT $1 PAUSE REPLICATION`, "noreplication")
c.DestSysSQL.ExpectErr(t, "tenant \"noreplication\" does not have an active replication job",
c.DestSysSQL.ExpectErr(t, `tenant "noreplication" does not have an active replication job`,
`ALTER TENANT $1 RESUME REPLICATION`, "noreplication")
})

Expand Down
12 changes: 12 additions & 0 deletions pkg/ccl/streamingccl/streamingest/testdata/simple
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ exec-sql as=source-tenant
IMPORT INTO d.x CSV DATA ('userfile:///dx/export*-n*.0.csv');
----

query-sql as=source-system
SHOW TENANTS
----
1 system ACTIVE
10 source ACTIVE

query-sql as=destination-system
SHOW TENANTS
----
1 system ACTIVE
2 destination REPLICATING

let $ts as=source-system
SELECT clock_timestamp()::timestamp::string
----
Expand Down
31 changes: 28 additions & 3 deletions pkg/sql/logictest/testdata/logic_test/tenant
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ id name status
query ITT colnames
SHOW TENANT "tenant-one"
----
id name status
id name status
2 tenant-one ACTIVE

query ITT colnames
Expand All @@ -73,13 +73,22 @@ SHOW TENANT three
id name status
4 three ACTIVE

query ITT colnames
SHOW TENANTS
----
id name status
1 system ACTIVE
2 tenant-one ACTIVE
3 two ACTIVE
4 three ACTIVE

statement error tenant "seven" does not exist
SHOW TENANT seven

statement error tenant "tenant-one" does not have an active replication job
query error pq: tenant "tenant-one" does not have an active replication job
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a test to this file to show the output of SHOW TENANTS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely, forgot about those.. thanks! done. also added some in the tenant datadriven test.

SHOW TENANT "tenant-one" WITH REPLICATION STATUS

statement error tenant "two" does not have an active replication job
query error pq: tenant "two" does not have an active replication job
SHOW TENANT two WITH REPLICATION STATUS

# Test creating a tenant with the same name as an existing tenant, but a unique
Expand Down Expand Up @@ -154,6 +163,9 @@ user testuser
statement error only users with the admin role are allowed to destroy tenant
DROP TENANT "not-allowed"

statement error only users with the admin role are allowed to show tenant
SHOW TENANTS

user root

subtest reclaim_name
Expand Down Expand Up @@ -187,3 +199,16 @@ statement ok
CREATE TENANT "1";
CREATE TENANT "a-b";
CREATE TENANT "hello-100"

query ITT colnames
SHOW TENANTS
----
id name status
1 system ACTIVE
2 tenant-one ACTIVE
3 two ACTIVE
4 three ACTIVE
8 to-be-reclaimed ACTIVE
9 1 ACTIVE
10 a-b ACTIVE
11 hello-100 ACTIVE
3 changes: 3 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/tenant_from_tenant
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ CREATE TENANT nope
statement error only the system tenant can show other tenants
SHOW TENANT nope

statement error only the system tenant can show other tenants
SHOW TENANTS

statement error only the system tenant can destroy other tenants
DROP TENANT nope
14 changes: 11 additions & 3 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -5541,13 +5541,20 @@ backup_kms:

// %Help: SHOW TENANT - display tenant information
// %Category: Misc
// %Text: SHOW TENANT <tenant_name> [WITH REPLICATION STATUS]
// %Text:
// SHOW TENANT <tenant_name> [WITH REPLICATION STATUS]
// SHOW TENANTS
show_tenant_stmt:
SHOW TENANT d_expr
SHOW TENANTS
{
$$.val = &tree.ShowTenant{
All: true,
}
}
| SHOW TENANT d_expr
{
$$.val = &tree.ShowTenant{
Name: $3.expr(),
WithReplication: false,
}
}
| SHOW TENANT d_expr WITH REPLICATION STATUS
Expand Down Expand Up @@ -16028,6 +16035,7 @@ unreserved_keyword:
| TEMPLATE
| TEMPORARY
| TENANT
| TENANTS
| TESTING_RELOCATE
| TEXT
| TIES
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/sem/tree/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,10 +864,16 @@ func (node *ShowTableStats) Format(ctx *FmtCtx) {
type ShowTenant struct {
Name Expr
WithReplication bool
All bool
}

// Format implements the NodeFormatter interface.
func (node *ShowTenant) Format(ctx *FmtCtx) {
if node.All {
ctx.WriteString("SHOW TENANTS")
return
}

ctx.WriteString("SHOW TENANT ")
ctx.FormatNode(node.Name)

Expand Down
Loading