Skip to content

Commit 2dce1ce

Browse files
craig[bot]rafissrickystewart
committed
144821: parser: fix format/parse roundtrip for unique index r=rafiss a=rafiss If a unique index was created with storage parameters or as a hash-sharded index, it would be formatted as a unique constraint. That was incorrect, since constraints don't support those syntaxes. fixes #144727 Release note: None 146002: logictest: skip testserver upgrade tests on `s390x` r=celiala a=rickystewart These tests do not make sense on `s390x` as there are no `s390x` releases right now. Epic: CRDB-21133 Release note: None Co-authored-by: Rafi Shamim <[email protected]> Co-authored-by: Ricky Stewart <[email protected]>
3 parents d9b15fd + dfa49ee + 24e85c1 commit 2dce1ce

File tree

7 files changed

+74
-25
lines changed

7 files changed

+74
-25
lines changed

pkg/sql/logictest/logic.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,9 @@ func (t *logicTest) setup(
20022002
if !bazel.BuiltWithBazel() {
20032003
skip.IgnoreLint(t.t(), "cockroach-go/testserver can only be uzed in bazel builds")
20042004
}
2005+
if runtime.GOARCH == "s390x" {
2006+
skip.IgnoreLint(t.t(), "cockroach-go/testserver is not operational on s390x")
2007+
}
20052008
if cfg.NumNodes != 3 {
20062009
t.Fatal("cockroach-go testserver tests must use 3 nodes")
20072010
}

pkg/sql/parser/sql.y

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11307,6 +11307,7 @@ index_def:
1130711307
Predicate: $11.expr(),
1130811308
Invisibility: $12.indexInvisibility(),
1130911309
},
11310+
FormatAsIndex: true,
1131011311
}
1131111312
}
1131211313
| INVERTED INDEX_BEFORE_PAREN '(' index_params ')' opt_partition_by_index opt_with_storage_parameter_list opt_where_clause opt_index_visible

pkg/sql/parser/testdata/create_table

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -555,35 +555,35 @@ CREATE TABLE _ (_ INT8 UNIQUE) -- identifiers removed
555555
parse
556556
CREATE TABLE a (b INT, UNIQUE INDEX foo (b))
557557
----
558-
CREATE TABLE a (b INT8, CONSTRAINT foo UNIQUE (b)) -- normalized!
559-
CREATE TABLE a (b INT8, CONSTRAINT foo UNIQUE (b)) -- fully parenthesized
560-
CREATE TABLE a (b INT8, CONSTRAINT foo UNIQUE (b)) -- literals removed
561-
CREATE TABLE _ (_ INT8, CONSTRAINT _ UNIQUE (_)) -- identifiers removed
558+
CREATE TABLE a (b INT8, UNIQUE INDEX foo (b)) -- normalized!
559+
CREATE TABLE a (b INT8, UNIQUE INDEX foo (b)) -- fully parenthesized
560+
CREATE TABLE a (b INT8, UNIQUE INDEX foo (b)) -- literals removed
561+
CREATE TABLE _ (_ INT8, UNIQUE INDEX _ (_)) -- identifiers removed
562562

563563
parse
564564
CREATE TABLE a (b INT, UNIQUE INDEX foo (b) WHERE c > 3)
565565
----
566-
CREATE TABLE a (b INT8, CONSTRAINT foo UNIQUE (b) WHERE c > 3) -- normalized!
567-
CREATE TABLE a (b INT8, CONSTRAINT foo UNIQUE (b) WHERE ((c) > (3))) -- fully parenthesized
568-
CREATE TABLE a (b INT8, CONSTRAINT foo UNIQUE (b) WHERE c > _) -- literals removed
569-
CREATE TABLE _ (_ INT8, CONSTRAINT _ UNIQUE (_) WHERE _ > 3) -- identifiers removed
566+
CREATE TABLE a (b INT8, UNIQUE INDEX foo (b) WHERE c > 3) -- normalized!
567+
CREATE TABLE a (b INT8, UNIQUE INDEX foo (b) WHERE ((c) > (3))) -- fully parenthesized
568+
CREATE TABLE a (b INT8, UNIQUE INDEX foo (b) WHERE c > _) -- literals removed
569+
CREATE TABLE _ (_ INT8, UNIQUE INDEX _ (_) WHERE _ > 3) -- identifiers removed
570570

571571
parse
572572
CREATE TABLE a (UNIQUE INDEX (b) PARTITION BY LIST (c) (PARTITION d VALUES IN (1)))
573573
----
574-
CREATE TABLE a (UNIQUE (b) PARTITION BY LIST (c) (PARTITION d VALUES IN (1))) -- normalized!
575-
CREATE TABLE a (UNIQUE (b) PARTITION BY LIST (c) (PARTITION d VALUES IN ((1)))) -- fully parenthesized
576-
CREATE TABLE a (UNIQUE (b) PARTITION BY LIST (c) (PARTITION d VALUES IN (_))) -- literals removed
577-
CREATE TABLE _ (UNIQUE (_) PARTITION BY LIST (_) (PARTITION _ VALUES IN (1))) -- identifiers removed
574+
CREATE TABLE a (UNIQUE INDEX (b) PARTITION BY LIST (c) (PARTITION d VALUES IN (1)))
575+
CREATE TABLE a (UNIQUE INDEX (b) PARTITION BY LIST (c) (PARTITION d VALUES IN ((1)))) -- fully parenthesized
576+
CREATE TABLE a (UNIQUE INDEX (b) PARTITION BY LIST (c) (PARTITION d VALUES IN (_))) -- literals removed
577+
CREATE TABLE _ (UNIQUE INDEX (_) PARTITION BY LIST (_) (PARTITION _ VALUES IN (1))) -- identifiers removed
578578

