Skip to content

Commit

Permalink
sql: Respect new bucket_count storage param for formatting
Browse files Browse the repository at this point in the history
a follow up of pr #76068

Release note (sql change): We have add support for the new `bucket_count`
storage param syntax. We prefer it over the old `WITH BUCKET_COUNT=xxx`
syntax. With this change, crdb outputs the new syntax for `SHOW CREATE`.
Though for the AST tree formatting, we still respect the old syntax if
user used it.
  • Loading branch information
chengxiong-ruan committed Feb 8, 2022
1 parent 844ac13 commit 9c2434a
Show file tree
Hide file tree
Showing 22 changed files with 221 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,7 @@ SET experimental_enable_hash_sharded_indexes = true

statement ok
CREATE TABLE hash_sharded_idx_table (
pk INT PRIMARY KEY USING HASH WITH BUCKET_COUNT = 8
pk INT PRIMARY KEY USING HASH WITH (bucket_count=8)
)

statement error cannot convert hash_sharded_idx_table to REGIONAL BY ROW as the table contains hash sharded indexes
Expand Down
10 changes: 5 additions & 5 deletions pkg/ccl/logictestccl/testdata/logic_test/regional_by_row
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ SET experimental_enable_hash_sharded_indexes = true

statement error hash sharded indexes are not compatible with REGIONAL BY ROW tables
CREATE TABLE regional_by_row_table (
pk INT PRIMARY KEY USING HASH WITH BUCKET_COUNT = 8
pk INT PRIMARY KEY USING HASH WITH (bucket_count=8)
) LOCALITY REGIONAL BY ROW

statement error hash sharded indexes are not compatible with REGIONAL BY ROW tables
CREATE TABLE regional_by_row_table (
pk INT NOT NULL,
a INT,
PRIMARY KEY(pk) USING HASH WITH BUCKET_COUNT = 8
PRIMARY KEY(pk) USING HASH WITH (bucket_count=8)
) LOCALITY REGIONAL BY ROW

statement error hash sharded indexes are not compatible with REGIONAL BY ROW tables
CREATE TABLE regional_by_row_table (
pk INT NOT NULL,
a INT,
INDEX(a) USING HASH WITH BUCKET_COUNT = 8
INDEX(a) USING HASH WITH (bucket_count=8)
) LOCALITY REGIONAL BY ROW

statement error multi-region tables with an INDEX containing PARTITION BY are not supported
Expand Down Expand Up @@ -333,10 +333,10 @@ CREATE INDEX bad_idx ON regional_by_row_table(a) PARTITION BY LIST (a) (
)

statement error hash sharded indexes are not compatible with REGIONAL BY ROW tables
CREATE INDEX bad_idx ON regional_by_row_table(a) USING HASH WITH BUCKET_COUNT = 8
CREATE INDEX bad_idx ON regional_by_row_table(a) USING HASH WITH (bucket_count=8)

statement error hash sharded indexes are not compatible with REGIONAL BY ROW tables
ALTER TABLE regional_by_row_table ALTER PRIMARY KEY USING COLUMNS(pk2) USING HASH WITH BUCKET_COUNT = 8
ALTER TABLE regional_by_row_table ALTER PRIMARY KEY USING COLUMNS(pk2) USING HASH WITH (bucket_count=8)

