From 59d7bd1da69007a1a22d1eacb9faae1cc25d4af3 Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Wed, 22 Aug 2018 17:28:20 +0200 Subject: [PATCH] sql/coltypes: remove the field `Name` in TArray It was not needed. This simplifies. Release note: None --- pkg/sql/coltypes/aliases.go | 2 +- pkg/sql/coltypes/arrays.go | 16 ++++++++++++---- pkg/sql/parser/parse_test.go | 2 ++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/sql/coltypes/aliases.go b/pkg/sql/coltypes/aliases.go index 568e2d98d950..7ea3703d2219 100644 --- a/pkg/sql/coltypes/aliases.go +++ b/pkg/sql/coltypes/aliases.go @@ -142,7 +142,7 @@ func ArrayOf(colType T, bounds []int32) (T, error) { if !canBeInArrayColType(colType) { return nil, pgerror.NewErrorf(pgerror.CodeFeatureNotSupportedError, "arrays of %s not allowed", colType) } - return &TArray{Name: colType.String() + "[]", ParamType: colType, Bounds: bounds}, nil + return &TArray{ParamType: colType, Bounds: bounds}, nil } var typNameLiterals map[string]T diff --git a/pkg/sql/coltypes/arrays.go b/pkg/sql/coltypes/arrays.go index db9cb3e3b355..3d57161880fc 100644 --- a/pkg/sql/coltypes/arrays.go +++ b/pkg/sql/coltypes/arrays.go @@ -22,21 +22,29 @@ import ( // TArray represents an ARRAY column type. type TArray struct { - Name string // ParamTyp is the type of the elements in this array. ParamType T Bounds []int32 } // TypeName implements the ColTypeFormatter interface. -func (node *TArray) TypeName() string { return node.Name } +func (node *TArray) TypeName() string { + return node.ParamType.TypeName() + "[]" +} // Format implements the ColTypeFormatter interface. func (node *TArray) Format(buf *bytes.Buffer, f lex.EncodeFlags) { - buf.WriteString(node.Name) if collation, ok := node.ParamType.(*TCollatedString); ok { - buf.WriteString(" COLLATE ") + // We cannot use node.ParamType.Format() directly here (and DRY + // across the two branches of the if) because if we have an array + // of collated strings, the COLLATE string must appear after the + // square brackets. + collation.TString.Format(buf, f) + buf.WriteString("[] COLLATE ") lex.EncodeUnrestrictedSQLIdent(buf, collation.Locale, f) + } else { + node.ParamType.Format(buf, f) + buf.WriteString("[]") } } diff --git a/pkg/sql/parser/parse_test.go b/pkg/sql/parser/parse_test.go index 100fabbf2ef7..889c54d99c37 100644 --- a/pkg/sql/parser/parse_test.go +++ b/pkg/sql/parser/parse_test.go @@ -231,7 +231,9 @@ func TestParse(t *testing.T) { {`CREATE TABLE a AS SELECT * FROM b UNION VALUES ('one', 1) ORDER BY c LIMIT 5`}, {`CREATE TABLE IF NOT EXISTS a AS SELECT * FROM b UNION VALUES ('one', 1) ORDER BY c LIMIT 5`}, {`CREATE TABLE a (b STRING COLLATE "DE")`}, + {`CREATE TABLE a (b STRING(3) COLLATE "DE")`}, {`CREATE TABLE a (b STRING[] COLLATE "DE")`}, + {`CREATE TABLE a (b STRING(3)[] COLLATE "DE")`}, {`CREATE VIEW a AS SELECT * FROM b`}, {`CREATE VIEW a AS SELECT b.* FROM b LIMIT 5`},