Skip to content

Commit

Permalink
sql: set hash-sharded index column type to INT8
Browse files Browse the repository at this point in the history
Shard columns of hash-sharded indexes are computed columns with an
expression in the form `mod(fnv32(crdb_internal.datums_to_bytes(...)))`.
The `mod()` builtin returns a value of type `INT8`, while the shard
column's type was previously `INT4`. Because these types did not match,
`mod` expressions were wrapped in the assignment cast function,
`crdb_internal.assignment_cast`, in mutation query plans.

This commit changes the type of newly created shard columns to `INT8`,
eliminating the need for an assignment cast. Because all integers are
encoded as varints in keys and values, this will not increase the amount
of space on disk required for these columns.

Release justification: This is a minor change to hash-sharded indexes,
which are a newly un-experimentalized feature in the upcoming release.

Release note (sql change): The type of shard columns created for
hash-sharded indexes have changed from `INT4` to `INT8`. This should
have no effect on behavior or performance.
  • Loading branch information
mgartner committed Mar 8, 2022
1 parent 8467577 commit fde3881
Show file tree
Hide file tree
Showing 11 changed files with 522 additions and 522 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ CREATE TABLE public.t_to_be_hashed (
a INT8 NOT NULL,
b STRING NOT NULL,
c INT8 NULL,
crdb_internal_c_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(c)), 16:::INT8)) VIRTUAL,
crdb_internal_c_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(c)), 16:::INT8)) VIRTUAL,
CONSTRAINT t_to_be_hashed_pkey PRIMARY KEY (a ASC),
INDEX t_to_be_hashed_c_idx (c ASC) USING HASH WITH (bucket_count=16),
FAMILY fam_0_a_b_c (a, b, c)
Expand All @@ -146,7 +146,7 @@ CREATE TABLE public.t_to_be_hashed (
a INT8 NOT NULL,
b STRING NOT NULL,
c INT8 NULL,
crdb_internal_c_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(c)), 16:::INT8)) VIRTUAL,
crdb_internal_c_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(c)), 16:::INT8)) VIRTUAL,
CONSTRAINT t_to_be_hashed_pkey PRIMARY KEY (a ASC),
INDEX t_to_be_hashed_c_idx (c ASC) USING HASH WITH (bucket_count=16),
UNIQUE INDEX t_to_be_hashed_c_key (c ASC) USING HASH WITH (bucket_count=16),
Expand All @@ -167,8 +167,8 @@ CREATE TABLE public.t_to_be_hashed (
a INT8 NOT NULL,
b STRING NOT NULL,
c INT8 NULL,
crdb_internal_c_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(c)), 16:::INT8)) VIRTUAL,
crdb_internal_a_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(a)), 16:::INT8)) VIRTUAL,
crdb_internal_c_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(c)), 16:::INT8)) VIRTUAL,
crdb_internal_a_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(a)), 16:::INT8)) VIRTUAL,
CONSTRAINT t_to_be_hashed_pkey PRIMARY KEY (a ASC) USING HASH WITH (bucket_count=16),
INDEX t_to_be_hashed_c_idx (c ASC) USING HASH WITH (bucket_count=16),
UNIQUE INDEX t_to_be_hashed_c_key (c ASC) USING HASH WITH (bucket_count=16),
Expand All @@ -195,11 +195,11 @@ query T
SELECT @2 FROM [SHOW CREATE TABLE t_idx_pk_hashed_1];
----
CREATE TABLE public.t_idx_pk_hashed_1 (
crdb_internal_a_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(a)), 16:::INT8)) VIRTUAL,
crdb_internal_a_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(a)), 16:::INT8)) VIRTUAL,
a INT8 NOT NULL,
b STRING NOT NULL,
c INT8 NULL,
crdb_internal_c_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(c)), 16:::INT8)) VIRTUAL,
crdb_internal_c_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(c)), 16:::INT8)) VIRTUAL,
CONSTRAINT t_idx_pk_hashed_1_pkey PRIMARY KEY (a ASC) USING HASH WITH (bucket_count=16),
INDEX t_idx_pk_hashed_1_c_idx (c ASC) USING HASH WITH (bucket_count=16),
FAMILY fam_0_a_b_c (a, b, c)
Expand Down Expand Up @@ -229,8 +229,8 @@ CREATE TABLE public.t_idx_pk_hashed_2 (
a INT8 NOT NULL,
b STRING NOT NULL,
c INT8 NULL,
crdb_internal_c_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(c)), 16:::INT8)) VIRTUAL,
crdb_internal_a_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(a)), 16:::INT8)) VIRTUAL,
crdb_internal_c_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(c)), 16:::INT8)) VIRTUAL,
crdb_internal_a_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(a)), 16:::INT8)) VIRTUAL,
CONSTRAINT t_idx_pk_hashed_2_pkey PRIMARY KEY (a ASC) USING HASH WITH (bucket_count=16),
INDEX t_idx_pk_hashed_2_c_idx (c ASC) USING HASH WITH (bucket_count=16),
FAMILY fam_0_a_b_c (a, b, c)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ CREATE TABLE public.t_to_be_hashed (
pk INT8 NOT NULL,
b INT8 NULL,
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
crdb_internal_b_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL,
crdb_internal_b_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL,
CONSTRAINT t_to_be_hashed_pkey PRIMARY KEY (pk ASC),
INDEX t_to_be_hashed_b_idx (b ASC) USING HASH WITH (bucket_count=16),
FAMILY fam_0_b_pk_crdb_region (b, pk, crdb_region)
Expand All @@ -79,7 +79,7 @@ CREATE TABLE public.t_to_be_hashed (
pk INT8 NOT NULL,
b INT8 NULL,
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
crdb_internal_b_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL,
crdb_internal_b_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL,
CONSTRAINT t_to_be_hashed_pkey PRIMARY KEY (pk ASC),
INDEX t_to_be_hashed_b_idx (b ASC) USING HASH WITH (bucket_count=16),
UNIQUE INDEX t_to_be_hashed_b_key (b ASC) USING HASH WITH (bucket_count=16),
Expand All @@ -96,8 +96,8 @@ CREATE TABLE public.t_to_be_hashed (
pk INT8 NOT NULL,
b INT8 NULL,
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
crdb_internal_b_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL,
crdb_internal_pk_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(pk)), 16:::INT8)) VIRTUAL,
crdb_internal_b_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL,
crdb_internal_pk_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(pk)), 16:::INT8)) VIRTUAL,
CONSTRAINT t_to_be_hashed_pkey PRIMARY KEY (pk ASC) USING HASH WITH (bucket_count=16),
INDEX t_to_be_hashed_b_idx (b ASC) USING HASH WITH (bucket_count=16),
UNIQUE INDEX t_to_be_hashed_b_key (b ASC) USING HASH WITH (bucket_count=16),
Expand All @@ -116,11 +116,11 @@ query T
SELECT @2 FROM [SHOW CREATE TABLE t_regional_pk_hashed_1];
----
CREATE TABLE public.t_regional_pk_hashed_1 (
crdb_internal_pk_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(pk)), 16:::INT8)) VIRTUAL,
crdb_internal_pk_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(pk)), 16:::INT8)) VIRTUAL,
pk INT8 NOT NULL,
b INT8 NULL,
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
crdb_internal_b_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL,
crdb_internal_b_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL,
CONSTRAINT t_regional_pk_hashed_1_pkey PRIMARY KEY (pk ASC) USING HASH WITH (bucket_count=16),
INDEX t_regional_pk_hashed_1_b_idx (b ASC) USING HASH WITH (bucket_count=16),
FAMILY fam_0_b_pk_crdb_region (b, pk, crdb_region)
Expand All @@ -142,8 +142,8 @@ CREATE TABLE public.t_regional_pk_hashed_2 (
pk INT8 NOT NULL,
b INT8 NULL,
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
crdb_internal_pk_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(pk)), 16:::INT8)) VIRTUAL,
crdb_internal_b_shard_16 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL,
crdb_internal_pk_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(pk)), 16:::INT8)) VIRTUAL,
crdb_internal_b_shard_16 INT8 NOT VISIBLE NOT NULL AS (mod(fnv32(crdb_internal.datums_to_bytes(b)), 16:::INT8)) VIRTUAL,
CONSTRAINT t_regional_pk_hashed_2_pkey PRIMARY KEY (pk ASC) USING HASH WITH (bucket_count=16),
INDEX t_regional_pk_hashed_2_b_idx (b ASC) USING HASH WITH (bucket_count=16),
FAMILY fam_0_b_pk_crdb_region (b, pk, crdb_region)
Expand Down
Loading

0 comments on commit fde3881

Please sign in to comment.