-
Notifications
You must be signed in to change notification settings - Fork 525
/
agg.yaml
1544 lines (1523 loc) · 83.3 KB
/
agg.yaml
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
# This file is automatically generated. See `src/frontend/planner_test/README.md` for more information.
- sql: |
values(sum(1));
binder_error: |-
Bind error: failed to bind expression: sum(1)
Caused by:
Invalid input syntax: aggregate functions are not allowed in VALUES
- sql: |
values(count(1));
binder_error: |-
Bind error: failed to bind expression: count(1)
Caused by:
Invalid input syntax: aggregate functions are not allowed in VALUES
- sql: |
values(min(1));
binder_error: |-
Bind error: failed to bind expression: min(1)
Caused by:
Invalid input syntax: aggregate functions are not allowed in VALUES
- sql: |
values(1 + max(1));
binder_error: |-
Bind error: failed to bind expression: 1 + max(1)
Caused by:
Invalid input syntax: aggregate functions are not allowed in VALUES
- sql: |
create table t (v1 int);
select v1 from t where min(v1);
binder_error: |-
Bind error: failed to bind expression: min(v1)
Caused by:
Invalid input syntax: aggregate functions are not allowed in WHERE
- sql: |
create table t(v1 int, v2 int, v3 int);
select v1, min(v2) + max(v3) * count(v1) as agg from t group by v1;
batch_plan: |-
BatchExchange { order: [], dist: Single }
└─BatchProject { exprs: [t.v1, (min(t.v2) + (max(t.v3) * count(t.v1))) as $expr1] }
└─BatchHashAgg { group_key: [t.v1], aggs: [min(t.v2), max(t.v3), count(t.v1)] }
└─BatchExchange { order: [], dist: HashShard(t.v1) }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
batch_local_plan: |-
BatchProject { exprs: [t.v1, (min(t.v2) + (max(t.v3) * count(t.v1))) as $expr1] }
└─BatchHashAgg { group_key: [t.v1], aggs: [min(t.v2), max(t.v3), count(t.v1)] }
└─BatchExchange { order: [], dist: Single }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
stream_plan: |-
StreamMaterialize { columns: [v1, agg], stream_key: [v1], pk_columns: [v1], pk_conflict: NoCheck }
└─StreamProject { exprs: [t.v1, (min(t.v2) + (max(t.v3) * count(t.v1))) as $expr1] }
└─StreamHashAgg { group_key: [t.v1], aggs: [min(t.v2), max(t.v3), count(t.v1), count] }
└─StreamExchange { dist: HashShard(t.v1) }
└─StreamTableScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- sql: |
create table t(v1 int, v2 int, v3 int);
select min(v1) + max(v2) * count(v3) as agg from t;
batch_plan: |-
BatchProject { exprs: [(min(min(t.v1)) + (max(max(t.v2)) * sum0(count(t.v3)))) as $expr1] }
└─BatchSimpleAgg { aggs: [min(min(t.v1)), max(max(t.v2)), sum0(count(t.v3))] }
└─BatchExchange { order: [], dist: Single }
└─BatchSimpleAgg { aggs: [min(t.v1), max(t.v2), count(t.v3)] }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
batch_local_plan: |-
BatchProject { exprs: [(min(t.v1) + (max(t.v2) * count(t.v3))) as $expr1] }
└─BatchSimpleAgg { aggs: [min(t.v1), max(t.v2), count(t.v3)] }
└─BatchExchange { order: [], dist: Single }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
stream_plan: |-
StreamMaterialize { columns: [agg], stream_key: [], pk_columns: [], pk_conflict: NoCheck }
└─StreamProject { exprs: [(min(min(t.v1)) + (max(max(t.v2)) * sum0(count(t.v3)))) as $expr2] }
└─StreamSimpleAgg { aggs: [min(min(t.v1)), max(max(t.v2)), sum0(count(t.v3)), count] }
└─StreamExchange { dist: Single }
└─StreamHashAgg { group_key: [$expr1], aggs: [min(t.v1), max(t.v2), count(t.v3), count] }
└─StreamProject { exprs: [t.v1, t.v2, t.v3, t._row_id, Vnode(t._row_id) as $expr1] }
└─StreamTableScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- sql: |
create table t(v1 int, v2 int);
select v1 from t group by v2;
planner_error: 'Invalid input syntax: column must appear in the GROUP BY clause or be used in an aggregate function'
- sql: |
create table t(v1 int, v2 int);
select sum(v1), v1 from t group by v2, v2;
planner_error: 'Invalid input syntax: column must appear in the GROUP BY clause or be used in an aggregate function'
- sql: |
create table t(v1 int, v2 int, v3 int);
select v3, min(v1) * avg(v1+v2) as agg from t group by v3;
batch_plan: |-
BatchExchange { order: [], dist: Single }
└─BatchProject { exprs: [t.v3, (min(t.v1)::Decimal * (sum($expr1)::Decimal / count($expr1)::Decimal)) as $expr2] }
└─BatchHashAgg { group_key: [t.v3], aggs: [min(t.v1), sum($expr1), count($expr1)] }
└─BatchExchange { order: [], dist: HashShard(t.v3) }
└─BatchProject { exprs: [t.v3, t.v1, (t.v1 + t.v2) as $expr1] }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
batch_local_plan: |-
BatchProject { exprs: [t.v3, (min(t.v1)::Decimal * (sum($expr1)::Decimal / count($expr1)::Decimal)) as $expr2] }
└─BatchHashAgg { group_key: [t.v3], aggs: [min(t.v1), sum($expr1), count($expr1)] }
└─BatchExchange { order: [], dist: Single }
└─BatchProject { exprs: [t.v3, t.v1, (t.v1 + t.v2) as $expr1] }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
stream_plan: |-
StreamMaterialize { columns: [v3, agg], stream_key: [v3], pk_columns: [v3], pk_conflict: NoCheck }
└─StreamProject { exprs: [t.v3, (min(t.v1)::Decimal * (sum($expr1)::Decimal / count($expr1)::Decimal)) as $expr2] }
└─StreamHashAgg { group_key: [t.v3], aggs: [min(t.v1), sum($expr1), count($expr1), count] }
└─StreamExchange { dist: HashShard(t.v3) }
└─StreamProject { exprs: [t.v3, t.v1, (t.v1 + t.v2) as $expr1, t._row_id] }
└─StreamTableScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: test logical_agg with complex group expression
sql: |
create table t(v1 int, v2 int);
select min(v1), sum(v1 + v2) from t group by v1 + v2;
logical_plan: |-
LogicalProject { exprs: [min(t.v1), sum($expr1)] }
└─LogicalAgg { group_key: [$expr1], aggs: [min(t.v1), sum($expr1)] }
└─LogicalProject { exprs: [(t.v1 + t.v2) as $expr1, t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t._row_id] }
- name: test logical_agg with complex group expression
sql: |
create table t(v1 int, v2 int, v3 int);
select v1, sum(v1 * v2) as sum from t group by (v1 + v2) / v3, v1;
logical_plan: |-
LogicalProject { exprs: [t.v1, sum($expr2)] }
└─LogicalAgg { group_key: [$expr1, t.v1], aggs: [sum($expr2)] }
└─LogicalProject { exprs: [((t.v1 + t.v2) / t.v3) as $expr1, t.v1, (t.v1 * t.v2) as $expr2] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id] }
- name: test logical_agg with complex group expression
sql: |
create table t(v1 int, v2 int);
select v1 + v2 from t group by v1 + v2;
logical_plan: |-
LogicalProject { exprs: [$expr1] }
└─LogicalAgg { group_key: [$expr1], aggs: [] }
└─LogicalProject { exprs: [(t.v1 + t.v2) as $expr1] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t._row_id] }
- name: "test logical_agg with complex group expression \nshould complain about nested agg call \n"
sql: |
create table t(v1 int, v2 int);
select avg(sum(v1 + v2)) from t group by v1 + v2;
planner_error: |-
Feature is not yet implemented: aggregate function inside aggregation calls
No tracking issue yet. Feel free to submit a feature request at https://github.com/risingwavelabs/risingwave/issues/new?labels=type%2Ffeature&template=feature_request.yml
- name: test logical_agg with complex select expression
sql: |
create table t(v1 int, v2 int);
select v1 + v2 from t group by v1, v2;
logical_plan: |-
LogicalProject { exprs: [(t.v1 + t.v2) as $expr1] }
└─LogicalAgg { group_key: [t.v1, t.v2], aggs: [] }
└─LogicalProject { exprs: [t.v1, t.v2] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t._row_id] }
- sql: |
create table t(v1 int, v2 int);
select v1 from t group by v1 + v2;
planner_error: 'Invalid input syntax: column must appear in the GROUP BY clause or be used in an aggregate function'
- name: group by output column ordinal ok
sql: |
select 4 + 5 group by 1;
logical_plan: |-
LogicalProject { exprs: [$expr1] }
└─LogicalAgg { group_key: [$expr1], aggs: [] }
└─LogicalProject { exprs: [(4:Int32 + 5:Int32) as $expr1] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
- name: group by const int expr
sql: |
select 4 + 5 group by 3 - 2; -- not folded
logical_plan: |-
LogicalProject { exprs: [(4:Int32 + 5:Int32) as $expr2] }
└─LogicalAgg { group_key: [$expr1], aggs: [] }
└─LogicalProject { exprs: [(3:Int32 - 2:Int32) as $expr1] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
- name: group by output column ordinal of agg
sql: |
select sum(2) group by 1;
planner_error: |-
Feature is not yet implemented: aggregate function inside GROUP BY
No tracking issue yet. Feel free to submit a feature request at https://github.com/risingwavelabs/risingwave/issues/new?labels=type%2Ffeature&template=feature_request.yml
- name: group by output column ordinal non-integer const
sql: |
select 4 + 5 group by null; -- no implicit cast
binder_error: 'Bind error: non-integer constant in GROUP BY'
- name: group by output column ordinal bigint
sql: |
select 4 + 5 group by 2147483648;
binder_error: 'Bind error: non-integer constant in GROUP BY'
- name: group by output column ordinal negative
sql: |
select 4 + 5 group by -2147483648;
binder_error: 'Bind error: GROUP BY position -2147483648 is not in select list'
- name: group by output column ordinal zero
sql: |
select 4 + 5 group by 0;
binder_error: 'Bind error: GROUP BY position 0 is not in select list'
- name: group by output column ordinal out of bound (excl extra order by exprs)
sql: |
select 4 + 5 group by 2 order by 7 + 8, 3 + 2;
binder_error: 'Bind error: GROUP BY position 2 is not in select list'
- name: group by output column name ok
sql: |
select 4 + 5 as a group by a;
logical_plan: |-
LogicalProject { exprs: [$expr1] }
└─LogicalAgg { group_key: [$expr1], aggs: [] }
└─LogicalProject { exprs: [(4:Int32 + 5:Int32) as $expr1] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
- name: group by output column name ambiguous 2
sql: |
select 4 + 5 as a, 2 + 3 as a group by a;
binder_error: 'Bind error: GROUP BY "a" is ambiguous'
- name: group by output column name ambiguous 3
sql: |
select 4 + 5 as a, 2 + 3 as a, 3 + 4 as a group by a;
binder_error: 'Bind error: GROUP BY "a" is ambiguous'
- name: group by output column name not ambiguous when unused
sql: |
select 4 + 5 as a, 2 + 3 as a, 3 + 4 as b group by b;
logical_plan: |-
LogicalProject { exprs: [(4:Int32 + 5:Int32) as $expr2, (2:Int32 + 3:Int32) as $expr3, $expr1] }
└─LogicalAgg { group_key: [$expr1], aggs: [] }
└─LogicalProject { exprs: [(3:Int32 + 4:Int32) as $expr1] }
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
- name: group by output column name not ambiguous when input preferred
sql: |
create table t (a int);
select 4 + 5 as a, 2 + 3 as a from t group by a;
logical_plan: |-
LogicalProject { exprs: [(4:Int32 + 5:Int32) as $expr1, (2:Int32 + 3:Int32) as $expr2] }
└─LogicalAgg { group_key: [t.a], aggs: [] }
└─LogicalProject { exprs: [t.a] }
└─LogicalScan { table: t, columns: [t.a, t._row_id] }
- name: group by output column name expr disallowed
sql: |
select 4 + 5 as a group by a + 1;
binder_error: |-
Bind error: failed to bind expression: a + 1
Caused by:
Item not found: Invalid column: a
- name: group by prefers input while order by prefers output
sql: |
create table t (a int);
select 4 + 5 as a from t group by a order by a;
batch_plan: |-
BatchExchange { order: [9:Int32 ASC], dist: Single }
└─BatchSort { order: [9:Int32 ASC] }
└─BatchProject { exprs: [9:Int32] }
└─BatchHashAgg { group_key: [t.a], aggs: [] }
└─BatchExchange { order: [], dist: HashShard(t.a) }
└─BatchScan { table: t, columns: [t.a], distribution: SomeShard }
- name: group by column not found
sql: |
select 4 + 5 as a group by b;
binder_error: |-
Bind error: failed to bind expression: b
Caused by:
Item not found: Invalid column: b
- sql: |
create table t(v1 int, v2 int);
select count(v1 + v2) as cnt, sum(v1 + v2) as sum from t;
batch_plan: |-
BatchSimpleAgg { aggs: [sum0(count($expr1)), sum(sum($expr1))] }
└─BatchExchange { order: [], dist: Single }
└─BatchSimpleAgg { aggs: [count($expr1), sum($expr1)] }
└─BatchProject { exprs: [(t.v1 + t.v2) as $expr1] }
└─BatchScan { table: t, columns: [t.v1, t.v2], distribution: SomeShard }
batch_local_plan: |-
BatchSimpleAgg { aggs: [count($expr1), sum($expr1)] }
└─BatchExchange { order: [], dist: Single }
└─BatchProject { exprs: [(t.v1 + t.v2) as $expr1] }
└─BatchScan { table: t, columns: [t.v1, t.v2], distribution: SomeShard }
stream_plan: |-
StreamMaterialize { columns: [cnt, sum], stream_key: [], pk_columns: [], pk_conflict: NoCheck }
└─StreamProject { exprs: [sum0(count($expr1)), sum(sum($expr1))] }
└─StreamSimpleAgg { aggs: [sum0(count($expr1)), sum(sum($expr1)), count] }
└─StreamExchange { dist: Single }
└─StreamStatelessSimpleAgg { aggs: [count($expr1), sum($expr1)] }
└─StreamProject { exprs: [(t.v1 + t.v2) as $expr1, t._row_id] }
└─StreamTableScan { table: t, columns: [t.v1, t.v2, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- sql: |
create table t(v1 int, v2 int, v3 int);
select v1, sum(v2 + v3) / count(v2 + v3) + max(v1) as agg from t group by v1;
batch_plan: |-
BatchExchange { order: [], dist: Single }
└─BatchProject { exprs: [t.v1, ((sum($expr1) / count($expr1)) + max(t.v1)) as $expr2] }
└─BatchHashAgg { group_key: [t.v1], aggs: [sum($expr1), count($expr1), max(t.v1)] }
└─BatchExchange { order: [], dist: HashShard(t.v1) }
└─BatchProject { exprs: [t.v1, (t.v2 + t.v3) as $expr1] }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
stream_plan: |-
StreamMaterialize { columns: [v1, agg], stream_key: [v1], pk_columns: [v1], pk_conflict: NoCheck }
└─StreamProject { exprs: [t.v1, ((sum($expr1) / count($expr1)) + max(t.v1)) as $expr2] }
└─StreamHashAgg { group_key: [t.v1], aggs: [sum($expr1), count($expr1), max(t.v1), count] }
└─StreamExchange { dist: HashShard(t.v1) }
└─StreamProject { exprs: [t.v1, (t.v2 + t.v3) as $expr1, t._row_id] }
└─StreamTableScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- sql: |
create table t (v1 real);
select v1, count(*) from t group by v1;
batch_plan: |-
BatchExchange { order: [], dist: Single }
└─BatchHashAgg { group_key: [t.v1], aggs: [count] }
└─BatchExchange { order: [], dist: HashShard(t.v1) }
└─BatchScan { table: t, columns: [t.v1], distribution: SomeShard }
- name: Use BatchSortAgg, when input provides order
sql: |
create table t(v1 int, v2 int);
create materialized view mv as select * from t order by v1 desc;
select v1, max(v2) from mv group by v1;
batch_plan: |-
BatchExchange { order: [], dist: Single }
└─BatchSortAgg { group_key: [mv.v1], aggs: [max(mv.v2)] }
└─BatchExchange { order: [mv.v1 DESC], dist: HashShard(mv.v1) }
└─BatchScan { table: mv, columns: [mv.v1, mv.v2], distribution: SomeShard }
- name: Use BatchSortAgg, when output requires order
sql: |
create table t(v1 int, v2 int);
select v1, max(v2) from t group by v1 order by v1 desc;
batch_plan: |-
BatchExchange { order: [t.v1 DESC], dist: Single }
└─BatchSort { order: [t.v1 DESC] }
└─BatchHashAgg { group_key: [t.v1], aggs: [max(t.v2)] }
└─BatchExchange { order: [], dist: HashShard(t.v1) }
└─BatchScan { table: t, columns: [t.v1, t.v2], distribution: SomeShard }
- name: Use BatchSortAgg, when required order satisfies input order
sql: |
create table t(k1 int, k2 int, v1 int);
SELECT max(v1), k1, k2 from t group by k1, k2 order by k1;
batch_plan: |-
BatchExchange { order: [t.k1 ASC], dist: Single }
└─BatchProject { exprs: [max(t.v1), t.k1, t.k2] }
└─BatchSort { order: [t.k1 ASC] }
└─BatchHashAgg { group_key: [t.k1, t.k2], aggs: [max(t.v1)] }
└─BatchExchange { order: [], dist: HashShard(t.k1, t.k2) }
└─BatchScan { table: t, columns: [t.k1, t.k2, t.v1], distribution: SomeShard }
- name: Use BatchSortAgg, when output requires order with swapped output
sql: |
create table t(v1 int, v2 int);
select max(v2), v1 from t group by v1 order by v1 desc;
batch_plan: |-
BatchExchange { order: [t.v1 DESC], dist: Single }
└─BatchProject { exprs: [max(t.v2), t.v1] }
└─BatchSort { order: [t.v1 DESC] }
└─BatchHashAgg { group_key: [t.v1], aggs: [max(t.v2)] }
└─BatchExchange { order: [], dist: HashShard(t.v1) }
└─BatchScan { table: t, columns: [t.v1, t.v2], distribution: SomeShard }
- name: Not use BatchSortAgg, when input provides order
sql: |
create table t(v1 int, v2 int);
create materialized view mv as select * from t order by v1 desc;
select v1, max(v2) from mv group by v1;
batch_plan: |-
BatchExchange { order: [], dist: Single }
└─BatchHashAgg { group_key: [mv.v1], aggs: [max(mv.v2)] }
└─BatchExchange { order: [], dist: HashShard(mv.v1) }
└─BatchScan { table: mv, columns: [mv.v1, mv.v2], distribution: SomeShard }
with_config_map:
RW_BATCH_ENABLE_SORT_AGG: 'false'
- name: Not use BatchSortAgg, when output requires order
sql: |
create table t(v1 int, v2 int);
select v1, max(v2) from t group by v1 order by v1 desc;
batch_plan: |-
BatchExchange { order: [t.v1 DESC], dist: Single }
└─BatchSort { order: [t.v1 DESC] }
└─BatchHashAgg { group_key: [t.v1], aggs: [max(t.v2)] }
└─BatchExchange { order: [], dist: HashShard(t.v1) }
└─BatchScan { table: t, columns: [t.v1, t.v2], distribution: SomeShard }
with_config_map:
RW_BATCH_ENABLE_SORT_AGG: 'false'
- name: Not use BatchSortAgg, when required order satisfies input order
sql: |
create table t(k1 int, k2 int, v1 int);
SELECT max(v1), k1, k2 from t group by k1, k2 order by k1;
batch_plan: |-
BatchExchange { order: [t.k1 ASC], dist: Single }
└─BatchProject { exprs: [max(t.v1), t.k1, t.k2] }
└─BatchSort { order: [t.k1 ASC] }
└─BatchHashAgg { group_key: [t.k1, t.k2], aggs: [max(t.v1)] }
└─BatchExchange { order: [], dist: HashShard(t.k1, t.k2) }
└─BatchScan { table: t, columns: [t.k1, t.k2, t.v1], distribution: SomeShard }
with_config_map:
RW_BATCH_ENABLE_SORT_AGG: 'false'
- name: Not use BatchSortAgg, when output requires order with swapped output
sql: |
create table t(v1 int, v2 int);
select max(v2), v1 from t group by v1 order by v1 desc;
batch_plan: |-
BatchExchange { order: [t.v1 DESC], dist: Single }
└─BatchProject { exprs: [max(t.v2), t.v1] }
└─BatchSort { order: [t.v1 DESC] }
└─BatchHashAgg { group_key: [t.v1], aggs: [max(t.v2)] }
└─BatchExchange { order: [], dist: HashShard(t.v1) }
└─BatchScan { table: t, columns: [t.v1, t.v2], distribution: SomeShard }
with_config_map:
RW_BATCH_ENABLE_SORT_AGG: 'false'
- sql: |
create table t (v1 real);
select count(*) from t;
batch_plan: |-
BatchSimpleAgg { aggs: [sum0(count)] }
└─BatchExchange { order: [], dist: Single }
└─BatchSimpleAgg { aggs: [count] }
└─BatchScan { table: t, columns: [], distribution: SomeShard }
- name: having with agg call
sql: |
create table t (v1 real);
select 1 from t having sum(v1) > 5;
batch_plan: |-
BatchProject { exprs: [1:Int32] }
└─BatchFilter { predicate: (sum(sum(t.v1)) > 5:Float64) }
└─BatchSimpleAgg { aggs: [sum(sum(t.v1))] }
└─BatchExchange { order: [], dist: Single }
└─BatchSimpleAgg { aggs: [sum(t.v1)] }
└─BatchScan { table: t, columns: [t.v1], distribution: SomeShard }
- name: having with group column
sql: |
create table t (v1 real);
select 1 from t group by v1 having v1 > 5;
logical_plan: |-
LogicalProject { exprs: [1:Int32] }
└─LogicalFilter { predicate: (t.v1 > 5:Int32::Float64) }
└─LogicalAgg { group_key: [t.v1], aggs: [] }
└─LogicalProject { exprs: [t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t._row_id] }
- name: having with non-group column
sql: |
create table t (v1 real, v2 int);
select 1 from t group by v1 having v2 > 5;
planner_error: 'Invalid input syntax: column must appear in the GROUP BY clause or be used in an aggregate function'
- name: distinct without agg
sql: |
create table t (v1 int, v2 int);
select distinct v1 from t;
logical_plan: |-
LogicalAgg { group_key: [t.v1], aggs: [] }
└─LogicalProject { exprs: [t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t._row_id] }
- name: distinct with agg
sql: |
create table t (v1 int, v2 int);
select distinct sum(v1) from t group by v2;
logical_plan: |-
LogicalAgg { group_key: [sum(t.v1)], aggs: [] }
└─LogicalProject { exprs: [sum(t.v1)] }
└─LogicalAgg { group_key: [t.v2], aggs: [sum(t.v1)] }
└─LogicalProject { exprs: [t.v2, t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t._row_id] }
- name: distinct on
sql: |
create table t (v1 int, v2 int, v3 int);
select distinct on (v1, v3) v1, v2 from t order by v3, v1;
logical_plan: |-
LogicalProject { exprs: [t.v1, t.v2] }
└─LogicalTopN { order: [t.v3 ASC, t.v1 ASC], limit: 1, offset: 0, group_key: [0, 2] }
└─LogicalProject { exprs: [t.v1, t.v2, t.v3] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id] }
batch_plan: |-
BatchProject { exprs: [t.v1, t.v2] }
└─BatchExchange { order: [t.v3 ASC, t.v1 ASC], dist: Single }
└─BatchSort { order: [t.v3 ASC, t.v1 ASC] }
└─BatchGroupTopN { order: [t.v3 ASC, t.v1 ASC], limit: 1, offset: 0, group_key: [0, 2] }
└─BatchExchange { order: [], dist: HashShard(t.v1, t.v3) }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
- name: distinct on
sql: |
create table t (v1 int, v2 int, v3 int);
select distinct on (v1, v3) v1, v2 from t order by v1, v3;
logical_plan: |-
LogicalProject { exprs: [t.v1, t.v2] }
└─LogicalTopN { order: [t.v1 ASC, t.v3 ASC], limit: 1, offset: 0, group_key: [0, 2] }
└─LogicalProject { exprs: [t.v1, t.v2, t.v3] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id] }
batch_plan: |-
BatchProject { exprs: [t.v1, t.v2] }
└─BatchExchange { order: [t.v1 ASC, t.v3 ASC], dist: Single }
└─BatchSort { order: [t.v1 ASC, t.v3 ASC] }
└─BatchGroupTopN { order: [t.v1 ASC, t.v3 ASC], limit: 1, offset: 0, group_key: [0, 2] }
└─BatchExchange { order: [], dist: HashShard(t.v1, t.v3) }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
- name: distinct on
sql: |
create table t (v1 int, v2 int);
select distinct on (v1) v1, v2 from t order by v1;
logical_plan: |-
LogicalTopN { order: [t.v1 ASC], limit: 1, offset: 0, group_key: [0] }
└─LogicalProject { exprs: [t.v1, t.v2] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t._row_id] }
- name: distinct on
sql: |
create table t (v1 int, v2 int, v3 int);
select distinct on(v1) v2 + v3 from t order by v1;
logical_plan: |-
LogicalProject { exprs: [$expr1] }
└─LogicalTopN { order: [t.v1 ASC], limit: 1, offset: 0, group_key: [1] }
└─LogicalProject { exprs: [(t.v2 + t.v3) as $expr1, t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id] }
batch_plan: |-
BatchProject { exprs: [$expr1] }
└─BatchExchange { order: [t.v1 ASC], dist: Single }
└─BatchSort { order: [t.v1 ASC] }
└─BatchGroupTopN { order: [t.v1 ASC], limit: 1, offset: 0, group_key: [1] }
└─BatchExchange { order: [], dist: HashShard(t.v1) }
└─BatchProject { exprs: [(t.v2 + t.v3) as $expr1, t.v1] }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
- name: arguments out-of-order
sql: |
create table t(v1 int, v2 int, v3 int);
select count(v3), min(v2), max(v1) from t;
logical_plan: |-
LogicalProject { exprs: [count(t.v3), min(t.v2), max(t.v1)] }
└─LogicalAgg { aggs: [count(t.v3), min(t.v2), max(t.v1)] }
└─LogicalProject { exprs: [t.v3, t.v2, t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [count(t.v3), min(t.v2), max(t.v1)] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t.v3] }
batch_plan: |-
BatchSimpleAgg { aggs: [sum0(count(t.v3)), min(min(t.v2)), max(max(t.v1))] }
└─BatchExchange { order: [], dist: Single }
└─BatchSimpleAgg { aggs: [count(t.v3), min(t.v2), max(t.v1)] }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
- name: simple-agg arguments out-of-order
sql: |
create table t(v1 int, v2 int, v3 int);
select min(v1) + max(v3) * count(v2) as agg from t;
logical_plan: |-
LogicalProject { exprs: [(min(t.v1) + (max(t.v3) * count(t.v2))) as $expr1] }
└─LogicalAgg { aggs: [min(t.v1), max(t.v3), count(t.v2)] }
└─LogicalProject { exprs: [t.v1, t.v3, t.v2] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalProject { exprs: [(min(t.v1) + (max(t.v3) * count(t.v2))) as $expr1] }
└─LogicalAgg { aggs: [min(t.v1), max(t.v3), count(t.v2)] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t.v3] }
batch_plan: |-
BatchProject { exprs: [(min(min(t.v1)) + (max(max(t.v3)) * sum0(count(t.v2)))) as $expr1] }
└─BatchSimpleAgg { aggs: [min(min(t.v1)), max(max(t.v3)), sum0(count(t.v2))] }
└─BatchExchange { order: [], dist: Single }
└─BatchSimpleAgg { aggs: [min(t.v1), max(t.v3), count(t.v2)] }
└─BatchScan { table: t, columns: [t.v1, t.v2, t.v3], distribution: SomeShard }
stream_plan: |-
StreamMaterialize { columns: [agg], stream_key: [], pk_columns: [], pk_conflict: NoCheck }
└─StreamProject { exprs: [(min(min(t.v1)) + (max(max(t.v3)) * sum0(count(t.v2)))) as $expr2] }
└─StreamSimpleAgg { aggs: [min(min(t.v1)), max(max(t.v3)), sum0(count(t.v2)), count] }
└─StreamExchange { dist: Single }
└─StreamHashAgg { group_key: [$expr1], aggs: [min(t.v1), max(t.v3), count(t.v2), count] }
└─StreamProject { exprs: [t.v1, t.v2, t.v3, t._row_id, Vnode(t._row_id) as $expr1] }
└─StreamTableScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: dup group key
sql: |
create table t(v1 int) with (appendonly = false);
select v1 from t group by v1, v1;
logical_plan: |-
LogicalProject { exprs: [t.v1] }
└─LogicalAgg { group_key: [t.v1], aggs: [] }
└─LogicalProject { exprs: [t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalAgg { group_key: [t.v1], aggs: [] }
└─LogicalScan { table: t, columns: [t.v1] }
stream_plan: |-
StreamMaterialize { columns: [v1], stream_key: [v1], pk_columns: [v1], pk_conflict: NoCheck }
└─StreamProject { exprs: [t.v1] }
└─StreamHashAgg { group_key: [t.v1], aggs: [count] }
└─StreamExchange { dist: HashShard(t.v1) }
└─StreamTableScan { table: t, columns: [t.v1, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: dup group key
sql: |
create table t(v1 int, v2 int, v3 int) with (appendonly = false);
select v2, min(v1) as min_v1, v3, max(v1) as max_v1 from t group by v3, v2, v2;
logical_plan: |-
LogicalProject { exprs: [t.v2, min(t.v1), t.v3, max(t.v1)] }
└─LogicalAgg { group_key: [t.v3, t.v2], aggs: [min(t.v1), max(t.v1)] }
└─LogicalProject { exprs: [t.v3, t.v2, t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalProject { exprs: [t.v2, min(t.v1), t.v3, max(t.v1)] }
└─LogicalAgg { group_key: [t.v2, t.v3], aggs: [min(t.v1), max(t.v1)] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t.v3] }
stream_plan: |-
StreamMaterialize { columns: [v2, min_v1, v3, max_v1], stream_key: [v2, v3], pk_columns: [v2, v3], pk_conflict: NoCheck }
└─StreamProject { exprs: [t.v2, min(t.v1), t.v3, max(t.v1)] }
└─StreamHashAgg { group_key: [t.v2, t.v3], aggs: [min(t.v1), max(t.v1), count] }
└─StreamExchange { dist: HashShard(t.v2, t.v3) }
└─StreamTableScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: order by agg input
sql: |
create table t(v1 int);
select sum(v1 order by v1) as s1 from t;
logical_plan: |-
LogicalProject { exprs: [sum(t.v1)] }
└─LogicalAgg { aggs: [sum(t.v1)] }
└─LogicalProject { exprs: [t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [sum(t.v1)] }
└─LogicalScan { table: t, columns: [t.v1] }
stream_plan: |-
StreamMaterialize { columns: [s1], stream_key: [], pk_columns: [], pk_conflict: NoCheck }
└─StreamProject { exprs: [sum(sum(t.v1))] }
└─StreamSimpleAgg { aggs: [sum(sum(t.v1)), count] }
└─StreamExchange { dist: Single }
└─StreamStatelessSimpleAgg { aggs: [sum(t.v1)] }
└─StreamTableScan { table: t, columns: [t.v1, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: order by other columns
sql: |
create table t(v1 int, v2 varchar);
select sum(v1 order by v2) as s1 from t;
logical_plan: |-
LogicalProject { exprs: [sum(t.v1)] }
└─LogicalAgg { aggs: [sum(t.v1)] }
└─LogicalProject { exprs: [t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [sum(t.v1)] }
└─LogicalScan { table: t, columns: [t.v1] }
stream_plan: |-
StreamMaterialize { columns: [s1], stream_key: [], pk_columns: [], pk_conflict: NoCheck }
└─StreamProject { exprs: [sum(sum(t.v1))] }
└─StreamSimpleAgg { aggs: [sum(sum(t.v1)), count] }
└─StreamExchange { dist: Single }
└─StreamStatelessSimpleAgg { aggs: [sum(t.v1)] }
└─StreamTableScan { table: t, columns: [t.v1, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: order by ASC/DESC and default
sql: |
create table t(v1 int, v2 varchar, v3 int);
select sum(v1 order by v1, v2 ASC, v3 DESC) as s1 from t;
logical_plan: |-
LogicalProject { exprs: [sum(t.v1)] }
└─LogicalAgg { aggs: [sum(t.v1)] }
└─LogicalProject { exprs: [t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t.v2, t.v3, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [sum(t.v1)] }
└─LogicalScan { table: t, columns: [t.v1] }
stream_plan: |-
StreamMaterialize { columns: [s1], stream_key: [], pk_columns: [], pk_conflict: NoCheck }
└─StreamProject { exprs: [sum(sum(t.v1))] }
└─StreamSimpleAgg { aggs: [sum(sum(t.v1)), count] }
└─StreamExchange { dist: Single }
└─StreamStatelessSimpleAgg { aggs: [sum(t.v1)] }
└─StreamTableScan { table: t, columns: [t.v1, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: filter clause
sql: |
create table t(v1 int);
select sum(v1) FILTER (WHERE v1 > 0) AS sa from t;
logical_plan: |-
LogicalProject { exprs: [sum(t.v1) filter((t.v1 > 0:Int32))] }
└─LogicalAgg { aggs: [sum(t.v1) filter((t.v1 > 0:Int32))] }
└─LogicalProject { exprs: [t.v1] }
└─LogicalScan { table: t, columns: [t.v1, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [sum(t.v1) filter((t.v1 > 0:Int32))] }
└─LogicalScan { table: t, columns: [t.v1] }
stream_plan: |-
StreamMaterialize { columns: [sa], stream_key: [], pk_columns: [], pk_conflict: NoCheck }
└─StreamProject { exprs: [sum(sum(t.v1) filter((t.v1 > 0:Int32)))] }
└─StreamSimpleAgg { aggs: [sum(sum(t.v1) filter((t.v1 > 0:Int32))), count] }
└─StreamExchange { dist: Single }
└─StreamStatelessSimpleAgg { aggs: [sum(t.v1) filter((t.v1 > 0:Int32))] }
└─StreamTableScan { table: t, columns: [t.v1, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: |
filter clause
extra calculation, should reuse result from project
sql: |
create table t(a int, b int);
select sum(a * b) filter (where a * b > 0) as sab from t;
logical_plan: |-
LogicalProject { exprs: [sum($expr1) filter(((t.a * t.b) > 0:Int32))] }
└─LogicalAgg { aggs: [sum($expr1) filter(((t.a * t.b) > 0:Int32))] }
└─LogicalProject { exprs: [t.a, t.b, (t.a * t.b) as $expr1] }
└─LogicalScan { table: t, columns: [t.a, t.b, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [sum($expr1) filter(((t.a * t.b) > 0:Int32))] }
└─LogicalProject { exprs: [t.a, t.b, (t.a * t.b) as $expr1] }
└─LogicalScan { table: t, columns: [t.a, t.b] }
- name: complex filter clause
sql: |
create table t(a int, b int);
select max(a * b) FILTER (WHERE a < b AND a + b < 100 AND a * b != a + b - 1) AS sab from t;
logical_plan: |-
LogicalProject { exprs: [max($expr1) filter((t.a < t.b) AND ((t.a + t.b) < 100:Int32) AND ((t.a * t.b) <> ((t.a + t.b) - 1:Int32)))] }
└─LogicalAgg { aggs: [max($expr1) filter((t.a < t.b) AND ((t.a + t.b) < 100:Int32) AND ((t.a * t.b) <> ((t.a + t.b) - 1:Int32)))] }
└─LogicalProject { exprs: [t.a, t.b, (t.a * t.b) as $expr1] }
└─LogicalScan { table: t, columns: [t.a, t.b, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [max($expr1) filter((t.a < t.b) AND ((t.a + t.b) < 100:Int32) AND ((t.a * t.b) <> ((t.a + t.b) - 1:Int32)))] }
└─LogicalProject { exprs: [t.a, t.b, (t.a * t.b) as $expr1] }
└─LogicalScan { table: t, columns: [t.a, t.b] }
stream_plan: |-
StreamMaterialize { columns: [sab], stream_key: [], pk_columns: [], pk_conflict: NoCheck }
└─StreamProject { exprs: [max(max($expr1) filter((t.a < t.b) AND ((t.a + t.b) < 100:Int32) AND ((t.a * t.b) <> ((t.a + t.b) - 1:Int32))))] }
└─StreamSimpleAgg { aggs: [max(max($expr1) filter((t.a < t.b) AND ((t.a + t.b) < 100:Int32) AND ((t.a * t.b) <> ((t.a + t.b) - 1:Int32)))), count] }
└─StreamExchange { dist: Single }
└─StreamHashAgg { group_key: [$expr2], aggs: [max($expr1) filter((t.a < t.b) AND ((t.a + t.b) < 100:Int32) AND ((t.a * t.b) <> ((t.a + t.b) - 1:Int32))), count] }
└─StreamProject { exprs: [t.a, t.b, (t.a * t.b) as $expr1, t._row_id, Vnode(t._row_id) as $expr2] }
└─StreamTableScan { table: t, columns: [t.a, t.b, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: avg filter clause + group by
sql: |
create table t(a int, b int);
select avg(a) FILTER (WHERE a > b) AS avga from t group by b ;
logical_plan: |-
LogicalProject { exprs: [(sum(t.a) filter((t.a > t.b))::Decimal / count(t.a) filter((t.a > t.b))::Decimal) as $expr1] }
└─LogicalAgg { group_key: [t.b], aggs: [sum(t.a) filter((t.a > t.b)), count(t.a) filter((t.a > t.b))] }
└─LogicalProject { exprs: [t.b, t.a] }
└─LogicalScan { table: t, columns: [t.a, t.b, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalProject { exprs: [(sum(t.a) filter((t.a > t.b))::Decimal / count(t.a) filter((t.a > t.b))::Decimal) as $expr1] }
└─LogicalAgg { group_key: [t.b], aggs: [sum(t.a) filter((t.a > t.b)), count(t.a) filter((t.a > t.b))] }
└─LogicalScan { table: t, columns: [t.a, t.b] }
stream_plan: |-
StreamMaterialize { columns: [avga, t.b(hidden)], stream_key: [t.b], pk_columns: [t.b], pk_conflict: NoCheck }
└─StreamProject { exprs: [(sum(t.a) filter((t.a > t.b))::Decimal / count(t.a) filter((t.a > t.b))::Decimal) as $expr1, t.b] }
└─StreamHashAgg { group_key: [t.b], aggs: [sum(t.a) filter((t.a > t.b)), count(t.a) filter((t.a > t.b)), count] }
└─StreamExchange { dist: HashShard(t.b) }
└─StreamTableScan { table: t, columns: [t.a, t.b, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: count filter clause
sql: |
create table t(a int, b int);
select count(*) FILTER (WHERE a > b) AS cnt_agb from t;
logical_plan: |-
LogicalProject { exprs: [count filter((t.a > t.b))] }
└─LogicalAgg { aggs: [count filter((t.a > t.b))] }
└─LogicalProject { exprs: [t.a, t.b] }
└─LogicalScan { table: t, columns: [t.a, t.b, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [count filter((t.a > t.b))] }
└─LogicalScan { table: t, columns: [t.a, t.b] }
stream_plan: |-
StreamMaterialize { columns: [cnt_agb], stream_key: [], pk_columns: [], pk_conflict: NoCheck }
└─StreamProject { exprs: [sum0(count filter((t.a > t.b)))] }
└─StreamSimpleAgg { aggs: [sum0(count filter((t.a > t.b))), count] }
└─StreamExchange { dist: Single }
└─StreamStatelessSimpleAgg { aggs: [count filter((t.a > t.b))] }
└─StreamTableScan { table: t, columns: [t.a, t.b, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: filter clause + non-boolean function
sql: |
create table t(a int, b int);
select avg(a) FILTER (WHERE abs(a)) AS avga from t;
binder_error: |-
Bind error: failed to bind expression: avg(a) FILTER(WHERE abs(a))
Caused by:
internal error: argument of FILTER must be boolean, not type Int32
- name: filter clause + subquery
sql: |
create table t(a int, b int);
select avg(a) FILTER (WHERE 0 < (select max(a) from t)) AS avga from t;
binder_error: |-
Bind error: failed to bind expression: avg(a) FILTER(WHERE 0 < (SELECT max(a) FROM t))
Caused by:
Feature is not yet implemented: subquery in filter clause
No tracking issue yet. Feel free to submit a feature request at https://github.com/risingwavelabs/risingwave/issues/new?labels=type%2Ffeature&template=feature_request.yml
- name: aggregation in filter clause
sql: |
create table t(a int, b int);
select avg(a) FILTER (WHERE a < avg(b)) AS avga from t;
binder_error: |-
Bind error: failed to bind expression: avg(a) FILTER(WHERE a < avg(b))
Caused by:
Feature is not yet implemented: aggregation function in filter clause
No tracking issue yet. Feel free to submit a feature request at https://github.com/risingwavelabs/risingwave/issues/new?labels=type%2Ffeature&template=feature_request.yml
- name: filter clause + non-boolean function
sql: |
create table t(a int, b int);
select abs(a) FILTER (WHERE a > 0) AS avga from t;
binder_error: |-
Bind error: failed to bind expression: abs(a) FILTER(WHERE a > 0)
Caused by:
Invalid input syntax: DISTINCT, ORDER BY or FILTER is only allowed in aggregation functions, but `abs` is not an aggregation function
- name: prune column before filter
sql: |
create table t(v1 int, v2 int);
with sub(a, b) as (select min(v1), sum(v2) filter (where v2 < 5) from t) select b from sub;
batch_plan: |-
BatchSimpleAgg { aggs: [sum(sum(t.v2) filter((t.v2 < 5:Int32)))] }
└─BatchExchange { order: [], dist: Single }
└─BatchSimpleAgg { aggs: [sum(t.v2) filter((t.v2 < 5:Int32))] }
└─BatchScan { table: t, columns: [t.v2], distribution: SomeShard }
stream_plan: |-
StreamMaterialize { columns: [b], stream_key: [], pk_columns: [], pk_conflict: NoCheck }
└─StreamProject { exprs: [sum(sum(t.v2) filter((t.v2 < 5:Int32)))] }
└─StreamSimpleAgg { aggs: [sum(sum(t.v2) filter((t.v2 < 5:Int32))), count] }
└─StreamExchange { dist: Single }
└─StreamStatelessSimpleAgg { aggs: [sum(t.v2) filter((t.v2 < 5:Int32))] }
└─StreamTableScan { table: t, columns: [t.v2, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: only distinct agg
sql: |
create table t(a int, b int, c int);
select a, count(distinct b) as distinct_b_num, sum(distinct c) filter(where c < 100) as distinct_c_sum from t group by a;
optimized_logical_plan_for_batch: |-
LogicalAgg { group_key: [t.a], aggs: [count(t.b) filter((flag = 0:Int64)), sum(t.c) filter((count filter((t.c < 100:Int32)) > 0:Int64) AND (flag = 1:Int64))] }
└─LogicalAgg { group_key: [t.a, t.b, t.c, flag], aggs: [count filter((t.c < 100:Int32))] }
└─LogicalExpand { column_subsets: [[t.a, t.b], [t.a, t.c]] }
└─LogicalScan { table: t, columns: [t.a, t.b, t.c] }
- name: single distinct agg and non-disintct agg
sql: |
create table t(a int, b int, c int);
select a, count(distinct b) as distinct_b_num, sum(c) as sum_c from t group by a;
optimized_logical_plan_for_batch: |-
LogicalAgg { group_key: [t.a], aggs: [count(t.b), sum(sum(t.c))] }
└─LogicalAgg { group_key: [t.a, t.b], aggs: [sum(t.c)] }
└─LogicalScan { table: t, columns: [t.a, t.b, t.c] }
batch_plan: |-
BatchExchange { order: [], dist: Single }
└─BatchHashAgg { group_key: [t.a], aggs: [count(t.b), sum(sum(t.c))] }
└─BatchExchange { order: [], dist: HashShard(t.a) }
└─BatchHashAgg { group_key: [t.a, t.b], aggs: [sum(t.c)] }
└─BatchExchange { order: [], dist: HashShard(t.a, t.b) }
└─BatchScan { table: t, columns: [t.a, t.b, t.c], distribution: SomeShard }
stream_plan: |-
StreamMaterialize { columns: [a, distinct_b_num, sum_c], stream_key: [a], pk_columns: [a], pk_conflict: NoCheck }
└─StreamProject { exprs: [t.a, count(distinct t.b), sum(t.c)] }
└─StreamHashAgg { group_key: [t.a], aggs: [count(distinct t.b), sum(t.c), count] }
└─StreamExchange { dist: HashShard(t.a) }
└─StreamTableScan { table: t, columns: [t.a, t.b, t.c, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: distinct agg and non-disintct agg with intersected argument
sql: |
create table t(a int, b int, c int);
select a, count(distinct b) as distinct_b_num, count(distinct c) as distinct_c_sum, sum(c) as sum_c from t group by a;
optimized_logical_plan_for_batch: |-
LogicalAgg { group_key: [t.a], aggs: [count(t.b) filter((flag = 1:Int64)), count(t.c) filter((flag = 0:Int64)), sum(sum(t.c)) filter((flag = 0:Int64))] }
└─LogicalAgg { group_key: [t.a, t.b, t.c, flag], aggs: [sum(t.c)] }
└─LogicalExpand { column_subsets: [[t.a, t.c], [t.a, t.b]] }
└─LogicalScan { table: t, columns: [t.a, t.b, t.c] }
batch_plan: |-
BatchExchange { order: [], dist: Single }
└─BatchHashAgg { group_key: [t.a], aggs: [count(t.b) filter((flag = 1:Int64)), count(t.c) filter((flag = 0:Int64)), sum(sum(t.c)) filter((flag = 0:Int64))] }
└─BatchExchange { order: [], dist: HashShard(t.a) }
└─BatchHashAgg { group_key: [t.a, t.b, t.c, flag], aggs: [sum(t.c)] }
└─BatchExchange { order: [], dist: HashShard(t.a, t.b, t.c, flag) }
└─BatchExpand { column_subsets: [[t.a, t.c], [t.a, t.b]] }
└─BatchScan { table: t, columns: [t.a, t.b, t.c], distribution: SomeShard }
stream_plan: |-
StreamMaterialize { columns: [a, distinct_b_num, distinct_c_sum, sum_c], stream_key: [a], pk_columns: [a], pk_conflict: NoCheck }
└─StreamProject { exprs: [t.a, count(distinct t.b), count(distinct t.c), sum(t.c)] }
└─StreamHashAgg { group_key: [t.a], aggs: [count(distinct t.b), count(distinct t.c), sum(t.c), count] }
└─StreamExchange { dist: HashShard(t.a) }
└─StreamTableScan { table: t, columns: [t.a, t.b, t.c, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: distinct agg with filter
sql: |
create table t(a int, b int, c int);
select a, count(distinct b) filter(where b < 100), sum(c) from t group by a;
optimized_logical_plan_for_batch: |-
LogicalAgg { group_key: [t.a], aggs: [count(t.b) filter((count filter((t.b < 100:Int32)) > 0:Int64)), sum(sum(t.c))] }
└─LogicalAgg { group_key: [t.a, t.b], aggs: [count filter((t.b < 100:Int32)), sum(t.c)] }
└─LogicalScan { table: t, columns: [t.a, t.b, t.c] }
batch_plan: |-
BatchExchange { order: [], dist: Single }
└─BatchHashAgg { group_key: [t.a], aggs: [count(t.b) filter((count filter((t.b < 100:Int32)) > 0:Int64)), sum(sum(t.c))] }
└─BatchExchange { order: [], dist: HashShard(t.a) }
└─BatchHashAgg { group_key: [t.a, t.b], aggs: [count filter((t.b < 100:Int32)), sum(t.c)] }
└─BatchExchange { order: [], dist: HashShard(t.a, t.b) }
└─BatchScan { table: t, columns: [t.a, t.b, t.c], distribution: SomeShard }
stream_plan: |-
StreamMaterialize { columns: [a, count, sum], stream_key: [a], pk_columns: [a], pk_conflict: NoCheck }
└─StreamProject { exprs: [t.a, count(distinct t.b) filter((t.b < 100:Int32)), sum(t.c)] }
└─StreamHashAgg { group_key: [t.a], aggs: [count(distinct t.b) filter((t.b < 100:Int32)), sum(t.c), count] }
└─StreamExchange { dist: HashShard(t.a) }
└─StreamTableScan { table: t, columns: [t.a, t.b, t.c, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- name: non-distinct agg with filter
sql: |
create table t(a int, b int, c int);
select a, count(distinct b), sum(c) filter(where b < 100) from t group by a;
optimized_logical_plan_for_batch: |-
LogicalAgg { group_key: [t.a], aggs: [count(t.b), sum(sum(t.c) filter((t.b < 100:Int32)))] }
└─LogicalAgg { group_key: [t.a, t.b], aggs: [sum(t.c) filter((t.b < 100:Int32))] }
└─LogicalScan { table: t, columns: [t.a, t.b, t.c] }
- name: combined order by & filter clauses
sql: |
create table t(a varchar, b int);
select sum(length(a) * b order by length(a) + b) filter (where b < 100 AND b * 2 > 10) as s1 from t;
logical_plan: |-
LogicalProject { exprs: [sum($expr1) filter((t.b < 100:Int32) AND ((t.b * 2:Int32) > 10:Int32))] }
└─LogicalAgg { aggs: [sum($expr1) filter((t.b < 100:Int32) AND ((t.b * 2:Int32) > 10:Int32))] }
└─LogicalProject { exprs: [t.b, (Length(t.a) * t.b) as $expr1] }
└─LogicalScan { table: t, columns: [t.a, t.b, t._row_id] }
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [sum($expr1) filter((t.b < 100:Int32) AND ((t.b * 2:Int32) > 10:Int32))] }
└─LogicalProject { exprs: [t.b, (Length(t.a) * t.b) as $expr1] }
└─LogicalScan { table: t, columns: [t.a, t.b] }
stream_plan: |-
StreamMaterialize { columns: [s1], stream_key: [], pk_columns: [], pk_conflict: NoCheck }
└─StreamProject { exprs: [sum(sum($expr1) filter((t.b < 100:Int32) AND ((t.b * 2:Int32) > 10:Int32)))] }
└─StreamSimpleAgg { aggs: [sum(sum($expr1) filter((t.b < 100:Int32) AND ((t.b * 2:Int32) > 10:Int32))), count] }
└─StreamExchange { dist: Single }
└─StreamStatelessSimpleAgg { aggs: [sum($expr1) filter((t.b < 100:Int32) AND ((t.b * 2:Int32) > 10:Int32))] }
└─StreamProject { exprs: [t.b, (Length(t.a) * t.b) as $expr1, t._row_id] }
└─StreamTableScan { table: t, columns: [t.a, t.b, t._row_id], pk: [t._row_id], dist: UpstreamHashShard(t._row_id) }
- sql: |
create table t(x int, y varchar);
select string_agg(y, ',' order by y), count(distinct x) from t;
planner_error: 'Invalid input syntax: Order by aggregates are disallowed to occur with distinct aggregates'
- sql: |
create table t(v1 int, v2 int);
with z(a, b) as (select count(distinct v1), count(v2) from t) select a from z;
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [count(t.v1)] }
└─LogicalAgg { group_key: [t.v1], aggs: [] }
└─LogicalScan { table: t, columns: [t.v1] }
- name: input is sharded by group key
sql: |
create table t(x int);
create index i on t(x);
select count(*) as cnt from i group by x;
batch_plan: |-
BatchExchange { order: [], dist: Single }
└─BatchProject { exprs: [count] }
└─BatchSortAgg { group_key: [i.x], aggs: [count] }
└─BatchScan { table: i, columns: [i.x], distribution: UpstreamHashShard(i.x) }
stream_plan: |-
StreamMaterialize { columns: [cnt, i.x(hidden)], stream_key: [i.x], pk_columns: [i.x], pk_conflict: NoCheck }
└─StreamProject { exprs: [count, i.x] }
└─StreamHashAgg { group_key: [i.x], aggs: [count] }
└─StreamTableScan { table: i, columns: [i.x, i.t._row_id], pk: [i.t._row_id], dist: UpstreamHashShard(i.x) }
- name: distinct aggregates only have one distinct argument doesn't need expand
sql: |
create table t(x int, y int);
select count(x), sum(distinct y), sum(distinct y) from t;
optimized_logical_plan_for_batch: |-
LogicalProject { exprs: [sum0(count(t.x)), sum(t.y), sum(t.y)] }
└─LogicalAgg { aggs: [sum0(count(t.x)), sum(t.y)] }
└─LogicalAgg { group_key: [t.y], aggs: [count(t.x)] }
└─LogicalScan { table: t, columns: [t.x, t.y] }
optimized_logical_plan_for_stream: |-
LogicalProject { exprs: [sum0(count(t.x)), sum(t.y), sum(t.y)] }
└─LogicalAgg { aggs: [sum0(count(t.x)), sum(t.y)] }
└─LogicalAgg { group_key: [t.y], aggs: [count(t.x)] }
└─LogicalScan { table: t, columns: [t.x, t.y] }
with_config_map:
RW_FORCE_SPLIT_DISTINCT_AGG: 'true'
- sql: |
create table t(x int, y int);
select count(y), sum(distinct y) from t;
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [sum0(count(t.y)), sum(t.y)] }
└─LogicalAgg { group_key: [t.y], aggs: [count(t.y)] }
└─LogicalScan { table: t, columns: [t.y] }
optimized_logical_plan_for_stream: |-
LogicalAgg { aggs: [sum0(count(t.y)), sum(t.y)] }
└─LogicalAgg { group_key: [t.y], aggs: [count(t.y)] }
└─LogicalScan { table: t, columns: [t.y] }
with_config_map:
RW_FORCE_SPLIT_DISTINCT_AGG: 'true'
- sql: |
create table t(x int, y int);
select count(distinct x), sum(distinct y) from t;
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [count(t.x) filter((flag = 0:Int64)), sum(t.y) filter((flag = 1:Int64))] }
└─LogicalAgg { group_key: [t.x, t.y, flag], aggs: [] }
└─LogicalExpand { column_subsets: [[t.x], [t.y]] }
└─LogicalScan { table: t, columns: [t.x, t.y] }
optimized_logical_plan_for_stream: |-
LogicalAgg { aggs: [count(t.x) filter((flag = 0:Int64)), sum(t.y) filter((flag = 1:Int64))] }
└─LogicalAgg { group_key: [t.x, t.y, flag], aggs: [] }
└─LogicalExpand { column_subsets: [[t.x], [t.y]] }
└─LogicalScan { table: t, columns: [t.x, t.y] }
with_config_map:
RW_FORCE_SPLIT_DISTINCT_AGG: 'true'
- sql: |
create table t(x varchar, y int);
select string_agg(x, ','), count(distinct y) from t;
planner_error: |-
Feature is not yet implemented: Non-distinct string_agg can't appear with distinct aggregates
No tracking issue yet. Feel free to submit a feature request at https://github.com/risingwavelabs/risingwave/issues/new?labels=type%2Ffeature&template=feature_request.yml
- name: remove unnecessary distinct for max and min
sql: |
create table t(x int, y int);
select max(distinct x), min(distinct y) from t;
optimized_logical_plan_for_batch: |-
LogicalAgg { aggs: [max(t.x), min(t.y)] }
└─LogicalScan { table: t, columns: [t.x, t.y] }
optimized_logical_plan_for_stream: |-
LogicalAgg { aggs: [max(t.x), min(t.y)] }
└─LogicalScan { table: t, columns: [t.x, t.y] }
with_config_map:
RW_FORCE_SPLIT_DISTINCT_AGG: 'true'
- name: agg filter - subquery
sql: |
/* This case is valid in PostgreSQL */
create table a (a1 int, a2 int);
select count(a1) filter (where (select true)) from a;
binder_error: |-
Bind error: failed to bind expression: count(a1) FILTER(WHERE (SELECT true))
Caused by:
Feature is not yet implemented: subquery in filter clause
No tracking issue yet. Feel free to submit a feature request at https://github.com/risingwavelabs/risingwave/issues/new?labels=type%2Ffeature&template=feature_request.yml
- name: agg filter - agg
sql: |
/* This case is valid in PostgreSQL */
create table a (a1 int, a2 int);
create table b (b1 int, b2 int);
select 1 from a having exists(