forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpr.rs
1731 lines (1604 loc) · 66.8 KB
/
expr.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
// 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 datafusion::datasource::empty::EmptyTable;
use super::*;
#[tokio::test]
async fn case_when() -> Result<()> {
let ctx = create_case_context()?;
let sql = "SELECT \
CASE WHEN c1 = 'a' THEN 1 \
WHEN c1 = 'b' THEN 2 \
END \
FROM t1";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+--------------------------------------------------------------------------------------+",
"| CASE WHEN #t1.c1 = Utf8(\"a\") THEN Int64(1) WHEN #t1.c1 = Utf8(\"b\") THEN Int64(2) END |",
"+--------------------------------------------------------------------------------------+",
"| 1 |",
"| 2 |",
"| |",
"| |",
"+--------------------------------------------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn case_when_else() -> Result<()> {
let ctx = create_case_context()?;
let sql = "SELECT \
CASE WHEN c1 = 'a' THEN 1 \
WHEN c1 = 'b' THEN 2 \
ELSE 999 END \
FROM t1";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+------------------------------------------------------------------------------------------------------+",
"| CASE WHEN #t1.c1 = Utf8(\"a\") THEN Int64(1) WHEN #t1.c1 = Utf8(\"b\") THEN Int64(2) ELSE Int64(999) END |",
"+------------------------------------------------------------------------------------------------------+",
"| 1 |",
"| 2 |",
"| 999 |",
"| 999 |",
"+------------------------------------------------------------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn case_when_with_base_expr() -> Result<()> {
let ctx = create_case_context()?;
let sql = "SELECT \
CASE c1 WHEN 'a' THEN 1 \
WHEN 'b' THEN 2 \
END \
FROM t1";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------------------------------------------------------------+",
"| CASE #t1.c1 WHEN Utf8(\"a\") THEN Int64(1) WHEN Utf8(\"b\") THEN Int64(2) END |",
"+---------------------------------------------------------------------------+",
"| 1 |",
"| 2 |",
"| |",
"| |",
"+---------------------------------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn case_when_else_with_base_expr() -> Result<()> {
let ctx = create_case_context()?;
let sql = "SELECT \
CASE c1 WHEN 'a' THEN 1 \
WHEN 'b' THEN 2 \
ELSE 999 END \
FROM t1";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------------------------------------------------------------------------------------------+",
"| CASE #t1.c1 WHEN Utf8(\"a\") THEN Int64(1) WHEN Utf8(\"b\") THEN Int64(2) ELSE Int64(999) END |",
"+-------------------------------------------------------------------------------------------+",
"| 1 |",
"| 2 |",
"| 999 |",
"| 999 |",
"+-------------------------------------------------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn case_when_else_with_null_contant() -> Result<()> {
let ctx = create_case_context()?;
let sql = "SELECT \
CASE WHEN c1 = 'a' THEN 1 \
WHEN NULL THEN 2 \
ELSE 999 END \
FROM t1";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+----------------------------------------------------------------------------------------+",
"| CASE WHEN #t1.c1 = Utf8(\"a\") THEN Int64(1) WHEN NULL THEN Int64(2) ELSE Int64(999) END |",
"+----------------------------------------------------------------------------------------+",
"| 1 |",
"| 999 |",
"| 999 |",
"| 999 |",
"+----------------------------------------------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
let sql = "SELECT CASE WHEN NULL THEN 'foo' ELSE 'bar' END";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+------------------------------------------------------+",
"| CASE WHEN NULL THEN Utf8(\"foo\") ELSE Utf8(\"bar\") END |",
"+------------------------------------------------------+",
"| bar |",
"+------------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn case_expr_with_null() -> Result<()> {
let ctx = SessionContext::new();
let sql = "select case when b is null then null else b end from (select a,b from (values (1,null),(2,3)) as t (a,b)) a;";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+------------------------------------------------+",
"| CASE WHEN #a.b IS NULL THEN NULL ELSE #a.b END |",
"+------------------------------------------------+",
"| |",
"| 3 |",
"+------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
let sql = "select case when b is null then null else b end from (select a,b from (values (1,1),(2,3)) as t (a,b)) a;";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+------------------------------------------------+",
"| CASE WHEN #a.b IS NULL THEN NULL ELSE #a.b END |",
"+------------------------------------------------+",
"| 1 |",
"| 3 |",
"+------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn case_expr_with_nulls() -> Result<()> {
let ctx = SessionContext::new();
let sql = "select case when b is null then null when b < 3 then null when b >=3 then b + 1 else b end from (select a,b from (values (1,null),(1,2),(2,3)) as t (a,b)) a";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+--------------------------------------------------------------------------------------------------------------------------+",
"| CASE WHEN #a.b IS NULL THEN NULL WHEN #a.b < Int64(3) THEN NULL WHEN #a.b >= Int64(3) THEN #a.b + Int64(1) ELSE #a.b END |",
"+--------------------------------------------------------------------------------------------------------------------------+",
"| |",
"| |",
"| 4 |",
"+--------------------------------------------------------------------------------------------------------------------------+"
];
assert_batches_eq!(expected, &actual);
let sql = "select case b when 1 then null when 2 then null when 3 then b + 1 else b end from (select a,b from (values (1,null),(1,2),(2,3)) as t (a,b)) a;";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+------------------------------------------------------------------------------------------------------------+",
"| CASE #a.b WHEN Int64(1) THEN NULL WHEN Int64(2) THEN NULL WHEN Int64(3) THEN #a.b + Int64(1) ELSE #a.b END |",
"+------------------------------------------------------------------------------------------------------------+",
"| |",
"| |",
"| 4 |",
"+------------------------------------------------------------------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn query_not() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Boolean, true)]));
let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(BooleanArray::from(vec![
Some(false),
None,
Some(true),
]))],
)?;
let table = MemTable::try_new(schema, vec![vec![data]])?;
let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT NOT c1 FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------------+",
"| NOT test.c1 |",
"+-------------+",
"| true |",
"| |",
"| false |",
"+-------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn csv_query_sum_cast() {
let ctx = SessionContext::new();
register_aggregate_csv_by_sql(&ctx).await;
// c8 = i32; c6 = i64
let sql = "SELECT c8 + c6 FROM aggregate_test_100";
// check that the physical and logical schemas are equal
execute(&ctx, sql).await;
}
#[tokio::test]
async fn query_is_null() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Float64, true)]));
let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(Float64Array::from(vec![
Some(1.0),
None,
Some(f64::NAN),
]))],
)?;
let table = MemTable::try_new(schema, vec![vec![data]])?;
let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT c1 IS NULL FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-----------------+",
"| test.c1 IS NULL |",
"+-----------------+",
"| false |",
"| true |",
"| false |",
"+-----------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn query_is_not_null() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Float64, true)]));
let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(Float64Array::from(vec![
Some(1.0),
None,
Some(f64::NAN),
]))],
)?;
let table = MemTable::try_new(schema, vec![vec![data]])?;
let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT c1 IS NOT NULL FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------+",
"| test.c1 IS NOT NULL |",
"+---------------------+",
"| true |",
"| false |",
"| true |",
"+---------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn query_is_true() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Boolean, true)]));
let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(BooleanArray::from(vec![
Some(true),
Some(false),
None,
]))],
)?;
let table = MemTable::try_new(schema, vec![vec![data]])?;
let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT c1 IS TRUE as t FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------+",
"| t |",
"+-------+",
"| true |",
"| false |",
"| false |",
"+-------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn query_is_false() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Boolean, true)]));
let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(BooleanArray::from(vec![
Some(true),
Some(false),
None,
]))],
)?;
let table = MemTable::try_new(schema, vec![vec![data]])?;
let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT c1 IS FALSE as f FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------+",
"| f |",
"+-------+",
"| false |",
"| true |",
"| false |",
"+-------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn query_is_not_true() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Boolean, true)]));
let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(BooleanArray::from(vec![
Some(true),
Some(false),
None,
]))],
)?;
let table = MemTable::try_new(schema, vec![vec![data]])?;
let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT c1 IS NOT TRUE as nt FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------+",
"| nt |",
"+-------+",
"| false |",
"| true |",
"| true |",
"+-------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn query_is_not_false() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Boolean, true)]));
let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(BooleanArray::from(vec![
Some(true),
Some(false),
None,
]))],
)?;
let table = MemTable::try_new(schema, vec![vec![data]])?;
let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT c1 IS NOT FALSE as nf FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------+",
"| nf |",
"+-------+",
"| true |",
"| false |",
"| true |",
"+-------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn query_is_unknown() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Boolean, true)]));
let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(BooleanArray::from(vec![
Some(true),
Some(false),
None,
]))],
)?;
let table = MemTable::try_new(schema, vec![vec![data]])?;
let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT c1 IS UNKNOWN as t FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------+",
"| t |",
"+-------+",
"| false |",
"| false |",
"| true |",
"+-------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn query_is_not_unknown() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Boolean, true)]));
let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(BooleanArray::from(vec![
Some(true),
Some(false),
None,
]))],
)?;
let table = MemTable::try_new(schema, vec![vec![data]])?;
let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT c1 IS NOT UNKNOWN as t FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------+",
"| t |",
"+-------+",
"| true |",
"| true |",
"| false |",
"+-------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn query_without_from() -> Result<()> {
// Test for SELECT <expression> without FROM.
// Should evaluate expressions in project position.
let ctx = SessionContext::new();
let sql = "SELECT 1";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+----------+",
"| Int64(1) |",
"+----------+",
"| 1 |",
"+----------+",
];
assert_batches_eq!(expected, &actual);
let sql = "SELECT 1+2, 3/4, cos(0)";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------+---------------------+---------------+",
"| Int64(1) + Int64(2) | Int64(3) / Int64(4) | cos(Int64(0)) |",
"+---------------------+---------------------+---------------+",
"| 3 | 0 | 1 |",
"+---------------------+---------------------+---------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn query_scalar_minus_array() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Int32, true)]));
let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(Int32Array::from(vec![
Some(0),
Some(1),
None,
Some(3),
]))],
)?;
let table = MemTable::try_new(schema, vec![vec![data]])?;
let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT 4 - c1 FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+--------------------+",
"| Int64(4) - test.c1 |",
"+--------------------+",
"| 4 |",
"| 3 |",
"| |",
"| 1 |",
"+--------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn test_string_concat_operator() -> Result<()> {
let ctx = SessionContext::new();
// concat 2 strings
let sql = "SELECT 'aa' || 'b'";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------------------------+",
"| Utf8(\"aa\") || Utf8(\"b\") |",
"+-------------------------+",
"| aab |",
"+-------------------------+",
];
assert_batches_eq!(expected, &actual);
// concat 4 strings as a string concat pipe.
let sql = "SELECT 'aa' || 'b' || 'cc' || 'd'";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+----------------------------------------------------+",
"| Utf8(\"aa\") || Utf8(\"b\") || Utf8(\"cc\") || Utf8(\"d\") |",
"+----------------------------------------------------+",
"| aabccd |",
"+----------------------------------------------------+",
];
assert_batches_eq!(expected, &actual);
// concat 2 strings and NULL, output should be NULL
let sql = "SELECT 'aa' || NULL || 'd'";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------------------+",
"| Utf8(\"aa\") || NULL || Utf8(\"d\") |",
"+---------------------------------+",
"| |",
"+---------------------------------+",
];
assert_batches_eq!(expected, &actual);
// concat 1 strings and 2 numeric
let sql = "SELECT 'a' || 42 || 23.3";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-----------------------------------------+",
"| Utf8(\"a\") || Int64(42) || Float64(23.3) |",
"+-----------------------------------------+",
"| a4223.3 |",
"+-----------------------------------------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}
#[tokio::test]
async fn test_not_expressions() -> Result<()> {
let ctx = SessionContext::new();
let sql = "SELECT not(true), not(false)";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------------------+--------------------+",
"| NOT Boolean(true) | NOT Boolean(false) |",
"+-------------------+--------------------+",
"| false | true |",
"+-------------------+--------------------+",
];
assert_batches_eq!(expected, &actual);
let sql = "SELECT null, not(null)";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+------+----------+",
"| NULL | NOT NULL |",
"+------+----------+",
"| | |",
"+------+----------+",
];
assert_batches_eq!(expected, &actual);
let sql = "SELECT NOT('hi')";
let result = plan_and_collect(&ctx, sql).await;
match result {
Ok(_) => panic!("expected error"),
Err(e) => {
assert_contains!(e.to_string(),
"NOT 'Literal { value: Utf8(\"hi\") }' can't be evaluated because the expression's type is Utf8, not boolean or NULL"
);
}
}
Ok(())
}
#[tokio::test]
async fn test_boolean_expressions() -> Result<()> {
test_expression!("true", "true");
test_expression!("false", "false");
test_expression!("false = false", "true");
test_expression!("true = false", "false");
Ok(())
}
#[tokio::test]
async fn test_mathematical_expressions_with_null() -> Result<()> {
test_expression!("sqrt(NULL)", "NULL");
test_expression!("sin(NULL)", "NULL");
test_expression!("cos(NULL)", "NULL");
test_expression!("tan(NULL)", "NULL");
test_expression!("asin(NULL)", "NULL");
test_expression!("acos(NULL)", "NULL");
test_expression!("atan(NULL)", "NULL");
test_expression!("floor(NULL)", "NULL");
test_expression!("ceil(NULL)", "NULL");
test_expression!("round(NULL)", "NULL");
test_expression!("trunc(NULL)", "NULL");
test_expression!("abs(NULL)", "NULL");
test_expression!("signum(NULL)", "NULL");
test_expression!("exp(NULL)", "NULL");
test_expression!("ln(NULL)", "NULL");
test_expression!("log2(NULL)", "NULL");
test_expression!("log10(NULL)", "NULL");
test_expression!("power(NULL, 2)", "NULL");
test_expression!("power(NULL, NULL)", "NULL");
test_expression!("power(2, NULL)", "NULL");
test_expression!("atan2(NULL, NULL)", "NULL");
test_expression!("atan2(1, NULL)", "NULL");
test_expression!("atan2(NULL, 1)", "NULL");
Ok(())
}
#[tokio::test]
#[cfg_attr(not(feature = "crypto_expressions"), ignore)]
async fn test_crypto_expressions() -> Result<()> {
test_expression!("md5('tom')", "34b7da764b21d298ef307d04d8152dc5");
test_expression!("digest('tom','md5')", "34b7da764b21d298ef307d04d8152dc5");
test_expression!("md5('')", "d41d8cd98f00b204e9800998ecf8427e");
test_expression!("digest('','md5')", "d41d8cd98f00b204e9800998ecf8427e");
test_expression!("md5(NULL)", "NULL");
test_expression!("digest(NULL,'md5')", "NULL");
test_expression!(
"sha224('tom')",
"0bf6cb62649c42a9ae3876ab6f6d92ad36cb5414e495f8873292be4d"
);
test_expression!(
"digest('tom','sha224')",
"0bf6cb62649c42a9ae3876ab6f6d92ad36cb5414e495f8873292be4d"
);
test_expression!(
"sha224('')",
"d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"
);
test_expression!(
"digest('','sha224')",
"d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"
);
test_expression!("sha224(NULL)", "NULL");
test_expression!("digest(NULL,'sha224')", "NULL");
test_expression!(
"sha256('tom')",
"e1608f75c5d7813f3d4031cb30bfb786507d98137538ff8e128a6ff74e84e643"
);
test_expression!(
"digest('tom','sha256')",
"e1608f75c5d7813f3d4031cb30bfb786507d98137538ff8e128a6ff74e84e643"
);
test_expression!(
"sha256('')",
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
);
test_expression!(
"digest('','sha256')",
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
);
test_expression!("sha256(NULL)", "NULL");
test_expression!("digest(NULL,'sha256')", "NULL");
test_expression!("sha384('tom')", "096f5b68aa77848e4fdf5c1c0b350de2dbfad60ffd7c25d9ea07c6c19b8a4d55a9187eb117c557883f58c16dfac3e343");
test_expression!("digest('tom','sha384')", "096f5b68aa77848e4fdf5c1c0b350de2dbfad60ffd7c25d9ea07c6c19b8a4d55a9187eb117c557883f58c16dfac3e343");
test_expression!("sha384('')", "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b");
test_expression!("digest('','sha384')", "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b");
test_expression!("sha384(NULL)", "NULL");
test_expression!("digest(NULL,'sha384')", "NULL");
test_expression!("sha512('tom')", "6e1b9b3fe840680e37051f7ad5e959d6f39ad0f8885d855166f55c659469d3c8b78118c44a2a49c72ddb481cd6d8731034e11cc030070ba843a90b3495cb8d3e");
test_expression!("digest('tom','sha512')", "6e1b9b3fe840680e37051f7ad5e959d6f39ad0f8885d855166f55c659469d3c8b78118c44a2a49c72ddb481cd6d8731034e11cc030070ba843a90b3495cb8d3e");
test_expression!("sha512('')", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
test_expression!("digest('','sha512')", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
test_expression!("sha512(NULL)", "NULL");
test_expression!("digest(NULL,'sha512')", "NULL");
test_expression!("digest(NULL,'blake2s')", "NULL");
test_expression!("digest(NULL,'blake2b')", "NULL");
test_expression!("digest('','blake2b')", "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce");
test_expression!("digest('tom','blake2b')", "482499a18da10a18d8d35ab5eb4c635551ec5b8d3ff37c3e87a632caf6680fe31566417834b4732e26e0203d1cad4f5366cb7ab57d89694e4c1fda3e26af2c23");
test_expression!(
"digest('','blake2s')",
"69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9"
);
test_expression!(
"digest('tom','blake2s')",
"5fc3f2b3a07cade5023c3df566e4d697d3823ba1b72bfb3e84cf7e768b2e7529"
);
test_expression!(
"digest('','blake3')",
"af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262"
);
Ok(())
}
#[tokio::test]
async fn test_array_index() -> Result<()> {
// By default PostgreSQL uses a one-based numbering convention for arrays, that is, an array of n elements starts with array[1] and ends with array[n]
test_expression!("([5,4,3,2,1])[1]", "5");
test_expression!("([5,4,3,2,1])[2]", "4");
test_expression!("([5,4,3,2,1])[5]", "1");
test_expression!("([[1, 2], [2, 3], [3,4]])[1]", "[1, 2]");
test_expression!("([[1, 2], [2, 3], [3,4]])[3]", "[3, 4]");
test_expression!("([[1, 2], [2, 3], [3,4]])[1][1]", "1");
test_expression!("([[1, 2], [2, 3], [3,4]])[2][2]", "3");
test_expression!("([[1, 2], [2, 3], [3,4]])[3][2]", "4");
// out of bounds
test_expression!("([5,4,3,2,1])[0]", "NULL");
test_expression!("([5,4,3,2,1])[6]", "NULL");
// test_expression!("([5,4,3,2,1])[-1]", "NULL");
test_expression!("([5,4,3,2,1])[100]", "NULL");
Ok(())
}
#[tokio::test]
async fn test_array_literals() -> Result<()> {
// Named, just another syntax
test_expression!("ARRAY[1,2,3,4,5]", "[1, 2, 3, 4, 5]");
// Unnamed variant
test_expression!("[1,2,3,4,5]", "[1, 2, 3, 4, 5]");
test_expression!("[true, false]", "[true, false]");
test_expression!("['str1', 'str2']", "[str1, str2]");
test_expression!("[[1,2], [3,4]]", "[[1, 2], [3, 4]]");
// TODO: Not supported in parser, uncomment when it will be available
// test_expression!(
// "[]",
// "[]"
// );
Ok(())
}
#[tokio::test]
async fn test_struct_literals() -> Result<()> {
test_expression!(
"STRUCT(1,2,3,4,5)",
"{\"c0\": 1, \"c1\": 2, \"c2\": 3, \"c3\": 4, \"c4\": 5}"
);
test_expression!("STRUCT(Null)", "{\"c0\": null}");
test_expression!("STRUCT(2)", "{\"c0\": 2}");
test_expression!("STRUCT('1',Null)", "{\"c0\": \"1\", \"c1\": null}");
test_expression!("STRUCT(true, false)", "{\"c0\": true, \"c1\": false}");
test_expression!(
"STRUCT('str1', 'str2')",
"{\"c0\": \"str1\", \"c1\": \"str2\"}"
);
Ok(())
}
#[tokio::test]
async fn binary_bitwise_shift() -> Result<()> {
test_expression!("2 << 10", "2048");
test_expression!("2048 >> 10", "2");
test_expression!("2048 << NULL", "NULL");
test_expression!("2048 >> NULL", "NULL");
Ok(())
}
#[tokio::test]
async fn test_interval_expressions() -> Result<()> {
// day nano intervals
test_expression!(
"interval '1'",
"0 years 0 mons 0 days 0 hours 0 mins 1.00 secs"
);
test_expression!(
"interval '1 second'",
"0 years 0 mons 0 days 0 hours 0 mins 1.00 secs"
);
test_expression!(
"interval '500 milliseconds'",
"0 years 0 mons 0 days 0 hours 0 mins 0.500 secs"
);
test_expression!(
"interval '5 second'",
"0 years 0 mons 0 days 0 hours 0 mins 5.00 secs"
);
test_expression!(
"interval '0.5 minute'",
"0 years 0 mons 0 days 0 hours 0 mins 30.00 secs"
);
test_expression!(
"interval '.5 minute'",
"0 years 0 mons 0 days 0 hours 0 mins 30.00 secs"
);
test_expression!(
"interval '5 minute'",
"0 years 0 mons 0 days 0 hours 5 mins 0.00 secs"
);
test_expression!(
"interval '5 minute 1 second'",
"0 years 0 mons 0 days 0 hours 5 mins 1.00 secs"
);
test_expression!(
"interval '1 hour'",
"0 years 0 mons 0 days 1 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '5 hour'",
"0 years 0 mons 0 days 5 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '1 day'",
"0 years 0 mons 1 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '1 week'",
"0 years 0 mons 7 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '2 weeks'",
"0 years 0 mons 14 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '1 day 1'",
"0 years 0 mons 1 days 0 hours 0 mins 1.00 secs"
);
test_expression!(
"interval '0.5'",
"0 years 0 mons 0 days 0 hours 0 mins 0.500 secs"
);
test_expression!(
"interval '0.5 day 1'",
"0 years 0 mons 0 days 12 hours 0 mins 1.00 secs"
);
test_expression!(
"interval '0.49 day'",
"0 years 0 mons 0 days 11 hours 45 mins 36.00 secs"
);
test_expression!(
"interval '0.499 day'",
"0 years 0 mons 0 days 11 hours 58 mins 33.596 secs"
);
test_expression!(
"interval '0.4999 day'",
"0 years 0 mons 0 days 11 hours 59 mins 51.364 secs"
);
test_expression!(
"interval '0.49999 day'",
"0 years 0 mons 0 days 11 hours 59 mins 59.136 secs"
);
test_expression!(
"interval '0.49999999999 day'",
"0 years 0 mons 0 days 12 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '5 day'",
"0 years 0 mons 5 days 0 hours 0 mins 0.00 secs"
);
// Hour is ignored, this matches PostgreSQL
test_expression!(
"interval '5 day' hour",
"0 years 0 mons 5 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '5 day 4 hours 3 minutes 2 seconds 100 milliseconds'",
"0 years 0 mons 5 days 4 hours 3 mins 2.100 secs"
);
// month intervals
test_expression!(
"interval '0.5 month'",
"0 years 0 mons 15 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '0.5' month",
"0 years 0 mons 15 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '1 month'",
"0 years 1 mons 0 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '1' MONTH",
"0 years 1 mons 0 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '5 month'",
"0 years 5 mons 0 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '13 month'",
"1 years 1 mons 0 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '0.5 year'",
"0 years 6 mons 0 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '1 year'",
"1 years 0 mons 0 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '1 decade'",
"10 years 0 mons 0 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '2 decades'",
"20 years 0 mons 0 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '1 century'",
"100 years 0 mons 0 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '2 year'",
"2 years 0 mons 0 days 0 hours 0 mins 0.00 secs"
);
test_expression!(
"interval '2' year",
"2 years 0 mons 0 days 0 hours 0 mins 0.00 secs"