Skip to content

Commit 145b2b7

Browse files
craig[bot]fqazi
andcommitted
Merge #145340
145340: sql: support default equivalent collations for index expressions r=fqazi a=fqazi Previously, index expressions would work with COLLATE C because we would always fall back to the legacy schema changer. When we moved to the declarative schema changer, this regressed because the declarative schema changer makes a copy of the AST by serializing and reparsing the statement. Unfortunately, the formatting for the collation expression did not correctly format out default equivalent collations. This patch updates the formatter for collation expressions to special-case default equivalent collations like POSIX, C, and DEFAULT. Fixes: #145246 Release note: None Co-authored-by: Faizan Qazi <[email protected]>
2 parents df7d315 + f8656a9 commit 145b2b7

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

pkg/sql/lex/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_library(
1212
deps = [
1313
"//pkg/sql/pgwire/pgcode",
1414
"//pkg/sql/pgwire/pgerror",
15+
"//pkg/util/collatedstring",
1516
"@com_github_cockroachdb_errors//:errors",
1617
"@org_golang_x_text//language",
1718
],

pkg/sql/lex/encode.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
2626
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
27+
"github.com/cockroachdb/cockroach/pkg/util/collatedstring"
2728
"github.com/cockroachdb/errors"
2829
"golang.org/x/text/language"
2930
)
@@ -42,8 +43,12 @@ func NormalizeLocaleName(s string) string {
4243
// need to be quoted, and they are considered equivalent to dash characters by
4344
// the CLDR standard: http://cldr.unicode.org/.
4445
func EncodeLocaleName(buf *bytes.Buffer, s string) {
45-
// If possible, try to normalize the case of the locale name.
46-
if normalized, err := language.Parse(s); err == nil {
46+
// If possible, try to normalize the case of the locale name (only for non-default
47+
// locales). Default locales will always be quoted so they can be parsed correctly.
48+
isDefaultLocale := collatedstring.IsDefaultEquivalentCollation(s)
49+
if isDefaultLocale {
50+
s = fmt.Sprintf("%q", s)
51+
} else if normalized, err := language.Parse(s); err == nil {
4752
s = normalized.String()
4853
}
4954
for i, n := 0, len(s); i < n; i++ {

pkg/sql/logictest/testdata/logic_test/create_index

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,33 @@ statement error pgcode 22023 pq: parameter "fillfactor" specified more than once
606606
CREATE INVERTED INDEX IF NOT EXISTS idx_b ON create_inverted_index_duplicate_storage_params_a (b) WITH (fillfactor=10, fillfactor=20);
607607

608608
subtest end
609+
610+
611+
subtest create_index_expression_with_default_collation
612+
613+
statement ok
614+
CREATE TABLE tbl_with_collate (c1 int);
615+
616+
statement ok
617+
CREATE UNIQUE INDEX tbl_with_collate_expr ON tbl_with_collate ((c1::text COLLATE "C"));
618+
619+
statement ok
620+
CREATE UNIQUE INDEX tbl_with_collate_expr2 ON tbl_with_collate ((c1::text COLLATE "POSIX"));
621+
622+
statement ok
623+
CREATE UNIQUE INDEX tbl_with_collate_expr3 ON tbl_with_collate ((c1::text COLLATE "en-us"));
624+
625+
626+
query TT
627+
SHOW CREATE TABLE tbl_with_collate
628+
----
629+
tbl_with_collate CREATE TABLE public.tbl_with_collate (
630+
c1 INT8 NULL,
631+
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
632+
CONSTRAINT tbl_with_collate_pkey PRIMARY KEY (rowid ASC),
633+
UNIQUE INDEX tbl_with_collate_expr ((c1::STRING) ASC),
634+
UNIQUE INDEX tbl_with_collate_expr2 ((c1::STRING) ASC),
635+
UNIQUE INDEX tbl_with_collate_expr3 ((c1::STRING COLLATE en_US) ASC)
636+
)
637+
638+
subtest end

0 commit comments

Comments
 (0)