# Try add a new unique column.
statement ok
Expand Down
23 changes: 19 additions & 4 deletions pkg/sql/catalog/catformat/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ func indexForDisplay(
f.WriteByte(')')

if index.IsSharded() {
fmt.Fprintf(f, " USING HASH WITH BUCKET_COUNT = %v",
index.Sharded.ShardBuckets)
if f.HasFlags(tree.FmtPGCatalog) {
fmt.Fprintf(f, " USING HASH WITH (bucket_count=%v)",
index.Sharded.ShardBuckets)
} else {
f.WriteString(" USING HASH")
}
}

if !isPrimary && len(index.StoreColumnNames) > 0 {
Expand Down Expand Up @@ -224,6 +228,7 @@ func FormatIndexElements(
func formatStorageConfigs(
table catalog.TableDescriptor, index *descpb.IndexDescriptor, f *tree.FmtCtx,
) error {
numCustomSettings := 0
if index.GeoConfig.S2Geometry != nil || index.GeoConfig.S2Geography != nil {
var s2Config *geoindex.S2Config

Expand All @@ -235,7 +240,6 @@ func formatStorageConfigs(
}

defaultS2Config := geoindex.DefaultS2Config()
numCustomSettings := 0
if *s2Config != *defaultS2Config {
for _, check := range []struct {
key string
Expand Down Expand Up @@ -294,10 +298,21 @@ func formatStorageConfigs(
}
}
}
}

if index.IsSharded() {
if numCustomSettings > 0 {
f.WriteString(")")
f.WriteString(", ")
} else {
f.WriteString(" WITH (")
}
f.WriteString(`bucket_count=`)
f.WriteString(strconv.FormatInt(int64(index.Sharded.ShardBuckets), 10))
numCustomSettings++
}

if numCustomSettings > 0 {
f.WriteString(")")
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/catalog/catformat/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,16 @@ func TestIndexForDisplay(t *testing.T) {
tableName: descpb.AnonymousTable,
partition: "",
displayMode: IndexDisplayDefOnly,
expected: "INDEX baz (a DESC) USING HASH WITH BUCKET_COUNT = 8",
pgExpected: "INDEX baz USING btree (a DESC) USING HASH WITH BUCKET_COUNT = 8",
expected: "INDEX baz (a DESC) USING HASH WITH (bucket_count=8)",
pgExpected: "INDEX baz USING btree (a DESC) USING HASH WITH (bucket_count=8)",
},
{
index: shardedIndex,
tableName: tableName,
partition: "",
displayMode: IndexDisplayShowCreate,
expected: "CREATE INDEX baz ON foo.public.bar (a DESC) USING HASH WITH BUCKET_COUNT = 8",
pgExpected: "CREATE INDEX baz ON foo.public.bar USING btree (a DESC) USING HASH WITH BUCKET_COUNT = 8",
expected: "CREATE INDEX baz ON foo.public.bar (a DESC) USING HASH WITH (bucket_count=8)",
pgExpected: "CREATE INDEX baz ON foo.public.bar USING btree (a DESC) USING HASH WITH (bucket_count=8)",
},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/catpb/catalog.proto
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ enum GeneratedAsIdentityType {
// As as example, sample field values for the following table:
//
// CREATE TABLE abc (
// a INT PRIMARY KEY USING HASH WITH BUCKET_COUNT=10, // column id: 1
// a INT PRIMARY KEY USING HASH WITH (bucket_count=10), // column id: 1
// b BYTES
// );
//
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/catalog/systemschema/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ CREATE TABLE system.statement_statistics (
) STORED,
CONSTRAINT "primary" PRIMARY KEY (aggregated_ts, fingerprint_id, transaction_fingerprint_id, plan_hash, app_name, node_id)
USING HASH WITH BUCKET_COUNT = 8,
USING HASH WITH (bucket_count=8),
INDEX "fingerprint_stats_idx" (fingerprint_id, transaction_fingerprint_id),
FAMILY "primary" (
crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_plan_hash_transaction_fingerprint_id_shard_8,
Expand Down Expand Up @@ -539,7 +539,7 @@ CREATE TABLE system.transaction_statistics (
)) STORED,
CONSTRAINT "primary" PRIMARY KEY (aggregated_ts, fingerprint_id, app_name, node_id)
USING HASH WITH BUCKET_COUNT = 8,
USING HASH WITH (bucket_count=8),
INDEX "fingerprint_stats_idx" (fingerprint_id),
FAMILY "primary" (
crdb_internal_aggregated_ts_app_name_fingerprint_id_node_id_shard_8,
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/tabledesc/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestIndexInterface(t *testing.T) {
INDEX s1 (c4 DESC, c5 DESC),
INVERTED INDEX s2 (c6),
INDEX s3 (c2, c3) STORING (c5, c6),
INDEX s4 (c5) USING HASH WITH BUCKET_COUNT=8,
INDEX s4 (c5) USING HASH WITH (bucket_count=8),
UNIQUE INDEX s5 (c1, c4) WHERE c4 = 'x',
INVERTED INDEX s6 (c7) WITH (s2_level_mod=2)
);
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/tabledesc/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func PrimaryKeyString(desc catalog.TableDescriptor) string {
f.WriteByte(')')
if primaryIdx.IsSharded() {
f.WriteString(
fmt.Sprintf(" USING HASH WITH BUCKET_COUNT = %v", primaryIdx.GetSharded().ShardBuckets),
fmt.Sprintf(" USING HASH WITH (bucket_count=%v)", primaryIdx.GetSharded().ShardBuckets),
)
}
return f.CloseAndGetString()
Expand Down
43 changes: 22 additions & 21 deletions pkg/sql/logictest/testdata/logic_test/alter_primary_key
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ CREATE TABLE t (
UNIQUE INDEX i4 (z), -- will be rewritten.
UNIQUE INDEX i5 (w) STORING (y), -- will be rewritten.
INVERTED INDEX i6 (v), -- will be rewritten.
INDEX i7 (z) USING HASH WITH BUCKET_COUNT = 4, -- will be rewritten.
INDEX i7 (z) USING HASH WITH (bucket_count=4), -- will be rewritten.
FAMILY (x, y, z, w, v)
);
INSERT INTO t VALUES (1, 2, 3, 4, '{}');
Expand All @@ -217,7 +217,7 @@ t CREATE TABLE public.t (
UNIQUE INDEX i4 (z ASC),
UNIQUE INDEX i5 (w ASC) STORING (y),
INVERTED INDEX i6 (v),
INDEX i7 (z ASC) USING HASH WITH BUCKET_COUNT = 4,
INDEX i7 (z ASC) USING HASH WITH (bucket_count=4),
FAMILY fam_0_x_y_z_w_v (x, y, z, w, v)
)

Expand Down Expand Up @@ -349,11 +349,11 @@ CREATE TABLE t (
x INT PRIMARY KEY,
y INT NOT NULL,
z INT,
INDEX i1 (z) USING HASH WITH BUCKET_COUNT = 5,
INDEX i1 (z) USING HASH WITH (bucket_count=5),
FAMILY (x, y, z)
);
INSERT INTO t VALUES (1, 2, 3);
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y) USING HASH WITH BUCKET_COUNT = 10
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y) USING HASH WITH (bucket_count=10)

query TT
SHOW CREATE t
Expand All @@ -364,9 +364,9 @@ t CREATE TABLE public.t (
z INT8 NULL,
crdb_internal_z_shard_5 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(z)), 5:::INT8)) VIRTUAL,
crdb_internal_y_shard_10 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(y)), 10:::INT8)) VIRTUAL,
CONSTRAINT t_pkey PRIMARY KEY (y ASC) USING HASH WITH BUCKET_COUNT = 10,
CONSTRAINT t_pkey PRIMARY KEY (y ASC) USING HASH WITH (bucket_count=10),
UNIQUE INDEX t_x_key (x ASC),
INDEX i1 (z ASC) USING HASH WITH BUCKET_COUNT = 5,
INDEX i1 (z ASC) USING HASH WITH (bucket_count=5),
FAMILY fam_0_x_y_z (x, y, z)
)