579579
# Regression test for #95238
580580
parse
581581
CREATE TABLE a (UNIQUE INDEX (b) PARTITION BY LIST ("c d") (PARTITION "e f" VALUES IN (1)))
582582
----
583-
CREATE TABLE a (UNIQUE (b) PARTITION BY LIST ("c d") (PARTITION "e f" VALUES IN (1))) -- normalized!
584-
CREATE TABLE a (UNIQUE (b) PARTITION BY LIST ("c d") (PARTITION "e f" VALUES IN ((1)))) -- fully parenthesized
585-
CREATE TABLE a (UNIQUE (b) PARTITION BY LIST ("c d") (PARTITION "e f" VALUES IN (_))) -- literals removed
586-
CREATE TABLE _ (UNIQUE (_) PARTITION BY LIST (_) (PARTITION _ VALUES IN (1))) -- identifiers removed
583+
CREATE TABLE a (UNIQUE INDEX (b) PARTITION BY LIST ("c d") (PARTITION "e f" VALUES IN (1)))
584+
CREATE TABLE a (UNIQUE INDEX (b) PARTITION BY LIST ("c d") (PARTITION "e f" VALUES IN ((1)))) -- fully parenthesized
585+
CREATE TABLE a (UNIQUE INDEX (b) PARTITION BY LIST ("c d") (PARTITION "e f" VALUES IN (_))) -- literals removed
586+
CREATE TABLE _ (UNIQUE INDEX (_) PARTITION BY LIST (_) (PARTITION _ VALUES IN (1))) -- identifiers removed
587587

588588
parse
589589
CREATE TABLE a (b INT8 UNIQUE WITHOUT INDEX)
@@ -2639,3 +2639,35 @@ CREATE TABLE a (a VECTOR)
26392639
CREATE TABLE a (a VECTOR) -- fully parenthesized
26402640
CREATE TABLE a (a VECTOR) -- literals removed
26412641
CREATE TABLE _ (_ VECTOR) -- identifiers removed
2642+
2643+
parse
2644+
CREATE TABLE t (a INT PRIMARY KEY, b INT, UNIQUE INDEX ( b NULLS FIRST ) USING HASH WITH ( 'foo' = 'bar' ))
2645+
----
2646+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX (b NULLS FIRST) USING HASH WITH ('foo' = 'bar')) -- normalized!
2647+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX (b NULLS FIRST) USING HASH WITH ('foo' = ('bar'))) -- fully parenthesized
2648+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX (b NULLS FIRST) USING HASH WITH ('foo' = '_')) -- literals removed
2649+
CREATE TABLE _ (_ INT8 PRIMARY KEY, _ INT8, UNIQUE INDEX (_ NULLS FIRST) USING HASH WITH ('foo' = 'bar')) -- identifiers removed
2650+
2651+
parse
2652+
CREATE TABLE t (a INT PRIMARY KEY, b INT, UNIQUE INDEX ( b NULLS FIRST ) USING HASH WITH BUCKET_COUNT = 10 WITH ( 'foo' = 'bar' ))
2653+
----
2654+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX (b NULLS FIRST) USING HASH WITH BUCKET_COUNT = 10 WITH ('foo' = 'bar')) -- normalized!
2655+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX (b NULLS FIRST) USING HASH WITH BUCKET_COUNT = (10) WITH ('foo' = ('bar'))) -- fully parenthesized
2656+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX (b NULLS FIRST) USING HASH WITH BUCKET_COUNT = _ WITH ('foo' = '_')) -- literals removed
2657+
CREATE TABLE _ (_ INT8 PRIMARY KEY, _ INT8, UNIQUE INDEX (_ NULLS FIRST) USING HASH WITH BUCKET_COUNT = 10 WITH ('foo' = 'bar')) -- identifiers removed
2658+
2659+
parse
2660+
CREATE TABLE t (a INT PRIMARY KEY, b INT, UNIQUE INDEX (b) USING HASH)
2661+
----
2662+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX (b) USING HASH) -- normalized!
2663+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX (b) USING HASH) -- fully parenthesized
2664+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX (b) USING HASH) -- literals removed
2665+
CREATE TABLE _ (_ INT8 PRIMARY KEY, _ INT8, UNIQUE INDEX (_) USING HASH) -- identifiers removed
2666+
2667+
parse
2668+
CREATE TABLE t (a INT PRIMARY KEY, b INT, UNIQUE INDEX idx (b) WITH ( 'foo' = 'bar' ))
2669+
----
2670+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX idx (b) WITH ('foo' = 'bar')) -- normalized!
2671+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX idx (b) WITH ('foo' = ('bar'))) -- fully parenthesized
2672+
CREATE TABLE t (a INT8 PRIMARY KEY, b INT8, UNIQUE INDEX idx (b) WITH ('foo' = '_')) -- literals removed
2673+
CREATE TABLE _ (_ INT8 PRIMARY KEY, _ INT8, UNIQUE INDEX _ (_) WITH ('foo' = 'bar')) -- identifiers removed

