Skip to content

Commit

Permalink
Merge #59121
Browse files Browse the repository at this point in the history
59121: sql: implement REGIONAL BY ROW AS r=ajstorm a=otan

Release note (sql change): Implement REGIONAL BY ROW AS ..., which
allows a column of type crdb_internal_region to be used as a column for
REGIONAL BY ROW multi-region properties.

Co-authored-by: Oliver Tan <[email protected]>
  • Loading branch information
craig[bot] and otan committed Jan 20, 2021
2 parents 3d01fda + 5d435d8 commit c6dff23
Show file tree
Hide file tree
Showing 8 changed files with 638 additions and 418 deletions.
14 changes: 14 additions & 0 deletions pkg/ccl/importccl/import_table_creation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/storage/cloud"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/errors"
"github.com/lib/pq/oid"
)

const (
Expand Down Expand Up @@ -318,3 +320,15 @@ func (r fkResolver) LookupTableByID(
) (*tabledesc.Immutable, error) {
return nil, errSchemaResolver
}

// Implements the sql.SchemaResolver interface.
func (r fkResolver) ResolveTypeByOID(ctx context.Context, oid oid.Oid) (*types.T, error) {
return nil, errSchemaResolver
}

// Implements the sql.SchemaResolver interface.
func (r fkResolver) ResolveType(
ctx context.Context, name *tree.UnresolvedObjectName,
) (*types.T, error) {
return nil, errSchemaResolver
}
94 changes: 92 additions & 2 deletions pkg/ccl/logictestccl/testdata/logic_test/regional_by_row
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ CREATE TABLE regional_by_row_table (
PARTITION BY LIST (pk) (PARTITION one VALUES IN ((1)))
LOCALITY REGIONAL BY ROW

statement error REGIONAL BY ROW AS is not yet supported
CREATE TABLE regional_by_row_table_on_col (a int) LOCALITY REGIONAL BY ROW AS a
statement error cannot specify crdb_region column in REGIONAL BY ROW table as the column is implicitly created by the system
CREATE TABLE regional_by_row_table (
pk int,
a int,
crdb_region crdb_internal_region
)
LOCALITY REGIONAL BY ROW

statement error REGIONAL BY ROW on a table with an INDEX containing PARTITION BY is not supported
CREATE TABLE regional_by_row_table (
Expand Down Expand Up @@ -170,6 +175,91 @@ ca-central-1 10 11 12
us-east-1 20 21 22
ap-southeast-2 23 24 25

statement error cannot use column crdb_region_col which has type INT8 in REGIONAL BY ROW AS\nDETAIL:\s+REGIONAL BY ROW AS must reference a column of type crdb_internal_region.
CREATE TABLE regional_by_row_table_as (
pk int PRIMARY KEY,
crdb_region_col int
) LOCALITY REGIONAL BY ROW AS crdb_region_col

statement error column no_exist_col in REGIONAL BY ROW AS does not exist
CREATE TABLE regional_by_row_table_as (
pk int PRIMARY KEY
) LOCALITY REGIONAL BY ROW AS no_exist_col

statement ok
CREATE TABLE regional_by_row_table_as (
pk int PRIMARY KEY,
a int,
b int,
crdb_region_col crdb_internal_region AS (
CASE
WHEN pk <= 10 THEN 'us-east-1'
ELSE 'ap-southeast-2'
END
) STORED,
INDEX (a),
UNIQUE (b),
FAMILY (pk, a, b)
) LOCALITY REGIONAL BY ROW AS crdb_region_col

query T
SELECT create_statement FROM [SHOW CREATE TABLE regional_by_row_table_as]
----
CREATE TABLE public.regional_by_row_table_as (
pk INT8 NOT NULL,
a INT8 NULL,
b INT8 NULL,
crdb_region_col public.crdb_internal_region NULL AS (CASE WHEN pk <= 10:::INT8 THEN 'us-east-1':::public.crdb_internal_region ELSE 'ap-southeast-2':::public.crdb_internal_region END) STORED,
CONSTRAINT "primary" PRIMARY KEY (pk ASC),
INDEX regional_by_row_table_as_a_idx (a ASC),
UNIQUE INDEX regional_by_row_table_as_b_key (b ASC),
FAMILY fam_0_pk_a_b_crdb_region_col (pk, a, b, crdb_region_col)
) LOCALITY REGIONAL BY ROW AS crdb_region_col;
ALTER PARTITION "ap-southeast-2" OF INDEX multi_region_test_db.public.regional_by_row_table_as@regional_by_row_table_as_a_idx CONFIGURE ZONE USING
num_replicas = 3,
constraints = '{+region=ap-southeast-2: 1}',
lease_preferences = '[[+region=ap-southeast-2]]';
ALTER PARTITION "ap-southeast-2" OF INDEX multi_region_test_db.public.regional_by_row_table_as@regional_by_row_table_as_b_key CONFIGURE ZONE USING
num_replicas = 3,
constraints = '{+region=ap-southeast-2: 1}',
lease_preferences = '[[+region=ap-southeast-2]]';
ALTER PARTITION "ap-southeast-2" OF INDEX multi_region_test_db.public.regional_by_row_table_as@primary CONFIGURE ZONE USING
num_replicas = 3,
constraints = '{+region=ap-southeast-2: 1}',
lease_preferences = '[[+region=ap-southeast-2]]';
ALTER PARTITION "ca-central-1" OF INDEX multi_region_test_db.public.regional_by_row_table_as@regional_by_row_table_as_a_idx CONFIGURE ZONE USING
num_replicas = 3,
constraints = '{+region=ca-central-1: 1}',
lease_preferences = '[[+region=ca-central-1]]';
ALTER PARTITION "ca-central-1" OF INDEX multi_region_test_db.public.regional_by_row_table_as@regional_by_row_table_as_b_key CONFIGURE ZONE USING
num_replicas = 3,
constraints = '{+region=ca-central-1: 1}',
lease_preferences = '[[+region=ca-central-1]]';
ALTER PARTITION "ca-central-1" OF INDEX multi_region_test_db.public.regional_by_row_table_as@primary CONFIGURE ZONE USING
num_replicas = 3,
constraints = '{+region=ca-central-1: 1}',
lease_preferences = '[[+region=ca-central-1]]';
ALTER PARTITION "us-east-1" OF INDEX multi_region_test_db.public.regional_by_row_table_as@regional_by_row_table_as_a_idx CONFIGURE ZONE USING
num_replicas = 3,
constraints = '{+region=us-east-1: 1}',
lease_preferences = '[[+region=us-east-1]]';
ALTER PARTITION "us-east-1" OF INDEX multi_region_test_db.public.regional_by_row_table_as@regional_by_row_table_as_b_key CONFIGURE ZONE USING
num_replicas = 3,
constraints = '{+region=us-east-1: 1}',
lease_preferences = '[[+region=us-east-1]]';
ALTER PARTITION "us-east-1" OF INDEX multi_region_test_db.public.regional_by_row_table_as@primary CONFIGURE ZONE USING
num_replicas = 3,
constraints = '{+region=us-east-1: 1}',
lease_preferences = '[[+region=us-east-1]]'

query TI
INSERT INTO regional_by_row_table_as (pk) VALUES (1), (10), (20)
RETURNING crdb_region_col, pk
----
us-east-1 1
us-east-1 10
ap-southeast-2 20

# Create some tables to validate that their zone configurations are adjusted appropriately.
statement ok
CREATE DATABASE alter_survive_db PRIMARY REGION "us-east-1" REGIONS "ca-central-1", "ap-southeast-2" SURVIVE REGION FAILURE
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ go_library(
"@com_github_lib_pq//oid",
"@com_github_prometheus_client_model//go",
"@in_gopkg_yaml_v2//:yaml_v2",
"@org_golang_google_protobuf//proto",
"@org_golang_x_net//trace",
"@org_golang_x_text//collate",
],
Expand Down
Loading

0 comments on commit c6dff23

Please sign in to comment.