Expand Down Expand Up @@ -406,7 +406,7 @@ SELECT * FROM t@i1
statement ok
DROP TABLE IF EXISTS t;
CREATE TABLE t (
x INT PRIMARY KEY USING HASH WITH BUCKET_COUNT=5,
x INT PRIMARY KEY USING HASH WITH (bucket_count=5),
y INT NOT NULL,
z INT,
INDEX i (z),
Expand All @@ -424,7 +424,7 @@ t CREATE TABLE public.t (
y INT8 NOT NULL,
z INT8 NULL,
CONSTRAINT t_pkey PRIMARY KEY (y ASC),
UNIQUE INDEX t_x_key (x ASC) USING HASH WITH BUCKET_COUNT = 5,
UNIQUE INDEX t_x_key (x ASC) USING HASH WITH (bucket_count=5),
INDEX i (z ASC),
FAMILY fam_0_x_y_z (x, y, z)
)
Expand Down Expand Up @@ -542,7 +542,7 @@ t CREATE TABLE public.t (
statement ok
DROP TABLE IF EXISTS t;
CREATE TABLE t (x INT NOT NULL);
ALTER TABLE t ADD PRIMARY KEY (x) USING HASH WITH BUCKET_COUNT=4
ALTER TABLE t ADD PRIMARY KEY (x) USING HASH WITH (bucket_count=4)

query TT
SHOW CREATE t
Expand All @@ -551,7 +551,7 @@ t CREATE TABLE public.t (
x INT8 NOT NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
crdb_internal_x_shard_4 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(x)), 4:::INT8)) VIRTUAL,
CONSTRAINT t_pkey PRIMARY KEY (x ASC) USING HASH WITH BUCKET_COUNT = 4,
CONSTRAINT t_pkey PRIMARY KEY (x ASC) USING HASH WITH (bucket_count=4),
FAMILY "primary" (x, rowid)
)

