-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
types.go
244 lines (220 loc) · 7.01 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package coltypes
import (
"fmt"
"github.com/cockroachdb/apd"
)
// T represents an exec physical type - a bytes representation of a particular
// column type.
type T int
//go:generate stringer -type=T
const (
// Bool is a column of type bool
Bool T = iota
// Bytes is a column of type []byte
Bytes
// Decimal is a column of type apd.Decimal
Decimal
// Int8 is a column of type int8
Int8
// Int16 is a column of type int16
Int16
// Int32 is a column of type int32
Int32
// Int64 is a column of type int64
Int64
// Float32 is a column of type float32
Float32
// Float64 is a column of type float64
Float64
// Unhandled is a temporary value that represents an unhandled type.
// TODO(jordan): this should be replaced by a panic once all types are
// handled.
Unhandled
)
// AllTypes is slice of all exec types.
var AllTypes []T
// CompatibleTypes maps a type to a slice of types that can be used with that
// type in a binary expression.
var CompatibleTypes map[T][]T
func init() {
for i := Bool; i < Unhandled; i++ {
AllTypes = append(AllTypes, i)
}
CompatibleTypes = make(map[T][]T)
CompatibleTypes[Bool] = append(CompatibleTypes[Bool], Bool)
CompatibleTypes[Bytes] = append(CompatibleTypes[Bytes], Bytes)
CompatibleTypes[Decimal] = append(CompatibleTypes[Decimal], Decimal)
CompatibleTypes[Int8] = append(CompatibleTypes[Int8], Int8)
CompatibleTypes[Int8] = append(CompatibleTypes[Int8], Int16)
CompatibleTypes[Int8] = append(CompatibleTypes[Int8], Int32)
CompatibleTypes[Int8] = append(CompatibleTypes[Int8], Int64)
CompatibleTypes[Int16] = append(CompatibleTypes[Int16], Int8)
CompatibleTypes[Int16] = append(CompatibleTypes[Int16], Int16)
CompatibleTypes[Int16] = append(CompatibleTypes[Int16], Int32)
CompatibleTypes[Int16] = append(CompatibleTypes[Int16], Int64)
CompatibleTypes[Int32] = append(CompatibleTypes[Int32], Int8)
CompatibleTypes[Int32] = append(CompatibleTypes[Int32], Int16)
CompatibleTypes[Int32] = append(CompatibleTypes[Int32], Int32)
CompatibleTypes[Int32] = append(CompatibleTypes[Int32], Int64)
CompatibleTypes[Int64] = append(CompatibleTypes[Int64], Int8)
CompatibleTypes[Int64] = append(CompatibleTypes[Int64], Int16)
CompatibleTypes[Int64] = append(CompatibleTypes[Int64], Int32)
CompatibleTypes[Int64] = append(CompatibleTypes[Int64], Int64)
CompatibleTypes[Float32] = append(CompatibleTypes[Float32], Float32)
CompatibleTypes[Float32] = append(CompatibleTypes[Float32], Float64)
CompatibleTypes[Float64] = append(CompatibleTypes[Float64], Float32)
CompatibleTypes[Float64] = append(CompatibleTypes[Float64], Float64)
}
// FromGoType returns the type for a Go value, if applicable. Shouldn't be used at
// runtime.
func FromGoType(v interface{}) T {
switch t := v.(type) {
case int8:
return Int8
case int16:
return Int16
case int32:
return Int32
case int, int64:
return Int64
case bool:
return Bool
case float32:
return Float32
case float64:
return Float64
case []byte:
return Bytes
case string:
return Bytes
case apd.Decimal:
return Decimal
default:
panic(fmt.Sprintf("type %T not supported yet", t))
}
}
// GoTypeName returns the stringified Go type for an exec type.
func (t T) GoTypeName() string {
switch t {
case Bool:
return "bool"
case Bytes:
return "[]byte"
case Decimal:
return "apd.Decimal"
case Int8:
return "int8"
case Int16:
return "int16"
case Int32:
return "int32"
case Int64:
return "int64"
case Float32:
return "float32"
case Float64:
return "float64"
default:
panic(fmt.Sprintf("unhandled type %d", t))
}
}
// Remove unused warnings for templating functions.
var (
_ = Bool.GoTypeSliceName
_ = Bool.Get
_ = Bool.Set
_ = Bool.Swap
_ = Bool.Slice
_ = Bool.CopySlice
_ = Bool.AppendSlice
_ = Bool.AppendVal
_ = Bool.Len
_ = Bool.Range
_ = Bool.Zero
)
// GoTypeSliceName returns how a slice of the receiver type is represented.
func (t T) GoTypeSliceName() string {
if t == Bytes {
return "*coldata.Bytes"
}
return "[]" + t.GoTypeName()
}
// Get is a function that should only be used in templates.
func (t T) Get(target, i string) string {
if t == Bytes {
return fmt.Sprintf("%s.Get(%s)", target, i)
}
return fmt.Sprintf("%s[%s]", target, i)
}
// Set is a function that should only be used in templates.
func (t T) Set(target, i, new string) string {
if t == Bytes {
return fmt.Sprintf("%s.Set(%s, %s)", target, i, new)
}
return fmt.Sprintf("%s[%s] = %s", target, i, new)
}
// Swap is a function that should only be used in templates.
func (t T) Swap(target, i, j string) string {
if t == Bytes {
return fmt.Sprintf("%s.Swap(%s, %s)", target, i, j)
}
return fmt.Sprintf("%[1]s[%[2]s], %[1]s[%[3]s] = %[1]s[%[3]s], %[1]s[%[2]s]", target, i, j)
}
// Slice is a function that should only be used in templates.
func (t T) Slice(target, start, end string) string {
if t == Bytes {
return fmt.Sprintf("%s.Slice(%s, %s)", target, start, end)
}
return fmt.Sprintf("%s[%s:%s]", target, start, end)
}
// CopySlice is a function that should only be used in templates.
func (t T) CopySlice(target, src, destIdx, srcStartIdx, srcEndIdx string) string {
if t == Bytes {
return fmt.Sprintf("%s.CopySlice(%s, %s, %s, %s)", target, src, destIdx, srcStartIdx, srcEndIdx)
}
return fmt.Sprintf("copy(%s[%s:], %s[%s:%s])", target, destIdx, src, srcStartIdx, srcEndIdx)
}
// AppendSlice is a function that should only be used in templates.
func (t T) AppendSlice(target, src, destIdx, srcStartIdx, srcEndIdx string) string {
if t == Bytes {
return fmt.Sprintf("%s.AppendSlice(%s, %s, %s, %s)", target, src, destIdx, srcStartIdx, srcEndIdx)
}
return fmt.Sprintf("%[1]s = append(%[1]s[:%s], %s[%s:%s]...)", target, destIdx, src, srcStartIdx, srcEndIdx)
}
// AppendVal is a function that should only be used in templates.
func (t T) AppendVal(target, v string) string {
if t == Bytes {
return fmt.Sprintf("%s.AppendVal(%s)", target, v)
}
return fmt.Sprintf("%[1]s = append(%[1]s, %s)", target, v)
}
// Len is a function that should only be used in templates.
func (t T) Len(target string) string {
if t == Bytes {
return fmt.Sprintf("%s.Len()", target)
}
return fmt.Sprintf("len(%s)", target)
}
// Range is a function that should only be used in templates.
func (t T) Range(loopVariableIdent string, target string) string {
if t == Bytes {
return fmt.Sprintf("%[1]s := 0; %[1]s < %[2]s.Len(); %[1]s++", loopVariableIdent, target)
}
return fmt.Sprintf("%[1]s := range %[2]s", loopVariableIdent, target)
}
// Zero is a function that should only be used in templates.
func (t T) Zero(target string) string {
if t == Bytes {
return fmt.Sprintf("%s.Zero()", target)
}
return fmt.Sprintf("for n := 0; n < len(%[1]s); n += copy(%[1]s[n:], zero%sColumn) {}", target, t.String())
}