forked from suharev7/clickhouse-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclickhouse.rs
1152 lines (1011 loc) · 37.5 KB
/
clickhouse.rs
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
extern crate chrono;
extern crate chrono_tz;
extern crate clickhouse_rs;
extern crate tokio;
use std::{
env,
f64::EPSILON,
fmt::Debug,
net::{Ipv4Addr, Ipv6Addr},
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};
use chrono::prelude::*;
use chrono_tz::Tz::{self, UCT, UTC};
use tokio::prelude::*;
use clickhouse_rs::{
errors::{codes, Error},
types::{Block, Decimal, FromSql, Enum16, Enum8,},
ClientHandle, Pool,
};
use uuid::Uuid;
type BoxFuture<T> = Box<dyn Future<Item = T, Error = Error> + Send>;
fn database_url() -> String {
env::var("DATABASE_URL").unwrap_or_else(|_| {
"tcp://localhost:9000?compression=lz4&ping_timeout=2s&retry_timeout=3s".into()
})
}
/// Same as `tokio::run`, but will panic if future panics and will return the result
/// of future execution.
fn run<F, T, U>(future: F) -> Result<T, U>
where
F: Future<Item = T, Error = U> + Send + 'static,
T: Send + 'static,
U: Send + 'static,
{
let mut runtime = tokio::runtime::Runtime::new().unwrap();
let result = runtime.block_on(future);
runtime.shutdown_on_idle().wait().unwrap();
result
}
#[test]
fn test_ping() {
let pool = Pool::new(database_url());
let done = pool.get_handle().and_then(ClientHandle::ping).map(|_| ());
run(done).unwrap()
}
#[test]
fn test_connection_by_wrong_address() {
let pool = Pool::new("tcp://badaddr:9000");
let done = pool.get_handle().and_then(ClientHandle::ping).map(|_| ());
run(done).unwrap_err();
}
#[test]
fn test_create_table() {
let ddl = "\
CREATE TABLE clickhouse_test_create_table (\
click_id FixedString(64), \
click_time DateTime\
) Engine=Memory";
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(move |c| c.execute("DROP TABLE IF EXISTS clickhouse_test_create_table"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.execute(ddl))
.map(|_| ());
let err = run(done).unwrap_err();
match err {
Error::Server(err) => assert_eq!(
err.message,
"DB::Exception: Table default.clickhouse_test_create_table already exists."
),
_ => panic!("{:?}", err),
}
}
#[test]
fn test_insert() {
let ddl = r"
CREATE TABLE clickhouse_test_insert (
int8 Int8,
int16 Int16,
int32 Int32,
int64 Int64,
uint8 UInt8,
uint16 UInt16,
uint32 UInt32,
uint64 UInt64,
float32 Float32,
float64 Float64,
string String,
date Date,
datetime DateTime,
datetime64 DateTime64(3, 'UTC'),
ipv4 IPv4,
ipv6 IPv6,
ipv4str IPv4,
ipv6str IPv6,
uuid UUID
) Engine=Memory";
let block = Block::new()
.column("int8", vec![-1_i8, -2, -3, -4, -5, -6, -7])
.column("int16", vec![-1_i16, -2, -3, -4, -5, -6, -7])
.column("int32", vec![-1_i32, -2, -3, -4, -5, -6, -7])
.column("int64", vec![-1_i64, -2, -3, -4, -5, -6, -7])
.column("uint8", vec![1_u8, 2, 3, 4, 5, 6, 7])
.column("uint16", vec![1_u16, 2, 3, 4, 5, 6, 7])
.column("uint32", vec![1_u32, 2, 3, 4, 5, 6, 7])
.column("uint64", vec![1_u64, 2, 3, 4, 5, 6, 7])
.column("float32", vec![1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
.column("float64", vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
.column("string", vec!["1", "2", "3", "4", "5", "6", "7"])
.column(
"date",
vec![
UCT.ymd(2016, 10, 22),
UCT.ymd(2016, 10, 22),
UCT.ymd(2016, 10, 22),
UCT.ymd(2016, 10, 22),
UCT.ymd(2016, 10, 22),
UCT.ymd(2016, 10, 22),
UCT.ymd(2016, 10, 22),
],
)
.column(
"datetime",
vec![
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
],
)
.column(
"datetime64",
vec![
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
UTC.ymd(2016, 10, 22).and_hms(12, 0, 0),
],
)
.column(
"ipv4",
vec![
Ipv4Addr::new(10, 10, 10, 10),
Ipv4Addr::new(172, 16, 10, 10),
Ipv4Addr::new(172, 29, 45, 14),
Ipv4Addr::new(10, 10, 10, 10),
Ipv4Addr::new(172, 16, 10, 10),
Ipv4Addr::new(172, 29, 45, 14),
Ipv4Addr::new(10, 10, 10, 10),
],
)
.column(
"ipv6",
vec![
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1),
Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff),
Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1),
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1),
Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff),
Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1),
Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1),
],
)
.column(
"ipv4str",
vec![
"10.10.10.10",
"172.16.10.10",
"172.29.45.14",
"10.10.10.10",
"172.16.10.10",
"172.29.45.14",
"10.10.10.10",
],
)
.column(
"ipv6str",
vec![
"2a02:aa08:e000:3100::2",
"2001:44c8:129:2632:33:0:252:2",
"2a02:e980:1e::1",
"2a02:aa08:e000:3100::2",
"2001:44c8:129:2632:33:0:252:2",
"2a02:e980:1e::1",
"2a02:aa08:e000:3100::2",
],
)
.column(
"uuid",
vec![
Uuid::parse_str("936DA01F9ABD4d9d80C702AF85C822A8").unwrap(),
Uuid::nil(),
Uuid::nil(),
Uuid::nil(),
Uuid::nil(),
Uuid::nil(),
Uuid::nil(),
],
);
let expected = block.clone();
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(move |c| c.execute("DROP TABLE IF EXISTS clickhouse_test_insert"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.insert("clickhouse_test_insert", block))
.and_then(move |c| c.query("SELECT * FROM clickhouse_test_insert").fetch_all())
.map(move |(_, actual)| {
assert_eq!(format!("{:?}", expected.as_ref()), format!("{:?}", &actual));
});
run(done).unwrap()
}
#[test]
fn test_select() {
let ddl = "
CREATE TABLE clickhouse_test_select (
id Int32,
code String,
date Date,
datetime DateTime
) Engine=Memory";
let block = Block::new()
.column("id", vec![1, 2, 3, 4])
.column("code", vec!["RU", "UA", "DE", "US"])
.column(
"date",
vec![
UTC.ymd(2014, 7, 8),
UTC.ymd(2014, 7, 8),
UTC.ymd(2014, 7, 8),
UTC.ymd(2014, 7, 9),
],
)
.column(
"datetime",
vec![
Tz::Singapore.ymd(2014, 7, 8).and_hms(14, 0, 0),
UTC.ymd(2014, 7, 8).and_hms(14, 0, 0),
UTC.ymd(2014, 7, 8).and_hms(14, 0, 0),
UTC.ymd(2014, 7, 8).and_hms(13, 0, 0),
],
);
let pool = Pool::new(database_url());
let done = pool.get_handle()
.and_then(|c| c.execute("DROP TABLE IF EXISTS clickhouse_test_select"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.insert("clickhouse_test_select", block))
.and_then(|c| c.query("SELECT COUNT(*) FROM clickhouse_test_select").fetch_all())
.and_then(|(c, r)| {
assert_eq!(4, r.get::<u64, _>(0, 0)?);
Ok(c)
})
.and_then(|c| c.query("SELECT COUNT(*) FROM clickhouse_test_select WHERE date = '2014-07-08'").fetch_all())
.and_then(|(c, r)| {
assert_eq!(3, r.get::<u64, _>(0, 0)?);
Ok(c)
})
.and_then(|c| c.query("SELECT COUNT(*) FROM clickhouse_test_select WHERE datetime = '2014-07-08 14:00:00'").fetch_all())
.and_then(|(c, r)| {
assert_eq!(2, r.get::<u64, _>(0, 0)?);
Ok(c)
})
.and_then(|c| c.query("SELECT COUNT(*) FROM clickhouse_test_select WHERE id IN (1, 2, 3)").fetch_all())
.and_then(|(c, r)| {
assert_eq!(3, r.get::<u64, _>(0, 0)?);
Ok(c)
})
.and_then(|c| c.query("SELECT COUNT(*) FROM clickhouse_test_select WHERE code IN ('US', 'DE', 'RU')").fetch_all())
.and_then(|(c, r)| {
assert_eq!(3, r.get::<u64, _>(0, 0)?);
Ok(c)
})
.and_then(|c| c.query("SELECT id FROM clickhouse_test_select ORDER BY id LIMIT 1").fetch_all())
.and_then(|(c, r)| {
assert_eq!(r.row_count(), 1);
assert_eq!(1, r.get::<i32, _>(0, "id")?);
Ok(c)
})
.and_then(|c| c.query("SELECT id FROM clickhouse_test_select ORDER BY id LIMIT 1, 2").fetch_all())
.and_then(|(_, r)| {
assert_eq!(r.row_count(), 2);
assert_eq!(2, r.get::<i32, _>(0, "id")?);
assert_eq!(3, r.get::<i32, _>(1, 0)?);
Ok(())
});
run(done).unwrap();
}
#[test]
fn test_simple_select() {
let pool = Pool::new(database_url());
let done = pool.get_handle()
.and_then(|c| c.query("SELECT a FROM (SELECT 1 AS a UNION ALL SELECT 2 AS a UNION ALL SELECT 3 AS a) ORDER BY a ASC").fetch_all())
.and_then(|(c, actual)| {
let expected = Block::new()
.column("a", vec![1_u8, 2, 3]);
assert_eq!(expected, actual);
Ok(c)
})
.and_then(|c| c.query("SELECT min(a) FROM (SELECT 1 AS a UNION ALL SELECT 2 AS a UNION ALL SELECT 3 AS a)").fetch_all())
.and_then(|(c, r)| {
assert_eq!(1, r.get::<u8, _>(0, 0)?);
Ok(c)
})
.and_then(|c| c.query("SELECT max(a) FROM (SELECT 1 AS a UNION ALL SELECT 2 AS a UNION ALL SELECT 3 AS a)").fetch_all())
.and_then(|(c, r)| {
assert_eq!(3, r.get::<u8, _>(0, 0)?);
Ok(c)
})
.and_then(|c| c.query("SELECT sum(a) FROM (SELECT 1 AS a UNION ALL SELECT 2 AS a UNION ALL SELECT 3 AS a)").fetch_all())
.and_then(|(c, r)| {
assert_eq!(6, r.get::<u64, _>(0, 0)?);
Ok(c)
})
.and_then(|c| c.query("SELECT median(a) FROM (SELECT 1 AS a UNION ALL SELECT 2 AS a UNION ALL SELECT 3 AS a)").fetch_all())
.and_then(|(_, r)| {
assert!((2_f64 - r.get::<f64, _>(0, 0)?).abs() < EPSILON);
Ok(())
});
run(done).unwrap();
}
#[test]
fn test_temporary_table() {
let ddl = "CREATE TEMPORARY TABLE clickhouse_test_temporary_table (ID UInt64);";
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(move |c| c.execute(ddl))
.and_then(|c| {
c.execute(
"INSERT INTO clickhouse_test_temporary_table (ID) \
SELECT number AS ID FROM system.numbers LIMIT 10",
)
})
.and_then(|c| {
c.query("SELECT ID AS ID FROM clickhouse_test_temporary_table")
.fetch_all()
})
.map(|(_, block)| {
let expected = Block::new().column("ID", (0_u64..10).collect::<Vec<_>>());
assert_eq!(block, expected)
});
run(done).unwrap();
}
#[test]
fn test_with_totals() {
let ddl = "
CREATE TABLE clickhouse_test_with_totals (
country String
) Engine=Memory";
let query = "
SELECT
country,
COUNT(*)
FROM clickhouse_test_with_totals
GROUP BY country
WITH TOTALS";
let block = Block::new().column("country", vec!["RU", "EN", "RU", "RU", "EN", "RU"]);
let expected = Block::new()
.column("country", vec!["EN", "RU", ""])
.column("country", vec![2u64, 4, 6]);
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(|c| c.execute("DROP TABLE IF EXISTS clickhouse_test_with_totals"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.insert("clickhouse_test_with_totals", block))
.and_then(move |c| c.query(query).fetch_all())
.map(move |(_, block)| assert_eq!(&expected, &block));
run(done).unwrap();
}
#[test]
fn test_stream_rows() {
let pool = Pool::new(database_url());
let done = pool.get_handle().and_then(|c| {
c.query("SELECT number FROM system.numbers LIMIT 10")
.stream_rows()
.fold(0_u64, |acc, row| -> Result<u64, Error> {
let number: u64 = row.get("number")?;
Ok(acc + number)
})
});
assert_eq!(45, run(done).unwrap());
}
#[test]
fn test_concurrent_queries() {
fn query_sum(n: u64) -> BoxFuture<u64> {
let sql = format!("SELECT number FROM system.numbers LIMIT {}", n);
let pool = Pool::new(database_url());
Box::new(
pool.get_handle()
.and_then(move |c| {
c.query(sql.as_str())
.fold(0_u64, |acc, row| Ok(acc + row.get::<u64, _>("number")?))
})
.map(|(_, value)| value),
)
}
let m = 250_000_u64;
let expected = m * (m - 1) / 2
+ (m * 2) * ((m * 2) - 1) / 2
+ (m * 3) * ((m * 3) - 1) / 2
+ (m * 4) * ((m * 4) - 1) / 2;
let requests = vec![
query_sum(m),
query_sum(m * 2),
query_sum(m * 3),
query_sum(m * 4),
];
let done = future::join_all(requests).and_then(move |xs| {
let actual: u64 = xs.iter().sum();
assert_eq!(actual, expected);
Ok(())
});
run(done).unwrap();
}
#[test]
fn test_big_block() {
let sql = "SELECT
number, number, number, number, number, number, number, number, number, number
FROM system.numbers LIMIT 20000";
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(move |c| c.query(sql).fetch_all())
.and_then(move |(_, block)| Ok(block.row_count()));
let actual = run(done).unwrap();
assert_eq!(actual, 20000)
}
#[test]
fn test_nullable() {
let ddl = "
CREATE TABLE clickhouse_test_nullable (
int8 Nullable(Int8),
int16 Nullable(Int16),
int32 Nullable(Int32),
int64 Nullable(Int64),
uint8 Nullable(UInt8),
uint16 Nullable(UInt16),
uint32 Nullable(UInt32),
uint64 Nullable(UInt64),
float32 Nullable(Float32),
float64 Nullable(Float64),
string Nullable(String),
date Nullable(Date),
datetime Nullable(DateTime),
datetime64 Nullable(DateTime64(3, 'UTC')),
ipv4 Nullable(IPv4),
ipv6 Nullable(IPv6),
uuid Nullable(UUID)
) Engine=Memory";
let query = "
SELECT
int8,
int16,
int32,
int64,
uint8,
uint16,
uint32,
uint64,
float32,
float64,
string,
date,
datetime,
datetime64,
ipv4,
ipv6,
uuid
FROM clickhouse_test_nullable";
let date_value: Date<Tz> = UTC.ymd(2016, 10, 22);
let date_time_value: DateTime<Tz> = UTC.ymd(2014, 7, 8).and_hms(14, 0, 0);
let block = Block::new()
.column("int8", vec![Some(1_i8)])
.column("int16", vec![Some(1_i16)])
.column("int32", vec![Some(1_i32)])
.column("int64", vec![Some(1_i64)])
.column("uint8", vec![Some(1_u8)])
.column("uint16", vec![Some(1_u16)])
.column("uint32", vec![Some(1_u32)])
.column("uint64", vec![Some(1_u64)])
.column("float32", vec![Some(1_f32)])
.column("float64", vec![Some(1_f64)])
.column("string", vec![Some("text")])
.column("date", vec![Some(date_value)])
.column("datetime", vec![Some(date_time_value)])
.column("datetime64", vec![Some(date_time_value)])
.column("ipv4", vec![Some(Ipv4Addr::new(127, 0, 0, 1))])
.column(
"ipv6",
vec![Some(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff))],
)
.column("uuid", vec![Some(Uuid::nil())]);
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(|c| c.execute("DROP TABLE IF EXISTS clickhouse_test_nullable"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.insert("clickhouse_test_nullable", block))
.and_then(move |c| c.query(query).fetch_all())
.and_then(move |(_, block)| {
let int8: Option<i8> = block.get(0, "int8")?;
let int16: Option<i16> = block.get(0, "int16")?;
let int32: Option<i32> = block.get(0, "int32")?;
let int64: Option<i64> = block.get(0, "int64")?;
let uint8: Option<u8> = block.get(0, "uint8")?;
let uint16: Option<u16> = block.get(0, "uint16")?;
let uint32: Option<u32> = block.get(0, "uint32")?;
let uint64: Option<u64> = block.get(0, "uint64")?;
let float32: Option<f32> = block.get(0, "float32")?;
let float64: Option<f64> = block.get(0, "float64")?;
let string: Option<&str> = block.get(0, "string")?;
let date: Option<Date<Tz>> = block.get(0, "date")?;
let datetime: Option<DateTime<Tz>> = block.get(0, "datetime")?;
let datetime64: Option<DateTime<Tz>> = block.get(0, "datetime64")?;
let ipv4: Option<Ipv4Addr> = block.get(0, "ipv4")?;
let ipv6: Option<Ipv6Addr> = block.get(0, "ipv6")?;
let uuid: Option<Uuid> = block.get(0, "uuid")?;
assert_eq!(int8, Some(1_i8));
assert_eq!(int16, Some(1_i16));
assert_eq!(int32, Some(1_i32));
assert_eq!(int64, Some(1_i64));
assert_eq!(uint8, Some(1_u8));
assert_eq!(uint16, Some(1_u16));
assert_eq!(uint32, Some(1_u32));
assert_eq!(uint64, Some(1_u64));
assert_eq!(float32, Some(1_f32));
assert_eq!(float64, Some(1_f64));
assert_eq!(string, Some("text"));
assert_eq!(date, Some(date_value));
assert_eq!(datetime, Some(date_time_value));
assert_eq!(datetime64, Some(date_time_value));
assert_eq!(ipv4, Some(Ipv4Addr::new(127, 0, 0, 1)));
assert_eq!(
ipv6,
Some(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff))
);
assert_eq!(uuid, Some(Uuid::nil()));
Ok(())
});
run(done).unwrap();
}
#[test]
fn test_generic_column() {
fn extract_to_vec<'a, T>(name: &str, block: &'a Block) -> Vec<T>
where
T: FromSql<'a> + Debug + 'static,
{
let n = block.row_count();
let mut result = Vec::with_capacity(n);
for row_index in 0..n {
let value: T = block.get(row_index, name).unwrap();
result.push(value)
}
result
}
let block = Block::new()
.column("int", vec![1u32, 2, 3])
.column("str", vec!["A", "B", "C"]);
let int_vec: Vec<u32> = extract_to_vec("int", &block);
let str_vec: Vec<String> = extract_to_vec("str", &block);
assert_eq!(int_vec, vec![1u32, 2, 3]);
assert_eq!(
str_vec,
vec!["A".to_string(), "B".to_string(), "C".to_string()]
);
}
#[test]
fn test_fixed_string() {
let ddl = "
CREATE TABLE clickhouse_test_fixed_string (
text FixedString(4),
opt_text Nullable(FixedString(4))
) Engine=Memory";
let query = "
SELECT
text,
opt_text
FROM clickhouse_test_fixed_string";
let block = Block::new()
.column("opt_text", vec![Some("text")])
.column("text", vec!["text"]);
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(|c| c.execute("DROP TABLE IF EXISTS clickhouse_test_fixed_string"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.insert("clickhouse_test_fixed_string", block))
.and_then(move |c| c.query(query).fetch_all())
.and_then(move |(_, block)| {
let text: &str = block.get(0, "text")?;
let opt_text: Option<&str> = block.get(0, "opt_text")?;
assert_eq!(text, "text");
assert_eq!(opt_text, Some("text"));
Ok(())
});
run(done).unwrap();
}
#[test]
fn test_binary_string() {
let ddl = "
CREATE TABLE IF NOT EXISTS clickhouse_binary_string (
text String,
fx_text FixedString(4),
opt_text Nullable(String),
fx_opt_text Nullable(FixedString(4))
) Engine=Memory";
let query = "
SELECT
text,
fx_text,
opt_text,
fx_opt_text
FROM clickhouse_binary_string";
let block = Block::new()
.column("text", vec![vec![0_u8, 159, 146, 150]])
.column("fx_text", vec![vec![0_u8, 159, 146, 150]])
.column("opt_text", vec![Some(vec![0_u8, 159, 146, 150])])
.column("fx_opt_text", vec![Some(vec![0_u8, 159, 146, 150])]);
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(|c| c.execute("DROP TABLE IF EXISTS clickhouse_binary_string"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.insert("clickhouse_binary_string", block))
.and_then(move |c| c.query(query).fetch_all())
.and_then(move |(_, block)| {
let text: &[u8] = block.get(0, "text")?;
let fx_text: &[u8] = block.get(0, "fx_text")?;
let opt_text: Option<&[u8]> = block.get(0, "opt_text")?;
let fx_opt_text: Option<&[u8]> = block.get(0, "fx_opt_text")?;
assert_eq!(1, block.row_count());
assert_eq!([0, 159, 146, 150].as_ref(), text);
assert_eq!([0, 159, 146, 150].as_ref(), fx_text);
assert_eq!(Some([0, 159, 146, 150].as_ref()), opt_text);
assert_eq!(Some([0, 159, 146, 150].as_ref()), fx_opt_text);
Ok(())
});
run(done).unwrap();
}
#[test]
fn test_enum_16_not_nullable() {
let ddl = "
CREATE TABLE IF NOT EXISTS clickhouse_enum16_non_nul (
enum_16_row Enum16(
'zero' = 5,
'first' = 6
)
) Engine=Memory";
let query = "
SELECT
enum_16_row
FROM clickhouse_enum16_non_nul";
let block = Block::new().column("enum_16_row", vec![Enum16::of(5), Enum16::of(6)]);
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(|c| c.execute("DROP TABLE IF EXISTS clickhouse_enum16_non_nul"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.insert("clickhouse_enum16_non_nul", block))
.and_then(move |c| c.query(query).fetch_all())
.and_then(move |(_, block)| {
let enum_16_a: Enum16 = block.get(0, "enum_16_row")?;
let enum_16_b: Enum16 = block.get(1, "enum_16_row")?;
assert_eq!(2, block.row_count());
assert_eq!(
vec!([Enum16::of(5), Enum16::of(6)]),
vec!([enum_16_a, enum_16_b])
);
Ok(())
});
run(done).unwrap();
}
#[test]
fn test_enum_16_nullable() {
let ddl = "
CREATE TABLE IF NOT EXISTS clickhouse_enum (
enum_16_row Nullable(Enum16(
'zero' = 5,
'first' = 6
))
) Engine=Memory";
let query = "
SELECT
enum_16_row
FROM clickhouse_enum";
let block = Block::new().column(
"enum_16_row",
vec![
Some(Enum16::of(5)),
Some(Enum16::of(6)),
Option::<Enum16>::None,
],
);
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(|c| c.execute("DROP TABLE IF EXISTS clickhouse_enum"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.insert("clickhouse_enum", block))
.and_then(move |c| c.query(query).fetch_all())
.and_then(move |(_, block)| {
let enum_16_a: Option<Enum16> = block.get(0, "enum_16_row")?;
let enum_16_b: Option<Enum16> = block.get(1, "enum_16_row")?;
assert_eq!(3, block.row_count());
assert_eq!(
vec!([Some(Enum16::of(5)), Some(Enum16::of(6))]),
vec!([enum_16_a, enum_16_b])
);
Ok(())
});
run(done).unwrap();
}
#[test]
fn test_enum8() {
let ddl = "
CREATE TABLE IF NOT EXISTS clickhouse_Enum (
enum_8_row Enum8(
'zero' = 1,
'first' = 2
)
) Engine=Memory";
let query = "
SELECT
enum_8_row
FROM clickhouse_Enum";
let block = Block::new().column("enum_8_row", vec![Enum8::of(1), Enum8::of(2)]);
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(|c| c.execute("DROP TABLE IF EXISTS clickhouse_Enum"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.insert("clickhouse_Enum", block))
.and_then(move |c| c.query(query).fetch_all())
.and_then(move |(_, block)| {
let enum_8_a: Enum8 = block.get(0, "enum_8_row")?;
let enum_8_b: Enum8 = block.get(1, "enum_8_row")?;
assert_eq!(2, block.row_count());
assert_eq!(
vec!([Enum8::of(1), Enum8::of(2)]),
vec!([enum_8_a, enum_8_b])
);
Ok(())
});
run(done).unwrap();
}
#[test]
fn test_array() {
let ddl = "
CREATE TABLE clickhouse_array (
u8 Array(UInt8),
u32 Array(UInt32),
f64 Array(Float64),
text1 Array(String),
text2 Array(String),
date Array(Date),
time Array(DateTime),
time64 Array(DateTime64(3, 'UTC'))
) Engine=Memory";
let query = "SELECT u8, u32, f64, text1, text2, date, time, time64 FROM clickhouse_array";
let date_value: Date<Tz> = UTC.ymd(2016, 10, 22);
let date_time_value: DateTime<Tz> = UTC.ymd(2014, 7, 8).and_hms(14, 0, 0);
let block = Block::new()
.column("u8", vec![vec![41_u8]])
.column("u32", vec![vec![42_u32]])
.column("f64", vec![vec![42_f64]])
.column("text1", vec![vec!["A"]])
.column("text2", vec![vec!["B".to_string()]])
.column("date", vec![vec![date_value]])
.column("time", vec![vec![date_time_value]])
.column("time64", vec![vec![date_time_value]]);
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(|c| c.execute("DROP TABLE IF EXISTS clickhouse_array"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.insert("clickhouse_array", block))
.and_then(move |c| c.query(query).fetch_all())
.and_then(move |(_, block)| {
let u8_vec: Vec<u8> = block.get(0, "u8")?;
let u32_vec: Vec<u32> = block.get(0, "u32")?;
let f64_vec: Vec<f64> = block.get(0, "f64")?;
let text1_vec: Vec<&str> = block.get(0, "text1")?;
let text2_vec: Vec<String> = block.get(0, "text2")?;
let date_vec: Vec<Date<Tz>> = block.get(0, "date")?;
let time_vec: Vec<DateTime<Tz>> = block.get(0, "time")?;
let time64_vec: Vec<DateTime<Tz>> = block.get(0, "time64")?;
assert_eq!(1, block.row_count());
assert_eq!(vec![41_u8], u8_vec);
assert_eq!(vec![42_u32], u32_vec);
assert_eq!(vec![42_f64], f64_vec);
assert_eq!(vec!["A"], text1_vec);
assert_eq!(vec!["B".to_string()], text2_vec);
assert_eq!(vec![date_value], date_vec);
assert_eq!(vec![date_time_value], time_vec);
assert_eq!(vec![date_time_value], time64_vec);
Ok(())
});
run(done).unwrap();
}
#[test]
#[allow(clippy::float_cmp)]
fn test_decimal() {
let ddl = "
CREATE TABLE clickhouse_decimal (
x Decimal(8, 3),
ox Nullable(Decimal(10, 2))
) Engine=Memory";
let query = "SELECT x, ox FROM clickhouse_decimal";
let block = Block::new()
.column("x", vec![Decimal::of(1.234, 3), Decimal::of(5, 3)])
.column("ox", vec![None, Some(Decimal::of(1.23, 2))]);
let pool = Pool::new(database_url());
let done = pool
.get_handle()
.and_then(|c| c.execute("DROP TABLE IF EXISTS clickhouse_decimal"))
.and_then(move |c| c.execute(ddl))
.and_then(move |c| c.insert("clickhouse_decimal", block))
.and_then(move |c| c.query(query).fetch_all())
.and_then(move |(_, block)| {
let x: Decimal = block.get(0, "x")?;
let ox: Option<Decimal> = block.get(1, "ox")?;
let ox0: Option<Decimal> = block.get(0, "ox")?;
assert_eq!(2, block.row_count());
assert_eq!(1.234, x.into());
assert_eq!(Some(1.23), ox.map(|v| v.into()));
assert_eq!(None, ox0);
Ok(())
});
run(done).unwrap();
}
#[test]
fn test_reconnect() {
let counter = Arc::new(AtomicUsize::new(0));
let url = format!("{}{}", database_url(), "&pool_max=1&pool_min=1");
let pool = Pool::new(url);
for _ in 0..2 {
let counter = counter.clone();
let done = pool
.get_handle()
.and_then(move |c| c.query("SELECT 1").fetch_all())
.and_then(move |(_, block)| {
let value: u8 = block.get(0, 0)?;
counter.fetch_add(value as usize, Ordering::SeqCst);
Ok(())
})
.map_err(|err| eprintln!("database error: {}", err));
run(done).unwrap();
}
assert_eq!(2, counter.load(Ordering::SeqCst))
}
#[test]
fn test_column_iter() {
let ddl = r"
CREATE TABLE clickhouse_test_column_iter (
uint64 UInt64,
str String,
fixed_str FixedString(1),
opt_str Nullable(String),
date Date,
datetime DateTime,
datetime64 DateTime64(3, 'UTC'),
decimal Decimal(8, 3),
array Array(UInt32),
ipv4 IPv4,
ipv6 IPv6,
uuid UUID
) Engine=Memory";
let query = r"SELECT * FROM clickhouse_test_column_iter";
let date_value: Date<Tz> = UTC.ymd(2016, 10, 22);
let date_time_value: DateTime<Tz> = UTC.ymd(2014, 7, 8).and_hms(14, 0, 0);