Expand Down Expand Up @@ -934,8 +934,8 @@ SELECT index_id, index_name FROM crdb_internal.table_indexes WHERE descriptor_na
# count shouldn't cause the old primary key to be copied.
statement ok
DROP TABLE IF EXISTS t CASCADE;
CREATE TABLE t (x INT PRIMARY KEY USING HASH WITH BUCKET_COUNT = 2);
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (x) USING HASH WITH BUCKET_COUNT=3
CREATE TABLE t (x INT PRIMARY KEY USING HASH WITH (bucket_count=2));
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (x) USING HASH WITH (bucket_count=3)

query TT
SHOW CREATE t
Expand All @@ -944,16 +944,16 @@ t CREATE TABLE public.t (
crdb_internal_x_shard_2 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(x)), 2:::INT8)) VIRTUAL,
x INT8 NOT NULL,
crdb_internal_x_shard_3 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(x)), 3:::INT8)) VIRTUAL,
CONSTRAINT t_pkey PRIMARY KEY (x ASC) USING HASH WITH BUCKET_COUNT = 3,
CONSTRAINT t_pkey PRIMARY KEY (x ASC) USING HASH WITH (bucket_count=3),
FAMILY "primary" (x)
)

# Changes on a hash sharded index that change the columns will cause the old
# primary key to be copied.
statement ok
DROP TABLE t;
CREATE TABLE t (x INT PRIMARY KEY USING HASH WITH BUCKET_COUNT = 2, y INT NOT NULL, FAMILY (x, y));
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y) USING HASH WITH BUCKET_COUNT=2
CREATE TABLE t (x INT PRIMARY KEY USING HASH WITH (bucket_count=2), y INT NOT NULL, FAMILY (x, y));
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y) USING HASH WITH (bucket_count=2)

query TT
SHOW CREATE t
Expand All @@ -963,8 +963,8 @@ t CREATE TABLE public.t (
x INT8 NOT NULL,
y INT8 NOT NULL,
crdb_internal_y_shard_2 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(y)), 2:::INT8)) VIRTUAL,
CONSTRAINT t_pkey PRIMARY KEY (y ASC) USING HASH WITH BUCKET_COUNT = 2,
UNIQUE INDEX t_x_key (x ASC) USING HASH WITH BUCKET_COUNT = 2,
CONSTRAINT t_pkey PRIMARY KEY (y ASC) USING HASH WITH (bucket_count=2),
UNIQUE INDEX t_x_key (x ASC) USING HASH WITH (bucket_count=2),
FAMILY fam_0_x_y (x, y)
)

Expand Down Expand Up @@ -1096,7 +1096,7 @@ 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
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];
Expand Down Expand Up @@ -1149,7 +1149,7 @@ SELECT index_name,column_name,direction FROM [SHOW INDEXES FROM t]
t_pkey i ASC

statement ok
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (i) USING HASH WITH BUCKET_COUNT = 2
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (i) USING HASH WITH (bucket_count=2)

