forked from ClickHouse/clickhouse-go
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclickhouse_columnar_test.go
261 lines (236 loc) · 5.81 KB
/
clickhouse_columnar_test.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package clickhouse_test
import (
"database/sql/driver"
"github.com/ClickHouse/clickhouse-go"
"github.com/ClickHouse/clickhouse-go/lib/data"
"github.com/stretchr/testify/assert"
"net"
"reflect"
"testing"
"time"
)
func Test_ColumnarInsert(t *testing.T) {
const (
ddl = `
CREATE TABLE clickhouse_test_columnar_insert (
uint8 UInt8,
uint8_null Nullable(UInt8),
uint16 UInt16,
uint16_null Nullable(UInt16),
uint32 UInt32,
uint32_null Nullable(UInt32),
uint64 UInt64,
uint64_null Nullable(UInt64),
float32 Float32,
float32_null Nullable(Float32),
float64 Float64,
float64_null Nullable(Float64),
string String,
string_null Nullable(String),
fString FixedString(2),
fString_null Nullable(FixedString(2)),
date Date,
date_null Nullable(Date),
datetime DateTime,
datetime_null Nullable(DateTime),
enum8 Enum8 ('a' = 1, 'b' = 2),
enum8_null Nullable(Enum8 ('a' = 1, 'b' = 2)),
enum16 Enum16('c' = 1, 'd' = 2),
enum16_null Nullable(Enum16('c' = 1, 'd' = 2)),
array Array(String),
arrayArray Array(Array(String)),
arrayWithValue Array(UInt64),
arrayWithValueFast Array(UInt64),
ipv4 IPv4,
ipv6 IPv6
) Engine=Memory
`
dml = `
INSERT INTO clickhouse_test_columnar_insert (
uint8,
uint8_null,
uint16,
uint16_null,
uint32,
uint32_null,
uint64,
uint64_null,
float32,
float32_null,
float64,
float64_null,
string,
string_null,
fString,
fString_null,
date,
date_null,
datetime,
datetime_null,
enum8,
enum8_null,
enum16,
enum16_null,
array,
arrayArray,
arrayWithValue,
arrayWithValueFast,
ipv4,
ipv6
) VALUES (
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
?,
?,
?,
?,
?,
?,
?,
?
)`
)
if connect, err := clickhouse.OpenDirect("tcp://127.0.0.1:9000?debug=true"); assert.NoError(t, err) {
{
connect.Begin()
stmt, _ := connect.Prepare("DROP TABLE IF EXISTS clickhouse_test_columnar_insert")
stmt.Exec([]driver.Value{})
connect.Commit()
}
{
if _, err := connect.Begin(); assert.NoError(t, err) {
if stmt, err := connect.Prepare(ddl); assert.NoError(t, err) {
if _, err := stmt.Exec([]driver.Value{}); assert.NoError(t, err) {
assert.NoError(t, connect.Commit())
}
}
}
}
{
if _, err := connect.Begin(); assert.NoError(t, err) {
if _, err := connect.Prepare(dml); assert.NoError(t, err) {
block, err := connect.Block()
assert.NoError(t, err)
block.Reserve()
block.NumRows = 100
for i := 0; i < 100; i++ {
block.WriteUInt8(0, uint8(i))
block.WriteUInt8Nullable(1, nil)
block.WriteUInt16(2, uint16(i))
block.WriteUInt16Nullable(3, nil)
block.WriteUInt32(4, uint32(i))
block.WriteUInt32Nullable(5, nil)
block.WriteUInt64(6, uint64(i))
block.WriteUInt64Nullable(7, nil)
block.WriteFloat32(8, float32(i))
block.WriteFloat32Nullable(9, nil)
block.WriteFloat64(10, float64(i))
block.WriteFloat64Nullable(11, nil)
block.WriteString(12, "string")
block.WriteStringNullable(13, nil)
block.WriteFixedString(14, []byte("CH"))
block.WriteFixedStringNullable(15, nil)
block.WriteDate(16, time.Now())
block.WriteDateNullable(17, nil)
block.WriteDateTime(18, time.Now())
block.WriteDateTimeNullable(19, nil)
block.WriteUInt8(20, 1)
block.WriteUInt8Nullable(21, nil)
block.WriteUInt16(22, 2)
block.WriteUInt16Nullable(23, nil)
block.WriteArray(24, []string{"A", "B", "C"})
block.WriteArray(25, [][]string{[]string{"A", "B"}, []string{"CC", "DD", "EE"}})
block.WriteArrayWithValue(26, newUint64SliceValueSimple([]uint64{1, 2, 3}))
block.WriteArrayWithValue(27, newUint64SliceValueFast([]uint64{10, 20, 30}))
block.WriteIP(28, net.ParseIP("213.180.204.62"))
block.WriteIP(29, net.ParseIP("2606:4700:5c::a29f:2e07"))
if !assert.NoError(t, err) {
return
}
}
assert.NoError(t, connect.Commit())
}
}
}
}
}
type uint64Value struct {
value uint64
}
func (v *uint64Value) Kind() reflect.Kind {
return reflect.String
}
func (v *uint64Value) Len() int {
panic("uint64 has no length")
}
func (v *uint64Value) Index(i int) data.Value {
panic("uint64 has no index")
}
func (v *uint64Value) Interface() interface{} {
return v.value
}
type uint64SliceValueSimple struct {
uint64Slice []uint64
}
func newUint64SliceValueSimple(v []uint64) *uint64SliceValueSimple {
return &uint64SliceValueSimple{uint64Slice: v}
}
func (v *uint64SliceValueSimple) Kind() reflect.Kind {
return reflect.Slice
}
func (v *uint64SliceValueSimple) Len() int {
return len(v.uint64Slice)
}
func (v *uint64SliceValueSimple) Index(i int) data.Value {
return &uint64Value{value: v.uint64Slice[i]}
}
func (v *uint64SliceValueSimple) Interface() interface{} {
return v.uint64Slice
}
type uint64SliceValueFast struct {
uint64Slice []uint64
uint64Value *uint64Value
value data.Value
}
func newUint64SliceValueFast(v []uint64) *uint64SliceValueFast {
var uint64Value uint64Value
return &uint64SliceValueFast{
uint64Slice: v,
uint64Value: &uint64Value,
value: &uint64Value,
}
}
func (v *uint64SliceValueFast) Kind() reflect.Kind {
return reflect.Slice
}
func (v *uint64SliceValueFast) Len() int {
return len(v.uint64Slice)
}
func (v *uint64SliceValueFast) Index(i int) data.Value {
v.uint64Value.value = v.uint64Slice[i]
// NB: This avoids the CPU cost of converting *uint64Value to data.Value.
return v.value
}
func (v *uint64SliceValueFast) Interface() interface{} {
return v.uint64Slice
}