forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregates.rs
821 lines (755 loc) · 30.8 KB
/
aggregates.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use super::*;
use datafusion::scalar::ScalarValue;
#[tokio::test]
async fn csv_query_avg_multi_batch() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT avg(c12) FROM aggregate_test_100";
let plan = ctx.create_logical_plan(sql).unwrap();
let plan = ctx.optimize(&plan).unwrap();
let plan = ctx.create_physical_plan(&plan).await.unwrap();
let runtime = ctx.state.lock().runtime_env.clone();
let results = collect(plan, runtime).await.unwrap();
let batch = &results[0];
let column = batch.column(0);
let array = column.as_any().downcast_ref::<Float64Array>().unwrap();
let actual = array.value(0);
let expected = 0.5089725;
// Due to float number's accuracy, different batch size will lead to different
// answers.
assert!((expected - actual).abs() < 0.01);
Ok(())
}
#[tokio::test]
async fn csv_query_avg() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT avg(c12) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["0.5089725099127211"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_covariance_1() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT covar_pop(c2, c12) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["-0.07916932235380847"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_covariance_2() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT covar(c2, c12) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["-0.07996901247859442"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_correlation() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT corr(c2, c12) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["-0.19064544190576607"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_variance_1() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT var_pop(c2) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["1.8675"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_variance_2() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT var_pop(c6) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["26156334342021890000000000000000000000"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_variance_3() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT var_pop(c12) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["0.09234223721582163"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_variance_4() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT var(c2) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["1.8863636363636365"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_variance_5() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT var_samp(c2) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["1.8863636363636365"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_stddev_1() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT stddev_pop(c2) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["1.3665650368716449"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_stddev_2() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT stddev_pop(c6) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["5114326382039172000"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_stddev_3() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT stddev_pop(c12) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["0.30387865541334363"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_stddev_4() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT stddev(c12) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["0.3054095399405338"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_stddev_5() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT stddev_samp(c12) FROM aggregate_test_100";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["0.3054095399405338"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_stddev_6() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "select stddev(sq.column1) from (values (1.1), (2.0), (3.0)) as sq";
let mut actual = execute(&mut ctx, sql).await;
actual.sort();
let expected = vec![vec!["0.9504384952922168"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_median_1() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT approx_median(c2) FROM aggregate_test_100";
let actual = execute(&mut ctx, sql).await;
let expected = vec![vec!["3"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_median_2() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT approx_median(c6) FROM aggregate_test_100";
let actual = execute(&mut ctx, sql).await;
let expected = vec![vec!["1146409980542786560"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_median_3() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT approx_median(c12) FROM aggregate_test_100";
let actual = execute(&mut ctx, sql).await;
let expected = vec![vec!["0.5550065410522981"]];
assert_float_eq(&expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_external_table_count() {
let mut ctx = ExecutionContext::new();
register_aggregate_csv_by_sql(&mut ctx).await;
let sql = "SELECT COUNT(c12) FROM aggregate_test_100";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+-------------------------------+",
"| COUNT(aggregate_test_100.c12) |",
"+-------------------------------+",
"| 100 |",
"+-------------------------------+",
];
assert_batches_eq!(expected, &actual);
}
#[tokio::test]
async fn csv_query_external_table_sum() {
let mut ctx = ExecutionContext::new();
// cast smallint and int to bigint to avoid overflow during calculation
register_aggregate_csv_by_sql(&mut ctx).await;
let sql =
"SELECT SUM(CAST(c7 AS BIGINT)), SUM(CAST(c8 AS BIGINT)) FROM aggregate_test_100";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+-------------------------------------------+-------------------------------------------+",
"| SUM(CAST(aggregate_test_100.c7 AS Int64)) | SUM(CAST(aggregate_test_100.c8 AS Int64)) |",
"+-------------------------------------------+-------------------------------------------+",
"| 13060 | 3017641 |",
"+-------------------------------------------+-------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
}
#[tokio::test]
async fn csv_query_count() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT count(c12) FROM aggregate_test_100";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+-------------------------------+",
"| COUNT(aggregate_test_100.c12) |",
"+-------------------------------+",
"| 100 |",
"+-------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_count_distinct() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT count(distinct c2) FROM aggregate_test_100";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+---------------------------------------+",
"| COUNT(DISTINCT aggregate_test_100.c2) |",
"+---------------------------------------+",
"| 5 |",
"+---------------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_count_distinct_expr() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT count(distinct c2 % 2) FROM aggregate_test_100";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+--------------------------------------------------+",
"| COUNT(DISTINCT aggregate_test_100.c2 % Int64(2)) |",
"+--------------------------------------------------+",
"| 2 |",
"+--------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_count_star() {
let mut ctx = ExecutionContext::new();
register_aggregate_csv_by_sql(&mut ctx).await;
let sql = "SELECT COUNT(*) FROM aggregate_test_100";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+-----------------+",
"| COUNT(UInt8(1)) |",
"+-----------------+",
"| 100 |",
"+-----------------+",
];
assert_batches_eq!(expected, &actual);
}
#[tokio::test]
async fn csv_query_count_one() {
let mut ctx = ExecutionContext::new();
register_aggregate_csv_by_sql(&mut ctx).await;
let sql = "SELECT COUNT(1) FROM aggregate_test_100";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+-----------------+",
"| COUNT(UInt8(1)) |",
"+-----------------+",
"| 100 |",
"+-----------------+",
];
assert_batches_eq!(expected, &actual);
}
#[tokio::test]
async fn csv_query_approx_count() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT approx_distinct(c9) count_c9, approx_distinct(cast(c9 as varchar)) count_c9_str FROM aggregate_test_100";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+----------+--------------+",
"| count_c9 | count_c9_str |",
"+----------+--------------+",
"| 100 | 99 |",
"+----------+--------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
// This test executes the APPROX_PERCENTILE_CONT aggregation against the test
// data, asserting the estimated quantiles are ±5% their actual values.
//
// Actual quantiles calculated with:
//
// ```r
// read_csv("./testing/data/csv/aggregate_test_100.csv") |>
// select_if(is.numeric) |>
// summarise_all(~ quantile(., c(0.1, 0.5, 0.9)))
// ```
//
// Giving:
//
// ```text
// c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12
// <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
// 1 1 -95.3 -22925. -1882606710 -7.25e18 18.9 2671. 472608672. 1.83e18 0.109 0.0714
// 2 3 15.5 4599 377164262 1.13e18 134. 30634 2365817608. 9.30e18 0.491 0.551
// 3 5 102. 25334. 1991374996. 7.37e18 231 57518. 3776538487. 1.61e19 0.834 0.946
// ```
//
// Column `c12` is omitted due to a large relative error (~10%) due to the small
// float values.
#[tokio::test]
async fn csv_query_approx_percentile_cont() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
// Generate an assertion that the estimated $percentile value for $column is
// within 5% of the $actual percentile value.
macro_rules! percentile_test {
($ctx:ident, column=$column:literal, percentile=$percentile:literal, actual=$actual:literal) => {
let sql = format!("SELECT (ABS(1 - CAST(approx_percentile_cont({}, {}) AS DOUBLE) / {}) < 0.05) AS q FROM aggregate_test_100", $column, $percentile, $actual);
let actual = execute_to_batches(&mut ctx, &sql).await;
//
// "+------+",
// "| q |",
// "+------+",
// "| true |",
// "+------+",
//
let want = ["+------+", "| q |", "+------+", "| true |", "+------+"];
assert_batches_eq!(want, &actual);
};
}
percentile_test!(ctx, column = "c2", percentile = 0.1, actual = 1.0);
percentile_test!(ctx, column = "c2", percentile = 0.5, actual = 3.0);
percentile_test!(ctx, column = "c2", percentile = 0.9, actual = 5.0);
////////////////////////////////////
percentile_test!(ctx, column = "c3", percentile = 0.1, actual = -95.3);
percentile_test!(ctx, column = "c3", percentile = 0.5, actual = 15.5);
percentile_test!(ctx, column = "c3", percentile = 0.9, actual = 102.0);
////////////////////////////////////
percentile_test!(ctx, column = "c4", percentile = 0.1, actual = -22925.0);
percentile_test!(ctx, column = "c4", percentile = 0.5, actual = 4599.0);
percentile_test!(ctx, column = "c4", percentile = 0.9, actual = 25334.0);
////////////////////////////////////
percentile_test!(ctx, column = "c5", percentile = 0.1, actual = -1882606710.0);
percentile_test!(ctx, column = "c5", percentile = 0.5, actual = 377164262.0);
percentile_test!(ctx, column = "c5", percentile = 0.9, actual = 1991374996.0);
////////////////////////////////////
percentile_test!(ctx, column = "c6", percentile = 0.1, actual = -7.25e18);
percentile_test!(ctx, column = "c6", percentile = 0.5, actual = 1.13e18);
percentile_test!(ctx, column = "c6", percentile = 0.9, actual = 7.37e18);
////////////////////////////////////
percentile_test!(ctx, column = "c7", percentile = 0.1, actual = 18.9);
percentile_test!(ctx, column = "c7", percentile = 0.5, actual = 134.0);
percentile_test!(ctx, column = "c7", percentile = 0.9, actual = 231.0);
////////////////////////////////////
percentile_test!(ctx, column = "c8", percentile = 0.1, actual = 2671.0);
percentile_test!(ctx, column = "c8", percentile = 0.5, actual = 30634.0);
percentile_test!(ctx, column = "c8", percentile = 0.9, actual = 57518.0);
////////////////////////////////////
percentile_test!(ctx, column = "c9", percentile = 0.1, actual = 472608672.0);
percentile_test!(ctx, column = "c9", percentile = 0.5, actual = 2365817608.0);
percentile_test!(ctx, column = "c9", percentile = 0.9, actual = 3776538487.0);
////////////////////////////////////
percentile_test!(ctx, column = "c10", percentile = 0.1, actual = 1.83e18);
percentile_test!(ctx, column = "c10", percentile = 0.5, actual = 9.30e18);
percentile_test!(ctx, column = "c10", percentile = 0.9, actual = 1.61e19);
////////////////////////////////////
percentile_test!(ctx, column = "c11", percentile = 0.1, actual = 0.109);
percentile_test!(ctx, column = "c11", percentile = 0.5, actual = 0.491);
percentile_test!(ctx, column = "c11", percentile = 0.9, actual = 0.834);
Ok(())
}
#[tokio::test]
async fn csv_query_approx_percentile_cont_from_sketch() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_tdigest_sketch_csv(&mut ctx).await?;
// for product_id 0, it only have one sketch. Its weight and mean are
// (1.0, 0.0021979088847082773)
// (1.0, 0.029233195772592535)
// (1.0, 0.02953530916529612)
// (1.0, 0.032653132550056085)
// (1.0, 0.047812293417679674)
// (1.0, 0.13492455955229932)
// (1.0, 0.18785685919192674)
// (1.0, 0.21010216933405235)
// (1.0, 0.22009543998557224)
// (1.0, 0.23287052835067323)
// (1.0, 0.2388792692348808)
// (1.0, 0.25282693081575114)
// (1.0, 0.37484882426246224)
// (1.0, 0.37979652769522465)
// (1.0, 0.38839056303777064)
// (1.0, 0.541631191849709)
// (1.0, 0.5904656835670964)
// (1.0, 0.6128169315116692)
// (1.0, 0.8088940492058253)
// (1.0, 0.8122934091010154)
let sql = "SELECT product_id, approx_percentile_cont_from_sketch(sketch, 0.0, 'tdigest') as min FROM tdigest_sketch GROUP BY 1 ORDER BY 1";
let actual = execute_to_batches(&mut ctx, &sql).await;
let expect = [
"+------------+-----------------------+",
"| product_id | min |",
"+------------+-----------------------+",
"| 0 | 0.0021979088847082773 |",
"| 1 | 0.0023559980599913155 |",
"| 2 | 0.007316463522836103 |",
"| 3 | 0.0015306502862820759 |",
"| 4 | 0.03922392755570692 |",
"| 7 | 0.03477179524923002 |",
"| 8 | 0.06138169953397199 |",
"| 9 | 0.00599653519565968 |",
"+------------+-----------------------+",
];
assert_batches_eq!(expect, &actual);
let sql = "SELECT product_id, approx_percentile_cont_from_sketch(sketch, 1.0, 'tdigest') as max FROM tdigest_sketch GROUP BY 1 ORDER BY 1";
let actual = execute_to_batches(&mut ctx, &sql).await;
let expect = [
"+------------+--------------------+",
"| product_id | max |",
"+------------+--------------------+",
"| 0 | 0.8122934091010154 |",
"| 1 | 0.9858872798659482 |",
"| 2 | 0.9861893898445978 |",
"| 3 | 0.9612868278105434 |",
"| 4 | 0.9858000457292002 |",
"| 7 | 0.9660152207885527 |",
"| 8 | 0.9958850843107127 |",
"| 9 | 0.9331910249355163 |",
"+------------+--------------------+",
];
assert_batches_eq!(expect, &actual);
let sql = "SELECT product_id, approx_percentile_cont_from_sketch(sketch, 0.5, 'tdigest') as median FROM tdigest_sketch GROUP BY 1 ORDER BY 1";
let actual = execute_to_batches(&mut ctx, &sql).await;
let expect = [
"+------------+---------------------+",
"| product_id | median |",
"+------------+---------------------+",
"| 0 | 0.2338901686186113 |",
"| 1 | 0.5141429290538841 |",
"| 2 | 0.5340384213931233 |",
"| 3 | 0.4937008569783703 |",
"| 4 | 0.42874820961319615 |",
"| 7 | 0.5145501438517295 |",
"| 8 | 0.4315186318564606 |",
"| 9 | 0.4527059188580085 |",
"+------------+---------------------+",
];
assert_batches_eq!(expect, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_sum_crossjoin() {
let mut ctx = ExecutionContext::new();
register_aggregate_csv_by_sql(&mut ctx).await;
let sql = "SELECT a.c1, b.c1, SUM(a.c2) FROM aggregate_test_100 as a CROSS JOIN aggregate_test_100 as b GROUP BY a.c1, b.c1 ORDER BY a.c1, b.c1";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+----+----+-----------+",
"| c1 | c1 | SUM(a.c2) |",
"+----+----+-----------+",
"| a | a | 1260 |",
"| a | b | 1140 |",
"| a | c | 1260 |",
"| a | d | 1080 |",
"| a | e | 1260 |",
"| b | a | 1302 |",
"| b | b | 1178 |",
"| b | c | 1302 |",
"| b | d | 1116 |",
"| b | e | 1302 |",
"| c | a | 1176 |",
"| c | b | 1064 |",
"| c | c | 1176 |",
"| c | d | 1008 |",
"| c | e | 1176 |",
"| d | a | 924 |",
"| d | b | 836 |",
"| d | c | 924 |",
"| d | d | 792 |",
"| d | e | 924 |",
"| e | a | 1323 |",
"| e | b | 1197 |",
"| e | c | 1323 |",
"| e | d | 1134 |",
"| e | e | 1323 |",
"+----+----+-----------+",
];
assert_batches_eq!(expected, &actual);
}
#[tokio::test]
async fn query_count_without_from() -> Result<()> {
let mut ctx = ExecutionContext::new();
let sql = "SELECT count(1 + 1)";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+----------------------------+",
"| COUNT(Int64(1) + Int64(1)) |",
"+----------------------------+",
"| 1 |",
"+----------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_array_agg() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql =
"SELECT array_agg(c13) FROM (SELECT * FROM aggregate_test_100 ORDER BY c13 LIMIT 2) test";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+------------------------------------------------------------------+",
"| ARRAYAGG(test.c13) |",
"+------------------------------------------------------------------+",
"| [0VVIHzxWtNOFLtnhjHEKjXaJOSLJfm, 0keZ5G8BffGwgF2RwQD59TFzMStxCB] |",
"+------------------------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_array_agg_empty() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql =
"SELECT array_agg(c13) FROM (SELECT * FROM aggregate_test_100 LIMIT 0) test";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+--------------------+",
"| ARRAYAGG(test.c13) |",
"+--------------------+",
"| [] |",
"+--------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_array_agg_one() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql =
"SELECT array_agg(c13) FROM (SELECT * FROM aggregate_test_100 ORDER BY c13 LIMIT 1) test";
let actual = execute_to_batches(&mut ctx, sql).await;
let expected = vec![
"+----------------------------------+",
"| ARRAYAGG(test.c13) |",
"+----------------------------------+",
"| [0VVIHzxWtNOFLtnhjHEKjXaJOSLJfm] |",
"+----------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_array_agg_distinct() -> Result<()> {
let mut ctx = ExecutionContext::new();
register_aggregate_csv(&mut ctx).await?;
let sql = "SELECT array_agg(distinct c2) FROM aggregate_test_100";
let actual = execute_to_batches(&mut ctx, sql).await;
// The results for this query should be something like the following:
// +------------------------------------------+
// | ARRAYAGG(DISTINCT aggregate_test_100.c2) |
// +------------------------------------------+
// | [4, 2, 3, 5, 1] |
// +------------------------------------------+
// Since ARRAY_AGG(DISTINCT) ordering is nondeterministic, check the schema and contents.
assert_eq!(
*actual[0].schema(),
Schema::new(vec![Field::new(
"ARRAYAGG(DISTINCT aggregate_test_100.c2)",
DataType::List(Box::new(Field::new("item", DataType::UInt32, true))),
false
),])
);
// We should have 1 row containing a list
let column = actual[0].column(0);
assert_eq!(column.len(), 1);
if let ScalarValue::List(Some(mut v), _) = ScalarValue::try_from_array(column, 0)? {
// workaround lack of Ord of ScalarValue
let cmp = |a: &ScalarValue, b: &ScalarValue| {
a.partial_cmp(b).expect("Can compare ScalarValues")
};
v.sort_by(cmp);
assert_eq!(
*v,
vec![
ScalarValue::UInt32(Some(1)),
ScalarValue::UInt32(Some(2)),
ScalarValue::UInt32(Some(3)),
ScalarValue::UInt32(Some(4)),
ScalarValue::UInt32(Some(5))
]
);
} else {
unreachable!();
}
Ok(())
}
#[tokio::test]
async fn aggregate_timestamps_sum() -> Result<()> {
let mut ctx = ExecutionContext::new();
ctx.register_table("t", table_with_timestamps()).unwrap();
let results = plan_and_collect(
&mut ctx,
"SELECT sum(nanos), sum(micros), sum(millis), sum(secs) FROM t",
)
.await
.unwrap_err();
assert_eq!(results.to_string(), "Error during planning: The function Sum does not support inputs of type Timestamp(Nanosecond, None).");
Ok(())
}
#[tokio::test]
async fn aggregate_timestamps_count() -> Result<()> {
let mut ctx = ExecutionContext::new();
ctx.register_table("t", table_with_timestamps()).unwrap();
let results = execute_to_batches(
&mut ctx,
"SELECT count(nanos), count(micros), count(millis), count(secs) FROM t",
)
.await;
let expected = vec![
"+----------------+-----------------+-----------------+---------------+",
"| COUNT(t.nanos) | COUNT(t.micros) | COUNT(t.millis) | COUNT(t.secs) |",
"+----------------+-----------------+-----------------+---------------+",
"| 3 | 3 | 3 | 3 |",
"+----------------+-----------------+-----------------+---------------+",
];
assert_batches_sorted_eq!(expected, &results);
Ok(())
}
#[tokio::test]
async fn aggregate_timestamps_min() -> Result<()> {
let mut ctx = ExecutionContext::new();
ctx.register_table("t", table_with_timestamps()).unwrap();
let results = execute_to_batches(
&mut ctx,
"SELECT min(nanos), min(micros), min(millis), min(secs) FROM t",
)
.await;
let expected = vec![
"+----------------------------+----------------------------+-------------------------+---------------------+",
"| MIN(t.nanos) | MIN(t.micros) | MIN(t.millis) | MIN(t.secs) |",
"+----------------------------+----------------------------+-------------------------+---------------------+",
"| 2011-12-13 11:13:10.123450 | 2011-12-13 11:13:10.123450 | 2011-12-13 11:13:10.123 | 2011-12-13 11:13:10 |",
"+----------------------------+----------------------------+-------------------------+---------------------+",
];
assert_batches_sorted_eq!(expected, &results);
Ok(())
}
#[tokio::test]
async fn aggregate_timestamps_max() -> Result<()> {
let mut ctx = ExecutionContext::new();
ctx.register_table("t", table_with_timestamps()).unwrap();
let results = execute_to_batches(
&mut ctx,
"SELECT max(nanos), max(micros), max(millis), max(secs) FROM t",
)
.await;
let expected = vec![
"+-------------------------+-------------------------+-------------------------+---------------------+",
"| MAX(t.nanos) | MAX(t.micros) | MAX(t.millis) | MAX(t.secs) |",
"+-------------------------+-------------------------+-------------------------+---------------------+",
"| 2021-01-01 05:11:10.432 | 2021-01-01 05:11:10.432 | 2021-01-01 05:11:10.432 | 2021-01-01 05:11:10 |",
"+-------------------------+-------------------------+-------------------------+---------------------+",
];
assert_batches_sorted_eq!(expected, &results);
Ok(())
}
#[tokio::test]
async fn aggregate_timestamps_avg() -> Result<()> {
let mut ctx = ExecutionContext::new();
ctx.register_table("t", table_with_timestamps()).unwrap();
let results = plan_and_collect(
&mut ctx,
"SELECT avg(nanos), avg(micros), avg(millis), avg(secs) FROM t",
)
.await
.unwrap_err();
assert_eq!(results.to_string(), "Error during planning: The function Avg does not support inputs of type Timestamp(Nanosecond, None).");
Ok(())
}