Skip to content

Commit

Permalink
sql: Add support for a show partitions command.
Browse files Browse the repository at this point in the history
SHOW PARTITIONS FROM TABLE <table>
SHOW PARTITIONS FROM DATABASE <database>
SHOW PARTITIONS FROM INDEX <index>

Returns a table containing the following columns.

* database_name
* table_name
* partition_name
* parent_partition
* column_names
* index_name
* partition_value
* zone_constraints (NULL if no constraints were specified)

To do this, changes were made to the crdb_internal.partitions table.

Release note (sql change): Add support for a SHOW PARTITIONS command.
  • Loading branch information
rohany committed Jul 31, 2019
1 parent 0e4ebab commit 573f863
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 22 deletions.
1 change: 1 addition & 0 deletions docs/generated/sql/bnf/show_var.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ show_stmt ::=
| show_databases_stmt
| show_grants_stmt
| show_indexes_stmt
| show_partitions_stmt
| show_jobs_stmt
| show_queries_stmt
| show_ranges_stmt
Expand Down
15 changes: 11 additions & 4 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ show_stmt ::=
| show_databases_stmt
| show_grants_stmt
| show_indexes_stmt
| show_partitions_stmt
| show_jobs_stmt
| show_queries_stmt
| show_ranges_stmt
Expand Down Expand Up @@ -479,6 +480,11 @@ show_indexes_stmt ::=
| 'SHOW' 'KEYS' 'FROM' table_name
| 'SHOW' 'KEYS' 'FROM' 'DATABASE' database_name

show_partitions_stmt ::=
'SHOW' 'PARTITIONS' 'FROM' 'TABLE' table_name
| 'SHOW' 'PARTITIONS' 'FROM' 'DATABASE' database_name
| 'SHOW' 'PARTITIONS' 'FROM' 'INDEX' table_index_name

show_jobs_stmt ::=
'SHOW' opt_automatic 'JOBS'

Expand Down Expand Up @@ -702,6 +708,7 @@ unreserved_keyword ::=
| 'PARENT'
| 'PARTIAL'
| 'PARTITION'
| 'PARTITIONS'
| 'PASSWORD'
| 'PAUSE'
| 'PHYSICAL'
Expand Down Expand Up @@ -1142,6 +1149,10 @@ for_grantee_clause ::=
'FOR' name_list
|

table_index_name ::=
table_name '@' index_name
| standalone_index_name

opt_automatic ::=
'AUTOMATIC'
|
Expand All @@ -1150,10 +1161,6 @@ opt_cluster ::=
'CLUSTER'
| 'LOCAL'

table_index_name ::=
table_name '@' index_name
| standalone_index_name

opt_compact ::=
'COMPACT'
|
Expand Down
26 changes: 13 additions & 13 deletions pkg/ccl/logictestccl/testdata/logic_test/crdb_internal
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# LogicTest: local

query IITTI colnames
query IITTITTT colnames
SELECT * FROM crdb_internal.partitions
----
table_id index_id parent_name name columns
table_id index_id parent_name name columns column_names list_value range_value

