Commit c8808ad5 authored by craig[bot]'s avatar craig[bot]

Merge #49565 #49721 #49815

49565: sql: serialize UDTs in expressions in a stable way r=otan,jordanlewis a=rohany

Fixes #49379.

This PR ensures that serialized expressions stored durably in table
descriptors are serialized in a format that is stable across changes to
user defined types present in those expressions. An effect of this
change is that these expressions must be reparsed and formatted in a
human readable way before display in statements like `SHOW CREATE
TABLE`. 

Release note: None

49721: storage: Add rocksdb-vs-pebble benchmark for ExportToSst r=itsbilal a=itsbilal

As part of the investigation into #49710, this change adds a
benchmark for ExportToSst that tests both RocksDB and Pebble.

Here are some example runs without contention (old = rocksdb,
new = pebble):

	name                                                   old time/op  new time/op  delta
	ExportToSst/rocksdb/numKeys=64/numRevisions=1-12       43.9µs ± 3%  34.5µs ± 4%  -21.33%  (p=0.000 n=10+10)
	ExportToSst/rocksdb/numKeys=64/numRevisions=10-12       281µs ± 3%   169µs ± 6%  -39.89%  (p=0.000 n=10+10)
	ExportToSst/rocksdb/numKeys=64/numRevisions=100-12     1.82ms ±22%  1.17ms ± 1%  -35.73%  (p=0.000 n=10+9)
	ExportToSst/rocksdb/numKeys=512/numRevisions=1-12       212µs ± 6%   111µs ± 3%  -47.77%  (p=0.000 n=10+9)
	ExportToSst/rocksdb/numKeys=512/numRevisions=10-12     1.91ms ± 1%  1.19ms ± 8%  -37.65%  (p=0.000 n=10+10)
	ExportToSst/rocksdb/numKeys=512/numRevisions=100-12    13.7ms ± 3%  10.1ms ±12%  -26.21%  (p=0.000 n=10+10)
	ExportToSst/rocksdb/numKeys=1024/numRevisions=1-12      390µs ± 1%   215µs ±12%  -44.94%  (p=0.000 n=10+10)
	ExportToSst/rocksdb/numKeys=1024/numRevisions=10-12    4.01ms ± 6%  2.40ms ±16%  -40.13%  (p=0.000 n=10+9)
	ExportToSst/rocksdb/numKeys=1024/numRevisions=100-12   27.9ms ± 2%  20.8ms ± 2%  -25.48%  (p=0.000 n=10+10)
	ExportToSst/rocksdb/numKeys=8192/numRevisions=1-12     2.97ms ± 2%  1.42ms ± 5%  -52.24%  (p=0.000 n=9+10)
	ExportToSst/rocksdb/numKeys=8192/numRevisions=10-12    32.8ms ± 7%  19.1ms ± 3%  -41.59%  (p=0.000 n=10+10)
	ExportToSst/rocksdb/numKeys=8192/numRevisions=100-12    224ms ± 3%   169ms ±25%  -24.64%  (p=0.000 n=9+10)
	ExportToSst/rocksdb/numKeys=65536/numRevisions=1-12    23.7ms ± 4%  13.4ms ±20%  -43.65%  (p=0.000 n=9+10)
	ExportToSst/rocksdb/numKeys=65536/numRevisions=10-12    264ms ± 4%   201ms ±24%  -23.92%  (p=0.000 n=10+10)
	ExportToSst/rocksdb/numKeys=65536/numRevisions=100-12   1.88s ± 6%   1.23s ± 8%  -34.70%  (p=0.000 n=10+8)

And some with contention=true:

	name                                                                   old time/op  new time/op  delta
	ExportToSst/rocksdb/numKeys=65536/numRevisions=10/contention=true-12    362ms ± 7%   168ms ± 3%  -53.60%  (p=0.000 n=10+10)
	ExportToSst/rocksdb/numKeys=65536/numRevisions=100/contention=true-12   2.24s ± 6%   1.24s ±10%  -44.50%  (p=0.000 n=10+10)

Release note: None.

49815: roachpb: refuse nil desc in NewRangeKeyMismatchError r=andreimatei a=andreimatei

Since recently RangeKeyMismatchError does not support nil descriptors,
but it still had code that pretended to deal with nils (even though a
nil would have exploded a bit later). Only one test caller was passing a
nil, and it turns out that was dead code.

