Skip to content

Commit

Permalink
Merge #73341 #73342
Browse files Browse the repository at this point in the history
73341: randgen: fix Postgres mutator to handle expression-based indexes r=rail a=rafiss

Release note: None

73342: kv/kvserver/uncertainty: remove observedts_test target from bazel build file r=chengxiong-ruan a=chengxiong-ruan

The test files have been deleted. Leaving observedts_test
target in build file fails CI for some PRs :(

Co-authored-by: Rafi Shamim <[email protected]>
Co-authored-by: Chengxiong Ruan <[email protected]>
  • Loading branch information
3 people committed Dec 1, 2021
3 parents 934ff47 + 15e31d9 + 547bfce commit 2559c4b
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 40 deletions.
1 change: 0 additions & 1 deletion pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ ALL_TESTS = [
"//pkg/kv/kvserver/tscache:tscache_test",
"//pkg/kv/kvserver/txnrecovery:txnrecovery_test",
"//pkg/kv/kvserver/txnwait:txnwait_test",
"//pkg/kv/kvserver/uncertainty:observedts_test",
"//pkg/kv/kvserver/uncertainty:uncertainty_test",
"//pkg/kv/kvserver:kvserver_test",
"//pkg/kv:kv_test",
Expand Down
33 changes: 0 additions & 33 deletions pkg/kv/kvserver/uncertainty/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,38 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "observedts",
srcs = [
"doc.go",
"limit.go",
"uncertainty.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvserver/observedts",
visibility = ["//visibility:public"],
deps = [
"//pkg/kv/kvserver/kvserverpb",
"//pkg/roachpb:with-mocks",
"//pkg/util/hlc",
],
)

go_test(
name = "observedts_test",
size = "small",
srcs = [
"limit_test.go",
"uncertainty_test.go",
],
embed = [":observedts"],
deps = [
"//pkg/kv/kvserver/kvserverpb",
"//pkg/roachpb:with-mocks",
"//pkg/util/hlc",
"//pkg/util/leaktest",
"@com_github_stretchr_testify//require",
],
)

go_library(
name = "uncertainty",
srcs = [
Expand Down
26 changes: 20 additions & 6 deletions pkg/sql/randgen/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,16 @@ func postgresCreateTableMutator(
// to their own statement.
var newCols tree.IndexElemList
for _, col := range def.Columns {
// Postgres doesn't support box2d as a btree index key.
colTypeFamily := colTypes[string(col.Column)].Family()
if colTypeFamily == types.Box2DFamily {
isBox2d := false
// NB: col.Column is empty for expression-based indexes.
if col.Expr == nil {
// Postgres doesn't support box2d as a btree index key.
colTypeFamily := colTypes[string(col.Column)].Family()
if colTypeFamily == types.Box2DFamily {
isBox2d = true
}
}
if isBox2d {
changed = true
} else {
newCols = append(newCols, col)
Expand All @@ -752,9 +759,16 @@ func postgresCreateTableMutator(
case *tree.UniqueConstraintTableDef:
var newCols tree.IndexElemList
for _, col := range def.Columns {
// Postgres doesn't support box2d as a btree index key.
colTypeFamily := colTypes[string(col.Column)].Family()
if colTypeFamily == types.Box2DFamily {
isBox2d := false
// NB: col.Column is empty for expression-based indexes.
if col.Expr == nil {
// Postgres doesn't support box2d as a btree index key.
colTypeFamily := colTypes[string(col.Column)].Family()
if colTypeFamily == types.Box2DFamily {
isBox2d = true
}
}
if isBox2d {
changed = true
} else {
newCols = append(newCols, col)
Expand Down
145 changes: 145 additions & 0 deletions pkg/sql/randgen/mutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,151 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/randutil"
)

func TestPostgresCreateTableMutator(t *testing.T) {
q := `
CREATE TABLE table1 (
col1_0
TIMESTAMP,
col1_1
REGPROC,
col1_2
BOX2D NOT NULL,
col1_3
"char" NOT NULL,
col1_4
GEOGRAPHY NULL,
col1_5
GEOGRAPHY,
col1_6
REGROLE NOT NULL,
col1_7
REGROLE NOT NULL,
col1_8
"char",
col1_9
INTERVAL,
col1_10
STRING AS (lower(col1_3)) STORED NOT NULL,
col1_11
STRING AS (lower(CAST(col1_1 AS STRING))) STORED,
col1_12
STRING AS (lower(CAST(col1_5 AS STRING))) STORED,
col1_13
STRING AS (lower(CAST(col1_0 AS STRING))) VIRTUAL,
col1_14
STRING AS (lower(CAST(col1_4 AS STRING))) STORED NULL,
col1_15
STRING AS (lower(CAST(col1_7 AS STRING))) STORED NOT NULL,
col1_16
STRING AS (lower(CAST(col1_0 AS STRING))) STORED,
INDEX (col1_9, col1_1, col1_12 ASC)
WHERE
(
(table1.col1_8 != 'X':::STRING OR table1.col1_16 < e'\x00':::STRING)
AND table1.col1_15 <= '':::STRING
)
AND table1.col1_14 >= e'\U00002603':::STRING,
UNIQUE (
col1_1 ASC,
lower(CAST(col1_5 AS STRING)) ASC,
col1_0,
col1_10,
col1_11 ASC,
col1_12 ASC,
col1_2 ASC,
col1_7,
col1_3,
col1_6,
col1_16
)
WHERE
(
(
(
(
(
(
(
(
table1.col1_0 < '-4713-11-24 00:00:00':::TIMESTAMP
AND table1.col1_14 = e'\'':::STRING
)
AND table1.col1_3 = '"':::STRING
)
OR table1.col1_8 >= e'\U00002603':::STRING
)
OR table1.col1_13 > '':::STRING
)
AND table1.col1_12 < '':::STRING
)
OR table1.col1_11 >= 'X':::STRING
)
AND table1.col1_10 <= e'\'':::STRING
)
OR table1.col1_16 != '':::STRING
)
OR table1.col1_15 >= '':::STRING,
UNIQUE (col1_1, col1_16 DESC, col1_6, col1_11 DESC, col1_9 ASC, col1_3 DESC, col1_2 ASC),
UNIQUE (col1_14 DESC, col1_15 DESC)
WHERE
(
(
(
(
(
(
(
(table1.col1_14 >= e'\'':::STRING OR table1.col1_16 >= 'X':::STRING)
OR table1.col1_3 <= 'X':::STRING
)
AND table1.col1_15 = 'X':::STRING
)
OR table1.col1_13 > '"':::STRING
)
OR table1.col1_12 >= '':::STRING
)
AND table1.col1_11 != '':::STRING
)
OR table1.col1_10 != e'\U00002603':::STRING
)
AND table1.col1_8 != e'\U00002603':::STRING
)
OR table1.col1_0 <= '-4713-11-24 00:00:00':::TIMESTAMP,
INVERTED INDEX (
col1_13,
col1_16,
col1_12,
col1_8,
col1_3 ASC,
col1_6,
col1_2,
col1_7,
col1_0 DESC,
lower(CAST(col1_7 AS STRING)) DESC,
col1_1,
col1_15 DESC,
col1_4
)
)`
rng, _ := randutil.NewTestRand()
// Set a deterministic seed so that PostgresCreateTableMutator performs a
// deterministic transformation.
rng.Seed(123)
mutated, changed := ApplyString(rng, q, PostgresCreateTableMutator)
if !changed {
t.Fatal("expected changed")
}
expect := `
CREATE TABLE table1 (col1_0 TIMESTAMP, col1_1 REGPROC, col1_2 BOX2D NOT NULL, col1_3 "char" NOT NULL, col1_4 GEOGRAPHY NULL, col1_5 GEOGRAPHY, col1_6 REGROLE NOT NULL, col1_7 REGROLE NOT NULL, col1_8 "char", col1_9 INTERVAL, col1_10 STRING NOT NULL AS (lower(col1_3)) STORED, col1_11 STRING AS (CASE WHEN col1_1 IS NULL THEN 'L*h':::STRING ELSE '#':::STRING END) STORED, col1_12 STRING AS (lower(CAST(col1_5 AS STRING))) STORED, col1_13 STRING AS (CASE WHEN col1_0 IS NULL THEN e'\x1c\t':::STRING ELSE e'(,Zh\x05\x1dW':::STRING END) VIRTUAL, col1_14 STRING NULL AS (lower(CAST(col1_4 AS STRING))) STORED, col1_15 STRING NOT NULL AS (CASE WHEN col1_7 IS NULL THEN e'\x0e,\x162/BJ\x12':::STRING ELSE e'#\x17\x07;\x0emi':::STRING END) STORED, col1_16 STRING AS (CASE WHEN col1_0 IS NULL THEN e'\x1eM\x02\x12_\x05\r':::STRING ELSE e'[jUDO\nt':::STRING END) STORED);
CREATE INDEX ON table1 (col1_9, col1_1, col1_12 ASC);
CREATE UNIQUE INDEX ON table1 (col1_1 ASC, lower(CAST(col1_5 AS STRING)) ASC, col1_0, col1_10, col1_11 ASC, col1_12 ASC, col1_7, col1_3, col1_6, col1_16);
CREATE UNIQUE INDEX ON table1 (col1_1, col1_16 DESC, col1_6, col1_11 DESC, col1_9 ASC, col1_3 DESC);
CREATE UNIQUE INDEX ON table1 (col1_14 DESC, col1_15 DESC);`
if strings.TrimSpace(mutated) != strings.TrimSpace(expect) {
t.Fatalf("expected:\n%s\ngot:\n%s", expect, mutated)
}
}

func TestPostgresMutator(t *testing.T) {
q := `
CREATE TABLE t (s STRING FAMILY fam1, b BYTES, FAMILY fam2 (b), PRIMARY KEY (s ASC, b DESC), INDEX (s) STORING (b))
Expand Down

0 comments on commit 2559c4b

Please sign in to comment.