forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.go
More file actions
146 lines (138 loc) · 2.98 KB
/
array.go
File metadata and controls
146 lines (138 loc) · 2.98 KB
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
package clickhouse
import (
"bytes"
"database/sql/driver"
"fmt"
"time"
)
func Array(v interface{}) *array {
return &array{
values: v,
}
}
func ArrayFixedString(len int, v []string) *array {
return &array{
values: v,
columnType: fmt.Sprintf("FixedString(%d)", len),
}
}
func ArrayDate(v []time.Time) *array {
return &array{
values: v,
columnType: "Date",
}
}
func ArrayDateTime(v []time.Time) *array {
return &array{
values: v,
columnType: "DateTime",
}
}
type array struct {
values interface{}
baseType interface{}
columnType string
}
func (a *array) Value() (driver.Value, error) {
var elements []interface{}
switch values := a.values.(type) {
case []time.Time:
if len(a.columnType) == 0 {
return nil, fmt.Errorf("unexpected column type")
}
for _, v := range values {
elements = append(elements, v)
}
case []string:
if len(a.columnType) == 0 {
a.columnType = "String"
}
for _, v := range values {
elements = append(elements, v)
}
case []float32:
a.columnType = "Float32"
for _, v := range values {
elements = append(elements, float64(v))
}
case []float64:
a.columnType = "Float64"
for _, v := range values {
elements = append(elements, v)
}
case []int8:
a.columnType = "Int8"
for _, v := range values {
elements = append(elements, int64(v))
}
case []int16:
a.columnType = "Int16"
for _, v := range values {
elements = append(elements, int64(v))
}
case []int32:
a.columnType = "Int32"
for _, v := range values {
elements = append(elements, int64(v))
}
case []int64:
a.columnType = "Int64"
for _, v := range values {
elements = append(elements, v)
}
case []uint8:
a.columnType = "UInt8"
for _, v := range values {
elements = append(elements, int64(v))
}
case []uint16:
a.columnType = "UInt16"
for _, v := range values {
elements = append(elements, int64(v))
}
case []uint32:
a.columnType = "UInt32"
for _, v := range values {
elements = append(elements, int64(v))
}
case []uint64:
a.columnType = "UInt64"
for _, v := range values {
elements = append(elements, int64(v))
}
default:
return nil, fmt.Errorf("unsupported array type %T", a.values)
}
buf := wb(len(a.columnType) + (2 * len(elements)) + 8)
if err := writeString(buf, a.columnType); err != nil {
return nil, err
}
if err := writeUvarint(buf, uint64(len(elements))); err != nil {
return nil, err
}
columnType, err := toColumnType(a.columnType)
if err != nil {
return nil, err
}
for _, value := range elements {
if err := write(buf, columnType, value); err != nil {
return nil, err
}
}
return buf.bytes(), nil
}
func arrayInfo(b []byte) (string, uint64, []byte, error) {
var (
err error
arrayLen uint64
columnType string
buff = bytes.NewBuffer(b)
)
if columnType, err = readString(buff); err != nil {
return "", 0, nil, err
}
if arrayLen, err = readUvarint(buff); err != nil {
return "", 0, nil, err
}
return columnType, arrayLen, buff.Bytes(), nil
}