Skip to content

Commit

Permalink
2-sql: added is_invisible to crdb_internal and SHOW INDEX
Browse files Browse the repository at this point in the history
This commit added added a new column `is_invisible` to
crdb_internal.table_indexes and to the output of SQL statements related to SHOW
INDEX, SHOW INDEXES, and SHOW KEYS.
  • Loading branch information
wenyihu6 committed Jul 6, 2022
1 parent ad3f113 commit 01eb55d
Show file tree
Hide file tree
Showing 30 changed files with 518 additions and 461 deletions.
4 changes: 2 additions & 2 deletions pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ SELECT * FROM crdb_internal.table_columns WHERE descriptor_name = ''
----
descriptor_id descriptor_name column_id column_name column_type nullable default_expr hidden

query ITITTBBBIT colnames
query ITITTBBBBIT colnames
SELECT * FROM crdb_internal.table_indexes WHERE descriptor_name = ''
----
descriptor_id descriptor_name index_id index_name index_type is_unique is_inverted is_sharded shard_bucket_count created_at
descriptor_id descriptor_name index_id index_name index_type is_unique is_inverted is_sharded is_hidden shard_bucket_count created_at

query ITITTITTB colnames
SELECT * FROM crdb_internal.index_columns WHERE descriptor_name = ''
Expand Down
3 changes: 3 additions & 0 deletions pkg/ccl/schemachangerccl/testdata/decomp/multiregion
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ ElementState:
indexId: 1
isConcurrently: false
isCreatedExplicitly: false
isHidden: false
isInverted: false
isUnique: true
keyColumnDirections:
Expand Down Expand Up @@ -366,6 +367,7 @@ ElementState:
indexId: 1
isConcurrently: false
isCreatedExplicitly: false
isHidden: false
isInverted: false
isUnique: true
keyColumnDirections:
Expand Down Expand Up @@ -622,6 +624,7 @@ ElementState:
indexId: 1
isConcurrently: false
isCreatedExplicitly: false
isHidden: false
isInverted: false
isUnique: true
keyColumnDirections:
Expand Down
4 changes: 4 additions & 0 deletions pkg/ccl/schemachangerccl/testdata/decomp/partitioning
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ ElementState:
indexId: 1
isConcurrently: false
isCreatedExplicitly: false
isHidden: false
isInverted: false
isUnique: true
keyColumnDirections:
Expand All @@ -118,6 +119,7 @@ ElementState:
indexId: 2
isConcurrently: false
isCreatedExplicitly: false
isHidden: false
isInverted: true
isUnique: false
keyColumnDirections:
Expand Down Expand Up @@ -439,6 +441,7 @@ ElementState:
indexId: 1
isConcurrently: false
isCreatedExplicitly: false
isHidden: false
isInverted: false
isUnique: true
keyColumnDirections:
Expand All @@ -460,6 +463,7 @@ ElementState:
indexId: 2
isConcurrently: false
isCreatedExplicitly: false
isHidden: false
isInverted: false
isUnique: true
keyColumnDirections:
Expand Down
12 changes: 6 additions & 6 deletions pkg/cli/testdata/doctor/test_recreate_zipdir

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2756,6 +2756,7 @@ CREATE TABLE crdb_internal.table_indexes (
is_unique BOOL NOT NULL,
is_inverted BOOL NOT NULL,
is_sharded BOOL NOT NULL,
is_hidden BOOL NOT NULL,
shard_bucket_count INT,
created_at TIMESTAMP
)
Expand Down Expand Up @@ -2801,6 +2802,7 @@ CREATE TABLE crdb_internal.table_indexes (
tree.MakeDBool(tree.DBool(idx.IsUnique())),
tree.MakeDBool(idx.GetType() == descpb.IndexDescriptor_INVERTED),
tree.MakeDBool(tree.DBool(idx.IsSharded())),
tree.MakeDBool(tree.DBool(idx.IsHidden())),
shardBucketCnt,
createdAt,
)
Expand Down
24 changes: 19 additions & 5 deletions pkg/sql/delegate/show_database_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,48 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)

// delegateShowDatabaseIndexes implements SHOW INDEX FROM DATABASE, SHOW INDEXES
// FROM DATABASE, SHOW KEYS FROM DATABASE which returns all the indexes in the
// given or current database.
func (d *delegator) delegateShowDatabaseIndexes(
n *tree.ShowDatabaseIndexes,
) (tree.Statement, error) {
name, err := d.getSpecifiedOrCurrentDatabase(n.Database)
if err != nil {
return nil, err
}

getAllIndexesQuery := `
SELECT
table_name,
index_name,
info.index_name,
non_unique::BOOL,
seq_in_index,
column_name,
direction,
storing::BOOL,
implicit::BOOL`
implicit::BOOL,
idx.is_hidden::BOOL`

if n.WithComment {
getAllIndexesQuery += `,
obj_description(pg_class.oid) AS comment`
}

getAllIndexesQuery += `
FROM
%s.information_schema.statistics`
FROM
%[1]s.information_schema.statistics AS info`

if n.WithComment {
getAllIndexesQuery += `
LEFT JOIN pg_class ON
statistics.index_name = pg_class.relname`
}

return parse(fmt.Sprintf(getAllIndexesQuery, n.Database.String()))
getAllIndexesQuery += `
LEFT JOIN %[1]s.crdb_internal.table_indexes AS idx ON
idx.index_name = info.index_name AND
idx.descriptor_name = info.table_name`

return parse(fmt.Sprintf(getAllIndexesQuery, name.String()))
}
12 changes: 10 additions & 2 deletions pkg/sql/delegate/show_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ ORDER BY
return d.showTableDetails(n.Name, showCreateQuery)
}