Release note: None
Co-authored-by: default avatarRohan Yadav <[email protected]>
Co-authored-by: default avatarBilal Akhtar <[email protected]>
Co-authored-by: default avatarAndrei Matei <[email protected]>
......@@ -14,7 +14,7 @@ dump d t
----
CREATE TABLE t (
a INT8 NOT NULL,
b INT8 NULL AS (a + 1) STORED,
b INT8 NULL AS (a + 1:::INT8) STORED,
CONSTRAINT "primary" PRIMARY KEY (a ASC),
FAMILY "primary" (a, b)
);
......
......@@ -169,7 +169,7 @@ func (db *testSender) Send(
}
repl := db.store.LookupReplica(rs.Key)
if repl == nil || !repl.Desc().ContainsKeyRange(rs.Key, rs.EndKey) {
return nil, roachpb.NewError(roachpb.NewRangeKeyMismatchError(rs.Key.AsRawKey(), rs.EndKey.AsRawKey(), nil))
panic(fmt.Sprintf("didn't find right replica for key: %s", rs.Key))
}
ba.RangeID = repl.RangeID
repDesc, err := repl.GetReplicaDescriptor()
......
......@@ -382,9 +382,12 @@ var _ ErrorDetailInterface = &RangeNotFoundError{}
// NewRangeKeyMismatchError initializes a new RangeKeyMismatchError.
func NewRangeKeyMismatchError(start, end Key, desc *RangeDescriptor) *RangeKeyMismatchError {
if desc != nil && !desc.IsInitialized() {
// We must never send uninitialized ranges back to the client (nil
// is fine) guard against regressions of #6027.
if desc == nil {
panic("NewRangeKeyMismatchError with nil descriptor")
}
if !desc.IsInitialized() {
// We must never send uninitialized ranges back to the client guard against
// regressions of #6027.
panic(fmt.Sprintf("descriptor is not initialized: %+v", desc))
}
return &RangeKeyMismatchError{
......
......@@ -36,6 +36,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
......@@ -1629,7 +1630,11 @@ CREATE TABLE crdb_internal.table_columns (
col := &table.Columns[i]
defStr := tree.DNull
if col.DefaultExpr != nil {
defStr = tree.NewDString(*col.DefaultExpr)
def, err := schemaexpr.DeserializeTableDescExpr(ctx, &p.semaCtx, table, *col.DefaultExpr)
if err != nil {
return err
}
defStr = tree.NewDString(tree.SerializeForDisplay(def))
}
row = row[:0]
row = append(row,
......
......@@ -133,7 +133,7 @@ func HydrateTypeSlice(evalCtx *tree.EvalContext, typs []*types.T) error {
// IndexedVar formatting function needs to be added on. It replaces placeholders
// with their values.
func ExprFmtCtxBase(evalCtx *tree.EvalContext) *tree.FmtCtx {
fmtCtx := tree.NewFmtCtx(tree.FmtDistSQLSerialization)
fmtCtx := tree.NewFmtCtx(tree.FmtCheckEquivalence)
fmtCtx.SetPlaceholderFormat(
func(fmtCtx *tree.FmtCtx, p *tree.Placeholder) {
d, err := p.Eval(evalCtx)
......@@ -173,7 +173,7 @@ func (e *Expression) Empty() bool {
// String implements the Stringer interface.
func (e Expression) String() string {
if e.LocalExpr != nil {
ctx := tree.NewFmtCtx(tree.FmtDistSQLSerialization)
ctx := tree.NewFmtCtx(tree.FmtCheckEquivalence)
ctx.FormatNode(e.LocalExpr)
return ctx.CloseAndGetString()
}
......
......@@ -22,6 +22,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkv"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/resolver"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
......@@ -172,20 +173,6 @@ func dNameOrNull(s string) tree.Datum {
return tree.NewDName(s)
}
func dStringPtrOrEmpty(s *string) tree.Datum {
if s == nil {
return emptyString
}
return tree.NewDString(*s)
}
func dStringPtrOrNull(s *string) tree.Datum {
if s == nil {
return tree.DNull
}
return tree.NewDString(*s)
}
func dIntFnOrNull(fn func() (int32, bool)) tree.Datum {
if n, ok := fn(); ok {
return tree.NewDInt(tree.DInt(n))
......@@ -387,14 +374,30 @@ https://www.postgresql.org/docs/9.5/infoschema-columns.html`,
collationSchema = pgCatalogNameDString
collationName = tree.NewDString(locale)
}
colDefault := tree.DNull
if column.DefaultExpr != nil {
colExpr, err := schemaexpr.DeserializeTableDescExpr(ctx, &p.semaCtx, table, *column.DefaultExpr)
if err != nil {
return err
}
colDefault = tree.NewDString(tree.SerializeForDisplay(colExpr))
}
colComputed := emptyString
if column.ComputeExpr != nil {
colExpr, err := schemaexpr.DeserializeTableDescExpr(ctx, &p.semaCtx, table, *column.ComputeExpr)
if err != nil {
return err
}
colComputed = tree.NewDString(tree.SerializeForDisplay(colExpr))
}
return addRow(
dbNameStr, // table_catalog
scNameStr, // table_schema
tree.NewDString(table.Name), // table_name
tree.NewDString(column.Name), // column_name
tree.NewDInt(tree.DInt(column.GetLogicalColumnID())), // ordinal_position
dStringPtrOrNull(column.DefaultExpr), // column_default
yesOrNoDatum(column.Nullable), // is_nullable
colDefault, // column_default
yesOrNoDatum(column.Nullable), // is_nullable
tree.NewDString(column.Type.InformationSchemaName()), // data_type
characterMaximumLength(column.Type), // character_maximum_length
characterOctetLength(column.Type), // character_octet_length
......@@ -430,7 +433,7 @@ https://www.postgresql.org/docs/9.5/infoschema-columns.html`,
tree.DNull, // identity_minimum
tree.DNull, // identity_cycle
yesOrNoDatum(column.IsComputed()), // is_generated
dStringPtrOrEmpty(column.ComputeExpr), // generation_expression
colComputed, // generation_expression
yesOrNoDatum(table.IsTable() &&
!table.IsVirtualTable() &&
!column.IsComputed(),
......
......@@ -91,7 +91,7 @@ query TTTTB colnames
SHOW CONSTRAINTS FROM t
----
table_name constraint_name constraint_type details validated
t check_a CHECK CHECK ((a > 0)) true
t check_a CHECK CHECK ((a > 0:::INT8)) true
t fk_f_ref_other FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t foo UNIQUE UNIQUE (b ASC) true
t primary PRIMARY KEY PRIMARY KEY (a ASC) true
......@@ -105,7 +105,7 @@ ALTER TABLE t DROP CONSTRAINT check_a
statement ok
INSERT INTO t (a, f) VALUES (-2, 9)
statement error validation of CHECK "a > 0" failed on row: a=-2, f=9, b=NULL, c=NULL
statement error validation of CHECK "a > 0:::INT8" failed on row: a=-2, f=9, b=NULL, c=NULL
ALTER TABLE t ADD CONSTRAINT check_a CHECK (a > 0)
statement ok
......@@ -120,7 +120,7 @@ INSERT INTO t (a) VALUES (-3)
query TTTTB
SHOW CONSTRAINTS FROM t
----
t check_a CHECK CHECK ((a > 0)) true
t check_a CHECK CHECK ((a > 0:::INT8)) true
t fk_f_ref_other FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t foo UNIQUE UNIQUE (b ASC) true
t primary PRIMARY KEY PRIMARY KEY (a ASC) true
......@@ -138,8 +138,8 @@ ALTER TABLE t ADD CHECK (a > 0)
query TTTTB
SHOW CONSTRAINTS FROM t
----
t check_a CHECK CHECK ((a > 0)) true
t check_a1 CHECK CHECK ((a > 0)) true
t check_a CHECK CHECK ((a > 0:::INT8)) true
t check_a1 CHECK CHECK ((a > 0:::INT8)) true
t fk_f_ref_other FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t foo UNIQUE UNIQUE (b ASC) true
t primary PRIMARY KEY PRIMARY KEY (a ASC) true
......@@ -160,8 +160,8 @@ ALTER TABLE t VALIDATE CONSTRAINT check_a
query TTTTB
SHOW CONSTRAINTS FROM t
----
t check_a CHECK CHECK ((a > 0)) true
t check_a1 CHECK CHECK ((a > 0)) true
t check_a CHECK CHECK ((a > 0:::INT8)) true
t check_a1 CHECK CHECK ((a > 0:::INT8)) true
t fk_f_ref_other FOREIGN KEY FOREIGN KEY (f) REFERENCES other(b) true
t foo UNIQUE UNIQUE (b ASC) true
t primary PRIMARY KEY PRIMARY KEY (a ASC) true
......@@ -374,24 +374,24 @@ ALTER TABLE t ADD g INT DEFAULT 1 CHECK (g > 0)
statement ok
ALTER TABLE t ADD h INT CHECK (h > 0) CHECK (h < 10) UNIQUE
statement error pq: validation of CHECK "i < 0" failed on row:.* i=1
statement error pq: validation of CHECK "i < 0:::INT8" failed on row:.* i=1
ALTER TABLE t ADD i INT DEFAULT 1 CHECK (i < 0)
statement error pq: validation of CHECK "i < g" failed on row:.* g=1.* i=1
ALTER TABLE t ADD i INT DEFAULT 1 CHECK (i < g)
statement error pq: validation of CHECK "i > 0" failed on row:.* g=1.* i=0
statement error pq: validation of CHECK "i > 0:::INT8" failed on row:.* g=1.* i=0
ALTER TABLE t ADD i INT AS (g - 1) STORED CHECK (i > 0)
query TTTTB
SHOW CONSTRAINTS FROM t
----
t check_f CHECK CHECK ((f > 1)) true
t check_g CHECK CHECK ((g > 0)) true
t check_h CHECK CHECK ((h > 0)) true
t check_h1 CHECK CHECK ((h < 10)) true
t primary PRIMARY KEY PRIMARY KEY (a ASC) true
t t_h_key UNIQUE UNIQUE (h ASC) true
t check_f CHECK CHECK ((f > 1:::INT8)) true
t check_g CHECK CHECK ((g > 0:::INT8)) true
t check_h CHECK CHECK ((h > 0:::INT8)) true
t check_h1 CHECK CHECK ((h < 10:::INT8)) true
t primary PRIMARY KEY PRIMARY KEY (a ASC) true
t t_h_key UNIQUE UNIQUE (h ASC) true
statement ok
DROP TABLE t
......@@ -424,13 +424,13 @@ ALTER TABLE t ADD COLUMN u INT UNIQUE, ADD COLUMN v INT UNIQUE, ADD CONSTRAINT c
query TTTTB
SHOW CONSTRAINTS FROM t
----
t check_c_b CHECK CHECK ((c > b)) true
t ck CHECK CHECK ((a > 0)) true
t primary PRIMARY KEY PRIMARY KEY (a ASC) true
t t_d_key UNIQUE UNIQUE (d ASC) true
t t_e_key UNIQUE UNIQUE (e ASC) true
t t_u_key UNIQUE UNIQUE (u ASC) true
t t_v_key UNIQUE UNIQUE (v ASC) true
t check_c_b CHECK CHECK ((c > b)) true
t ck CHECK CHECK ((a > 0:::INT8)) true
t primary PRIMARY KEY PRIMARY KEY (a ASC) true
t t_d_key UNIQUE UNIQUE (d ASC) true
t t_e_key UNIQUE UNIQUE (e ASC) true
t t_u_key UNIQUE UNIQUE (u ASC) true
t t_v_key UNIQUE UNIQUE (v ASC) true
statement ok
DROP TABLE t
......@@ -971,7 +971,7 @@ CREATE TABLE t (a int);
statement ok
INSERT INTO t VALUES (10), (15), (17)
statement error pq: validation of CHECK "a < 16" failed on row: a=17
statement error pq: validation of CHECK "a < 16:::INT8" failed on row: a=17
ALTER TABLE t ADD CHECK (a < 16)
statement ok
......@@ -983,13 +983,13 @@ ALTER TABLE t ADD CHECK (a < 16) NOT VALID
query TTTTB
SHOW CONSTRAINTS FROM t
----
t check_a CHECK CHECK ((a < 100)) true
t check_a1 CHECK CHECK ((a < 16)) false
t check_a CHECK CHECK ((a < 100:::INT8)) true
t check_a1 CHECK CHECK ((a < 16:::INT8)) false
query error pq: failed to satisfy CHECK constraint \(a < 16\)
query error pq: failed to satisfy CHECK constraint \(a < 16:::INT8\)
INSERT INTO t VALUES (20)
statement error pq: validation of CHECK "a < 16" failed on row: a=17
statement error pq: validation of CHECK "a < 16:::INT8" failed on row: a=17
ALTER TABLE t VALIDATE CONSTRAINT check_a1
statement ok
......@@ -1001,8 +1001,8 @@ ALTER TABLE t VALIDATE CONSTRAINT check_a1
query TTTTB
SHOW CONSTRAINTS FROM t
----
t check_a CHECK CHECK ((a < 100)) true
t check_a1 CHECK CHECK ((a < 16)) true
t check_a CHECK CHECK ((a < 100:::INT8)) true
t check_a1 CHECK CHECK ((a < 16:::INT8)) true
subtest regression_42858
......
......@@ -2235,20 +2235,20 @@ b 20
c 10
# Perform another cascading update that should fail c.less_than_100.
statement error pq: failed to satisfy CHECK constraint \(id < 100\)
statement error pq: failed to satisfy CHECK constraint \(id < 100:::INT8\)
UPDATE a SET id = id*10;
# Perform another cascading update that should fail b.less_than_1000 or
# c.less_than_100. The order of which check fails first is not deterministic.
statement error pq: failed to satisfy CHECK constraint \(id < (100|1000)\)
statement error pq: failed to satisfy CHECK constraint \(id < (100|1000):::INT8\)
UPDATE a SET id = id*1000;
# Perform another cascading update that should fail b.less_than_1000.
statement error pq: failed to satisfy CHECK constraint \(id < 1000\)
statement error pq: failed to satisfy CHECK constraint \(id < 1000:::INT8\)
UPDATE a SET id = id*1000 WHERE id > 10;
# And check another direct cascading constraint c.no_99.
statement error pq: failed to satisfy CHECK constraint \(id != 99\)
statement error pq: failed to satisfy CHECK constraint \(id != 99:::INT8\)
UPDATE a SET id = 99 WHERE id = 10;
# But it should still be possible to cascade an update that doesn't hit c.
......@@ -2340,18 +2340,18 @@ c 10 20
c 20 10
# Try to update one value to fail c.less_than_100
statement error pq: failed to satisfy CHECK constraint \(\(id1 \+ id2\) < 100\)
statement error pq: failed to satisfy CHECK constraint \(\(id1 \+ id2\) < 100:::INT8\)
UPDATE a SET id = id*10;
# Try to update one value to fail c.less_than_100 or c.less_than_1000
statement error pq: failed to satisfy CHECK constraint \(\(id1 \+ id2\) < 100\)
statement error pq: failed to satisfy CHECK constraint \(\(id1 \+ id2\) < 100:::INT8\)
UPDATE a SET id = id*10;
# Try to update one value to fail c.less_than_100
statement error pq: failed to satisfy CHECK constraint \(\(id1 \+ id2\) < 1000\)
statement error pq: failed to satisfy CHECK constraint \(\(id1 \+ id2\) < 1000:::INT8\)
UPDATE a SET id = 1000 WHERE id = 30;
statement error pq: failed to satisfy CHECK constraint \(\(id1 \+ id2\) < 1000\)
statement error pq: failed to satisfy CHECK constraint \(\(id1 \+ id2\) < 1000:::INT8\)
UPDATE a SET id = 1000 WHERE id = 40;
# Update a value that would fail the check if it was cascaded, but wasn't.
......@@ -3991,7 +3991,7 @@ ALTER TABLE self_ab ADD CONSTRAINT fk FOREIGN KEY (b) REFERENCES self_ab (a) ON
# This update would cause the references to 2 to get reset to the default,
# which violates the check.
statement error failed to satisfy CHECK constraint \(b != 1\)
statement error failed to satisfy CHECK constraint \(b != 1:::INT8\)
UPDATE self_ab SET a = 3 WHERE a = 2
# Make sure the check fails when we apply the same update through a cascade.
......@@ -4004,5 +4004,5 @@ INSERT INTO self_ab_parent VALUES (1), (2)
statement ok
ALTER TABLE self_ab ADD CONSTRAINT fk2 FOREIGN KEY (a) REFERENCES self_ab_parent (p) ON UPDATE CASCADE
statement error failed to satisfy CHECK constraint \(b != 1\)
statement error failed to satisfy CHECK constraint \(b != 1:::INT8\)
UPDATE self_ab_parent SET p = 3 WHERE p = 2
......@@ -19,13 +19,13 @@ ALTER TABLE t1 DROP COLUMN to_delete
statement ok
INSERT INTO t1 (a, b) VALUES (4, -2)
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(a > 0\)
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(a > 0:::INT8\)
INSERT INTO t1 VALUES (-3, -1)
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(b < 0\)
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(b < 0:::INT8\)
INSERT INTO t1 VALUES (3, 1)
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(b > -100\)
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(b > \(-100\):::INT8\)
INSERT INTO t1 VALUES (3, -101)
statement ok
......@@ -40,7 +40,7 @@ INSERT INTO t1 (b) VALUES (-1)
statement ok
CREATE TABLE t2 (a INT DEFAULT -1 CHECK (a >= 0), b INT CHECK (b <= 0), CHECK (b < a))
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(a >= 0\)
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(a >= 0:::INT8\)
INSERT INTO t2 (b) VALUES (-2)
### Rename column with check constraint
......@@ -48,7 +48,7 @@ INSERT INTO t2 (b) VALUES (-2)
statement ok
ALTER TABLE t2 RENAME COLUMN b TO c
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(c <= 0\)
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(c <= 0:::INT8\)
INSERT INTO t2 (a, c) VALUES (2, 1)
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(c < a\)
......@@ -221,11 +221,11 @@ t7 CREATE TABLE t7 (
y INT8 NULL,
z INT8 NULL,
FAMILY "primary" (x, y, z, rowid),
CONSTRAINT check_x CHECK (x > 0),
CONSTRAINT check_x_y CHECK ((x + y) > 0),
CONSTRAINT check_y_z CHECK ((y + z) > 0),
CONSTRAINT check_y_z1 CHECK ((y + z) = 0),
CONSTRAINT named_constraint CHECK (z = 1)
CONSTRAINT check_x CHECK (x > 0:::INT8),
CONSTRAINT check_x_y CHECK ((x + y) > 0:::INT8),
CONSTRAINT check_y_z CHECK ((y + z) > 0:::INT8),
CONSTRAINT check_y_z1 CHECK ((y + z) = 0:::INT8),
CONSTRAINT named_constraint CHECK (z = 1:::INT8)
)
# Check that table references are dequalified in their stored representation.
......@@ -256,9 +256,9 @@ SHOW CREATE TABLE t8
t8 CREATE TABLE t8 (
a INT8 NULL,
FAMILY "primary" (a, rowid),
CONSTRAINT check_a CHECK (a > 0),
CONSTRAINT check_a1 CHECK (a > 0),
CONSTRAINT check_a2 CHECK (a > 0)
CONSTRAINT check_a CHECK (a > 0:::INT8),
CONSTRAINT check_a1 CHECK (a > 0:::INT8),
CONSTRAINT check_a2 CHECK (a > 0:::INT8)
)
statement ok
......
......@@ -352,7 +352,7 @@ SHOW CREATE TABLE quoted_coll
quoted_coll CREATE TABLE quoted_coll (
a STRING COLLATE en NULL,
b STRING COLLATE en_US NULL,
c STRING COLLATE en_Us NULL DEFAULT 'c':::STRING COLLATE en_Us,
c STRING COLLATE en_Us NULL DEFAULT 'c':::STRING COLLATE en_us,
d STRING COLLATE en_u_ks_level1 NULL DEFAULT 'd':::STRING::STRING COLLATE en_u_ks_level1,
e STRING COLLATE en_us NULL AS (a COLLATE en_us) STORED,
FAMILY "primary" (a, b, c, d, e, rowid)
......
......@@ -13,7 +13,7 @@ SHOW CREATE TABLE with_no_column_refs
with_no_column_refs CREATE TABLE with_no_column_refs (
a INT8 NULL,
b INT8 NULL,
c INT8 NULL AS (3) STORED,
c INT8 NULL AS (3:::INT8) STORED,
FAMILY "primary" (a, b, c, rowid)
)
......@@ -31,7 +31,7 @@ SHOW CREATE TABLE extra_parens
extra_parens CREATE TABLE extra_parens (
a INT8 NULL,
b INT8 NULL,
c INT8 NULL AS ((3)) STORED,
c INT8 NULL AS (3:::INT8) STORED,
FAMILY "primary" (a, b, c, rowid)
)
......@@ -654,7 +654,7 @@ SELECT generation_expression FROM information_schema.columns
WHERE table_name = 'x' and column_name = 'b'
----
generation_expression
a * 2
a * 2:::INT8
query I
SELECT count(*) FROM information_schema.columns
......@@ -674,8 +674,8 @@ SHOW CREATE TABLE x
----
x CREATE TABLE x (
a INT8 NULL,
b INT8 NULL AS (a * 2) STORED,
c INT8 NOT NULL AS (a + 4) STORED,
b INT8 NULL AS (a * 2:::INT8) STORED,
c INT8 NOT NULL AS (a + 4:::INT8) STORED,
FAMILY "primary" (a, b, rowid, c)
)
......@@ -787,7 +787,7 @@ query TT
SHOW CREATE t42418
----
t42418 CREATE TABLE t42418 (
x INT8 NULL AS (1) STORED,
y INT8 NULL AS (1) STORED,
x INT8 NULL AS (1:::INT8) STORED,
y INT8 NULL AS (1:::INT8) STORED,
FAMILY "primary" (x, rowid, y)
)
......@@ -157,7 +157,7 @@ like_constraints CREATE TABLE like_constraints (
h INT8 NULL,
j JSONB NULL,
FAMILY "primary" (a, b, c, h, j, rowid),
CONSTRAINT check_a CHECK (a > 3)
CONSTRAINT check_a CHECK (a > 3:::INT8)
)
statement ok
......@@ -189,7 +189,7 @@ SHOW CREATE TABLE like_generated
like_generated CREATE TABLE like_generated (
a INT8 NOT NULL,
b STRING NOT NULL,
c DECIMAL NULL AS (a + 3) STORED,
c DECIMAL NULL AS (a + 3:::DECIMAL) STORED,
h INT8 NULL,
j JSONB NULL,
FAMILY "primary" (a, b, c, h, j, rowid)
......@@ -219,7 +219,7 @@ SHOW CREATE TABLE like_all
like_all CREATE TABLE like_all (
a INT8 NOT NULL,
b STRING NOT NULL DEFAULT 'foo':::STRING,
c DECIMAL NULL AS (a + 3) STORED,
c DECIMAL NULL AS (a + 3:::DECIMAL) STORED,
h INT8 NULL,
j JSONB NULL,
CONSTRAINT "primary" PRIMARY KEY (a ASC, b ASC),
......@@ -227,7 +227,7 @@ like_all CREATE TABLE like_all (
INDEX like_table_c_idx (c ASC) STORING (j),
INVERTED INDEX like_table_j_idx (j),
FAMILY "primary" (a, b, c, h, j),
CONSTRAINT check_a CHECK (a > 3)
CONSTRAINT check_a CHECK (a > 3:::INT8)
)
statement ok
......@@ -241,7 +241,7 @@ SHOW CREATE TABLE like_mixed
like_mixed CREATE TABLE like_mixed (
a INT8 NOT NULL,
b STRING NOT NULL DEFAULT 'foo':::STRING,
c DECIMAL NULL AS (a + 3) STORED,
c DECIMAL NULL AS (a + 3:::DECIMAL) STORED,
h INT8 NULL,
j JSONB NULL,
CONSTRAINT "primary" PRIMARY KEY (a ASC, b ASC),
......
......@@ -418,7 +418,7 @@ SELECT ARRAY['hello']::_greeting, ARRAY['hello'::greeting]
{hello} {hello}
# Test that we can't mix enums in an array.
query error pq: expected 'cockroach'::dbs to be of type greeting, found type dbs
query error pq: expected 'cockroach'::test.public.dbs to be of type greeting, found type dbs
SELECT ARRAY['hello'::greeting, 'cockroach'::dbs]
statement ok
......@@ -469,3 +469,119 @@ WHERE
descriptor_name = 'enum_array' AND column_name = 'x'
----
x family:ArrayFamily width:0 precision:0 locale:"" visible_type:0 oid:100064 array_contents:<InternalType:<family:EnumFamily width:0 precision:0 locale:"" visible_type:0 oid:100063 time_precision_is_set:false udt_metadata:<stable_type_id:63 stable_array_type_id:64 > > TypeMeta:<> > time_precision_is_set:false udt_metadata:<stable_type_id:64 stable_array_type_id:0 >
# Test tables using enums in DEFAULT expressions.
statement ok
CREATE TABLE enum_default (
x INT,
y greeting DEFAULT 'hello',
z BOOL DEFAULT ('hello':::greeting IS OF (greeting, greeting)),
FAMILY (x, y, z)
);
INSERT INTO enum_default VALUES (1), (2)
query ITB rowsort
SELECT * FROM enum_default
----
1 hello true
2 hello true
# Test that enum default values are formatted in human readable ways.
query TT
SHOW CREATE enum_default
----
enum_default CREATE TABLE enum_default (
x INT8 NULL,
y test.public.greeting NULL DEFAULT 'hello':::test.public.greeting,
z BOOL NULL DEFAULT 'hello':::test.public.greeting IS OF (test.public.greeting, test.public.greeting),
FAMILY fam_0_x_y_z_rowid (x, y, z, rowid)
)
# Test crdb_internal.table_columns.
query TT
SELECT
column_name, default_expr
FROM
crdb_internal.table_columns
WHERE
descriptor_name='enum_default' AND (column_name = 'y' OR column_name = 'z')
ORDER BY
column_name
----
y 'hello':::test.public.greeting
z 'hello':::test.public.greeting IS OF (test.public.greeting, test.public.greeting)
# Test information_schema.columns.
query TT
SELECT
column_name, column_default
FROM
information_schema.columns
WHERE
table_name='enum_default' AND (column_name = 'y' OR column_name = 'z')
ORDER BY
column_name
----
y 'hello':::test.public.greeting
z 'hello':::test.public.greeting IS OF (test.public.greeting, test.public.greeting)
# Test computed columns with enum values.
statement ok
CREATE TABLE enum_computed (
x INT,
y greeting AS ('hello') STORED,
z BOOL AS (w = 'howdy') STORED,
w greeting,
FAMILY (x, y, z)
);
INSERT INTO enum_computed (x, w) VALUES (1, 'hello'), (2, 'hello')
query ITBT rowsort
SELECT * FROM enum_computed
----
1 hello false hello
2 hello false hello
query TT
SHOW CREATE enum_computed
----
enum_computed CREATE TABLE enum_computed (
x INT8 NULL,
y test.public.greeting NULL AS ('hello':::test.public.greeting) STORED,
z BOOL NULL AS (w = 'howdy':::test.public.greeting) STORED,
w test.public.greeting NULL,
FAMILY fam_0_x_y_z_w_rowid (x, y, z, w, rowid)
)
# Test information_schema.columns.
query TT
SELECT
column_name, generation_expression
FROM
information_schema.columns
WHERE
table_name='enum_computed' AND (column_name = 'y' OR column_name = 'z')
ORDER BY
column_name
----
y 'hello':::test.public.greeting
z w = 'howdy':::test.public.greeting
# Test check constraints with enum values.
statement ok
CREATE TABLE enum_checks (
x greeting,
CHECK (x = 'hello'::greeting),
CHECK ('hello':::greeting = 'hello':::greeting)
);
INSERT INTO enum_checks VALUES ('hello')
query TT
SHOW CREATE enum_checks
----
enum_checks CREATE TABLE enum_checks (
x test.public.greeting NULL,
FAMILY "primary" (x, rowid),
CONSTRAINT check_x CHECK (x = 'hello':::test.public.greeting::test.public.greeting),
CONSTRAINT "check" CHECK ('hello':::test.public.greeting = 'hello':::test.public.greeting)
)
......@@ -1050,8 +1050,8 @@ ORDER BY CONSTRAINT_CATALOG, CONSTRAINT_NAME
----
constraint_catalog constraint_schema constraint_name check_clause
constraint_db public 541687103_59_1_not_null p IS NOT NULL
constraint_db public c2 ((a < 99))
constraint_db public check_a ((a > 4))
constraint_db public c2 ((a < 99:::INT8))
constraint_db public check_a ((a > 4:::INT8))
query TTTTTTT colnames
SELECT *
......@@ -1077,8 +1077,8 @@ ORDER BY tc.table_schema, tc.table_name, cc.constraint_name
----
table_schema table_name constraint_name check_clause
public t1 541687103_59_1_not_null p IS NOT NULL
public t1 c2 ((a < 99))
public t1 check_a ((a > 4))
public t1 c2 ((a < 99:::INT8))
public t1 check_a ((a > 4:::INT8))
statement ok
DROP DATABASE constraint_db CASCADE
......@@ -1391,7 +1391,7 @@ WHERE table_schema = 'public' AND table_name = 'computed'
----
column_name is_generated generation_expression is_updatable
a NO · YES
b YES a + 1 NO
b YES a + 1:::INT8 NO
rowid NO · YES
statement ok
......
......@@ -27,7 +27,7 @@ INSERT INTO kv VALUES (1, 2) RETURNING NOTHING
statement ok
UPSERT INTO kv VALUES (2, 2) RETURNING NOTHING
statement error failed to satisfy CHECK constraint \(v < 100\)
statement error failed to satisfy CHECK constraint \(v < 100:::INT8\)
UPSERT INTO kv VALUES (2, 500) RETURNING NOTHING