Skip to content

Commit 7e8456c

Browse files
craig[bot]spilchen
andcommitted
Merge #158045
158045: sql: rename all shard columns when a key column is renamed r=spilchen a=spilchen The `RenameColumnInTable` helper updated the index descriptors for every index that depended on the renamed column, but it returned after renaming the first hidden shard column. As soon as a table had more than one hash-sharded index using the same key columns, subsequent shard columns kept their old names. This was a problem with the legacy schema changer and DSC. This commit fixes `RenameColumnInTable` to iterate over every shard column that needs to be renamed. Closes #156907 Release note (bug fix): Renaming a column that participates in multiple hash-sharded indexes no longer fails. Co-authored-by: Matt Spilchen <[email protected]>
2 parents 4d1a96a + 7348a5c commit 7e8456c

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

pkg/sql/catalog/tabledesc/table.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,12 +622,14 @@ func RenameColumnInTable(
622622
return err
623623
}
624624
if !canBeRenamed {
625-
return nil
625+
continue
626626
}
627627
// Recursively rename the shard column.
628628
// We don't need to worry about deeper than one recursive call because
629629
// shard columns cannot refer to each other.
630-
return RenameColumnInTable(tableDesc, shardCol, newShardColName, nil /* isShardColumnRenameable */)
630+
if err := RenameColumnInTable(tableDesc, shardCol, newShardColName, nil /* isShardColumnRenameable */); err != nil {
631+
return err
632+
}
631633
}
632634

633635
return nil

pkg/sql/logictest/testdata/logic_test/rename_column

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,56 @@ CREATE TABLE public.rename_add_alter_pk_tbl (
305305
UNIQUE INDEX rename_add_alter_pk_tbl_b_orig_key (b_orig ASC),
306306
FAMILY f (b_orig, b, a, b_old)
307307
) WITH (schema_locked = true);
308+
309+
subtest rename_multipl_shard_col
310+
311+
statement ok
312+
DROP TABLE IF EXISTS tab1
313+
314+
# Create a table that has more than 1 shard column.
315+
statement ok
316+
CREATE TABLE tab1 (
317+
a INT NOT NULL,
318+
b DATE NOT NULL,
319+
c INT NOT NULL,
320+
d INT NOT NULL,
321+
PRIMARY KEY (d, b, a) USING HASH WITH BUCKET_COUNT = 16,
322+
UNIQUE INDEX (d, b, a, c) USING HASH WITH BUCKET_COUNT = 16,
323+
FAMILY f1 (a,b,c,d)
324+
) WITH (schema_locked = false);
325+
326+
query TT
327+
SHOW CREATE TABLE tab1;
328+
----
329+
tab1 CREATE TABLE public.tab1 (
330+
a INT8 NOT NULL,
331+
b DATE NOT NULL,
332+
c INT8 NOT NULL,
333+
d INT8 NOT NULL,
334+
crdb_internal_a_b_d_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(md5(crdb_internal.datums_to_bytes(a, b, d))), 16:::INT8)) VIRTUAL,
335+
crdb_internal_a_b_c_d_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(md5(crdb_internal.datums_to_bytes(a, b, c, d))), 16:::INT8)) VIRTUAL,
336+
CONSTRAINT tab1_pkey PRIMARY KEY (d ASC, b ASC, a ASC) USING HASH WITH (bucket_count=16),
337+
UNIQUE INDEX tab1_d_b_a_c_key (d ASC, b ASC, a ASC, c ASC) USING HASH WITH (bucket_count=16),
338+
FAMILY f1 (a, b, c, d)
339+
);
340+
341+
statement ok
342+
ALTER TABLE tab1 RENAME COLUMN d TO rename_d
343+
344+
# Ensure rename handled both shard columns.
345+
query TT
346+
SHOW CREATE TABLE tab1;
347+
----
348+
tab1 CREATE TABLE public.tab1 (
349+
a INT8 NOT NULL,
350+
b DATE NOT NULL,
351+
c INT8 NOT NULL,
352+
rename_d INT8 NOT NULL,
353+
crdb_internal_a_b_rename_d_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(md5(crdb_internal.datums_to_bytes(a, b, rename_d))), 16:::INT8)) VIRTUAL,
354+
crdb_internal_a_b_c_rename_d_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(md5(crdb_internal.datums_to_bytes(a, b, c, rename_d))), 16:::INT8)) VIRTUAL,
355+
CONSTRAINT tab1_pkey PRIMARY KEY (rename_d ASC, b ASC, a ASC) USING HASH WITH (bucket_count=16),
356+
UNIQUE INDEX tab1_d_b_a_c_key (rename_d ASC, b ASC, a ASC, c ASC) USING HASH WITH (bucket_count=16),
357+
FAMILY f1 (a, b, c, rename_d)
358+
);
359+
360+
subtest end

0 commit comments

Comments
 (0)