Skip to content

Commit 182daab

Browse files
committed
initial commit
0 parents  commit 182daab

File tree

652 files changed

+144888
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

652 files changed

+144888
-0
lines changed

coltypes/aliases.go

+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
// Copyright 2017 The Cockroach Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License.
14+
15+
package coltypes
16+
17+
import (
18+
"strings"
19+
20+
"github.com/lib/pq/oid"
21+
22+
"github.com/eandre/sqlparse/pkg/util/pgerror"
23+
"github.com/eandre/sqlparse/sem/types"
24+
)
25+
26+
var (
27+
// Bool is an immutable T instance.
28+
Bool = &TBool{}
29+
30+
// Bit is an immutable T instance.
31+
Bit = &TBitArray{Width: 1}
32+
// VarBit is an immutable T instance.
33+
VarBit = &TBitArray{Width: 0, Variable: true}
34+
35+
// Int is an immutable T instance.
36+
Int = &TInt{}
37+
// Int2 is an immutable T instance.
38+
Int2 = &TInt{Width: 16}
39+
// Int4 is an immutable T instance.
40+
Int4 = &TInt{Width: 32}
41+
// Int8 is an immutable T instance.
42+
Int8 = &TInt{Width: 64}
43+
44+
// Serial is an immutable T instance.
45+
Serial = &TSerial{TInt: Int}
46+
// Serial2 is an immutable T instance.
47+
Serial2 = &TSerial{TInt: Int2}
48+
// Serial4 is an immutable T instance.
49+
Serial4 = &TSerial{TInt: Int4}
50+
// Serial8 is an immutable T instance.
51+
Serial8 = &TSerial{TInt: Int8}
52+
53+
// Float4 is an immutable T instance.
54+
Float4 = &TFloat{Short: true}
55+
// Float8 is an immutable T instance.
56+
Float8 = &TFloat{}
57+
58+
// Decimal is an immutable T instance.
59+
Decimal = &TDecimal{}
60+
61+
// Date is an immutable T instance.
62+
Date = &TDate{}
63+
64+
// Time is an immutable T instance.
65+
Time = &TTime{}
66+
67+
// Timestamp is an immutable T instance.
68+
Timestamp = &TTimestamp{}
69+
// TimestampWithTZ is an immutable T instance.
70+
TimestampWithTZ = &TTimestampTZ{}
71+
72+
// Interval is an immutable T instance.
73+
Interval = &TInterval{}
74+
75+
// Char is an immutable T instance. See strings.go for details.
76+
Char = &TString{Variant: TStringVariantCHAR, N: 1}
77+
// VarChar is an immutable T instance. See strings.go for details.
78+
VarChar = &TString{Variant: TStringVariantVARCHAR}
79+
// String is an immutable T instance. See strings.go for details.
80+
String = &TString{Variant: TStringVariantSTRING}
81+
// QChar is an immutable T instance. See strings.go for details.
82+
QChar = &TString{Variant: TStringVariantQCHAR}
83+
84+
// Name is an immutable T instance.
85+
Name = &TName{}
86+
87+
// Bytes is an immutable T instance.
88+
Bytes = &TBytes{}
89+
90+
// Int2vector is an immutable T instance.
91+
Int2vector = &TVector{Name: "INT2VECTOR", ParamType: Int}
92+
93+
// UUID is an immutable T instance.
94+
UUID = &TUUID{}
95+
96+
// INet is an immutable T instance.
97+
INet = &TIPAddr{}
98+
99+
// JSON is an immutable T instance.
100+
JSON = &TJSON{}
101+
102+
// Oid is an immutable T instance.
103+
Oid = &TOid{Name: "OID"}
104+
// RegClass is an immutable T instance.
105+
RegClass = &TOid{Name: "REGCLASS"}
106+
// RegNamespace is an immutable T instance.
107+
RegNamespace = &TOid{Name: "REGNAMESPACE"}
108+
// RegProc is an immutable T instance.
109+
RegProc = &TOid{Name: "REGPROC"}
110+
// RegProcedure is an immutable T instance.
111+
RegProcedure = &TOid{Name: "REGPROCEDURE"}
112+
// RegType is an immutable T instance.
113+
RegType = &TOid{Name: "REGTYPE"}
114+
115+
// OidVector is an immutable T instance.
116+
OidVector = &TVector{Name: "OIDVECTOR", ParamType: Oid}
117+
)
118+
119+
var errBitLengthNotPositive = pgerror.NewError(pgerror.CodeInvalidParameterValueError,
120+
"length for type bit must be at least 1")
121+
122+
// NewBitArrayType creates a new BIT type with the given bit width.
123+
func NewBitArrayType(width int, varying bool) (*TBitArray, error) {
124+
if width < 1 {
125+
return nil, errBitLengthNotPositive
126+
}
127+
return &TBitArray{Width: uint(width), Variable: varying}, nil
128+
}
129+
130+
var errFloatPrecAtLeast1 = pgerror.NewError(pgerror.CodeInvalidParameterValueError,
131+
"precision for type float must be at least 1 bit")
132+
var errFloatPrecMax54 = pgerror.NewError(pgerror.CodeInvalidParameterValueError,
133+
"precision for type float must be less than 54 bits")
134+
135+
// NewFloat creates a type alias for FLOAT with the given precision.
136+
func NewFloat(prec int64) (*TFloat, error) {
137+
if prec < 1 {
138+
return nil, errFloatPrecAtLeast1
139+
}
140+
if prec <= 24 {
141+
return Float4, nil
142+
}
143+
if prec <= 54 {
144+
return Float8, nil
145+
}
146+
return nil, errFloatPrecMax54
147+
}
148+
149+
// ArrayOf creates a type alias for an array of the given element type and fixed bounds.
150+
func ArrayOf(colType T, bounds []int32) (T, error) {
151+
if !canBeInArrayColType(colType) {
152+
return nil, pgerror.NewErrorf(pgerror.CodeFeatureNotSupportedError, "arrays of %s not allowed", colType)
153+
}
154+
return &TArray{ParamType: colType, Bounds: bounds}, nil
155+
}
156+
157+
var typNameLiterals map[string]T
158+
159+
func init() {
160+
typNameLiterals = make(map[string]T)
161+
for o, t := range types.OidToType {
162+
name := strings.ToLower(oid.TypeName[o])
163+
if _, ok := typNameLiterals[name]; !ok {
164+
colTyp, err := DatumTypeToColumnType(t)
165+
if err != nil {
166+
continue
167+
}
168+
typNameLiterals[name] = colTyp
169+
}
170+
}
171+
}
172+
173+
// TypeForNonKeywordTypeName returns the column type for the string name of a
174+
// type, if one exists. The third return value indicates:
175+
// 0 if no error or the type is not known in postgres.
176+
// -1 if the type is known in postgres.
177+
// >0 for a github issue number.
178+
func TypeForNonKeywordTypeName(name string) (T, bool, int) {
179+
t, ok := typNameLiterals[name]
180+
if ok {
181+
return t, ok, 0
182+
}
183+
return nil, false, postgresPredefinedTypeIssues[name]
184+
}
185+
186+
// The following map must include all types predefined in PostgreSQL
187+
// that are also not yet defined in CockroachDB and link them to
188+
// github issues. It is also possible, but not necessary, to include
189+
// PostgreSQL types that are already implemented in CockroachDB.
190+
var postgresPredefinedTypeIssues = map[string]int{
191+
"box": 21286,
192+
"cidr": 18846,
193+
"circle": 21286,
194+
"line": 21286,
195+
"lseg": 21286,
196+
"macaddr": -1,
197+
"macaddr8": -1,
198+
"money": -1,
199+
"path": 21286,
200+
"pg_lsn": -1,
201+
"point": 21286,
202+
"polygon": 21286,
203+
"tsquery": 7821,
204+
"tsvector": 7821,
205+
"txid_snapshot": -1,
206+
"xml": -1,
207+
}

