forked from databricks/databricks-sql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameters.go
181 lines (170 loc) · 4.08 KB
/
parameters.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
package dbsql
import (
"database/sql/driver"
"fmt"
"strconv"
"strings"
"time"
"github.com/databricks/databricks-sql-go/internal/cli_service"
)
type Parameter struct {
Name string
Type SqlType
Value any
}
type SqlType int
const (
SqlUnkown SqlType = iota
SqlString
SqlDate
SqlTimestamp
SqlFloat
SqlDecimal
SqlDouble
SqlInteger
SqlBigInt
SqlSmallInt
SqlTinyInt
SqlBoolean
SqlIntervalMonth
SqlIntervalDay
)
func (s SqlType) String() string {
switch s {
case SqlString:
return "STRING"
case SqlDate:
return "DATE"
case SqlTimestamp:
return "TIMESTAMP"
case SqlFloat:
return "FLOAT"
case SqlDecimal:
return "DECIMAL"
case SqlDouble:
return "DOUBLE"
case SqlInteger:
return "INTEGER"
case SqlBigInt:
return "BIGINT"
case SqlSmallInt:
return "SMALLINT"
case SqlTinyInt:
return "TINYINT"
case SqlBoolean:
return "BOOLEAN"
case SqlIntervalMonth:
return "INTERVAL MONTH"
case SqlIntervalDay:
return "INTERVAL DAY"
}
return "unknown"
}
func valuesToParameters(namedValues []driver.NamedValue) []Parameter {
var params []Parameter
for i := range namedValues {
newParam := *new(Parameter)
namedValue := namedValues[i]
param, ok := namedValue.Value.(Parameter)
if ok {
newParam.Name = param.Name
newParam.Value = param.Value
newParam.Type = param.Type
} else {
newParam.Name = namedValue.Name
newParam.Value = namedValue.Value
}
params = append(params, newParam)
}
return params
}
func inferTypes(params []Parameter) {
for i := range params {
param := ¶ms[i]
if param.Type == SqlUnkown {
switch value := param.Value.(type) {
case bool:
param.Value = strconv.FormatBool(value)
param.Type = SqlBoolean
case string:
param.Value = value
param.Type = SqlString
case int:
param.Value = strconv.Itoa(value)
param.Type = SqlInteger
case uint:
param.Value = strconv.FormatUint(uint64(value), 10)
param.Type = SqlInteger
case int8:
param.Value = strconv.Itoa(int(value))
param.Type = SqlInteger
case uint8:
param.Value = strconv.FormatUint(uint64(value), 10)
param.Type = SqlInteger
case int16:
param.Value = strconv.Itoa(int(value))
param.Type = SqlInteger
case uint16:
param.Value = strconv.FormatUint(uint64(value), 10)
param.Type = SqlInteger
case int32:
param.Value = strconv.Itoa(int(value))
param.Type = SqlInteger
case uint32:
param.Value = strconv.FormatUint(uint64(value), 10)
param.Type = SqlInteger
case int64:
param.Value = strconv.Itoa(int(value))
param.Type = SqlInteger
case uint64:
param.Value = strconv.FormatUint(uint64(value), 10)
param.Type = SqlInteger
case float32:
param.Value = strconv.FormatFloat(float64(value), 'f', -1, 32)
param.Type = SqlFloat
case time.Time:
param.Value = value.String()
param.Type = SqlTimestamp
default:
s := fmt.Sprintf("%s", param.Value)
param.Value = s
param.Type = SqlString
}
}
}
}
func convertNamedValuesToSparkParams(values []driver.NamedValue) []*cli_service.TSparkParameter {
var sparkParams []*cli_service.TSparkParameter
sqlParams := valuesToParameters(values)
inferTypes(sqlParams)
for i := range sqlParams {
sqlParam := sqlParams[i]
sparkParamValue := sqlParam.Value.(string)
var sparkParamType string
if sqlParam.Type == SqlDecimal {
sparkParamType = inferDecimalType(sparkParamValue)
} else {
sparkParamType = sqlParam.Type.String()
}
sparkParam := cli_service.TSparkParameter{Name: &sqlParam.Name, Type: &sparkParamType, Value: &cli_service.TSparkParameterValue{StringValue: &sparkParamValue}}
sparkParams = append(sparkParams, &sparkParam)
}
return sparkParams
}
func inferDecimalType(d string) (t string) {
var overall int
var after int
if strings.HasPrefix(d, "0.") {
// Less than one
overall = len(d) - 2
after = len(d) - 2
} else if !strings.Contains(d, ".") {
// Less than one
overall = len(d)
after = 0
} else {
components := strings.Split(d, ".")
overall, after = len(components[0])+len(components[1]), len(components[1])
}
return fmt.Sprintf("DECIMAL(%d,%d)", overall, after)
}