// delegateShowIndexes implements SHOW INDEX FROM, SHOW INDEXES FROM, SHOW KEYS
// FROM which returns all the indexes in the given table.
func (d *delegator) delegateShowIndexes(n *tree.ShowIndexes) (tree.Statement, error) {
sqltelemetry.IncrementShowCounter(sqltelemetry.Indexes)
getIndexesQuery := `
Expand All @@ -111,7 +113,8 @@ SELECT
column_name,
direction,
storing::BOOL,
implicit::BOOL`
implicit::BOOL,
idx.is_hidden::BOOL`

if n.WithComment {
getIndexesQuery += `,
Expand All @@ -129,13 +132,18 @@ FROM
pg_indexes.indexname = s.index_name`
}

getIndexesQuery += `
LEFT JOIN %[4]s.crdb_internal.table_indexes AS idx ON
idx.index_name = s.index_name AND
idx.descriptor_name = s.table_name`

getIndexesQuery += `
WHERE
table_catalog=%[1]s
AND table_schema=%[5]s
AND table_name=%[2]s
ORDER BY
1, 2, 3, 4, 5, 6, 7, 8;`
1, 2, 3, 4, 5, 6, 7, 8, 9;`

return d.showTableDetails(n.Table, getIndexesQuery)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/sql/descriptor_mutation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,11 +1051,11 @@ CREATE TABLE t.test (a STRING PRIMARY KEY, b STRING, c STRING, INDEX foo (c));
mt.CheckQueryResults(t,
"SHOW INDEXES FROM t.test",
[][]string{
{"test", "test_pkey", "false", "1", "a", "ASC", "false", "false"},
{"test", "test_pkey", "false", "2", "b", "N/A", "true", "false"},
{"test", "test_pkey", "false", "3", "d", "N/A", "true", "false"},
{"test", "ufo", "true", "1", "d", "ASC", "false", "false"},
{"test", "ufo", "true", "2", "a", "ASC", "false", "true"},
{"test", "test_pkey", "false", "1", "a", "ASC", "false", "false", "false"},
{"test", "test_pkey", "false", "2", "b", "N/A", "true", "false", "false"},
{"test", "test_pkey", "false", "3", "d", "N/A", "true", "false", "false"},
{"test", "ufo", "true", "1", "d", "ASC", "false", "false", "false"},
{"test", "ufo", "true", "2", "a", "ASC", "false", "true", "false"},
},
)

Expand Down
76 changes: 45 additions & 31 deletions pkg/sql/logictest/testdata/logic_test/alter_primary_key
Original file line number Diff line number Diff line change
Expand Up @@ -1138,15 +1138,17 @@ statement ok
create table t1(id integer not null, id2 integer not null, name varchar(32));

query TTT
select index_name,column_name,direction from [show indexes from t1] where index_name like 'primary%';
select index_name,column_name,direction from [show indexes from t1] where index_name like 'primary%'
ORDER BY index_name, seq_in_index
----

statement ok
alter table t1 alter primary key using columns(id, id2);


query TTT
select index_name,column_name,direction from [show indexes from t1];
select index_name,column_name,direction from [show indexes from t1]
ORDER BY index_name, seq_in_index
----
t1_pkey id ASC
t1_pkey id2 ASC
Expand All @@ -1159,7 +1161,8 @@ alter table t1 alter primary key using columns(id, id2);


query TTT
select index_name,column_name,direction from [show indexes from t1];
select index_name,column_name,direction from [show indexes from t1]
ORDER BY index_name, seq_in_index
----
t1_pkey id ASC
t1_pkey id2 ASC
Expand All @@ -1171,7 +1174,8 @@ statement ok
alter table t1 drop constraint t1_pkey, alter primary key using columns(id, id2);

query TTT
select index_name,column_name,direction from [show indexes from t1];
select index_name,column_name,direction from [show indexes from t1]
ORDER BY index_name, seq_in_index
----
t1_pkey id ASC
t1_pkey id2 ASC
Expand All @@ -1183,77 +1187,82 @@ alter table t1 alter primary key using columns(id);


query TTT
select index_name,column_name,direction from [show indexes from t1];
select index_name,column_name,direction from [show indexes from t1]
ORDER BY index_name, seq_in_index
----
t1_id_id2_key id ASC
t1_id_id2_key id2 ASC
t1_pkey id ASC
t1_pkey id2 N/A
t1_pkey name N/A
t1_pkey rowid N/A
t1_id_id2_key id ASC
t1_id_id2_key id2 ASC

statement ok
alter table t1 alter primary key using columns(id desc);


query TTT
select index_name,column_name,direction from [show indexes from t1];
select index_name,column_name,direction from [show indexes from t1]
ORDER BY index_name, seq_in_index
----
t1_id_id2_key id ASC
t1_id_id2_key id2 ASC
t1_id_key id ASC
t1_pkey id DESC
t1_pkey id2 N/A
t1_pkey name N/A
t1_pkey rowid N/A
t1_id_id2_key id ASC
t1_id_id2_key id2 ASC
t1_id_key id ASC


statement ok
alter table t1 alter primary key using columns(id desc);

query TTT
select index_name,column_name,direction from [show indexes from t1];
select index_name,column_name,direction from [show indexes from t1]
ORDER BY index_name, seq_in_index
----
t1_id_id2_key id ASC
t1_id_id2_key id2 ASC
t1_id_key id ASC
t1_pkey id DESC
t1_pkey id2 N/A
t1_pkey name N/A
t1_pkey rowid N/A
t1_id_id2_key id ASC
t1_id_id2_key id2 ASC
t1_id_key id ASC

statement ok
alter table t1 alter primary key using columns(id desc);

query TTT
select index_name,column_name,direction from [show indexes from t1];
select index_name,column_name,direction from [show indexes from t1]
ORDER BY index_name, seq_in_index
----
t1_id_id2_key id ASC
t1_id_id2_key id2 ASC
t1_id_key id ASC
t1_pkey id DESC
t1_pkey id2 N/A
t1_pkey name N/A
t1_pkey rowid N/A
t1_id_id2_key id ASC
t1_id_id2_key id2 ASC
t1_id_key id ASC

statement ok
alter table t1 alter primary key using columns(id) USING HASH WITH (bucket_count=10)

query TTT
select index_name,column_name,direction from [show indexes from t1];
select index_name,column_name,direction from [show indexes from t1]
ORDER BY index_name, seq_in_index
----
t1_pkey crdb_internal_id_shard_10 ASC
t1_pkey id ASC
t1_pkey id2 N/A
t1_pkey name N/A
t1_pkey rowid N/A
t1_id_key1 id DESC
t1_id_key1 crdb_internal_id_shard_10 ASC
t1_id_id2_key id ASC
t1_id_id2_key id2 ASC
t1_id_id2_key crdb_internal_id_shard_10 ASC
t1_id_key id ASC
t1_id_key crdb_internal_id_shard_10 ASC
t1_id_key1 id DESC
t1_id_key1 crdb_internal_id_shard_10 ASC
t1_pkey crdb_internal_id_shard_10 ASC
t1_pkey id ASC
t1_pkey id2 N/A
t1_pkey name N/A
t1_pkey rowid N/A

statement ok
CREATE TABLE table_with_virtual_cols (
Expand Down Expand Up @@ -1288,6 +1297,7 @@ CREATE TABLE t (i INT PRIMARY KEY)

query TTT
SELECT index_name,column_name,direction FROM [SHOW INDEXES FROM t]
ORDER BY index_name, seq_in_index
----
t_pkey i ASC

Expand All @@ -1296,6 +1306,7 @@ ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (i) USING HASH WITH (bucket_count=

query TTT
SELECT index_name,column_name,direction FROM [SHOW INDEXES FROM t]
ORDER BY index_name, seq_in_index
----
t_pkey crdb_internal_i_shard_2 ASC
t_pkey i ASC
Expand All @@ -1305,6 +1316,7 @@ ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (i);

query TTT
SELECT index_name,column_name,direction FROM [SHOW INDEXES FROM t]
ORDER BY index_name, seq_in_index
----
t_pkey i ASC

Expand Down Expand Up @@ -1602,11 +1614,12 @@ ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (b);

query TTT
SELECT index_name, column_name, direction FROM [SHOW INDEXES FROM t]
ORDER BY index_name, seq_in_index
----
t_pkey b ASC
t_pkey a N/A
t_a_key a ASC
t_a_key b ASC
t_pkey b ASC
t_pkey a N/A

# But if the existing index is not strictly equal to the (old) primary key
# (even if, for example, the (old) primary key is a strict prefix of an
Expand All @@ -1626,10 +1639,11 @@ ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (b);
# on only (a) but the other on (a, b).
query TTTB
SELECT index_name, column_name, direction, storing FROM [SHOW INDEXES FROM t]
ORDER BY index_name, seq_in_index
----
t_pkey b ASC false
t_pkey a N/A true
t_a_b_key a ASC false
t_a_b_key b ASC false
t_a_key a ASC false
t_a_key b ASC true
t_pkey b ASC false
t_pkey a N/A true
Loading

0 comments on commit 01eb55d

Please sign in to comment.