coltypes/arith.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright 2017 The Cockroach Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License.
14+
15+
package coltypes
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
21+
"github.com/eandre/sqlparse/lex"
22+
)
23+
24+
// TBool represents a BOOLEAN type.
25+
type TBool struct{}
26+
27+
// TypeName implements the ColTypeFormatter interface.
28+
func (node *TBool) TypeName() string { return "BOOL" }
29+
30+
// Format implements the ColTypeFormatter interface.
31+
func (node *TBool) Format(buf *bytes.Buffer, f lex.EncodeFlags) {
32+
buf.WriteString(node.TypeName())
33+
}
34+
35+
// TInt represents an INT, INTEGER, SMALLINT or BIGINT type.
36+
type TInt struct {
37+
Width int
38+
}
39+
40+
// IntegerTypeNames maps a TInt data width to a canonical type name.
41+
var IntegerTypeNames = map[int]string{
42+
0: "INT",
43+
16: "INT2",
44+
32: "INT4",
45+
64: "INT8",
46+
}
47+
48+
// TypeName implements the ColTypeFormatter interface.
49+
func (node *TInt) TypeName() string { return IntegerTypeNames[node.Width] }
50+
51+
// Format implements the ColTypeFormatter interface.
52+
func (node *TInt) Format(buf *bytes.Buffer, f lex.EncodeFlags) {
53+
buf.WriteString(node.TypeName())
54+
}
55+
56+
// TSerial represents a SERIAL type.
57+
type TSerial struct{ *TInt }
58+
59+
var serialNames = map[int]string{
60+
0: "SERIAL",
61+
16: "SERIAL2",
62+
32: "SERIAL4",
63+
64: "SERIAL8",
64+
}
65+
66+
// TypeName implements the ColTypeFormatter interface.
67+
func (node *TSerial) TypeName() string { return serialNames[node.Width] }
68+
69+
// Format implements the ColTypeFormatter interface.
70+
func (node *TSerial) Format(buf *bytes.Buffer, _ lex.EncodeFlags) {
71+
buf.WriteString(node.TypeName())
72+
}
73+
74+
// TFloat represents a REAL or DOUBLE type.
75+
type TFloat struct{ Short bool }
76+
77+
// TypeName implements the ColTypeFormatter interface.
78+
func (node *TFloat) TypeName() string {
79+
if node.Short {
80+
return "FLOAT4"
81+
}
82+
return "FLOAT8"
83+
}
84+
85+
// Format implements the ColTypeFormatter interface.
86+
func (node *TFloat) Format(buf *bytes.Buffer, f lex.EncodeFlags) {
87+
buf.WriteString(node.TypeName())
88+
}
89+
90+
// TDecimal represents a DECIMAL or NUMERIC type.
91+
type TDecimal struct {
92+
Prec int
93+
Scale int
94+
}
95+
96+
// TypeName implements the ColTypeFormatter interface.
97+
func (node *TDecimal) TypeName() string { return "DECIMAL" }
98+
99+
// Format implements the ColTypeFormatter interface.
100+
func (node *TDecimal) Format(buf *bytes.Buffer, f lex.EncodeFlags) {
101+
buf.WriteString(node.TypeName())
102+
if node.Prec > 0 {
103+
fmt.Fprintf(buf, "(%d", node.Prec)
104+
if node.Scale > 0 {
105+
fmt.Fprintf(buf, ",%d", node.Scale)
106+
}
107+
buf.WriteByte(')')
108+
}
109+
}

