forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickhouse_columnar_test.go
212 lines (187 loc) · 4.54 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
package clickhouse_test
import (
"database/sql/driver"
"net"
"reflect"
"testing"
"time"
"github.com/ClickHouse/clickhouse-go"
"github.com/ClickHouse/clickhouse-go/lib/data"
"github.com/stretchr/testify/assert"
)
func Test_ColumnarInsert(t *testing.T) {
const (
ddl = `
CREATE TABLE clickhouse_test_columnar_insert (
uint8 UInt8,
uint16 UInt16,
uint32 UInt32,
uint64 UInt64,
float32 Float32,
float64 Float64,
string String,
fString FixedString(2),
date Date,
datetime DateTime,
enum8 Enum8 ('a' = 1, 'b' = 2),
enum16 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,
uint16,
uint32,
uint64,
float32,
float64,
string,
fString,
date,
datetime,
enum8,
enum16,
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.WriteUInt16(1, uint16(i))
block.WriteUInt32(2, uint32(i))
block.WriteUInt64(3, uint64(i))
block.WriteFloat32(4, float32(i))
block.WriteFloat64(5, float64(i))
block.WriteString(6, "string")
block.WriteFixedString(7, []byte("CH"))
block.WriteDate(8, time.Now())
block.WriteDateTime(9, time.Now())
block.WriteUInt8(10, 1)
block.WriteUInt16(11, 2)
block.WriteArray(12, []string{"A", "B", "C"})
block.WriteArray(13, [][]string{[]string{"A", "B"}, []string{"CC", "DD", "EE"}})
block.WriteArrayWithValue(14, newUint64SliceValueSimple([]uint64{1, 2, 3}))
block.WriteArrayWithValue(15, newUint64SliceValueFast([]uint64{10, 20, 30}))
block.WriteIP(16, net.ParseIP("213.180.204.62"))
block.WriteIP(17, 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
}