-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtypes.go
292 lines (264 loc) · 7.95 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// 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
// NumberTypes is a slice containing all numeric types.
var NumberTypes = []T{Int8, Int16, Int32, Int64, Float32, Float64, Decimal}
// IntTypes is a slice containing all int types.
var IntTypes = []T{Int8, Int16, Int32, Int64}
// FloatTypes is a slice containing all float types.
var FloatTypes = []T{Float32, Float64}
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], NumberTypes...)
CompatibleTypes[Int8] = append(CompatibleTypes[Int8], NumberTypes...)
CompatibleTypes[Int16] = append(CompatibleTypes[Int16], NumberTypes...)
CompatibleTypes[Int32] = append(CompatibleTypes[Int32], NumberTypes...)
CompatibleTypes[Int64] = append(CompatibleTypes[Int64], NumberTypes...)
CompatibleTypes[Float32] = append(CompatibleTypes[Float32], NumberTypes...)
CompatibleTypes[Float64] = append(CompatibleTypes[Float64], NumberTypes...)
}
// 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 {
switch t {
case Bytes:
return fmt.Sprintf("%s.Get(%s)", target, i)
}
return fmt.Sprintf("%s[%s]", target, i)
}
// CopyVal is a function that should only be used in templates.
func (t T) CopyVal(dest, src string) string {
switch t {
case Bytes:
return fmt.Sprintf("%[1]s = append(%[1]s[:0], %[2]s...)", dest, src)
case Decimal:
return fmt.Sprintf("%s.Set(&%s)", dest, src)
}
return fmt.Sprintf("%s = %s", dest, src)
}
// Set is a function that should only be used in templates.
func (t T) Set(target, i, new string) string {
switch t {
case Bytes:
return fmt.Sprintf("%s.Set(%s, %s)", target, i, new)
case Decimal:
return fmt.Sprintf("%s[%s].Set(&%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 {
switch t {
case Bytes:
return fmt.Sprintf("%s.Swap(%s, %s)", target, i, j)
case Decimal:
return fmt.Sprintf(`var tmp apd.Decimal
tmp.Set(&%[1]s[%[2]s])
%[1]s[%[2]s].Set(&%[1]s[%[3]s])
%[1]s[%[3]s].Set(&%[1]s[%[2]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 {
switch t {
case Bytes:
return fmt.Sprintf("%s.CopySlice(%s, %s, %s, %s)", target, src, destIdx, srcStartIdx, srcEndIdx)
case Decimal:
return fmt.Sprintf(`
__tgt_slice := %[1]s[%[2]s:]
__src_slice := %[3]s[%[4]s:%[5]s]
for i := range __src_slice {
__tgt_slice[i].Set(&__src_slice[i])
}`, target, destIdx, src, 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 {
switch t {
case Bytes:
return fmt.Sprintf("%s.AppendSlice(%s, %s, %s, %s)", target, src, destIdx, srcStartIdx, srcEndIdx)
case Decimal:
return fmt.Sprintf(`__desiredCap := %[2]s + %[5]s - %[4]s
if cap(%[1]s) >= __desiredCap {
%[1]s = %[1]s[:__desiredCap]
} else {
__new_slice := make([]apd.Decimal, __desiredCap)
copy(__new_slice, %[1]s)
%[1]s = __new_slice
}
__src_slice := %[3]s[%[4]s:%[5]s]
__dst_slice := %[1]s[%[2]s:]
for i := range __src_slice {
__dst_slice[i].Set(&__src_slice[i])
}`, target, destIdx, src, 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 {
switch t {
case Bytes:
return fmt.Sprintf("%s.AppendVal(%s)", target, v)
case Decimal:
return fmt.Sprintf(`%[1]s = append(%[1]s, apd.Decimal{})
%[1]s[len(%[1]s)-1].Set(&%[2]s)`, target, v)
}
return fmt.Sprintf("%[1]s = append(%[1]s, %[2]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 {
switch t {
case Bytes:
return fmt.Sprintf("%s.Zero()", target)
case Decimal:
return fmt.Sprintf(`for n := 0; n < len(%[1]s); n++ {
%[1]s[n].SetInt64(0)
}`, target)
}
return fmt.Sprintf("for n := 0; n < len(%[1]s); n += copy(%[1]s[n:], zero%sColumn) {}", target, t.String())
}