-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
tests.rs
3769 lines (3419 loc) Β· 130 KB
/
tests.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
use crate::rows::{rows, NULL};
use crate::SqlClient;
use async_compression::tokio::write::GzipEncoder;
use cubestore::queryplanner::pretty_printers::{pp_phys_plan, pp_phys_plan_ext, PPOptions};
use cubestore::queryplanner::MIN_TOPK_STREAM_ROWS;
use cubestore::sql::timestamp_from_string;
use cubestore::store::DataFrame;
use cubestore::table::{Row, TableValue, TimestampValue};
use cubestore::util::decimal::Decimal;
use itertools::Itertools;
use pretty_assertions::assert_eq;
use std::env;
use std::fs::File;
use std::future::Future;
use std::io::Write;
use std::panic::RefUnwindSafe;
use std::path::Path;
use std::pin::Pin;
use tokio::io::{AsyncWriteExt, BufWriter};
pub type TestFn = Box<
dyn Fn(Box<dyn SqlClient>) -> Pin<Box<dyn Future<Output = ()> + Send>>
+ Send
+ Sync
+ RefUnwindSafe,
>;
pub fn sql_tests() -> Vec<(&'static str, TestFn)> {
return vec![
t("insert", insert),
t("select_test", select_test),
t("negative_numbers", negative_numbers),
t("custom_types", custom_types),
t("group_by_boolean", group_by_boolean),
t("group_by_decimal", group_by_decimal),
t("group_by_nulls", group_by_nulls),
t("float_decimal_scale", float_decimal_scale),
t("float_merge", float_merge),
t("join", join),
t("three_tables_join", three_tables_join),
t(
"three_tables_join_with_filter",
three_tables_join_with_filter,
),
t("three_tables_join_with_union", three_tables_join_with_union),
t("in_list", in_list),
t("numeric_cast", numeric_cast),
t("numbers_to_bool", numbers_to_bool),
t("union", union),
t("timestamp_select", timestamp_select),
t("column_escaping", column_escaping),
t("information_schema", information_schema),
t("case_column_escaping", case_column_escaping),
t("inner_column_escaping", inner_column_escaping),
t("convert_tz", convert_tz),
t("coalesce", coalesce),
t("count_distinct_crash", count_distinct_crash),
t(
"count_distinct_group_by_crash",
count_distinct_group_by_crash,
),
t("count_distinct_take_crash", count_distinct_take_crash),
t("create_schema_if_not_exists", create_schema_if_not_exists),
t(
"create_index_before_ingestion",
create_index_before_ingestion,
),
t("ambiguous_join_sort", ambiguous_join_sort),
t("join_with_aliases", join_with_aliases),
t("group_by_without_aggregates", group_by_without_aggregates),
t("create_table_with_location", create_table_with_location),
t("create_table_with_url", create_table_with_url),
t("create_table_fail_and_retry", create_table_fail_and_retry),
t("empty_crash", empty_crash),
t("bytes", bytes),
t("hyperloglog", hyperloglog),
t("hyperloglog_empty_inputs", hyperloglog_empty_inputs),
t("hyperloglog_empty_group_by", hyperloglog_empty_group_by),
t("hyperloglog_inserts", hyperloglog_inserts),
t("hyperloglog_inplace_group_by", hyperloglog_inplace_group_by),
t("hyperloglog_postgres", hyperloglog_postgres),
t("hyperloglog_snowflake", hyperloglog_snowflake),
t("planning_inplace_aggregate", planning_inplace_aggregate),
t("planning_hints", planning_hints),
t("planning_inplace_aggregate2", planning_inplace_aggregate2),
t("topk_large_inputs", topk_large_inputs),
t("planning_simple", planning_simple),
t("planning_joins", planning_joins),
t("planning_3_table_joins", planning_3_table_joins),
t("topk_query", topk_query),
t("topk_decimals", topk_decimals),
t("offset", offset),
t("having", having),
t("rolling_window_join", rolling_window_join),
t("rolling_window_query", rolling_window_query),
t("rolling_window_exprs", rolling_window_exprs),
t(
"rolling_window_query_timestamps",
rolling_window_query_timestamps,
),
t(
"rolling_window_extra_aggregate",
rolling_window_extra_aggregate,
),
t(
"rolling_window_extra_aggregate_timestamps",
rolling_window_extra_aggregate_timestamps,
),
t(
"rolling_window_one_week_interval",
rolling_window_one_week_interval,
),
t("rolling_window_offsets", rolling_window_offsets),
t("decimal_index", decimal_index),
t("float_index", float_index),
t("date_add", date_add),
t("now", now),
t("dump", dump),
t("unsorted_merge_assertion", unsorted_merge_assertion),
t("unsorted_data_timestamps", unsorted_data_timestamps),
];
fn t<F>(name: &'static str, f: fn(Box<dyn SqlClient>) -> F) -> (&'static str, TestFn)
where
F: Future<Output = ()> + Send + 'static,
{
(name, Box::new(move |c| Box::pin(f(c))))
}
}
async fn insert(service: Box<dyn SqlClient>) {
let _ = service.exec_query("CREATE SCHEMA Foo").await.unwrap();
let _ = service
.exec_query(
"CREATE TABLE Foo.Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)",
)
.await
.unwrap();
service.exec_query(
"INSERT INTO Foo.Persons
(
PersonID,
LastName,
FirstName,
Address,
City
)
VALUES
(23, 'LastName 1', 'FirstName 1', 'Address 1', 'City 1'), (38, 'LastName 21', 'FirstName 2', 'Address 2', 'City 2'),
(24, 'LastName 3', 'FirstName 1', 'Address 1', 'City 1'), (37, 'LastName 22', 'FirstName 2', 'Address 2', 'City 2'),
(25, 'LastName 4', 'FirstName 1', 'Address 1', 'City 1'), (36, 'LastName 23', 'FirstName 2', 'Address 2', 'City 2'),
(26, 'LastName 5', 'FirstName 1', 'Address 1', 'City 1'), (35, 'LastName 24', 'FirstName 2', 'Address 2', 'City 2'),
(27, 'LastName 6', 'FirstName 1', 'Address 1', 'City 1'), (34, 'LastName 25', 'FirstName 2', 'Address 2', 'City 2'),
(28, 'LastName 7', 'FirstName 1', 'Address 1', 'City 1'), (33, 'LastName 26', 'FirstName 2', 'Address 2', 'City 2'),
(29, 'LastName 8', 'FirstName 1', 'Address 1', 'City 1'), (32, 'LastName 27', 'FirstName 2', 'Address 2', 'City 2'),
(30, 'LastName 9', 'FirstName 1', 'Address 1', 'City 1'), (31, 'LastName 28', 'FirstName 2', 'Address 2', 'City 2')"
).await.unwrap();
service.exec_query("INSERT INTO Foo.Persons
(LastName, PersonID, FirstName, Address, City)
VALUES
('LastName 1', 23, 'FirstName 1', 'Address 1', 'City 1'), ('LastName 2', 22, 'FirstName 2', 'Address 2', 'City 2');").await.unwrap();
}
async fn select_test(service: Box<dyn SqlClient>) {
let _ = service.exec_query("CREATE SCHEMA Foo").await.unwrap();
let _ = service
.exec_query(
"CREATE TABLE Foo.Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);",
)
.await
.unwrap();
service
.exec_query(
"INSERT INTO Foo.Persons
(LastName, PersonID, FirstName, Address, City)
VALUES
('LastName 1', 23, 'FirstName 1', 'Address 1', 'City 1'),
('LastName 2', 22, 'FirstName 2', 'Address 2', 'City 2');",
)
.await
.unwrap();
let result = service
.exec_query("SELECT PersonID person_id from Foo.Persons")
.await
.unwrap();
assert_eq!(result.get_rows()[0], Row::new(vec![TableValue::Int(22)]));
assert_eq!(result.get_rows()[1], Row::new(vec![TableValue::Int(23)]));
}
async fn negative_numbers(service: Box<dyn SqlClient>) {
let _ = service.exec_query("CREATE SCHEMA foo").await.unwrap();
let _ = service
.exec_query("CREATE TABLE foo.values (int_value int)")
.await
.unwrap();
service
.exec_query("INSERT INTO foo.values (int_value) VALUES (-153)")
.await
.unwrap();
let result = service
.exec_query("SELECT * from foo.values")
.await
.unwrap();
assert_eq!(result.get_rows()[0], Row::new(vec![TableValue::Int(-153)]));
}
async fn custom_types(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();
service
.exec_query("CREATE TABLE foo.values (int_value mediumint, b1 bytes, b2 varbinary)")
.await
.unwrap();
service
.exec_query("INSERT INTO foo.values (int_value, b1, b2) VALUES (-153, X'0a', X'0b')")
.await
.unwrap();
}
async fn group_by_boolean(service: Box<dyn SqlClient>) {
let _ = service.exec_query("CREATE SCHEMA foo").await.unwrap();
let _ = service
.exec_query("CREATE TABLE foo.bool_group (bool_value boolean)")
.await
.unwrap();
service.exec_query(
"INSERT INTO foo.bool_group (bool_value) VALUES (true), (false), (true), (false), (false)"
).await.unwrap();
// TODO compaction fails the test in between?
// service.exec_query(
// "INSERT INTO foo.bool_group (bool_value) VALUES (true), (false), (true), (false), (false)"
// ).await.unwrap();
let result = service
.exec_query("SELECT count(*) from foo.bool_group")
.await
.unwrap();
assert_eq!(result.get_rows()[0], Row::new(vec![TableValue::Int(5)]));
let result = service
.exec_query("SELECT count(*) from foo.bool_group where bool_value = true")
.await
.unwrap();
assert_eq!(result.get_rows()[0], Row::new(vec![TableValue::Int(2)]));
let result = service
.exec_query("SELECT count(*) from foo.bool_group where bool_value = 'true'")
.await
.unwrap();
assert_eq!(result.get_rows()[0], Row::new(vec![TableValue::Int(2)]));
let result = service
.exec_query(
"SELECT g.bool_value, count(*) from foo.bool_group g GROUP BY 1 ORDER BY 2 DESC",
)
.await
.unwrap();
assert_eq!(
result.get_rows()[0],
Row::new(vec![TableValue::Boolean(false), TableValue::Int(3)])
);
assert_eq!(
result.get_rows()[1],
Row::new(vec![TableValue::Boolean(true), TableValue::Int(2)])
);
}
async fn group_by_decimal(service: Box<dyn SqlClient>) {
let _ = service.exec_query("CREATE SCHEMA foo").await.unwrap();
let _ = service
.exec_query("CREATE TABLE foo.decimal_group (id INT, decimal_value DECIMAL)")
.await
.unwrap();
service.exec_query(
"INSERT INTO foo.decimal_group (id, decimal_value) VALUES (1, 100), (2, 200), (3, 100), (4, 100), (5, 200)"
).await.unwrap();
let result = service
.exec_query("SELECT count(*) from foo.decimal_group")
.await
.unwrap();
assert_eq!(result.get_rows()[0], Row::new(vec![TableValue::Int(5)]));
let result = service
.exec_query("SELECT count(*) from foo.decimal_group where decimal_value = 200")
.await
.unwrap();
assert_eq!(result.get_rows()[0], Row::new(vec![TableValue::Int(2)]));
let result = service
.exec_query(
"SELECT g.decimal_value, count(*) from foo.decimal_group g GROUP BY 1 ORDER BY 2 DESC",
)
.await
.unwrap();
assert_eq!(
result.get_rows(),
&vec![
Row::new(vec![
TableValue::Decimal(Decimal::new(100 * 100_000)),
TableValue::Int(3)
]),
Row::new(vec![
TableValue::Decimal(Decimal::new(200 * 100_000)),
TableValue::Int(2)
])
]
);
}
async fn group_by_nulls(service: Box<dyn SqlClient>) {
let _ = service.exec_query("CREATE SCHEMA s").await.unwrap();
let _ = service
.exec_query("CREATE TABLE s.data (id int, n int)")
.await
.unwrap();
service
.exec_query(
"INSERT INTO s.data (id, n) VALUES (NULL, 1), (NULL, 2), (NULL, 3), (1, 1), (2, 2)",
)
.await
.unwrap();
let result = service
.exec_query("SELECT id, sum(n) from s.data group by 1 order by 1")
.await
.unwrap();
assert_eq!(
to_rows(&result),
rows(&[(None, 6), (Some(1), 1), (Some(2), 2)])
);
}
async fn float_decimal_scale(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();
service
.exec_query("CREATE TABLE foo.decimal_group (id INT, decimal_value FLOAT)")
.await
.unwrap();
service.exec_query(
"INSERT INTO foo.decimal_group (id, decimal_value) VALUES (1, 677863988852), (2, 677863988852.123e-10), (3, 6778639882.123e+3)"
).await.unwrap();
let result = service
.exec_query("SELECT SUM(decimal_value) FROM foo.decimal_group")
.await
.unwrap();
assert_eq!(
result.get_rows(),
&vec![Row::new(vec![TableValue::Float(7456503871042.786.into())])]
);
}
async fn float_merge(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA s").await.unwrap();
service
.exec_query("CREATE TABLE s.f1 (n float)")
.await
.unwrap();
service
.exec_query("INSERT INTO s.f1 (n) VALUES (1.0), (2.0)")
.await
.unwrap();
service
.exec_query("CREATE TABLE s.f2 (n float)")
.await
.unwrap();
service
.exec_query("INSERT INTO s.f2 (n) VALUES (1.0), (3.0)")
.await
.unwrap();
let r = service
.exec_query(
"SELECT n \
FROM (SELECT * from s.f1 UNION ALL SELECT * FROM s.f2) \
GROUP BY 1 \
ORDER BY 1",
)
.await
.unwrap();
assert_eq!(
to_rows(&r),
vec![
vec![TableValue::Float(1.0.into())],
vec![TableValue::Float(2.0.into())],
vec![TableValue::Float(3.0.into())],
]
);
}
async fn join(service: Box<dyn SqlClient>) {
let _ = service.exec_query("CREATE SCHEMA foo").await.unwrap();
let _ = service
.exec_query("CREATE TABLE foo.orders (customer_id text, amount int)")
.await
.unwrap();
let _ = service
.exec_query("CREATE TABLE foo.customers (id text, city text, state text)")
.await
.unwrap();
service
.exec_query(
"INSERT INTO foo.orders (customer_id, amount) VALUES ('a', 10), ('b', 2), ('b', 3)",
)
.await
.unwrap();
service.exec_query(
"INSERT INTO foo.customers (id, city, state) VALUES ('a', 'San Francisco', 'CA'), ('b', 'New York', 'NY')"
).await.unwrap();
let result = service.exec_query("SELECT c.city, sum(o.amount) from foo.orders o JOIN foo.customers c ON o.customer_id = c.id GROUP BY 1 ORDER BY 2 DESC").await.unwrap();
assert_eq!(
to_rows(&result),
vec![
vec![
TableValue::String("San Francisco".to_string()),
TableValue::Int(10)
],
vec![
TableValue::String("New York".to_string()),
TableValue::Int(5)
]
]
);
// Same query, reverse comparison order.
let result2 = service.exec_query("SELECT c.city, sum(o.amount) from foo.orders o JOIN foo.customers c ON c.id = o.customer_id GROUP BY 1 ORDER BY 2 DESC").await.unwrap();
assert_eq!(result.get_rows(), result2.get_rows());
// Join on non-existing field.
assert!(service.exec_query("SELECT c.id, sum(o.amount) FROM foo.customers c JOIN foo.orders o ON c.id = o.not_found")
.await.is_err());
assert!(service.exec_query("SELECT c.id, sum(o.amount) FROM foo.customers c JOIN foo.orders o ON o.not_found = c.id")
.await.is_err());
// Join on ambiguous fields.
let result = service
.exec_query(
"SELECT c.id, k.id FROM foo.customers c JOIN foo.customers k ON id = id ORDER BY 1",
)
.await
.unwrap();
assert_eq!(to_rows(&result), rows(&[("a", "a"), ("b", "b")]));
}
async fn three_tables_join(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();
service
.exec_query(
"CREATE TABLE foo.orders (orders_customer_id text, orders_product_id int, amount int)",
)
.await
.unwrap();
service
.exec_query("CREATE INDEX orders_by_product ON foo.orders (orders_product_id)")
.await
.unwrap();
service
.exec_query("CREATE TABLE foo.customers (customer_id text, city text, state text)")
.await
.unwrap();
service
.exec_query("CREATE TABLE foo.products (product_id int, name text)")
.await
.unwrap();
service.exec_query(
"INSERT INTO foo.orders (orders_customer_id, orders_product_id, amount) VALUES ('a', 1, 10), ('b', 2, 2), ('b', 2, 3)"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.orders (orders_customer_id, orders_product_id, amount) VALUES ('b', 1, 10), ('c', 2, 2), ('c', 2, 3)"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.orders (orders_customer_id, orders_product_id, amount) VALUES ('c', 1, 10), ('d', 2, 2), ('d', 2, 3)"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.customers (customer_id, city, state) VALUES ('a', 'San Francisco', 'CA'), ('b', 'New York', 'NY')"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.customers (customer_id, city, state) VALUES ('c', 'San Francisco', 'CA'), ('d', 'New York', 'NY')"
).await.unwrap();
service
.exec_query(
"INSERT INTO foo.products (product_id, name) VALUES (1, 'Potato'), (2, 'Tomato')",
)
.await
.unwrap();
let result = service
.exec_query(
"SELECT city, name, sum(amount) FROM foo.orders o \
LEFT JOIN foo.customers c ON orders_customer_id = customer_id \
LEFT JOIN foo.products p ON orders_product_id = product_id \
GROUP BY 1, 2 ORDER BY 3 DESC, 1 ASC, 2 ASC",
)
.await
.unwrap();
let expected = vec![
Row::new(vec![
TableValue::String("San Francisco".to_string()),
TableValue::String("Potato".to_string()),
TableValue::Int(20),
]),
Row::new(vec![
TableValue::String("New York".to_string()),
TableValue::String("Potato".to_string()),
TableValue::Int(10),
]),
Row::new(vec![
TableValue::String("New York".to_string()),
TableValue::String("Tomato".to_string()),
TableValue::Int(10),
]),
Row::new(vec![
TableValue::String("San Francisco".to_string()),
TableValue::String("Tomato".to_string()),
TableValue::Int(5),
]),
];
assert_eq!(result.get_rows(), &expected);
let result = service
.exec_query(
"SELECT city, name, sum(amount) FROM foo.orders o \
LEFT JOIN foo.customers c ON orders_customer_id = customer_id \
LEFT JOIN foo.products p ON orders_product_id = product_id \
WHERE customer_id = 'b' AND product_id IN ('2')
GROUP BY 1, 2 ORDER BY 3 DESC, 1 ASC, 2 ASC",
)
.await
.unwrap();
let expected = vec![Row::new(vec![
TableValue::String("New York".to_string()),
TableValue::String("Tomato".to_string()),
TableValue::Int(5),
])];
assert_eq!(result.get_rows(), &expected);
}
async fn three_tables_join_with_filter(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();
service
.exec_query(
"CREATE TABLE foo.orders (orders_customer_id text, orders_product_id int, amount int)",
)
.await
.unwrap();
service
.exec_query("CREATE INDEX orders_by_product ON foo.orders (orders_product_id)")
.await
.unwrap();
service
.exec_query("CREATE TABLE foo.customers (customer_id text, city text, state text)")
.await
.unwrap();
service
.exec_query("CREATE TABLE foo.products (product_id int, name text)")
.await
.unwrap();
service.exec_query(
"INSERT INTO foo.orders (orders_customer_id, orders_product_id, amount) VALUES ('a', 1, 10), ('b', 2, 2), ('b', 2, 3)"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.orders (orders_customer_id, orders_product_id, amount) VALUES ('b', 1, 10), ('c', 2, 2), ('c', 2, 3)"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.orders (orders_customer_id, orders_product_id, amount) VALUES ('c', 1, 10), ('d', 2, 2), ('d', 2, 3)"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.customers (customer_id, city, state) VALUES ('a', 'San Francisco', 'CA'), ('b', 'New York', 'NY')"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.customers (customer_id, city, state) VALUES ('c', 'San Francisco', 'CA'), ('d', 'New York', 'NY')"
).await.unwrap();
service
.exec_query(
"INSERT INTO foo.products (product_id, name) VALUES (1, 'Potato'), (2, 'Tomato')",
)
.await
.unwrap();
let result = service
.exec_query(
"SELECT city, name, sum(amount) FROM foo.orders o \
LEFT JOIN foo.products p ON orders_product_id = product_id \
LEFT JOIN foo.customers c ON orders_customer_id = customer_id \
WHERE customer_id = 'a' \
GROUP BY 1, 2 ORDER BY 3 DESC, 1 ASC, 2 ASC",
)
.await
.unwrap();
let expected = vec![Row::new(vec![
TableValue::String("San Francisco".to_string()),
TableValue::String("Potato".to_string()),
TableValue::Int(10),
])];
assert_eq!(result.get_rows(), &expected);
}
async fn three_tables_join_with_union(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();
service.exec_query("CREATE TABLE foo.orders_1 (orders_customer_id text, orders_product_id int, amount int)").await.unwrap();
service.exec_query("CREATE TABLE foo.orders_2 (orders_customer_id text, orders_product_id int, amount int)").await.unwrap();
service
.exec_query("CREATE INDEX orders_by_product_1 ON foo.orders_1 (orders_product_id)")
.await
.unwrap();
service
.exec_query("CREATE INDEX orders_by_product_2 ON foo.orders_2 (orders_product_id)")
.await
.unwrap();
service
.exec_query("CREATE TABLE foo.customers (customer_id text, city text, state text)")
.await
.unwrap();
service
.exec_query("CREATE TABLE foo.products (product_id int, name text)")
.await
.unwrap();
service.exec_query(
"INSERT INTO foo.orders_1 (orders_customer_id, orders_product_id, amount) VALUES ('a', 1, 10), ('b', 2, 2), ('b', 2, 3)"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.orders_1 (orders_customer_id, orders_product_id, amount) VALUES ('b', 1, 10), ('c', 2, 2), ('c', 2, 3)"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.orders_2 (orders_customer_id, orders_product_id, amount) VALUES ('c', 1, 10), ('d', 2, 2), ('d', 2, 3)"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.customers (customer_id, city, state) VALUES ('a', 'San Francisco', 'CA'), ('b', 'New York', 'NY')"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.customers (customer_id, city, state) VALUES ('c', 'San Francisco', 'CA'), ('d', 'New York', 'NY')"
).await.unwrap();
service
.exec_query(
"INSERT INTO foo.products (product_id, name) VALUES (1, 'Potato'), (2, 'Tomato')",
)
.await
.unwrap();
let result = service.exec_query(
"SELECT city, name, sum(amount) FROM (SELECT * FROM foo.orders_1 UNION ALL SELECT * FROM foo.orders_2) o \
LEFT JOIN foo.customers c ON orders_customer_id = customer_id \
LEFT JOIN foo.products p ON orders_product_id = product_id \
WHERE customer_id = 'a' \
GROUP BY 1, 2 ORDER BY 3 DESC, 1 ASC, 2 ASC"
).await.unwrap();
let expected = vec![Row::new(vec![
TableValue::String("San Francisco".to_string()),
TableValue::String("Potato".to_string()),
TableValue::Int(10),
])];
assert_eq!(result.get_rows(), &expected);
}
async fn in_list(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();
service
.exec_query("CREATE TABLE foo.customers (id text, city text, state text)")
.await
.unwrap();
service.exec_query(
"INSERT INTO foo.customers (id, city, state) VALUES ('a', 'San Francisco', 'CA'), ('b', 'New York', 'NY'), ('c', 'San Diego', 'CA'), ('d', 'Austin', 'TX')"
).await.unwrap();
let result = service
.exec_query("SELECT count(*) from foo.customers WHERE state in ('CA', 'TX')")
.await
.unwrap();
assert_eq!(result.get_rows()[0], Row::new(vec![TableValue::Int(3)]));
}
async fn numeric_cast(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();
service
.exec_query("CREATE TABLE foo.managers (id text, department_id int)")
.await
.unwrap();
service.exec_query(
"INSERT INTO foo.managers (id, department_id) VALUES ('a', 1), ('b', 3), ('c', 3), ('d', 5)"
).await.unwrap();
let result = service
.exec_query("SELECT count(*) from foo.managers WHERE department_id in ('3', '5')")
.await
.unwrap();
assert_eq!(result.get_rows()[0], Row::new(vec![TableValue::Int(3)]));
}
async fn numbers_to_bool(service: Box<dyn SqlClient>) {
let r = service
.exec_query("SELECT 1 = TRUE, FALSE = 0, -1 = TRUE, 123 = TRUE")
.await
.unwrap();
assert_eq!(to_rows(&r), rows(&[(true, true, true, true)]));
service.exec_query("CREATE SCHEMA s").await.unwrap();
service
.exec_query("CREATE TABLE s.bools (b boolean, i int)")
.await
.unwrap();
service
.exec_query(
"INSERT INTO s.bools(b, i) VALUES (true, 0), (false, 0), (true, 123), (false, 123)",
)
.await
.unwrap();
// Compare array with constant.
let r = service
.exec_query("SELECT b, b = 1, b = 0, b = 123 FROM s.bools GROUP BY 1, 2, 3 ORDER BY 1")
.await
.unwrap();
assert_eq!(
to_rows(&r),
rows(&[(false, false, true, false), (true, true, false, true)])
);
// Compare array with array.
let r = service
.exec_query("SELECT b, i, b = i FROM s.bools ORDER BY 1, 2")
.await
.unwrap();
assert_eq!(
to_rows(&r),
rows(&[
(false, 0, true),
(false, 123, false),
(true, 0, false),
(true, 123, true)
])
);
// Other types work fine.
let r = service
.exec_query("SELECT 1 = 1, '1' = 1, 'foo' = 'foo'")
.await
.unwrap();
assert_eq!(to_rows(&r), rows(&[(true, true, true)]))
}
async fn union(service: Box<dyn SqlClient>) {
let _ = service.exec_query("CREATE SCHEMA foo").await.unwrap();
let _ = service
.exec_query("CREATE TABLE foo.orders1 (customer_id text, amount int)")
.await
.unwrap();
let _ = service
.exec_query("CREATE TABLE foo.orders2 (customer_id text, amount int)")
.await
.unwrap();
service
.exec_query(
"INSERT INTO foo.orders1 (customer_id, amount) VALUES ('a', 10), ('b', 2), ('b', 3)",
)
.await
.unwrap();
service
.exec_query(
"INSERT INTO foo.orders2 (customer_id, amount) VALUES ('b', 20), ('c', 20), ('b', 30)",
)
.await
.unwrap();
let result = service
.exec_query(
"SELECT `u`.customer_id, sum(`u`.amount) FROM \
(select * from foo.orders1 union all select * from foo.orders2) `u` \
WHERE `u`.customer_id like '%' GROUP BY 1 ORDER BY 2 DESC",
)
.await
.unwrap();
assert_eq!(
result.get_rows()[0],
Row::new(vec![
TableValue::String("b".to_string()),
TableValue::Int(55)
])
);
assert_eq!(
result.get_rows()[1],
Row::new(vec![
TableValue::String("c".to_string()),
TableValue::Int(20)
])
);
assert_eq!(
result.get_rows()[2],
Row::new(vec![
TableValue::String("a".to_string()),
TableValue::Int(10)
])
);
}
async fn timestamp_select(service: Box<dyn SqlClient>) {
let _ = service.exec_query("CREATE SCHEMA foo").await.unwrap();
let _ = service
.exec_query("CREATE TABLE foo.timestamps (t timestamp)")
.await
.unwrap();
service.exec_query(
"INSERT INTO foo.timestamps (t) VALUES ('2020-01-01T00:00:00.000Z'), ('2020-01-02T00:00:00.000Z'), ('2020-01-03T00:00:00.000Z')"
).await.unwrap();
service.exec_query(
"INSERT INTO foo.timestamps (t) VALUES ('2020-01-01T00:00:00.000Z'), ('2020-01-02T00:00:00.000Z'), ('2020-01-03T00:00:00.000Z')"
).await.unwrap();
let result = service.exec_query("SELECT count(*) from foo.timestamps WHERE t >= to_timestamp('2020-01-02T00:00:00.000Z')").await.unwrap();
assert_eq!(result.get_rows()[0], Row::new(vec![TableValue::Int(4)]));
}
async fn column_escaping(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();
service
.exec_query("CREATE TABLE foo.timestamps (t timestamp, amount int)")
.await
.unwrap();
service
.exec_query(
"INSERT INTO foo.timestamps (t, amount) VALUES \
('2020-01-01T00:00:00.000Z', 1), \
('2020-01-01T00:01:00.000Z', 2), \
('2020-01-02T00:10:00.000Z', 3)",
)
.await
.unwrap();
let result = service
.exec_query(
"SELECT date_trunc('day', `timestamp`.t) `day`, sum(`timestamp`.amount) \
FROM foo.timestamps `timestamp` \
WHERE `timestamp`.t >= to_timestamp('2020-01-02T00:00:00.000Z') GROUP BY 1",
)
.await
.unwrap();
assert_eq!(
result.get_rows()[0],
Row::new(vec![
TableValue::Timestamp(TimestampValue::new(1577923200000000000)),
TableValue::Int(3)
])
);
}
async fn information_schema(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();
service
.exec_query("CREATE TABLE foo.timestamps (t timestamp, amount int)")
.await
.unwrap();
let result = service
.exec_query("SELECT schema_name FROM information_schema.schemata")
.await
.unwrap();
assert_eq!(
result.get_rows(),
&vec![Row::new(vec![TableValue::String("foo".to_string())])]
);
let result = service
.exec_query("SELECT table_name FROM information_schema.tables")
.await
.unwrap();
assert_eq!(
result.get_rows(),
&vec![Row::new(vec![TableValue::String("timestamps".to_string())])]
);
}
async fn case_column_escaping(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();
service
.exec_query("CREATE TABLE foo.timestamps (t timestamp, amount int)")
.await
.unwrap();
service
.exec_query(
"INSERT INTO foo.timestamps (t, amount) VALUES \
('2020-01-01T00:00:00.000Z', 1), \
('2020-01-01T00:01:00.000Z', 2), \
('2020-01-02T00:10:00.000Z', 3)",
)
.await
.unwrap();
let result = service.exec_query(
"SELECT date_trunc('day', `timestamp`.t) `day`, sum(CASE WHEN `timestamp`.t > to_timestamp('2020-01-02T00:01:00.000Z') THEN `timestamp`.amount END) \
FROM foo.timestamps `timestamp` \
WHERE `timestamp`.t >= to_timestamp('2020-01-02T00:00:00.000Z') GROUP BY 1"
).await.unwrap();
assert_eq!(
result.get_rows()[0],
Row::new(vec![
TableValue::Timestamp(TimestampValue::new(1577923200000000000)),
TableValue::Int(3)
])
);
}
async fn inner_column_escaping(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();
service
.exec_query("CREATE TABLE foo.timestamps (t timestamp, amount int)")
.await
.unwrap();