query TTT
SELECT index_name,column_name,direction FROM [SHOW INDEXES FROM t]
Expand Down Expand Up @@ -1385,8 +1385,9 @@ ALTER TABLE t_test_param ALTER PRIMARY KEY USING COLUMNS (b) WITH (bucket_count=
statement error pq: "bucket_count" storage parameter and "BUCKET_COUNT" cannot be set at the same time
ALTER TABLE t_test_param ALTER PRIMARY KEY USING COLUMNS (b) USING HASH WITH BUCKET_COUNT = 5 WITH (bucket_count=5);

# Make sure old BUCKET_COUNT syntax still works
statement ok
ALTER TABLE t_test_param ALTER PRIMARY KEY USING COLUMNS (b) USING HASH WITH (bucket_count=5);
ALTER TABLE t_test_param ALTER PRIMARY KEY USING COLUMNS (b) USING HASH WITH BUCKET_COUNT = 5;

query T
SELECT @2 FROM [SHOW CREATE TABLE t_test_param]
Expand All @@ -1395,7 +1396,7 @@ CREATE TABLE public.t_test_param (
a INT8 NOT NULL,
b INT8 NOT NULL,
crdb_internal_b_shard_5 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 5:::INT8)) VIRTUAL,
CONSTRAINT t_test_param_pkey PRIMARY KEY (b ASC) USING HASH WITH BUCKET_COUNT = 5,
CONSTRAINT t_test_param_pkey PRIMARY KEY (b ASC) USING HASH WITH (bucket_count=5),
UNIQUE INDEX t_test_param_a_key (a ASC),
FAMILY fam_0_a_b (a, b)
)
12 changes: 6 additions & 6 deletions pkg/sql/logictest/testdata/logic_test/create_index
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ CREATE TABLE telemetry (

statement ok
CREATE INVERTED INDEX ON telemetry (z);
CREATE INDEX ON telemetry (y) USING HASH WITH BUCKET_COUNT = 4
CREATE INDEX ON telemetry (y) USING HASH WITH (bucket_count=4)

query T rowsort
SELECT feature_name FROM crdb_internal.feature_usage
Expand Down Expand Up @@ -363,7 +363,7 @@ SET CLUSTER SETTING jobs.registry.interval.cancel = DEFAULT;
statement ok
SET experimental_use_new_schema_changer = $schema_changer_state

subtest test_bucket_count_storage_param
subtest test_old_bucket_count_syntax

statement ok
CREATE TABLE t_hash (
Expand All @@ -374,10 +374,10 @@ CREATE TABLE t_hash (
);

statement ok
CREATE INDEX idx_t_hash_a ON t_hash (a) USING HASH WITH (bucket_count=5);
CREATE INDEX idx_t_hash_a ON t_hash (a) USING HASH WITH BUCKET_COUNT = 5;

statement ok
CREATE UNIQUE INDEX idx_t_hash_b ON t_hash (b) USING HASH WITH (bucket_count=5);
CREATE UNIQUE INDEX idx_t_hash_b ON t_hash (b) USING HASH WITH BUCKET_COUNT = 5;

query T
SELECT @2 FROM [SHOW CREATE TABLE t_hash]
Expand All @@ -389,7 +389,7 @@ CREATE TABLE public.t_hash (
crdb_internal_a_shard_5 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(a)), 5:::INT8)) VIRTUAL,
crdb_internal_b_shard_5 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 5:::INT8)) VIRTUAL,
CONSTRAINT t_hash_pkey PRIMARY KEY (pk ASC),
INDEX idx_t_hash_a (a ASC) USING HASH WITH BUCKET_COUNT = 5,
UNIQUE INDEX idx_t_hash_b (b ASC) USING HASH WITH BUCKET_COUNT = 5,
INDEX idx_t_hash_a (a ASC) USING HASH WITH (bucket_count=5),
UNIQUE INDEX idx_t_hash_b (b ASC) USING HASH WITH (bucket_count=5),
FAMILY fam_0_pk_a_b (pk, a, b)
)
Loading

0 comments on commit 9c2434a

Please sign in to comment.