forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataparser_test.go
790 lines (773 loc) · 19.3 KB
/
dataparser_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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
package clickhouse
import (
"math"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestParseData(t *testing.T) {
type testCase struct {
name string
inputtype string
inputopt *DataParserOptions
inputdata string
output interface{}
failParseDesc bool
failNewParser bool
failParseData bool
}
losAngeles, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
t.Fatalf("failed to load time zone America/Los_Angeles: %v", err)
}
moscow, err := time.LoadLocation("Europe/Moscow")
if err != nil {
t.Fatalf("failed to load time zone Europe/Moscow: %v", err)
}
testCases := []*testCase{
{
name: "nullable string",
inputtype: "Nullable(String)",
inputdata: `\N`,
output: nil,
},
{
name: "nullable int not null",
inputtype: "Nullable(UInt64)",
inputdata: "655",
output: uint64(655),
},
{
name: "string",
inputtype: "String",
inputdata: "hello world",
output: "hello world",
},
{
name: "fixed string",
inputtype: "FixedString(10)",
inputdata: `hello\0\0\0\0\0`,
output: "hello\x00\x00\x00\x00\x00",
},
{
name: "string with escaping",
inputtype: "String",
inputdata: `hello \'world`,
output: "hello 'world",
},
{
name: "string with incorrect escaping",
inputtype: "String",
inputdata: `hello world\`,
failParseData: true,
},
{
name: "int",
inputtype: "UInt64",
inputdata: "123",
output: uint64(123),
},
{
name: "float",
inputtype: "Float32",
inputdata: "-inf",
output: float32(math.Inf(-1)),
},
{
name: "decimal",
inputtype: "Decimal(9,4)",
inputdata: "123",
output: "123",
},
{
name: "date",
inputtype: "Date",
inputdata: "2018-01-02",
output: time.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC),
},
{
name: "zero date",
inputtype: "Date",
inputdata: "0000-00-00",
output: time.Time{},
},
{
name: "with special timezone",
inputtype: "Date",
inputdata: "2019-06-29",
inputopt: &DataParserOptions{
Location: losAngeles,
},
output: time.Date(2019, 6, 29, 0, 0, 0, 0, losAngeles),
},
{
name: "enum",
inputtype: "Enum8('hello' = 1, 'world' = 2)",
inputdata: "hello",
output: "hello",
},
{
name: "uuid",
inputtype: "UUID",
inputdata: "c79a9747-7cef-4b11-8177-380f7ed462a4",
output: "c79a9747-7cef-4b11-8177-380f7ed462a4",
},
{
name: "datetime, without options and argument",
inputtype: "DateTime",
inputdata: "2018-01-02 12:34:56",
output: time.Date(2018, 1, 2, 12, 34, 56, 0, time.UTC),
},
{
name: "datetime, with argument",
inputtype: "DateTime('America/Los_Angeles')",
inputdata: "2018-01-02 12:34:56",
output: time.Date(2018, 1, 2, 12, 34, 56, 0, losAngeles),
},
{
name: "datetime with argument, but location nil",
inputtype: "DateTime('America/Los_Angeles')",
inputdata: "2018-01-02 12:34:56",
inputopt: &DataParserOptions{
Location: nil,
},
output: time.Date(2018, 1, 2, 12, 34, 56, 0, losAngeles),
},
{
name: "datetime without argument, but use location",
inputtype: "DateTime",
inputdata: "2018-01-02 12:34:56",
inputopt: &DataParserOptions{
Location: moscow,
},
output: time.Date(2018, 1, 2, 12, 34, 56, 0, moscow),
},
{
name: "datetime with argument and location, ingnore argument",
inputtype: "DateTime('America/Los_Angeles')",
inputdata: "2018-01-02 12:34:56",
inputopt: &DataParserOptions{
Location: moscow,
},
output: time.Date(2018, 1, 2, 12, 34, 56, 0, moscow),
},
{
name: "datetime with argument and location, prefer argument",
inputtype: "DateTime('America/Los_Angeles')",
inputdata: "2018-01-02 12:34:56",
inputopt: &DataParserOptions{
Location: moscow,
UseDBLocation: true,
},
output: time.Date(2018, 1, 2, 12, 34, 56, 0, losAngeles),
},
{
name: "datetime in nowhere",
inputtype: "DateTime('Nowhere')",
inputdata: "2018-01-02 12:34:56",
failNewParser: true,
},
{
name: "zero datetime",
inputtype: "DateTime",
inputdata: "0000-00-00 00:00:00",
output: time.Time{},
},
{
name: "short datetime",
inputtype: "DateTime",
inputdata: "000-00-00 00:00:00",
output: time.Time{},
failParseData: true,
},
{
name: "malformed datetime",
inputtype: "DateTime",
inputdata: "a000-00-00 00:00:00",
output: time.Time{},
failParseData: true,
},
{
name: "tuple",
inputtype: "Tuple(String, Float64, Int16, UInt16, Int64)",
inputdata: "('hello world',32.1,-1,2,3)",
output: struct {
Field0 string
Field1 float64
Field2 int16
Field3 uint16
Field4 int64
}{"hello world", 32.1, -1, 2, 3},
},
{
name: "array of strings",
inputtype: "Array(String)",
inputdata: `['hello world\',','goodbye galaxy']`,
output: []string{"hello world',", "goodbye galaxy"},
},
{
name: "array of unquoted strings",
inputtype: "Array(String)",
inputdata: "[hello,world]",
failParseData: true,
},
{
name: "array with unfinished quoted string",
inputtype: "Array(String)",
inputdata: "['hello','world]",
failParseData: true,
},
{
name: "array of ints",
inputtype: "Array(UInt64)",
inputdata: "[1,2,3]",
output: []uint64{1, 2, 3},
},
{
name: "array of dates",
inputtype: "Array(Date)",
inputdata: "['2018-01-02','0000-00-00']",
output: []time.Time{
time.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC),
{},
},
},
{
name: "empty array of ints",
inputtype: "Array(Int8)",
inputdata: "[]",
output: []int8{},
},
{
name: "empty array of nothing",
inputtype: "Array(Nothing)",
inputdata: "[]",
output: []struct{}{},
},
{
name: "array of tuples",
inputtype: "Array(Tuple(String, Float32))",
inputdata: "[('hello world',32.1),('goodbye galaxy',42.0)]",
output: []struct {
Field0 string
Field1 float32
}{
{
"hello world",
float32(32.1),
},
{
"goodbye galaxy",
float32(42.0),
},
},
},
{
name: "malformed array element",
inputtype: "Array(UInt8)",
inputdata: "[1,2,'3']",
failParseData: true,
},
{
name: "array without left bracket",
inputtype: "Array(Int8)",
inputdata: "1,2,3]",
failParseData: true,
},
{
name: "array without right bracket",
inputtype: "Array(UInt64)",
inputdata: "[1,2,3",
failParseData: true,
},
{
name: "wrong character between tuple elements",
inputtype: "Tuple(String, String)",
inputdata: "('hello'.'world')",
failParseData: true,
},
{
name: "malformed tuple element",
inputtype: "Tuple(UInt32, Int32)",
inputdata: "(1,'2')",
failParseData: true,
},
{
name: "tuple without left paren",
inputtype: "Tuple(Int8, Int8)",
inputdata: "1,2)",
failParseData: true,
},
{
name: "tuple without right paren",
inputtype: "Tuple(UInt8, Int8)",
inputdata: "(1,2",
failParseData: true,
},
{
name: "low cardinality string",
inputtype: "LowCardinality(String)",
inputdata: "hello",
output: "hello",
},
{
name: "low cardinality string with escaping",
inputtype: "LowCardinality(String)",
inputdata: `hello \'world`,
output: "hello 'world",
},
{
name: "low cardinality string with incorrect escaping",
inputtype: "LowCardinality(String)",
inputdata: `hello world\`,
failParseData: true,
},
{
name: "low cardinality UInt64",
inputtype: "LowCardinality(UInt64)",
inputdata: "123",
output: uint64(123),
},
{
name: "ipv4",
inputtype: "IPv4",
inputdata: "127.0.0.1",
output: "127.0.0.1",
},
{
name: "ipv6",
inputtype: "IPv6",
inputdata: "2a02:aa08:e000:3100::2",
output: "2a02:aa08:e000:3100::2",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(tt *testing.T) {
desc, err := ParseTypeDesc(tc.inputtype)
if tc.failParseDesc {
assert.Error(tt, err)
return
}
if !assert.NoError(tt, err) {
return
}
parser, err := newDataParser(desc, false, tc.inputopt)
if tc.failNewParser {
assert.Error(tt, err)
return
}
if !assert.NoError(tt, err) {
return
}
output, err := parser.Parse(strings.NewReader(tc.inputdata))
if tc.failParseData {
assert.Error(tt, err)
return
}
if !assert.NoError(tt, err) {
return
}
assert.Equal(tt, tc.output, output)
})
}
}
func TestParseDataNewNullableArray(t *testing.T) {
type testCase struct {
name string
inputtype string
inputopt *DataParserOptions
inputdata string
output interface{}
failParseDesc bool
failNewParser bool
failParseData bool
}
losAngeles, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
t.Fatalf("failed to load time zone America/Los_Angeles: %v", err)
}
moscow, err := time.LoadLocation("Europe/Moscow")
if err != nil {
t.Fatalf("failed to load time zone Europe/Moscow: %v", err)
}
t.Log(losAngeles, moscow)
testCases := []*testCase{
{
name: "array of nullable null String",
inputtype: "Array(Nullable(String))",
inputdata: `['ss','\N','dd','ff']`,
output: []string{"ss", "dd", "ff"},
},
{
name: "array(nullable(uuid))",
inputtype: "Array(Nullable(UUID))",
inputdata: `['c79a9747-7cef-4b11-8177-380f7ed462a4','\N','00000000-0000-0000-0000-000000000000']`,
output: []string{"c79a9747-7cef-4b11-8177-380f7ed462a4", "00000000-0000-0000-0000-000000000000"},
},
{
name: "array of nullable null UInt64",
inputtype: "Array(Nullable(UInt64))",
inputdata: `[1,\N,5,9]`,
output: []uint64{1, 5, 9},
},
{
name: "array of nullable null UInt16",
inputtype: "Array(Nullable(UInt16))",
inputdata: `[1,2,\N]`,
output: []uint16{1, 2},
},
{
name: "malformed array(nullable(date))",
inputtype: "Array(Nullable(Date))",
inputdata: `['a000-00-00 00:00:00', '\N']`,
output: []time.Time{{}},
failParseData: true,
},
{
name: "array of nullable null Float64",
inputtype: "Array(Nullable(Float64))",
inputdata: `[1.9,\N,5.3,9.9]`,
output: []float64{1.9, 5.3, 9.9},
},
{
name: "array(nullable(datetime)), without options and argument",
inputtype: "Array(Nullable(DateTime))",
inputdata: `['2018-01-02 12:34:56','\N','0000-00-00 00:00:00']`,
output: []time.Time{
time.Date(2018, 1, 2, 12, 34, 56, 0, time.UTC),
{},
},
},
{
name: "nullable null string",
inputtype: "Nullable(String)",
inputdata: `\N`,
output: nil,
},
{
name: "nullable null UInt64",
inputtype: "Nullable(UInt64)",
inputdata: `\N`,
output: nil,
},
{
name: "nullable null decimal",
inputtype: "Nullable(Decimal)",
inputdata: `\N`,
output: nil,
},
{
name: "nullable null datetime",
inputtype: "Nullable(DateTime)",
inputdata: `\N`,
output: nil,
},
{
name: "nullable null date",
inputtype: "Nullable(Date)",
inputdata: `\N`,
output: nil,
},
{
name: "nullable not null datetime",
inputtype: "Nullable(DateTime)",
inputdata: "2018-01-02 12:34:56",
output: time.Date(2018, 1, 2, 12, 34, 56, 0, time.UTC),
},
{
name: "nullable not null date",
inputtype: "Nullable(Date)",
inputdata: "2018-01-02",
output: time.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC),
},
{
name: "nullable null enum",
inputtype: "Nullable(Enum8('hello' = 1, 'world' = 2))",
inputdata: `\N`,
output: nil,
},
{
name: "nullable null uuid",
inputtype: "Nullable(UUID)",
inputdata: `\N`,
output: nil,
},
{
name: "nullable null Date",
inputtype: "Nullable(Date)",
inputdata: `\N`,
output: nil,
},
{
name: "nullable(decimal)",
inputtype: "Nullable(Decimal(9,4))",
inputdata: "123",
output: "123",
},
{
name: "array(nullable(decimal))",
inputtype: "Array(Nullable(Decimal(9,4)))",
inputdata: "['123','555.6']",
output: []string{"123", "555.6"},
},
{
name: "nullable(enum)",
inputtype: "Nullable(Enum8('hello' = 1, 'world' = 2))",
inputdata: "hello",
output: "hello",
},
{
name: "array(nullable(enum))",
inputtype: "Array(Nullable(Enum8('hello' = 1, 'world' = 2)))",
inputdata: "['hello','hi']",
output: []string{"hello", "hi"},
},
{
name: "nullable(uuid)",
inputtype: "Nullable(UUID)",
inputdata: "c79a9747-7cef-4b11-8177-380f7ed462a4",
output: "c79a9747-7cef-4b11-8177-380f7ed462a4",
},
{
name: "array(nullable(uuid))",
inputtype: "Array(Nullable(UUID))",
inputdata: "['c79a9747-7cef-4b11-8177-380f7ed462a4','00000000-0000-0000-0000-000000000000']",
output: []string{"c79a9747-7cef-4b11-8177-380f7ed462a4", "00000000-0000-0000-0000-000000000000"},
},
{
name: "nullable low cardinality string",
inputtype: "Nullable(LowCardinality(String))",
inputdata: "hello",
output: "hello",
},
{
name: "array of nullable low cardinality string",
inputtype: "Array(Nullable(LowCardinality(String)))",
inputdata: "['hello','hi']",
output: []string{"hello", "hi"},
},
{
name: "array of nullable low cardinality UInt64",
inputtype: "Array(Nullable(LowCardinality(UInt64)))",
inputdata: "[123,555]",
output: []uint64{123, 555},
},
//////////////////////////////////////////////////////
{
name: "array(nullable(datetime)), without options and argument",
inputtype: "Array(Nullable(DateTime))",
inputdata: "['2018-01-02 12:34:56','0000-00-00 00:00:00']",
output: []time.Time{
time.Date(2018, 1, 2, 12, 34, 56, 0, time.UTC),
{},
},
},
{
name: "array(nullable(datetime)), with argument",
inputtype: "Array(Nullable(DateTime('America/Los_Angeles')))",
inputdata: "['2018-01-02 12:34:56','0000-00-00 00:00:00']",
output: []time.Time{
time.Date(2018, 1, 2, 12, 34, 56, 0, losAngeles),
{},
},
},
{
name: "array(nullable(datetime)) with argument, but location nil",
inputtype: "Array(Nullable(DateTime('America/Los_Angeles')))",
inputdata: "['2018-01-02 12:34:56','0000-00-00 00:00:00']",
inputopt: &DataParserOptions{
Location: nil,
},
output: []time.Time{
time.Date(2018, 1, 2, 12, 34, 56, 0, losAngeles),
{},
},
},
{
name: "array(nullable(datetime)) without argument, but use location",
inputtype: "Array(Nullable(DateTime))",
inputdata: "['2018-01-02 12:34:56','0000-00-00 00:00:00']",
inputopt: &DataParserOptions{
Location: moscow,
},
output: []time.Time{
time.Date(2018, 1, 2, 12, 34, 56, 0, moscow),
{},
},
},
{
name: "array(nullable(datetime)) with argument and location, ingnore argument",
inputtype: "Array(Nullable(DateTime('America/Los_Angeles')))",
inputdata: "['2018-01-02 12:34:56','0000-00-00 00:00:00']",
inputopt: &DataParserOptions{
Location: moscow,
},
output: []time.Time{
time.Date(2018, 1, 2, 12, 34, 56, 0, moscow),
{},
},
},
{
name: "array(nullable(datetime)) with argument and location, prefer argument",
inputtype: "Array(Nullable(DateTime('America/Los_Angeles')))",
inputdata: "['2018-01-02 12:34:56','0000-00-00 00:00:00']",
inputopt: &DataParserOptions{
Location: moscow,
UseDBLocation: true,
},
output: []time.Time{
time.Date(2018, 1, 2, 12, 34, 56, 0, losAngeles),
{},
},
},
{
name: "array(nullable(datetime)) in nowhere",
inputtype: "Array(Nullable(DateTime('Nowhere')))",
inputdata: "['2018-01-02 12:34:56','0000-00-00 00:00:00']",
failNewParser: true,
},
{
name: "zero array(nullable(datetime))",
inputtype: "Array(Nullable(DateTime))",
inputdata: "['0000-00-00 00:00:00','0000-00-00 00:00:00']",
output: []time.Time{
{},
{},
},
},
{
name: "short array(nullable(datetime))",
inputtype: "Array(Nullable(DateTime))",
inputdata: "['000-00-00 00:00:00','000-00-00 00:00:00']",
output: []time.Time{
{},
{},
},
failParseData: true,
},
{
name: "malformed array(nullable(datetime))",
inputtype: "Array(Nullable(DateTime))",
inputdata: "['0000-00-00 00:00:00','a000-00-00 00:00:00']",
output: []time.Time{
{},
{},
},
failParseData: true,
},
////////////////////////////////////////////////////
{
name: "array of dates",
inputtype: "Array(Date)",
inputdata: "['2018-01-02','0000-00-00']",
output: []time.Time{
time.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC),
{},
},
},
{
name: "array of nullable dates",
inputtype: "Array(Nullable(Date))",
inputdata: "['2018-01-02','0000-00-00']",
output: []time.Time{
time.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC),
{},
},
},
{
name: "malformed array(nullable(date))",
inputtype: "Array(Nullable(Date))",
inputdata: "['a000-00-00 00:00:00']",
output: []time.Time{{}},
failParseData: true,
},
{
name: "array of nullable dates with special timezone",
inputtype: "Array(Nullable(Date))",
inputdata: "['2019-06-29','0000-00-00']",
inputopt: &DataParserOptions{
Location: losAngeles,
},
output: []time.Time{
time.Date(2019, 6, 29, 0, 0, 0, 0, losAngeles),
{},
},
},
//////////////////////////////////////////////////
{
name: "array of ints",
inputtype: "Array(UInt64)",
inputdata: "[1,2,3]",
output: []uint64{1, 2, 3},
},
{
name: "array of nullable ints",
inputtype: "Array(Nullable(UInt64))",
inputdata: "[1,2,3]",
output: []uint64{1, 2, 3},
},
{
name: "empty array of nullable ints",
inputtype: "Array(Nullable(UInt64))",
inputdata: "[]",
output: []uint64{},
},
{
name: "bad array of nullable ints",
inputtype: "Array(Nullable(UInt64))",
inputdata: "[1,,]",
output: []uint64{},
failParseData: true,
},
//////////////////////////////////////////////////
{
name: "array of strings",
inputtype: "Array(String)",
inputdata: "['133','2']",
output: []string{"133", "2"},
},
{
name: "array of nullable strings",
inputtype: "Array(Nullable(String))",
inputdata: `['a\taa\',','255']`,
output: []string{"a\taa',", "255"},
},
{
name: "array of nullable strings",
inputtype: "Array(Nullable(String))",
inputdata: "['aaa,]",
output: nil,
failParseData: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(tt *testing.T) {
desc, err := ParseTypeDesc(tc.inputtype)
if tc.failParseDesc {
assert.Error(tt, err)
return
}
if !assert.NoError(tt, err) {
return
}
parser, err := newDataParser(desc, false, tc.inputopt)
if tc.failNewParser {
assert.Error(tt, err)
return
}
if !assert.NoError(tt, err) {
return
}
output, err := parser.Parse(strings.NewReader(tc.inputdata))
if tc.failParseData {
assert.Error(tt, err)
return
}
if !assert.NoError(tt, err) {
return
}
assert.Equal(tt, tc.output, output)
})
}
}