forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatype.go
195 lines (182 loc) · 5.13 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
// Copyright (c) 2017-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"bytes"
"database/sql"
"database/sql/driver"
"fmt"
)
type snowflakeType int
const (
fixedType snowflakeType = iota
realType
textType
dateType
variantType
timestampLtzType
timestampNtzType
timestampTzType
objectType
arrayType
binaryType
timeType
booleanType
// the following are not snowflake types per se but internal types
nullType
sliceType
changeType
unSupportedType
)
var snowflakeTypes = [...]string{"FIXED", "REAL", "TEXT", "DATE", "VARIANT",
"TIMESTAMP_LTZ", "TIMESTAMP_NTZ", "TIMESTAMP_TZ", "OBJECT", "ARRAY",
"BINARY", "TIME", "BOOLEAN", "NULL", "SLICE", "CHANGE_TYPE", "NOT_SUPPORTED"}
func (st snowflakeType) String() string {
return snowflakeTypes[st]
}
func (st snowflakeType) Byte() byte {
return byte(st)
}
func getSnowflakeType(typ string) snowflakeType {
for i, sft := range snowflakeTypes {
if sft == typ {
return snowflakeType(i)
} else if snowflakeType(i) == nullType {
break
}
}
return nullType
}
var (
// DataTypeFixed is a FIXED datatype.
DataTypeFixed = []byte{fixedType.Byte()}
// DataTypeReal is a REAL datatype.
DataTypeReal = []byte{realType.Byte()}
// DataTypeText is a TEXT datatype.
DataTypeText = []byte{textType.Byte()}
// DataTypeDate is a Date datatype.
DataTypeDate = []byte{dateType.Byte()}
// DataTypeVariant is a TEXT datatype.
DataTypeVariant = []byte{variantType.Byte()}
// DataTypeTimestampLtz is a TIMESTAMP_LTZ datatype.
DataTypeTimestampLtz = []byte{timestampLtzType.Byte()}
// DataTypeTimestampNtz is a TIMESTAMP_NTZ datatype.
DataTypeTimestampNtz = []byte{timestampNtzType.Byte()}
// DataTypeTimestampTz is a TIMESTAMP_TZ datatype.
DataTypeTimestampTz = []byte{timestampTzType.Byte()}
// DataTypeObject is a OBJECT datatype.
DataTypeObject = []byte{objectType.Byte()}
// DataTypeArray is a ARRAY datatype.
DataTypeArray = []byte{arrayType.Byte()}
// DataTypeBinary is a BINARY datatype.
DataTypeBinary = []byte{binaryType.Byte()}
// DataTypeTime is a TIME datatype.
DataTypeTime = []byte{timeType.Byte()}
// DataTypeBoolean is a BOOLEAN datatype.
DataTypeBoolean = []byte{booleanType.Byte()}
)
// dataTypeMode returns the subsequent data type in a string representation.
func dataTypeMode(v driver.Value) (tsmode snowflakeType, err error) {
if bd, ok := v.([]byte); ok {
switch {
case bytes.Equal(bd, DataTypeDate):
tsmode = dateType
case bytes.Equal(bd, DataTypeTime):
tsmode = timeType
case bytes.Equal(bd, DataTypeTimestampLtz):
tsmode = timestampLtzType
case bytes.Equal(bd, DataTypeTimestampNtz):
tsmode = timestampNtzType
case bytes.Equal(bd, DataTypeTimestampTz):
tsmode = timestampTzType
case bytes.Equal(bd, DataTypeBinary):
tsmode = binaryType
default:
return nullType, fmt.Errorf(errMsgInvalidByteArray, v)
}
} else {
return nullType, fmt.Errorf(errMsgInvalidByteArray, v)
}
return tsmode, 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
}