coltypes/arrays.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2017 The Cockroach Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License.
14+
15+
package coltypes
16+
17+
import (
18+
"bytes"
19+
20+
"github.com/eandre/sqlparse/lex"
21+
)
22+
23+
// TArray represents an ARRAY column type.
24+
type TArray struct {
25+
// ParamTyp is the type of the elements in this array.
26+
ParamType T
27+
Bounds []int32
28+
}
29+
30+
// TypeName implements the ColTypeFormatter interface.
31+
func (node *TArray) TypeName() string {
32+
return node.ParamType.TypeName() + "[]"
33+
}
34+
35+
// Format implements the ColTypeFormatter interface.
36+
func (node *TArray) Format(buf *bytes.Buffer, f lex.EncodeFlags) {
37+
if collation, ok := node.ParamType.(*TCollatedString); ok {
38+
// We cannot use node.ParamType.Format() directly here (and DRY
39+
// across the two branches of the if) because if we have an array
40+
// of collated strings, the COLLATE string must appear after the
41+
// square brackets.
42+
collation.TString.Format(buf, f)
43+
buf.WriteString("[] COLLATE ")
44+
lex.EncodeUnrestrictedSQLIdent(buf, collation.Locale, f)
45+
} else {
46+
node.ParamType.Format(buf, f)
47+
buf.WriteString("[]")
48+
}
49+
}
50+
51+
// canBeInArrayColType returns true if the given T is a valid
52+
// element type for an array column type.
53+
func canBeInArrayColType(t T) bool {
54+
switch t.(type) {
55+
case *TJSON:
56+
return false
57+
default:
58+
return true
59+
}
60+
}
61+
62+
// TVector is the base for VECTOR column types, which are Postgres's
63+
// older, limited version of ARRAYs. These are not meant to be persisted,
64+
// because ARRAYs are a strict superset.
65+
type TVector struct {
66+
Name string
67+
ParamType T
68+
}
69+
70+
// TypeName implements the ColTypeFormatter interface.
71+
func (node *TVector) TypeName() string { return node.Name }
72+
73+
// Format implements the ColTypeFormatter interface.
74+
func (node *TVector) Format(buf *bytes.Buffer, _ lex.EncodeFlags) {
75+
buf.WriteString(node.TypeName())
76+
}

0 commit comments

Comments
 (0)