statement ok
CREATE TABLE t1 (
Expand Down Expand Up @@ -33,16 +33,16 @@ CREATE table t2 (a STRING PRIMARY KEY) PARTITION BY LIST (a) (
PARTITION pfoo VALUES IN ('foo')
)

query IITTI
query IITTITTT
SELECT * FROM crdb_internal.partitions ORDER BY table_id, index_id, name
----
53 1 NULL p12 1
53 1 p12 p12p3 1
53 1 p12p3 p12p3p8 1
53 1 NULL p6 1
53 1 p6 p6p7 1
53 1 p6 p6p8 1
53 1 p6 p6px 1
53 1 p12 pd 1
53 2 NULL p00 2
54 1 NULL pfoo 1
53 1 NULL p12 1 a (1), (2) NULL
53 1 p12 p12p3 1 b (3) NULL
53 1 p12p3 p12p3p8 1 c (8) NULL
53 1 NULL p6 1 a (6) NULL
53 1 p6 p6p7 1 b NULL (MINVALUE) TO (7)
53 1 p6 p6p8 1 b NULL (7) TO (8)
53 1 p6 p6px 1 b NULL (8) TO (MAXVALUE)
53 1 p12 pd 1 b (DEFAULT) NULL
53 2 NULL p00 2 a, b (0, 0) NULL
54 1 NULL pfoo 1 a ('foo') NULL
139 changes: 139 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/distsql_partitioning
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# LogicTest: 5node-dist

# Tests for the show partitions command.

statement ok
CREATE TABLE t1 (x INT PRIMARY KEY)

statement ok
ALTER TABLE t1 PARTITION BY LIST (x) (
PARTITION p1 VALUES IN (1),
PARTITION p2 VALUES IN (2),
PARTITION p3 VALUES IN (3)
)

statement ok
ALTER PARTITION p1 OF TABLE t1 CONFIGURE ZONE USING constraints='[+dc=dc1]';
ALTER PARTITION p2 OF TABLE t1 CONFIGURE ZONE USING constraints='[+dc=dc2]';
ALTER PARTITION p3 OF TABLE t1 CONFIGURE ZONE USING constraints='[+dc=dc3]'

query TTTTTTTT colnames
SHOW PARTITIONS FROM DATABASE test
----
database_name table_name partition_name parent_partition column_names index_name partition_value zone_constraints
test t1 p1 NULL x t1@primary (1) [+dc=dc1]
test t1 p2 NULL x t1@primary (2) [+dc=dc2]
test t1 p3 NULL x t1@primary (3) [+dc=dc3]

query TTTTTTTT
SHOW PARTITIONS FROM TABLE t1
----
test t1 p1 NULL x t1@primary (1) [+dc=dc1]
test t1 p2 NULL x t1@primary (2) [+dc=dc2]
test t1 p3 NULL x t1@primary (3) [+dc=dc3]

query TTTTTTTT
SHOW PARTITIONS FROM INDEX t1@primary
----
test t1 p1 NULL x t1@primary (1) [+dc=dc1]
test t1 p2 NULL x t1@primary (2) [+dc=dc2]
test t1 p3 NULL x t1@primary (3) [+dc=dc3]

statement ok
CREATE TABLE t2 (x INT PRIMARY KEY)

statement ok
ALTER TABLE t2 PARTITION BY RANGE (x) (
PARTITION p1 VALUES FROM (1) TO (2),
PARTITION p2 VALUES FROM (2) TO (3)
)

statement ok
ALTER PARTITION p1 OF TABLE t2 CONFIGURE ZONE USING constraints='[+dc=dc1]';
ALTER PARTITION p2 OF TABLE t2 CONFIGURE ZONE USING constraints='[+dc=dc2]'

query TTTTTTTT
SHOW PARTITIONS FROM DATABASE test
----
test t1 p1 NULL x t1@primary (1) [+dc=dc1]
test t1 p2 NULL x t1@primary (2) [+dc=dc2]
test t1 p3 NULL x t1@primary (3) [+dc=dc3]
test t2 p1 NULL x t2@primary (1) TO (2) [+dc=dc1]
test t2 p2 NULL x t2@primary (2) TO (3) [+dc=dc2]

query TTTTTTTT
SHOW PARTITIONS FROM TABLE t2
----
test t2 p1 NULL x t2@primary (1) TO (2) [+dc=dc1]
test t2 p2 NULL x t2@primary (2) TO (3) [+dc=dc2]

query TTTTTTTT
SHOW PARTITIONS FROM INDEX t2@primary
----
test t2 p1 NULL x t2@primary (1) TO (2) [+dc=dc1]
test t2 p2 NULL x t2@primary (2) TO (3) [+dc=dc2]

statement ok
CREATE TABLE t3 (x INT PRIMARY KEY, y INT, INDEX sec (y))

statement ok
ALTER TABLE t3 PARTITION BY LIST (x) (
PARTITION p1 VALUES IN (1),
PARTITION p2 VALUES IN (2)
)

statement ok
ALTER INDEX sec PARTITION BY LIST (y) (
PARTITION p3 VALUES IN (3),
PARTITION p4 VALUES IN (4)
)

statement ok
ALTER PARTITION p1 OF TABLE t3 CONFIGURE ZONE USING constraints='[+dc=dc1]';
ALTER PARTITION p2 OF TABLE t3 CONFIGURE ZONE USING constraints='[+dc=dc2]';
ALTER PARTITION p3 OF TABLE t3 CONFIGURE ZONE USING constraints='[+dc=dc3]';
ALTER PARTITION p4 OF TABLE t3 CONFIGURE ZONE USING constraints='[+dc=dc4]'

query TTTTTTTT
SHOW PARTITIONS FROM TABLE t3
----
test t3 p1 NULL x t3@primary (1) [+dc=dc1]
test t3 p2 NULL x t3@primary (2) [+dc=dc2]
test t3 p3 NULL y t3@sec (3) [+dc=dc3]
test t3 p4 NULL y t3@sec (4) [+dc=dc4]

query TTTTTTTT
SHOW PARTITIONS FROM INDEX t3@sec
----
test t3 p3 NULL y t3@sec (3) [+dc=dc3]
test t3 p4 NULL y t3@sec (4) [+dc=dc4]

statement ok
CREATE TABLE t4 (x INT, y INT, PRIMARY KEY (x, y))

statement ok
ALTER TABLE t4 PARTITION BY LIST (x) (
PARTITION p1 VALUES IN (1) PARTITION BY LIST (y) (
PARTITION p1_a VALUES in (2),
PARTITION p1_b VALUES IN (3)
),
PARTITION p2 VALUES IN (4) PARTITION BY LIST (y) (
PARTITION p2_a VALUES IN (5)
)
)

statement ok
ALTER PARTITION p1 OF TABLE t4 CONFIGURE ZONE USING constraints='[+dc=dc1]';
ALTER PARTITION p1_a OF TABLE t4 CONFIGURE ZONE USING constraints='[+dc=dc2]';
ALTER PARTITION p1_b OF TABLE t4 CONFIGURE ZONE USING constraints='[+dc=dc3]';
ALTER PARTITION p2 OF TABLE t4 CONFIGURE ZONE USING constraints='[+dc=dc4]';
ALTER PARTITION p2_a OF TABLE t4 CONFIGURE ZONE USING constraints='[+dc=dc5]'

query TTTTTTTT
SHOW PARTITIONS FROM TABLE t4
----
test t4 p1 NULL x t4@primary (1) [+dc=dc1]
test t4 p1_a p1 y t4@primary (2) [+dc=dc2]
test t4 p1_b p1 y t4@primary (3) [+dc=dc3]
test t4 p2 NULL x t4@primary (4) [+dc=dc4]
test t4 p2_a p2 y t4@primary (5) [+dc=dc5]
58 changes: 57 additions & 1 deletion pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2265,14 +2265,48 @@ func addPartitioningRows(
indexID := tree.NewDInt(tree.DInt(index.ID))
numColumns := tree.NewDInt(tree.DInt(partitioning.NumColumns))

var buf bytes.Buffer
for i := uint32(colOffset); i < uint32(colOffset)+partitioning.NumColumns; i++ {
if i != uint32(colOffset) {
buf.WriteString(`, `)
}
buf.WriteString(index.ColumnNames[i])
}
colNames := tree.NewDString(buf.String())

var a sqlbase.DatumAlloc

// We don't need real prefixes in the DecodePartitionTuple calls because we
// only use the tree.Datums part of the output.
fakePrefixDatums := make([]tree.Datum, colOffset)
for i := range fakePrefixDatums {
fakePrefixDatums[i] = tree.DNull
}

for _, l := range partitioning.List {
var buf bytes.Buffer
for j, values := range l.Values {
if j != 0 {
buf.WriteString(`, `)
}
tuple, _, err := sqlbase.DecodePartitionTuple(
&a, table, index, partitioning, values, fakePrefixDatums,
)
if err != nil {
return err
}
buf.WriteString(tuple.String())
}
name := tree.NewDString(l.Name)
if err := addRow(
tableID,
indexID,
parentName,
name,
numColumns,
colNames,
tree.NewDString(buf.String()),
tree.DNull,
); err != nil {
return err
}
Expand All @@ -2284,12 +2318,31 @@ func addPartitioningRows(
}

for _, r := range partitioning.Range {
var buf bytes.Buffer
fromTuple, _, err := sqlbase.DecodePartitionTuple(
&a, table, index, partitioning, r.FromInclusive, fakePrefixDatums,
)
if err != nil {
return err
}
buf.WriteString(fromTuple.String())
buf.WriteString(" TO ")
toTuple, _, err := sqlbase.DecodePartitionTuple(
&a, table, index, partitioning, r.ToExclusive, fakePrefixDatums,
)
if err != nil {
return err
}
buf.WriteString(toTuple.String())
if err := addRow(
tableID,
indexID,
parentName,
tree.NewDString(r.Name),
numColumns,
colNames,
tree.DNull,
tree.NewDString(buf.String()),
); err != nil {
return err
}
Expand All @@ -2310,7 +2363,10 @@ CREATE TABLE crdb_internal.partitions (
index_id INT NOT NULL,
parent_name STRING,
name STRING NOT NULL,
columns INT NOT NULL
columns INT NOT NULL,
column_names STRING,
list_value STRING,
range_value STRING
)
`,
populate: func(ctx context.Context, p *planner, dbContext *DatabaseDescriptor, addRow func(...tree.Datum) error) error {
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/delegate/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func TryDelegate(
case *tree.ShowConstraints:
return d.delegateShowConstraints(t)

case *tree.ShowPartitions:
return d.delegateShowPartitions(t)

case *tree.ShowGrants:
return d.delegateShowGrants(t)

Expand Down
Loading

0 comments on commit 573f863

Please sign in to comment.