forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatype.go
244 lines (227 loc) · 6.77 KB
/
datatype.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 (c) 2017-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"bytes"
"database/sql"
"fmt"
"strings"
)
type snowflakeType int
const (
fixedType snowflakeType = iota
realType
textType
dateType
variantType
timestampLtzType
timestampNtzType
timestampTzType
objectType
arrayType
binaryType
timeType
booleanType
nullType
// the following are not snowflake types per se but internal types
sliceType
changeType
unSupportedType
)
var snowflakeToDriverType = map[string]snowflakeType{
"FIXED": fixedType,
"REAL": realType,
"TEXT": textType,
"DATE": dateType,
"VARIANT": variantType,
"TIMESTAMP_LTZ": timestampLtzType,
"TIMESTAMP_NTZ": timestampNtzType,
"TIMESTAMP_TZ": timestampTzType,
"OBJECT": objectType,
"ARRAY": arrayType,
"BINARY": binaryType,
"TIME": timeType,
"BOOLEAN": booleanType,
"NULL": nullType,
"SLICE": sliceType,
"CHANGE_TYPE": changeType,
"NOT_SUPPORTED": unSupportedType}
var driverTypeToSnowflake = invertMap(snowflakeToDriverType)
func invertMap(m map[string]snowflakeType) map[snowflakeType]string {
inv := make(map[snowflakeType]string)
for k, v := range m {
if _, ok := inv[v]; ok {
panic("failed to create driverTypeToSnowflake map due to duplicated values")
}
inv[v] = k
}
return inv
}
func (st snowflakeType) Byte() byte {
return byte(st)
}
func (st snowflakeType) String() string {
return driverTypeToSnowflake[st]
}
func getSnowflakeType(typ string) snowflakeType {
return snowflakeToDriverType[strings.ToUpper(typ)]
}
// SnowflakeDataType is the type used by clients to explicitly indicate the type
// of an argument to ExecContext and friends. We use a separate public-facing
// type rather than a Go primitive type so that we can always differentiate
// between args that indicate type and args that are values.
type SnowflakeDataType []byte
// Equals checks if dt and o represent the same type indicator
func (dt SnowflakeDataType) Equals(o SnowflakeDataType) bool {
return bytes.Equal(([]byte)(dt), ([]byte)(o))
}
var (
// DataTypeFixed is a FIXED datatype.
DataTypeFixed = SnowflakeDataType{fixedType.Byte()}
// DataTypeReal is a REAL datatype.
DataTypeReal = SnowflakeDataType{realType.Byte()}
// DataTypeText is a TEXT datatype.
DataTypeText = SnowflakeDataType{textType.Byte()}
// DataTypeDate is a Date datatype.
DataTypeDate = SnowflakeDataType{dateType.Byte()}
// DataTypeVariant is a TEXT datatype.
DataTypeVariant = SnowflakeDataType{variantType.Byte()}
// DataTypeTimestampLtz is a TIMESTAMP_LTZ datatype.
DataTypeTimestampLtz = SnowflakeDataType{timestampLtzType.Byte()}
// DataTypeTimestampNtz is a TIMESTAMP_NTZ datatype.
DataTypeTimestampNtz = SnowflakeDataType{timestampNtzType.Byte()}
// DataTypeTimestampTz is a TIMESTAMP_TZ datatype.
DataTypeTimestampTz = SnowflakeDataType{timestampTzType.Byte()}
// DataTypeObject is a OBJECT datatype.
DataTypeObject = SnowflakeDataType{objectType.Byte()}
// DataTypeArray is a ARRAY datatype.
DataTypeArray = SnowflakeDataType{arrayType.Byte()}
// DataTypeBinary is a BINARY datatype.
DataTypeBinary = SnowflakeDataType{binaryType.Byte()}
// DataTypeTime is a TIME datatype.
DataTypeTime = SnowflakeDataType{timeType.Byte()}
// DataTypeBoolean is a BOOLEAN datatype.
DataTypeBoolean = SnowflakeDataType{booleanType.Byte()}
// DataTypeNull is a NULL datatype.
DataTypeNull = SnowflakeDataType{nullType.Byte()}
)
func clientTypeToInternal(cType SnowflakeDataType) (iType snowflakeType, err error) {
if cType != nil {
switch {
case cType.Equals(DataTypeFixed):
iType = fixedType
case cType.Equals(DataTypeReal):
iType = realType
case cType.Equals(DataTypeText):
iType = textType
case cType.Equals(DataTypeDate):
iType = dateType
case cType.Equals(DataTypeVariant):
iType = variantType
case cType.Equals(DataTypeTimestampLtz):
iType = timestampLtzType
case cType.Equals(DataTypeTimestampNtz):
iType = timestampNtzType
case cType.Equals(DataTypeTimestampTz):
iType = timestampTzType
case cType.Equals(DataTypeObject):
iType = objectType
case cType.Equals(DataTypeArray):
iType = arrayType
case cType.Equals(DataTypeBinary):
iType = binaryType
case cType.Equals(DataTypeTime):
iType = timeType
case cType.Equals(DataTypeBoolean):
iType = booleanType
case cType.Equals(DataTypeNull):
iType = nullType
default:
return nullType, fmt.Errorf(errMsgInvalidByteArray, ([]byte)(cType))
}
} else {
return nullType, fmt.Errorf(errMsgInvalidByteArray, nil)
}
return iType, nil
}
// SnowflakeParameter includes the columns output from SHOW PARAMETER command.
type SnowflakeParameter struct {
Key string
Value string
Default string
Level string
Description string
SetByUser string
SetInJob string
SetOn string
SetByThreadID string
SetByThreadName string
SetByClass string
ParameterComment string
Type string
IsExpired string
ExpiresAt string
SetByControllingParameter string
ActivateVersion string
PartialRollout string
Unknown string // Reserve for added parameter
}
func populateSnowflakeParameter(colname string, p *SnowflakeParameter) interface{} {
switch colname {
case "key":
return &p.Key
case "value":
return &p.Value
case "default":
return &p.Default
case "level":
return &p.Level
case "description":
return &p.Description
case "set_by_user":
return &p.SetByUser
case "set_in_job":
return &p.SetInJob
case "set_on":
return &p.SetOn
case "set_by_thread_id":
return &p.SetByThreadID
case "set_by_thread_name":
return &p.SetByThreadName
case "set_by_class":
return &p.SetByClass
case "parameter_comment":
return &p.ParameterComment
case "type":
return &p.Type
case "is_expired":
return &p.IsExpired
case "expires_at":
return &p.ExpiresAt
case "set_by_controlling_parameter":
return &p.SetByControllingParameter
case "activate_version":
return &p.ActivateVersion
case "partial_rollout":
return &p.PartialRollout
default:
debugPanicf("unknown type: %v", colname)
return &p.Unknown
}
}
// ScanSnowflakeParameter binds SnowflakeParameter variable with an array of column buffer.
func ScanSnowflakeParameter(rows *sql.Rows) (*SnowflakeParameter, error) {
var err error
var columns []string
columns, err = rows.Columns()
if err != nil {
return nil, err
}
colNum := len(columns)
p := SnowflakeParameter{}
cols := make([]interface{}, colNum)
for i := 0; i < colNum; i++ {
cols[i] = populateSnowflakeParameter(columns[i], &p)
}
err = rows.Scan(cols...)
return &p, err
}