forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
239 lines (219 loc) · 5.44 KB
/
encoder.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
package clickhouse
import (
"database/sql/driver"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
const (
dateFormat = "2006-01-02"
timeFormat = "2006-01-02 15:04:05"
timeZoneBorder = "\\'"
)
var (
textEncode encoder = new(textEncoder)
)
type encoder interface {
Encode(value driver.Value) ([]byte, error)
}
type decoder interface {
Decode(t string, value []byte) (driver.Value, error)
}
type textEncoder struct {
}
type textDecoder struct {
location *time.Location
useDBLocation bool
}
// Encode encodes driver value into string
// Note: there is 2 convention:
// type string will be quoted
// type []byte will be encoded as is (raw string)
func (e *textEncoder) Encode(value driver.Value) ([]byte, error) {
switch v := value.(type) {
case array:
return e.encodeArray(reflect.ValueOf(v.v))
case []byte:
return v, nil
}
vv := reflect.ValueOf(value)
switch vv.Kind() {
case reflect.Interface, reflect.Ptr:
if vv.IsNil() {
return []byte("NULL"), nil
}
return e.Encode(vv.Elem().Interface())
case reflect.Slice, reflect.Array:
return e.encodeArray(vv)
}
return []byte(e.encode(value)), nil
}
func (e *textEncoder) encode(value driver.Value) string {
if value == nil {
return "NULL"
}
switch v := value.(type) {
case bool:
if v {
return "1"
}
return "0"
case int:
return strconv.FormatInt(int64(v), 10)
case int8:
return strconv.FormatInt(int64(v), 10)
case int16:
return strconv.FormatInt(int64(v), 10)
case int32:
return strconv.FormatInt(int64(v), 10)
case int64:
return strconv.FormatInt(v, 10)
case uint:
return strconv.FormatUint(uint64(v), 10)
case uint8:
return strconv.FormatUint(uint64(v), 10)
case uint16:
return strconv.FormatUint(uint64(v), 10)
case uint32:
return strconv.FormatUint(uint64(v), 10)
case uint64:
return strconv.FormatUint(v, 10)
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 32)
case float64:
return strconv.FormatFloat(v, 'f', -1, 64)
case string:
return quote(escape(v))
case time.Time:
return formatTime(v)
}
return fmt.Sprint(value)
}
// EncodeArray encodes a go slice or array as Clickhouse Array
func (e *textEncoder) encodeArray(value reflect.Value) ([]byte, error) {
if value.Kind() != reflect.Slice && value.Kind() != reflect.Array {
return nil, fmt.Errorf("expected array or slice, got %s", value.Kind())
}
res := make([]byte, 0)
res = append(res, '[')
for i := 0; i < value.Len(); i++ {
if i > 0 {
res = append(res, ',')
}
tmp, err := e.Encode(value.Index(i).Interface())
if err != nil {
return nil, err
}
res = append(res, tmp...)
}
return append(res, ']'), nil
}
func (d *textDecoder) Decode(t string, value []byte) (driver.Value, error) {
v := string(value)
switch t {
case "Date":
uv := unquote(v)
if uv == "0000-00-00" {
return time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC), nil
}
return time.ParseInLocation(dateFormat, uv, d.location)
case "DateTime":
uv := unquote(v)
if uv == "0000-00-00 00:00:00" {
return time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC), nil
}
return time.ParseInLocation(timeFormat, uv, d.location)
case "UInt8":
vv, err := strconv.ParseUint(v, 10, 8)
return uint8(vv), err
case "UInt16":
vv, err := strconv.ParseUint(v, 10, 16)
return uint16(vv), err
case "UInt32":
vv, err := strconv.ParseUint(v, 10, 32)
return uint32(vv), err
case "UInt64":
return strconv.ParseUint(v, 10, 64)
case "Int8":
vv, err := strconv.ParseInt(v, 10, 8)
return int8(vv), err
case "Int16":
vv, err := strconv.ParseInt(v, 10, 16)
return int16(vv), err
case "Int32":
vv, err := strconv.ParseInt(v, 10, 32)
return int32(vv), err
case "Int64":
return strconv.ParseInt(v, 10, 64)
case "Float32":
vv, err := strconv.ParseFloat(v, 64)
return float32(vv), err
case "Float64":
return strconv.ParseFloat(v, 64)
case "String":
return unescape(unquote(v)), nil
}
// got zoned datetime
if strings.HasPrefix(t, "DateTime") {
var (
loc *time.Location
err error
)
if d.useDBLocation {
left := strings.Index(t, timeZoneBorder)
if left == -1 {
return nil, fmt.Errorf("time zone not found")
}
right := strings.LastIndex(t, timeZoneBorder)
timeZoneName := t[left+len(timeZoneBorder) : right]
loc, err = time.LoadLocation(timeZoneName)
if err != nil {
return nil, err
}
} else {
loc = d.location
}
var t time.Time
if t, err = time.ParseInLocation(timeFormat, unquote(v), loc); err != nil {
return t, err
}
return t.In(d.location), nil
}
if strings.HasPrefix(t, "FixedString") {
return unescape(unquote(v)), nil
}
if strings.HasPrefix(t, "Array") {
if len(v) > 0 && v[0] == '[' && v[len(v)-1] == ']' {
var items []string
subType := t[6 : len(t)-1]
// check that array is not empty ([])
if len(v) > 2 {
// check if array of strings and not empty (['example'])
if subType == "String" || strings.HasPrefix(subType, "FixedString") {
items = strings.Split(v[2:len(v)-2], "','")
for i, v := range items {
items[i] = unescape(v)
}
} else {
items = strings.Split(v[1:len(v)-1], ",")
}
}
r := reflect.MakeSlice(reflect.SliceOf(columnType(subType)), len(items), len(items))
for i, item := range items {
vv, err := d.Decode(subType, []byte(item))
if err != nil {
return nil, err
}
r.Index(i).Set(reflect.ValueOf(vv))
}
return r.Interface(), nil
}
return nil, ErrMalformed
}
if strings.HasPrefix(t, "Enum") {
return unquote(v), nil
}
return value, nil
}