pkg/sql/sem/tree/create.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,10 @@ type UniqueConstraintTableDef struct {
11121112
PrimaryKey bool
11131113
WithoutIndex bool
11141114
IfNotExists bool
1115+
// FormatAsIndex indicates if the constraint should be formatted as an index
1116+
// definition. This is needed since indexes support syntax for things like
1117+
// storage parameters and sharding, while constraints do not.
1118+
FormatAsIndex bool
11151119
}
11161120

11171121
// SetName implements the TableDef interface.
@@ -1126,7 +1130,7 @@ func (node *UniqueConstraintTableDef) SetIfNotExists() {
11261130

11271131
// Format implements the NodeFormatter interface.
11281132
func (node *UniqueConstraintTableDef) Format(ctx *FmtCtx) {
1129-
if node.Name != "" {
1133+
if node.Name != "" && !node.FormatAsIndex {
11301134
ctx.WriteString("CONSTRAINT ")
11311135
if node.IfNotExists {
11321136
ctx.WriteString("IF NOT EXISTS ")
@@ -1138,6 +1142,13 @@ func (node *UniqueConstraintTableDef) Format(ctx *FmtCtx) {
11381142
ctx.WriteString("PRIMARY KEY ")
11391143
} else {
11401144
ctx.WriteString("UNIQUE ")
1145+
if node.FormatAsIndex {
1146+
ctx.WriteString("INDEX ")
1147+
if node.Name != "" {
1148+
ctx.FormatNode(&node.Name)
1149+
ctx.WriteByte(' ')
1150+
}
1151+
}
11411152
}
11421153
if node.WithoutIndex {
11431154
ctx.WriteString("WITHOUT INDEX ")

pkg/sql/sem/tree/pretty.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,12 +1810,19 @@ func (node *UniqueConstraintTableDef) doc(p *PrettyCfg) pretty.Doc {
18101810
if node.WithoutIndex {
18111811
title = pretty.ConcatSpace(title, pretty.Keyword("WITHOUT INDEX"))
18121812
}
1813+
if node.FormatAsIndex {
1814+
title = pretty.ConcatSpace(title, pretty.Keyword("INDEX"))
1815+
}
18131816
}
1814-
title = pretty.ConcatSpace(title, p.bracket("(", p.Doc(&node.Columns), ")"))
18151817
if node.Name != "" {
1816-
clauses = append(clauses, title)
1817-
title = pretty.ConcatSpace(pretty.Keyword("CONSTRAINT"), p.Doc(&node.Name))
1818+
if node.FormatAsIndex {
1819+
title = pretty.ConcatSpace(title, p.Doc(&node.Name))
1820+
} else {
1821+
constraint := pretty.ConcatSpace(pretty.Keyword("CONSTRAINT"), p.Doc(&node.Name))
1822+
title = pretty.ConcatSpace(constraint, title)
1823+
}
18181824
}
1825+
title = pretty.ConcatSpace(title, p.bracket("(", p.Doc(&node.Columns), ")"))
18191826
if node.Sharded != nil {
18201827
clauses = append(clauses, p.Doc(node.Sharded))
18211828
}

pkg/testutils/sqlutils/parse.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ func parseOne(t *testing.T, input string, p Parser) (tree.NodeFormatter, error)
7070
func VerifyParseFormat(
7171
t *testing.T, input, pos string, p Parser, reParseWithoutLiterals bool,
7272
) string {
73-
t.Helper()
74-
7573
// Check parse.
7674
stmts, err := parse(t, input, p)
7775
if err != nil {

pkg/testutils/sqlutils/pretty.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
// VerifyStatementPrettyRoundtrip verifies that the SQL statements in s
1616
// correctly round trip through the pretty printer.
1717
func VerifyStatementPrettyRoundtrip(t *testing.T, sql string) {
18-
t.Helper()
19-
2018
stmts, err := parser.Parse(sql)
2119
if err != nil {
2220
t.Fatalf("%s: %s", err, sql)
@@ -41,7 +39,6 @@ func VerifyStatementPrettyRoundtrip(t *testing.T, sql string) {
4139
func verifyStatementPrettyRoundTrip(
4240
t *testing.T, sql string, origStmt tree.NodeFormatter, p Parser,
4341
) {
44-
t.Helper()
4542
// Dataflow of the statement through these checks:
4643
//
4744
// sql (from test file)

0 commit comments

Comments
 (0)