Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 6b1ba59

Browse files
authored
Merge pull request ClickHouse#223 from zxc111/master
support Array(IPv4) && Array(IPv6) type
2 parents a1ffb9f + 996a384 commit 6b1ba59

File tree

15 files changed

+359
-56
lines changed

15 files changed

+359
-56
lines changed

clickhouse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (ch *clickhouse) CheckNamedValue(nv *driver.NamedValue) error {
185185
[]float32, []float64,
186186
[]string:
187187
return nil
188-
case net.IP:
188+
case net.IP, *net.IP:
189189
return nil
190190
case driver.Valuer:
191191
value, err := v.Value()

clickhouse_direct_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ func Test_DirectArrayT(t *testing.T) {
158158
date Array(Date),
159159
datetime Array(DateTime),
160160
enum8 Array(Enum8 ('a' = 1, 'b' = 2)),
161-
enum16 Array(Enum16('c' = 1, 'd' = 2))
161+
enum16 Array(Enum16('c' = 1, 'd' = 2)),
162+
ipv4 Array(IPv4),
163+
ipv6 Array(IPv6)
162164
) Engine=Memory
163165
`
164166
dml = `
@@ -178,7 +180,9 @@ func Test_DirectArrayT(t *testing.T) {
178180
date,
179181
datetime,
180182
enum8,
181-
enum16
183+
enum16,
184+
ipv4,
185+
ipv6
182186
) VALUES (
183187
?,
184188
?,
@@ -195,6 +199,8 @@ func Test_DirectArrayT(t *testing.T) {
195199
?,
196200
?,
197201
?,
202+
?,
203+
?,
198204
?
199205
)
200206
`
@@ -239,6 +245,8 @@ func Test_DirectArrayT(t *testing.T) {
239245
clickhouse.ArrayDateTime([]time.Time{time.Now(), time.Now()}),
240246
clickhouse.Array([]string{"a", "b"}),
241247
clickhouse.Array([]string{"c", "d"}),
248+
clickhouse.Array([]string{"1.2.3.4", "2.2.3.4"}),
249+
clickhouse.Array([]string{"2001:0db8:85a3:0000:0000:8a2e:0370:7334"}),
242250
})
243251
if !assert.NoError(t, err) {
244252
return

clickhouse_test.go

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ func Test_Insert(t *testing.T) {
8989
string String,
9090
fString FixedString(2),
9191
date Date,
92-
datetime DateTime
92+
datetime DateTime,
93+
ipv4 IPv4,
94+
ipv6 IPv6,
95+
ipv4str FixedString(16),
96+
ipv6str FixedString(16)
9397
) Engine=Memory
9498
`
9599
dml = `
@@ -107,7 +111,11 @@ func Test_Insert(t *testing.T) {
107111
string,
108112
fString,
109113
date,
110-
datetime
114+
datetime,
115+
ipv4,
116+
ipv6,
117+
ipv4str,
118+
ipv6str
111119
) VALUES (
112120
?,
113121
?,
@@ -122,6 +130,10 @@ func Test_Insert(t *testing.T) {
122130
?,
123131
?,
124132
?,
133+
?,
134+
?,
135+
?,
136+
?,
125137
?
126138
)
127139
`
@@ -140,7 +152,11 @@ func Test_Insert(t *testing.T) {
140152
string,
141153
fString,
142154
date,
143-
datetime
155+
datetime,
156+
ipv4,
157+
ipv6,
158+
ipv4str,
159+
ipv6str
144160
FROM clickhouse_test_insert
145161
`
146162
)
@@ -154,10 +170,14 @@ func Test_Insert(t *testing.T) {
154170
-1*i, -2*i, -4*i, -8*i, // int
155171
uint8(1*i), uint16(2*i), uint32(4*i), uint64(8*i), // uint
156172
1.32*float32(i), 1.64*float64(i), //float
157-
fmt.Sprintf("string %d", i), // string
158-
"RU", //fixedstring,
159-
time.Now(), //date
160-
time.Now(), //datetime
173+
fmt.Sprintf("string %d", i), // string
174+
"RU", //fixedstring,
175+
time.Now(), //date
176+
time.Now(), //datetime
177+
"1.2.3.4", // ipv4
178+
"2001:0db8:85a3:0000:0000:8a2e:0370:7334", //ipv6
179+
column.IP(net.ParseIP("127.0.0.1").To4()),
180+
column.IP(net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")),
161181
)
162182
if !assert.NoError(t, err) {
163183
return
@@ -182,6 +202,10 @@ func Test_Insert(t *testing.T) {
182202
FixedString string
183203
Date time.Time
184204
DateTime time.Time
205+
Ipv6 column.IP
206+
Ipv4 column.IP
207+
Ipv4str column.IP
208+
Ipv6str column.IP
185209
}
186210
if rows, err := connect.Query(query); assert.NoError(t, err) {
187211
var count int
@@ -202,6 +226,10 @@ func Test_Insert(t *testing.T) {
202226
&item.FixedString,
203227
&item.Date,
204228
&item.DateTime,
229+
&item.Ipv4,
230+
&item.Ipv6,
231+
&item.Ipv4str,
232+
&item.Ipv6str,
205233
)
206234
if !assert.NoError(t, err) {
207235
return
@@ -505,7 +533,9 @@ func Test_ArrayT(t *testing.T) {
505533
date Array(Date),
506534
datetime Array(DateTime),
507535
enum8 Array(Enum8 ('a' = 1, 'b' = 2)),
508-
enum16 Array(Enum16('c' = 1, 'd' = 2))
536+
enum16 Array(Enum16('c' = 1, 'd' = 2)),
537+
ipv4 Array(IPv4),
538+
ipv6 Array(IPv6)
509539
) Engine=Memory
510540
`
511541
dml = `
@@ -525,7 +555,9 @@ func Test_ArrayT(t *testing.T) {
525555
date,
526556
datetime,
527557
enum8,
528-
enum16
558+
enum16,
559+
ipv4,
560+
ipv6
529561
) VALUES (
530562
?,
531563
?,
@@ -542,6 +574,8 @@ func Test_ArrayT(t *testing.T) {
542574
?,
543575
?,
544576
?,
577+
?,
578+
?,
545579
?
546580
)
547581
`
@@ -560,7 +594,9 @@ func Test_ArrayT(t *testing.T) {
560594
string,
561595
fString,
562596
date,
563-
datetime
597+
datetime,
598+
ipv4,
599+
ipv6
564600
FROM clickhouse_test_array
565601
`
566602
)
@@ -587,6 +623,8 @@ func Test_ArrayT(t *testing.T) {
587623
[]time.Time{time.Now(), time.Now()},
588624
[]string{"a", "b"},
589625
[]string{"c", "d"},
626+
[]string{"127.0.0.1", "1.2.3.4"},
627+
[]string{"2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
590628
)
591629
if !assert.NoError(t, err) {
592630
return
@@ -608,6 +646,8 @@ func Test_ArrayT(t *testing.T) {
608646
[]time.Time{time.Now(), time.Now()},
609647
[]string{"a", "b"},
610648
[]string{"c", "d"},
649+
[]string{"127.0.0.1", "1.2.3.4"},
650+
[]string{"2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
611651
)
612652
if !assert.NoError(t, err) {
613653
return
@@ -630,6 +670,8 @@ func Test_ArrayT(t *testing.T) {
630670
FixedString []string
631671
Date []time.Time
632672
DateTime []time.Time
673+
Ipv4 []column.IP
674+
Ipv6 []column.IP
633675
}
634676
if rows, err := connect.Query(query); assert.NoError(t, err) {
635677
var count int
@@ -650,6 +692,8 @@ func Test_ArrayT(t *testing.T) {
650692
&item.FixedString,
651693
&item.Date,
652694
&item.DateTime,
695+
&item.Ipv4,
696+
&item.Ipv6,
653697
)
654698
if !assert.NoError(t, err) {
655699
return
@@ -678,6 +722,10 @@ func Test_ArrayT(t *testing.T) {
678722
item.Date,
679723
item.DateTime,
680724
)
725+
t.Logf("Ipv4=%v, Ipv6=%v",
726+
item.Ipv4,
727+
item.Ipv6,
728+
)
681729
}
682730
assert.Equal(t, int(20), count)
683731
}

lib/column/array.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package column
22

33
import (
44
"fmt"
5+
"net"
56
"reflect"
67
"strings"
78
"time"
@@ -88,31 +89,33 @@ loop:
8889
}
8990

9091
var scanType interface{}
91-
switch t := column.ScanType().Kind(); t {
92-
case reflect.Int8:
92+
switch t := column.ScanType(); t {
93+
case arrayBaseTypes[int8(0)]:
9394
scanType = []int8{}
94-
case reflect.Int16:
95+
case arrayBaseTypes[int16(0)]:
9596
scanType = []int16{}
96-
case reflect.Int32:
97+
case arrayBaseTypes[int32(0)]:
9798
scanType = []int32{}
98-
case reflect.Int64:
99+
case arrayBaseTypes[int64(0)]:
99100
scanType = []int64{}
100-
case reflect.Uint8:
101+
case arrayBaseTypes[uint8(0)]:
101102
scanType = []uint8{}
102-
case reflect.Uint16:
103+
case arrayBaseTypes[uint16(0)]:
103104
scanType = []uint16{}
104-
case reflect.Uint32:
105+
case arrayBaseTypes[uint32(0)]:
105106
scanType = []uint32{}
106-
case reflect.Uint64:
107+
case arrayBaseTypes[uint64(0)]:
107108
scanType = []uint64{}
108-
case reflect.Float32:
109+
case arrayBaseTypes[float32(0)]:
109110
scanType = []float32{}
110-
case reflect.Float64:
111+
case arrayBaseTypes[float64(0)]:
111112
scanType = []float64{}
112-
case reflect.String:
113+
case arrayBaseTypes[string("")]:
113114
scanType = []string{}
114-
case baseTypes[time.Time{}].Kind():
115+
case arrayBaseTypes[time.Time{}]:
115116
scanType = []time.Time{}
117+
case arrayBaseTypes[IPv4{}], arrayBaseTypes[IPv6{}]:
118+
scanType = []net.IP{}
116119
default:
117120
return nil, fmt.Errorf("unsupported Array type '%s'", column.ScanType().Name())
118121
}

0 commit comments

Comments
 (0)