Skip to content

Commit

Permalink
Merge #28959
Browse files Browse the repository at this point in the history
28959: sql/coltypes: remove the field `Name` in TArray r=knz a=knz

First commits from #28955 and priors.
Forked from #28690.

It was not needed. This simplifies.

Release note: None

Co-authored-by: Raphael 'kena' Poss <[email protected]>
  • Loading branch information
craig[bot] and knz committed Aug 23, 2018
2 parents de89a6c + f64a081 commit af862b3
Show file tree
Hide file tree
Showing 58 changed files with 845 additions and 569 deletions.
2 changes: 1 addition & 1 deletion docs/RFCS/20170317_settings_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ A new system.settings table, keyed by string settings names would be created.
CREATE TABLE system.settings (
name STRING PRIMARY KEY,
value STRING,
updated TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
updated TIMESTAMPTZ DEFAULT NOW() NOT NULL,
valueType char NOT NULL DEFAULT 's',
)
```
Expand Down
10 changes: 5 additions & 5 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -1887,8 +1887,8 @@ when_clause ::=
'WHEN' a_expr 'THEN' a_expr

character_base ::=
'CHARACTER' opt_varying
| 'CHAR' opt_varying
char_aliases
| char_aliases 'VARYING'
| 'VARCHAR'
| 'STRING'

Expand Down Expand Up @@ -2040,9 +2040,9 @@ tuple1_unambiguous_values ::=
a_expr ','
| a_expr ',' expr_list

opt_varying ::=
'VARYING'
|
char_aliases ::=
'CHAR'
| 'CHARACTER'

window_definition ::=
window_name 'AS' window_specification
Expand Down
4 changes: 2 additions & 2 deletions pkg/ccl/importccl/import_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,12 @@ COPY t (a, b, c) FROM stdin;

const (
testPgdumpCreateCities = `CREATE TABLE cities (
city STRING(80) NOT NULL,
city VARCHAR(80) NOT NULL,
CONSTRAINT cities_pkey PRIMARY KEY (city ASC),
FAMILY "primary" (city)
)`
testPgdumpCreateWeather = `CREATE TABLE weather (
city STRING(80) NULL,
city VARCHAR(80) NULL,
temp_lo INTEGER NULL,
temp_hi INTEGER NULL,
prcp FLOAT4 NULL,
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/importccl/read_import_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ func strOpt(in *coltypes.TString, i int) coltypes.T {
return in
}
res := *in
res.N = i
res.N = uint(i)
return &res
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/partitionccl/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ func allPartitioningTests(rng *rand.Rand) []partitioningTest {
case sqlbase.ColumnType_COLLATEDSTRING:
typ.Locale = sqlbase.RandCollationLocale(rng)
colType = fmt.Sprintf(`STRING COLLATE %s`, *typ.Locale)
case sqlbase.ColumnType_JSON:
case sqlbase.ColumnType_JSONB:
// Not indexable.
continue
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ func dumpTableData(w io.Writer, conn *sqlConn, clusterTS string, bmd basicMetada
if err != nil {
return err
}
case coltypes.JSON, coltypes.JSONB:
case coltypes.JSON:
d, err = tree.ParseDJSON(string(t))
if err != nil {
return err
Expand Down
8 changes: 5 additions & 3 deletions pkg/cli/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func TestDumpRandom(t *testing.T) {
fr real,
d date,
m timestamp,
mtz timestamptz,
n interval,
o bool,
e decimal,
Expand All @@ -212,7 +213,7 @@ func TestDumpRandom(t *testing.T) {
u uuid,
ip inet,
j json,
PRIMARY KEY (rowid, i, si, bi, f, fr, d, m, n, o, e, s, b, u, ip)
PRIMARY KEY (rowid, i, si, bi, f, fr, d, m, mtz, n, o, e, s, b, u, ip)
);
SET extra_float_digits = 3;
`, nil); err != nil {
Expand Down Expand Up @@ -287,6 +288,7 @@ func TestDumpRandom(t *testing.T) {
f, // fr
d,
m,
m,
[]byte(n), // intervals come out as `[]byte`s
o,
[]byte(e), // decimals come out as `[]byte`s
Expand All @@ -296,14 +298,14 @@ func TestDumpRandom(t *testing.T) {
[]byte(ip.String()),
[]byte(j.String()),
}
if err := conn.Exec("INSERT INTO d.t VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)", vals); err != nil {
if err := conn.Exec("INSERT INTO d.t VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)", vals); err != nil {
t.Fatal(err)
}
generatedRows = append(generatedRows, vals[1:])
}

check := func(table string) {
q := fmt.Sprintf("SELECT i, si, bi, f, fr, d, m, n, o, e, s, b, u, ip, j FROM %s ORDER BY rowid", table)
q := fmt.Sprintf("SELECT i, si, bi, f, fr, d, m, mtz, n, o, e, s, b, u, ip, j FROM %s ORDER BY rowid", table)
nrows, err := conn.Query(q, nil)
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/testdata/dump/inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ dump d t
----
----
CREATE TABLE t (
a JSON NULL,
b JSON NULL,
a JSONB NULL,
b JSONB NULL,
INVERTED INDEX idx (a),
INVERTED INDEX idx2 (b),
FAMILY "primary" (a, b, rowid)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/testdata/dump/row
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ CREATE TABLE t (
e DECIMAL NULL,
u UUID NULL,
ip INET NULL,
j JSON NULL,
j JSONB NULL,
ary STRING[] NULL,
tz TIMESTAMP WITH TIME ZONE NULL,
tz TIMESTAMPTZ NULL,
e1 DECIMAL(2) NULL,
e2 DECIMAL(2,1) NULL,
s1 STRING(1) NULL,
Expand Down
22 changes: 10 additions & 12 deletions pkg/sql/coltypes/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ var (
// Interval is an immutable T instance.
Interval = &TInterval{}

// Char is an immutable T instance.
Char = &TString{Name: "CHAR"}
// VarChar is an immutable T instance.
VarChar = &TString{Name: "VARCHAR"}
// String is an immutable T instance.
String = &TString{Name: "STRING"}
// QChar is an immutable T instance.
QChar = &TString{Name: `"char"`}
// Char is an immutable T instance. See strings.go for details.
Char = &TString{Variant: TStringVariantCHAR, N: 1}
// VarChar is an immutable T instance. See strings.go for details.
VarChar = &TString{Variant: TStringVariantVARCHAR}
// String is an immutable T instance. See strings.go for details.
String = &TString{Variant: TStringVariantSTRING}
// QChar is an immutable T instance. See strings.go for details.
QChar = &TString{Variant: TStringVariantQCHAR}

// Name is an immutable T instance.
Name = &TName{}
Expand All @@ -99,9 +99,7 @@ var (
INet = &TIPAddr{}

// JSON is an immutable T instance.
JSON = &TJSON{Name: "JSON"}
// JSONB is an immutable T instance.
JSONB = &TJSON{Name: "JSONB"}
JSON = &TJSON{}

// Oid is an immutable T instance.
Oid = &TOid{Name: "OID"}
Expand Down Expand Up @@ -144,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
Expand Down
16 changes: 12 additions & 4 deletions pkg/sql/coltypes/arrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("[]")
}
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/coltypes/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ func DatumTypeToColumnType(t types.T) (T, error) {

switch typ := t.(type) {
case types.TCollatedString:
return &TCollatedString{Name: "STRING", Locale: typ.Locale}, nil
return &TCollatedString{
TString: TString{Variant: TStringVariantSTRING},
Locale: typ.Locale,
}, nil
case types.TArray:
elemTyp, err := DatumTypeToColumnType(typ.Typ)
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions pkg/sql/coltypes/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ func (node *TIPAddr) Format(buf *bytes.Buffer, f lex.EncodeFlags) {
}

// TJSON represents the JSON column type.
type TJSON struct {
Name string
}
type TJSON struct{}

// TypeName implements the ColTypeFormatter interface.
func (node *TJSON) TypeName() string { return node.Name }
func (node *TJSON) TypeName() string { return "JSONB" }

// Format implements the ColTypeFormatter interface.
func (node *TJSON) Format(buf *bytes.Buffer, _ lex.EncodeFlags) {
buf.WriteString(node.Name)
buf.WriteString(node.TypeName())
}

// TOid represents an OID type, which is the type of system object
Expand Down
85 changes: 71 additions & 14 deletions pkg/sql/coltypes/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,77 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/lex"
)

// TStringVariant distinguishes between flavors of string
// types. Even though values of string types of different variants
// have the same on-disk and in-memory repr, the type metadata for
// them has different behavior wrt pretty-printing and introspection.
type TStringVariant int

const (
// TStringVariantSTRING is the canonical CockroachDB string type.
//
// It is reported as STRING in SHOW CREATE but "text" in
// introspection for compatibility with PostgreSQL.
//
// It has no default maximum width.
TStringVariantSTRING TStringVariant = iota

// TStringVariantCHAR is the "standard SQL" string type of maximum length.
//
// It is reported as CHAR in SHOW CREATE and "character" in
// introspection for compatibility with PostgreSQL.
//
// Its default maximum with is 1. It always has a maximum width.
TStringVariantCHAR

// TStringVariantVARCHAR is the "standard SQL" string type of varying
// length.
//
// It is reported as VARCHAR in SHOW CREATE and "character varying"
// in introspection for compatibility with PostgreSQL.
//
// It has no default maximum length but can be associated with one
// in the syntax.
TStringVariantVARCHAR

// TStringVariantQCHAR is a special PostgreSQL-only type supported for
// compatibility. It behaves like VARCHAR, its maximum width cannot
// be modified, and has a peculiar name in the syntax and
// introspection.
//
// It is reported as "char" (with double quotes included) in SHOW
// CREATE and "char" in introspection for compatibility
// with PostgreSQL.
TStringVariantQCHAR
)

// String implements the fmt.Stringer interface.
func (v TStringVariant) String() string {
switch v {
case TStringVariantCHAR:
return "CHAR"
case TStringVariantVARCHAR:
return "VARCHAR"
case TStringVariantQCHAR:
return `"char"`
default:
return "STRING"
}
}

// TString represents a STRING, CHAR or VARCHAR type.
type TString struct {
Name string
N int
Variant TStringVariant
N uint
}

// TypeName implements the ColTypeFormatter interface.
func (node *TString) TypeName() string { return node.Name }
func (node *TString) TypeName() string { return node.Variant.String() }

// Format implements the ColTypeFormatter interface.
func (node *TString) Format(buf *bytes.Buffer, f lex.EncodeFlags) {
buf.WriteString(node.Name)
if node.N > 0 {
buf.WriteString(node.TypeName())
if !(node.Variant == TStringVariantCHAR && node.N == 1) && node.N > 0 {
fmt.Fprintf(buf, "(%d)", node.N)
}
}
Expand All @@ -60,21 +118,20 @@ func (node *TBytes) Format(buf *bytes.Buffer, f lex.EncodeFlags) {
buf.WriteString(node.TypeName())
}

// TCollatedString represents a STRING, CHAR or VARCHAR type with a
// collation locale.
// TCollatedString represents a STRING, CHAR, QCHAR or VARCHAR type
// with a collation locale.
type TCollatedString struct {
Name string
N int
TString
Locale string
}

// TypeName implements the ColTypeFormatter interface.
func (node *TCollatedString) TypeName() string { return node.Name }

// Format implements the ColTypeFormatter interface.
func (node *TCollatedString) Format(buf *bytes.Buffer, f lex.EncodeFlags) {
buf.WriteString(node.Name)
if node.N > 0 {
buf.WriteString(node.TypeName())
// In general, if there is a specified width we want to print it next
// to the type. However, in the specific case of CHAR, the default
// is 1 and the width should be omitted in that case.
if node.N > 0 && !(node.Variant == TStringVariantCHAR && node.N == 1) {
fmt.Fprintf(buf, "(%d)", node.N)
}
buf.WriteString(" COLLATE ")
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/coltypes/timedate.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (node *TTimestampTZ) TypeName() string { return "TIMESTAMPTZ" }

// Format implements the ColTypeFormatter interface.
func (node *TTimestampTZ) Format(buf *bytes.Buffer, f lex.EncodeFlags) {
buf.WriteString("TIMESTAMP WITH TIME ZONE")
buf.WriteString(node.TypeName())
}

// TInterval represents an INTERVAL type
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/copy_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestCopyNullInfNaN(t *testing.T) {
e DECIMAL NULL,
u UUID NULL,
ip INET NULL,
tz TIMESTAMP WITH TIME ZONE NULL
tz TIMESTAMPTZ NULL
);
`); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestCopyRandom(t *testing.T) {
b BYTES,
u UUID,
ip INET,
tz TIMESTAMP WITH TIME ZONE
tz TIMESTAMPTZ
);
SET extra_float_digits = 3; -- to preserve floats entirely
`); err != nil {
Expand Down
Loading

0 comments on commit af862b3

Please sign in to comment.