forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
223 lines (190 loc) · 6.28 KB
/
util.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
// Copyright (c) 2017-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"database/sql/driver"
"io"
"strings"
"sync"
"time"
)
type contextKey string
const (
multiStatementCount contextKey = "MULTI_STATEMENT_COUNT"
asyncMode contextKey = "ASYNC_MODE_QUERY"
queryIDChannel contextKey = "QUERY_ID_CHANNEL"
snowflakeRequestIDKey contextKey = "SNOWFLAKE_REQUEST_ID"
fetchResultByID contextKey = "SF_FETCH_RESULT_BY_ID"
fileStreamFile contextKey = "STREAMING_PUT_FILE"
fileTransferOptions contextKey = "FILE_TRANSFER_OPTIONS"
enableHigherPrecision contextKey = "ENABLE_HIGHER_PRECISION"
arrowBatches contextKey = "ARROW_BATCHES"
)
const (
describeOnly contextKey = "DESCRIBE_ONLY"
cancelRetry contextKey = "CANCEL_RETRY"
streamChunkDownload contextKey = "STREAM_CHUNK_DOWNLOAD"
)
// WithMultiStatement returns a context that allows the user to execute the desired number of sql queries in one query
func WithMultiStatement(ctx context.Context, num int) (context.Context, error) {
return context.WithValue(ctx, multiStatementCount, num), nil
}
// WithAsyncMode returns a context that allows execution of query in async mode
func WithAsyncMode(ctx context.Context) context.Context {
return context.WithValue(ctx, asyncMode, true)
}
// WithQueryIDChan returns a context that contains the channel to receive the query ID
func WithQueryIDChan(ctx context.Context, c chan<- string) context.Context {
return context.WithValue(ctx, queryIDChannel, c)
}
// WithRequestID returns a new context with the specified snowflake request id
func WithRequestID(ctx context.Context, requestID UUID) context.Context {
return context.WithValue(ctx, snowflakeRequestIDKey, requestID)
}
// WithStreamDownloader returns a context that allows the use of a stream based chunk downloader
func WithStreamDownloader(ctx context.Context) context.Context {
return context.WithValue(ctx, streamChunkDownload, true)
}
// WithFetchResultByID returns a context that allows retrieving the result by query ID
func WithFetchResultByID(ctx context.Context, queryID string) context.Context {
return context.WithValue(ctx, fetchResultByID, queryID)
}
// WithFileStream returns a context that contains the address of the file stream to be PUT
func WithFileStream(ctx context.Context, reader io.Reader) context.Context {
return context.WithValue(ctx, fileStreamFile, reader)
}
// WithFileTransferOptions returns a context that contains the address of file transfer options
func WithFileTransferOptions(ctx context.Context, options *SnowflakeFileTransferOptions) context.Context {
return context.WithValue(ctx, fileTransferOptions, options)
}
// WithDescribeOnly returns a context that enables a describe only query
func WithDescribeOnly(ctx context.Context) context.Context {
return context.WithValue(ctx, describeOnly, true)
}
// WithHigherPrecision returns a context that enables higher precision by
// returning a *big.Int or *big.Float variable when querying rows for column
// types with numbers that don't fit into its native Golang counterpart
func WithHigherPrecision(ctx context.Context) context.Context {
return context.WithValue(ctx, enableHigherPrecision, true)
}
// WithArrowBatches returns a context that allows users to retrieve
// array.Record download workers upon querying
func WithArrowBatches(ctx context.Context) context.Context {
return context.WithValue(ctx, arrowBatches, true)
}
// Get the request ID from the context if specified, otherwise generate one
func getOrGenerateRequestIDFromContext(ctx context.Context) UUID {
requestID, ok := ctx.Value(snowflakeRequestIDKey).(UUID)
if ok && requestID != nilUUID {
return requestID
}
return NewUUID()
}
// integer min
func intMin(a, b int) int {
if a < b {
return a
}
return b
}
// integer max
func intMax(a, b int) int {
if a > b {
return a
}
return b
}
func int64Max(a, b int64) int64 {
if a > b {
return a
}
return b
}
func getMin(arr []int) int {
if len(arr) == 0 {
return -1
}
min := arr[0]
for _, v := range arr {
if v <= min {
min = v
}
}
return min
}
// time.Duration max
func durationMax(d1, d2 time.Duration) time.Duration {
if d1-d2 > 0 {
return d1
}
return d2
}
// time.Duration min
func durationMin(d1, d2 time.Duration) time.Duration {
if d1-d2 < 0 {
return d1
}
return d2
}
// toNamedValues converts a slice of driver.Value to a slice of driver.NamedValue for Go 1.8 SQL package
func toNamedValues(values []driver.Value) []driver.NamedValue {
namedValues := make([]driver.NamedValue, len(values))
for idx, value := range values {
namedValues[idx] = driver.NamedValue{Name: "", Ordinal: idx + 1, Value: value}
}
return namedValues
}
// TokenAccessor manages the session token and master token
type TokenAccessor interface {
GetTokens() (token string, masterToken string, sessionID int64)
SetTokens(token string, masterToken string, sessionID int64)
Lock() error
Unlock()
}
type simpleTokenAccessor struct {
token string
masterToken string
sessionID int64
accessorLock sync.Mutex // Used to implement accessor's Lock and Unlock
tokenLock sync.RWMutex // Used to synchronize SetTokens and GetTokens
}
func getSimpleTokenAccessor() TokenAccessor {
return &simpleTokenAccessor{sessionID: -1}
}
func (sta *simpleTokenAccessor) Lock() error {
sta.accessorLock.Lock()
return nil
}
func (sta *simpleTokenAccessor) Unlock() {
sta.accessorLock.Unlock()
}
func (sta *simpleTokenAccessor) GetTokens() (token string, masterToken string, sessionID int64) {
sta.tokenLock.RLock()
defer sta.tokenLock.RUnlock()
return sta.token, sta.masterToken, sta.sessionID
}
func (sta *simpleTokenAccessor) SetTokens(token string, masterToken string, sessionID int64) {
sta.tokenLock.Lock()
defer sta.tokenLock.Unlock()
sta.token = token
sta.masterToken = masterToken
sta.sessionID = sessionID
}
func escapeForCSV(value string) string {
if value == "" {
return "\"\""
}
if strings.Contains(value, "\"") || strings.Contains(value, "\n") ||
strings.Contains(value, ",") || strings.Contains(value, "\\") {
return "\"" + strings.ReplaceAll(value, "\"", "\"\"") + "\""
}
return value
}
func getSnowflakeError(err error) (*SnowflakeError, bool) {
var sfError *SnowflakeError
var ok bool
if err != nil {
sfError, ok = err.(*SnowflakeError)
}
return sfError, ok
}