-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
aggregate
1288 lines (1092 loc) · 32.9 KB
/
aggregate
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
# LogicTest: default parallel-stmts distsql
statement ok
CREATE TABLE kv (
k INT PRIMARY KEY,
v INT,
w INT,
s STRING
)
# Aggregate functions return NULL if there are no rows.
query IIIIRRRRBBT
SELECT MIN(1), MAX(1), COUNT(1), SUM_INT(1), AVG(1), SUM(1), STDDEV(1), VARIANCE(1), BOOL_AND(true), BOOL_AND(false), XOR_AGG(b'\x01') FROM kv
----
NULL NULL 0 NULL NULL NULL NULL NULL NULL NULL NULL
# Aggregate functions return NULL if there are no rows.
query T
SELECT ARRAY_AGG(1) FROM kv
----
NULL
query IIIIRRRRBBT
SELECT MIN(v), MAX(v), COUNT(v), SUM_INT(1), AVG(v), SUM(v), STDDEV(v), VARIANCE(v), BOOL_AND(v = 1), BOOL_AND(v = 1), XOR_AGG(s::bytes) FROM kv
----
NULL NULL 0 NULL NULL NULL NULL NULL NULL NULL NULL
query T
SELECT ARRAY_AGG(v) FROM kv
----
NULL
# Aggregate functions triggers aggregation and computation when there is no source.
query IIIIRRRRBBT
SELECT MIN(1), COUNT(1), MAX(1), SUM_INT(1), AVG(1)::float, SUM(1), STDDEV(1), VARIANCE(1), BOOL_AND(true), BOOL_OR(true), TO_HEX(XOR_AGG(b'\x01'))
----
1 1 1 1 1 1 NULL NULL true true 01
# Aggregate functions triggers aggregation and computation when there is no source.
query T
SELECT ARRAY_AGG(1)
----
{1}
# Check that COALESCE using aggregate results over an empty table
# work properly.
query I
SELECT COALESCE(MAX(1), 0) FROM generate_series(1,0)
----
0
query I
SELECT COUNT_ROWS() FROM generate_series(1,100)
----
100
# Same, using arithmetic on COUNT.
query I
SELECT 1 + COUNT(*) FROM generate_series(1,0)
----
1
# Same, using an empty table.
# The following test *must* occur before the first INSERT to the tables,
# so that it can observe an empty table.
query II
SELECT COUNT(*), COALESCE(MAX(k), 1) FROM kv
----
0 1
# Same, using a subquery. (#12705)
query I
SELECT (SELECT COALESCE(MAX(1), 0) FROM generate_series(1,0))
----
0
statement OK
INSERT INTO kv VALUES
(1, 2, 3, 'a'),
(3, 4, 5, 'a'),
(5, NULL, 5, NULL),
(6, 2, 3, 'b'),
(7, 2, 2, 'b'),
(8, 4, 2, 'A')
# Aggregate functions triggers aggregation and computation for every row even when applied to a constant.
# NB: The XOR result is 00 because \x01 is XOR'd an even number of times.
query IIIIRRRRBBT
SELECT MIN(1), COUNT(1), MAX(1), SUM_INT(1), AVG(1)::float, SUM(1), STDDEV(1), VARIANCE(1)::float, BOOL_AND(true), BOOL_OR(true), TO_HEX(XOR_AGG(b'\x01')) FROM kv
----
1 6 1 6 1 6 0 0 true true 00
# Aggregate functions triggers aggregation and computation for every row even when applied to a constant.
query T
SELECT ARRAY_AGG(1) FROM kv
----
{1,1,1,1,1,1}
# Even with no aggregate functions, grouping occurs in the presence of GROUP BY.
query I rowsort
SELECT 1 FROM kv GROUP BY v
----
1
1
1
# Presence of HAVING triggers aggregation, reducing results to one row (even without GROUP BY).
query I rowsort
SELECT 3 FROM kv HAVING TRUE
----
3
query error column "k" must appear in the GROUP BY clause or be used in an aggregate function
SELECT COUNT(*), k FROM kv
query error unsupported comparison operator: <string> < <int>
SELECT COUNT(*) FROM kv GROUP BY s < 5
query II rowsort
SELECT COUNT(*), k FROM kv GROUP BY k
----
1 1
1 3
1 5
1 6
1 7
1 8
# GROUP BY specified using column index works.
query II rowsort
SELECT COUNT(*), k FROM kv GROUP BY 2
----
1 1
1 3
1 5
1 6
1 7
1 8
query error aggregate functions are not allowed in GROUP BY
SELECT * FROM kv GROUP BY v, COUNT(DISTINCT w)
query error aggregate functions are not allowed in GROUP BY
SELECT COUNT(DISTINCT w) FROM kv GROUP BY 1
query error aggregate functions are not allowed in RETURNING
INSERT INTO kv (k, v) VALUES (99, 100) RETURNING sum(v)
query error aggregate functions are not allowed in LIMIT
SELECT SUM(v) FROM kv GROUP BY k LIMIT SUM(v)
query error aggregate functions are not allowed in OFFSET
SELECT SUM(v) FROM kv GROUP BY k LIMIT 1 OFFSET SUM(v)
query error aggregate functions are not allowed in VALUES
INSERT INTO kv (k, v) VALUES (99, COUNT(1))
query error GROUP BY position 5 is not in select list
SELECT COUNT(*), k FROM kv GROUP BY 5
query error GROUP BY position 0 is not in select list
SELECT COUNT(*), k FROM kv GROUP BY 0
query error non-integer constant in GROUP BY
SELECT 1 GROUP BY 'a'
# Qualifying a name in the SELECT, the GROUP BY, both or neither should not affect validation.
query IT rowsort
SELECT COUNT(*), kv.s FROM kv GROUP BY s
----
1 A
1 NULL
2 a
2 b
query IT rowsort
SELECT COUNT(*), s FROM kv GROUP BY kv.s
----
1 A
1 NULL
2 a
2 b
query IT rowsort
SELECT COUNT(*), kv.s FROM kv GROUP BY kv.s
----
1 A
1 NULL
2 a
2 b
query IT rowsort
SELECT COUNT(*), s FROM kv GROUP BY s
----
1 A
1 NULL
2 a
2 b
# Grouping by more than one column works.
query III rowsort
SELECT v, COUNT(*), w FROM kv GROUP BY v, w
----
2 1 2
2 2 3
4 1 2
4 1 5
NULL 1 5
# Grouping by more than one column using column numbers works.
query III rowsort
SELECT v, COUNT(*), w FROM kv GROUP BY 1, 3
----
2 1 2
2 2 3
4 1 2
4 1 5
NULL 1 5
# Selecting and grouping on a function expression works.
query IT rowsort
SELECT COUNT(*), UPPER(s) FROM kv GROUP BY UPPER(s)
----
1 NULL
2 B
3 A
# Selecting and grouping on a constant works.
query I
SELECT COUNT(*) FROM kv GROUP BY 1+2
----
6
query I
SELECT COUNT(*) FROM kv GROUP BY length('abc')
----
6
# Selecting a function of something which is grouped works.
query IT rowsort
SELECT COUNT(*), UPPER(s) FROM kv GROUP BY s
----
1 A
1 NULL
2 A
2 B
# Selecting a value that is not grouped, even if a function of it it, does not work.
query error column "s" must appear in the GROUP BY clause or be used in an aggregate function
SELECT COUNT(*), s FROM kv GROUP BY UPPER(s)
# Selecting and grouping on a more complex expression works.
query ITTTTT
EXPLAIN (VERBOSE) SELECT COUNT(*), k+v FROM kv GROUP BY k+v
----
0 group (empty) (empty) ("count(*)", "k + v") (empty)
0 (empty) aggregate 0 count_rows() (empty) (empty)
0 (empty) aggregate 1 test.kv.k + test.kv.v (empty) (empty)
1 render (empty) (empty) ("k + v") (empty)
1 (empty) render 0 test.kv.k + test.kv.v (empty) (empty)
2 scan (empty) (empty) (k, v, w[omitted], s[omitted]) (empty)
2 (empty) table kv@primary (empty) (empty)
2 (empty) spans ALL (empty) (empty)
query II rowsort
SELECT COUNT(*), k+v FROM kv GROUP BY k+v
----
1 12
1 3
1 7
1 8
1 9
1 NULL
# Selecting a more complex expression, made up of things which are each grouped, works.
query ITTTTT
EXPLAIN (VERBOSE) SELECT COUNT(*), k+v FROM kv GROUP BY k, v
----
0 render (empty) (empty) ("count(*)", "k + v") (empty)
0 (empty) render 0 "count(*)" (empty) (empty)
0 (empty) render 1 k + v (empty) (empty)
1 group (empty) (empty) ("count(*)", k, v) (empty)
1 (empty) aggregate 0 count_rows() (empty) (empty)
1 (empty) aggregate 1 test.kv.k (empty) (empty)
1 (empty) aggregate 2 test.kv.v (empty) (empty)
2 render (empty) (empty) (k, v) (empty)
2 (empty) render 0 test.kv.k (empty) (empty)
2 (empty) render 1 test.kv.v (empty) (empty)
3 scan (empty) (empty) (k, v, w[omitted], s[omitted]) (empty)
3 (empty) table kv@primary (empty) (empty)
3 (empty) spans ALL (empty) (empty)
query II rowsort
SELECT COUNT(*), k+v FROM kv GROUP BY k, v
----
1 12
1 3
1 7
1 8
1 9
1 NULL
query error column "v" must appear in the GROUP BY clause or be used in an aggregate function
SELECT COUNT(*), k+v FROM kv GROUP BY k
query error column "k" must appear in the GROUP BY clause or be used in an aggregate function
SELECT COUNT(*), k+v FROM kv GROUP BY v
query error column "v" must appear in the GROUP BY clause or be used in an aggregate function
SELECT COUNT(*), v/(k+v) FROM kv GROUP BY k+v
query error aggregate functions are not allowed in WHERE
SELECT k FROM kv WHERE AVG(k) > 1
query error aggregate functions are not allowed in the argument of max()
SELECT MAX(AVG(k)) FROM kv
# Test case from #2761.
query II rowsort
SELECT count(kv.k) AS count_1, kv.v + kv.w AS lx FROM kv GROUP BY kv.v + kv.w
----
1 4
1 6
1 9
1 NULL
2 5
query TI rowsort
SELECT s, COUNT(*) FROM kv GROUP BY s HAVING COUNT(*) > 1
----
a 2
b 2
query TII rowsort
SELECT UPPER(s), COUNT(DISTINCT s), COUNT(DISTINCT UPPER(s)) FROM kv GROUP BY UPPER(s) HAVING COUNT(DISTINCT s) > 1
----
A 2 1
query II rowsort
SELECT MAX(k), MIN(v) FROM kv HAVING MIN(v) > 2
----
query II rowsort
SELECT MAX(k), MIN(v) FROM kv HAVING MAX(v) > 2
----
8 2
query error aggregate functions are not allowed in the argument of max()
SELECT MAX(k), MIN(v) FROM kv HAVING MAX(MIN(v)) > 2
query error argument of HAVING must be type bool, not type int
SELECT MAX(k), MIN(v) FROM kv HAVING k
# Expressions listed in the HAVING clause must conform to same validation as the SELECT clause (grouped or aggregated).
query error column "k" must appear in the GROUP BY clause or be used in an aggregate function
SELECT 3 FROM kv GROUP BY v HAVING k > 5
# pg has a special case for grouping on primary key, which would allow this, but we do not.
# See http://www.postgresql.org/docs/current/static/sql-select.html#SQL-GROUPBY
query error column "v" must appear in the GROUP BY clause or be used in an aggregate function
SELECT 3 FROM kv GROUP BY k HAVING v > 2
query error column "k" must appear in the GROUP BY clause or be used in an aggregate function
SELECT k FROM kv HAVING k > 7
query error syntax error at or near ","
SELECT COUNT(*, 1) FROM kv
query I
SELECT COUNT(*)
----
1
query I
SELECT COUNT(k) from kv
----
6
query I
SELECT COUNT(1)
----
1
query I
SELECT COUNT(1) from kv
----
6
query error unknown signature: count\(int, int\)
SELECT COUNT(k, v) FROM kv
query II
SELECT v, COUNT(k) FROM kv GROUP BY v ORDER BY v
----
NULL 1
2 3
4 2
query II
SELECT v, COUNT(k) FROM kv GROUP BY v ORDER BY v DESC
----
4 2
2 3
NULL 1
query II
SELECT v, COUNT(k) FROM kv GROUP BY v ORDER BY COUNT(k) DESC
----
2 3
4 2
NULL 1
query II
SELECT v, COUNT(k) FROM kv GROUP BY v ORDER BY v-COUNT(k)
----
NULL 1
2 3
4 2
query II
SELECT v, COUNT(k) FROM kv GROUP BY v ORDER BY 1 DESC
----
4 2
2 3
NULL 1
query III colnames
SELECT COUNT(*), COUNT(k), COUNT(kv.v) FROM kv
----
count(*) count(k) count(kv.v)
6 6 5
query error cannot use "kv\.\*" in this context
SELECT COUNT(kv.*) FROM kv
query III
SELECT COUNT(DISTINCT k), COUNT(DISTINCT v), COUNT(DISTINCT (v)) FROM kv
----
6 2 2
query TIII rowsort
SELECT UPPER(s), COUNT(DISTINCT k), COUNT(DISTINCT v), COUNT(DISTINCT (v)) FROM kv GROUP BY UPPER(s)
----
A 3 2 2
B 2 1 1
NULL 1 0 0
query I
SELECT COUNT((k, v)) FROM kv
----
6
query I
SELECT COUNT(DISTINCT (k, v)) FROM kv
----
6
query I
SELECT COUNT(DISTINCT (k, (v))) FROM kv
----
6
query I
SELECT COUNT((k, v)) FROM kv LIMIT 1
----
6
query I
SELECT COUNT((k, v)) FROM kv OFFSET 1
----
query I
SELECT COUNT(k)+COUNT(kv.v) FROM kv
----
11
query II
SELECT COUNT(NULL::int), COUNT((NULL, NULL))
----
0 1
query IIII
SELECT MIN(k), MAX(k), MIN(v), MAX(v) FROM kv
----
1 8 2 4
# Even if no input rows match, we expect a row (of nulls).
query IIII
SELECT MIN(k), MAX(k), MIN(v), MAX(v) FROM kv WHERE k > 8
----
NULL NULL NULL NULL
query TT
SELECT ARRAY_AGG(k), ARRAY_AGG(s) FROM kv
----
{1,3,5,6,7,8} {"a","a",NULL,"b","b","A"}
query T
SELECT array_agg(s) FROM kv WHERE s IS NULL
----
{NULL}
query RRRR
SELECT AVG(k), AVG(v), SUM(k), SUM(v) FROM kv
----
5 2.8 30 14
query RRRR
SELECT AVG(k::decimal), AVG(v::decimal), SUM(k::decimal), SUM(v::decimal) FROM kv
----
5 2.8 30 14
query RRRR
SELECT AVG(DISTINCT k), AVG(DISTINCT v), SUM(DISTINCT k), SUM(DISTINCT v) FROM kv
----
5 3 30 6
query R
SELECT AVG(k) * 2.0 + MAX(v)::DECIMAL FROM kv
----
14.0
# Verify things work with distsql when some of the nodes emit no results in the
# local stage.
query R
SELECT AVG(k) * 2.0 + MAX(v)::DECIMAL FROM kv WHERE w*2 = k
----
14.0
query ITTT
EXPLAIN (EXPRS) SELECT COUNT(k) FROM kv
----
0 group (empty) (empty)
0 (empty) aggregate 0 count(k)
1 render (empty) (empty)
1 (empty) render 0 k
2 scan (empty) (empty)
2 (empty) table kv@primary
2 (empty) spans ALL
query ITTT
EXPLAIN (EXPRS) SELECT COUNT(k), SUM(k), MAX(k) FROM kv
----
0 group (empty) (empty)
0 (empty) aggregate 0 count(k)
0 (empty) aggregate 1 sum(k)
0 (empty) aggregate 2 max(k)
1 render (empty) (empty)
1 (empty) render 0 k
2 scan (empty) (empty)
2 (empty) table kv@primary
2 (empty) spans ALL
statement ok
CREATE TABLE abc (
a CHAR PRIMARY KEY,
b FLOAT,
c BOOLEAN,
d DECIMAL
)
statement ok
INSERT INTO abc VALUES ('one', 1.5, true, 5::decimal), ('two', 2.0, false, 1.1::decimal)
query ITTT
EXPLAIN (EXPRS) SELECT MIN(a) FROM abc
----
0 group (empty) (empty)
0 (empty) aggregate 0 min(a)
1 render (empty) (empty)
1 (empty) render 0 a
2 scan (empty) (empty)
2 (empty) table abc@primary
2 (empty) spans /#-
2 (empty) limit 1
query TRBR
SELECT MIN(a), MIN(b), MIN(c), MIN(d) FROM abc
----
one 1.5 false 1.1
query TRBR
SELECT MAX(a), MAX(b), MAX(c), MAX(d) FROM abc
----
two 2 true 5
query RRRR
SELECT AVG(b), SUM(b), AVG(d), SUM(d) FROM abc
----
1.75 3.5 3.05 6.1
# Verify summing of intervals
statement ok
CREATE TABLE intervals (
a INTERVAL PRIMARY KEY
)
statement ok
INSERT INTO intervals VALUES (INTERVAL '1 year 2 months 3 days 4 seconds'), (INTERVAL '2 year 3 months 4 days 5 seconds'), (INTERVAL '10000ms')
query T
SELECT SUM(a) FROM intervals
----
3y5mon7d19s
query error unknown signature: avg\(string\)
SELECT AVG(a) FROM abc
query error unknown signature: avg\(bool\)
SELECT AVG(c) FROM abc
query error unknown signature: avg\(tuple{string, bool}\)
SELECT AVG((a,c)) FROM abc
query error unknown signature: sum\(string\)
SELECT SUM(a) FROM abc
query error unknown signature: sum\(bool\)
SELECT SUM(c) FROM abc
query error unknown signature: sum\(tuple{string, bool}\)
SELECT SUM((a,c)) FROM abc
statement ok
CREATE TABLE xyz (
x INT PRIMARY KEY,
y INT,
z FLOAT,
INDEX xy (x, y),
INDEX zyx (z, y, x),
FAMILY (x),
FAMILY (y),
FAMILY (z)
)
statement ok
INSERT INTO xyz VALUES (1, 2, 3.0), (4, 5, 6.0), (7, NULL, 8.0)
query I
SELECT MIN(x) FROM xyz
----
1
query ITTT
EXPLAIN (EXPRS) SELECT MIN(x) FROM xyz
----
0 group (empty) (empty)
0 (empty) aggregate 0 min(x)
1 render (empty) (empty)
1 (empty) render 0 x
2 scan (empty) (empty)
2 (empty) table xyz@xy
2 (empty) spans /#-
2 (empty) limit 1
query I
SELECT MIN(x) FROM xyz WHERE x in (0, 4, 7)
----
4
query ITTT
EXPLAIN (EXPRS) SELECT MIN(x) FROM xyz WHERE x in (0, 4, 7)
----
0 group (empty) (empty)
0 (empty) aggregate 0 min(x)
1 render (empty) (empty)
1 (empty) render 0 x
2 scan (empty) (empty)
2 (empty) table xyz@xy
2 (empty) spans /0-/1 /4-/5 /7-/8
2 (empty) limit 1
query I
SELECT MAX(x) FROM xyz
----
7
query ITTT
EXPLAIN (EXPRS) SELECT MAX(x) FROM xyz
----
0 group (empty) (empty)
0 (empty) aggregate 0 max(x)
1 render (empty) (empty)
1 (empty) render 0 x
2 revscan (empty) (empty)
2 (empty) table xyz@xy
2 (empty) spans /#-
2 (empty) limit 1
query I
SELECT MIN(y) FROM xyz WHERE x = 1
----
2
query ITTT
EXPLAIN (EXPRS) SELECT MIN(y) FROM xyz WHERE x = 1
----
0 group (empty) (empty)
0 (empty) aggregate 0 min(y)
1 render (empty) (empty)
1 (empty) render 0 y
2 scan (empty) (empty)
2 (empty) table xyz@xy
2 (empty) spans /1/#-/2
2 (empty) limit 1
query I
SELECT MAX(y) FROM xyz WHERE x = 1
----
2
query ITTT
EXPLAIN (EXPRS) SELECT MAX(y) FROM xyz WHERE x = 1
----
0 group (empty) (empty)
0 (empty) aggregate 0 max(y)
1 render (empty) (empty)
1 (empty) render 0 y
2 revscan (empty) (empty)
2 (empty) table xyz@xy
2 (empty) spans /1/#-/2
2 (empty) limit 1
query I
SELECT MIN(y) FROM xyz WHERE x = 7
----
NULL
query ITTT
EXPLAIN (EXPRS) SELECT MIN(y) FROM xyz WHERE x = 7
----
0 group (empty) (empty)
0 (empty) aggregate 0 min(y)
1 render (empty) (empty)
1 (empty) render 0 y
2 scan (empty) (empty)
2 (empty) table xyz@xy
2 (empty) spans /7/#-/8
2 (empty) limit 1
query I
SELECT MAX(y) FROM xyz WHERE x = 7
----
NULL
query ITTT
EXPLAIN (EXPRS) SELECT MAX(y) FROM xyz WHERE x = 7
----
0 group (empty) (empty)
0 (empty) aggregate 0 max(y)
1 render (empty) (empty)
1 (empty) render 0 y
2 revscan (empty) (empty)
2 (empty) table xyz@xy
2 (empty) spans /7/#-/8
2 (empty) limit 1
query I
SELECT MIN(x) FROM xyz WHERE (y, z) = (2, 3.0)
----
1
query ITTT
EXPLAIN (EXPRS) SELECT MIN(x) FROM xyz WHERE (y, z) = (2, 3.0)
----
0 group (empty) (empty)
0 (empty) aggregate 0 min(x)
1 render (empty) (empty)
1 (empty) render 0 x
2 scan (empty) (empty)
2 (empty) table xyz@zyx
2 (empty) spans /3/2/#-/3/3
2 (empty) limit 1
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT MIN(x) FROM xyz WHERE (y, z) = (2, 3.0)]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /xyz/zyx/3.0/2/1 -> NULL
output row: [1]
query I
SELECT MAX(x) FROM xyz WHERE (z, y) = (3.0, 2)
----
1
query ITTT
EXPLAIN (EXPRS) SELECT MAX(x) FROM xyz WHERE (z, y) = (3.0, 2)
----
0 group (empty) (empty)
0 (empty) aggregate 0 max(x)
1 render (empty) (empty)
1 (empty) render 0 x
2 revscan (empty) (empty)
2 (empty) table xyz@zyx
2 (empty) spans /3/2/#-/3/3
2 (empty) limit 1
query RRR
SELECT VARIANCE(x), VARIANCE(y::decimal), round(VARIANCE(z), 14) FROM xyz
----
9 4.5 6.33333333333333
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT VARIANCE(x), VARIANCE(y::decimal), round(VARIANCE(z), 14) FROM xyz]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /xyz/primary/1 -> NULL
fetched: /xyz/primary/1/y -> 2
fetched: /xyz/primary/1/z -> 3.0
fetched: /xyz/primary/4 -> NULL
fetched: /xyz/primary/4/y -> 5
fetched: /xyz/primary/4/z -> 6.0
fetched: /xyz/primary/7 -> NULL
fetched: /xyz/primary/7/z -> 8.0
output row: [9 4.5 6.33333333333333]
query R
SELECT VARIANCE(x) FROM xyz WHERE x = 10
----
NULL
query R
SELECT VARIANCE(x) FROM xyz WHERE x = 1
----
NULL
query ITTT
EXPLAIN (EXPRS) SELECT VARIANCE(x) FROM xyz WHERE x = 1
----
0 group (empty) (empty)
0 (empty) aggregate 0 variance(x)
1 render (empty) (empty)
1 (empty) render 0 x
2 scan (empty) (empty)
2 (empty) table xyz@xy
2 (empty) spans /1-/2
query RRR
SELECT STDDEV(x), STDDEV(y::decimal), round(STDDEV(z), 14) FROM xyz
----
3 2.1213203435596425732 2.51661147842358
query R
SELECT STDDEV(x) FROM xyz WHERE x = 1
----
NULL
# Verify we only look at one row for MIN when we have an index on that column.
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT MIN(z) FROM xyz]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /xyz/zyx/3.0/2/1 -> NULL
output row: [3.0]
query RRR
SELECT AVG(1::int)::float, AVG(2::float)::float, AVG(3::decimal)::float
----
1 2 3
query III
SELECT COUNT(2::int), COUNT(3::float), COUNT(4::decimal)
----
1 1 1
query RRR
SELECT SUM(1::int), SUM(2::float), SUM(3::decimal)
----
1 2 3
query RRR
SELECT VARIANCE(1::int), VARIANCE(1::float), VARIANCE(1::decimal)
----
NULL NULL NULL
query RRR
SELECT STDDEV(1::int), STDDEV(1::float), STDDEV(1::decimal)
----
NULL NULL NULL
# Ensure subqueries don't trigger aggregation.
query B
SELECT x > (SELECT avg(0)) FROM xyz LIMIT 1
----
true
statement ok
CREATE TABLE bools (b BOOL)
query BB
SELECT BOOL_AND(b), BOOL_OR(b) FROM bools
----
NULL NULL
statement OK
INSERT INTO bools VALUES (true), (true), (true)
query BB
SELECT BOOL_AND(b), BOOL_OR(b) FROM bools
----
true true
statement OK
INSERT INTO bools VALUES (false), (false)
query BB
SELECT BOOL_AND(b), BOOL_OR(b) FROM bools
----
false true
statement OK
DELETE FROM bools WHERE b
query BB
SELECT BOOL_AND(b), BOOL_OR(b) FROM bools
----
false false
query T
SELECT CONCAT_AGG(s) FROM (SELECT s FROM kv ORDER BY k)
----
aabbA
# Tests for the single-row optimization.
statement OK
CREATE TABLE ab (
a INT PRIMARY KEY,
b INT,
FAMILY (a),
FAMILY (b)
)
statement OK
INSERT INTO ab VALUES
(1, 10),
(2, 20),
(3, 30),
(4, 40),
(5, 50)
query ITTT
EXPLAIN (EXPRS) SELECT MIN(a) FROM abc
----
0 group (empty) (empty)
0 (empty) aggregate 0 min(a)
1 render (empty) (empty)
1 (empty) render 0 a
2 scan (empty) (empty)
2 (empty) table abc@primary
2 (empty) spans /#-
2 (empty) limit 1
# Verify we only buffer one row.
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT MIN(a) FROM ab]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /ab/primary/1 -> NULL
fetched: /ab/primary/1/b -> 10
output row: [1]
query ITTT
EXPLAIN (EXPRS) SELECT MAX(a) FROM abc
----
0 group (empty) (empty)
0 (empty) aggregate 0 max(a)
1 render (empty) (empty)
1 (empty) render 0 a
2 revscan (empty) (empty)
2 (empty) table abc@primary
2 (empty) spans /#-
2 (empty) limit 1
# Verify we only buffer one row.
query T
SELECT message FROM [SHOW KV TRACE FOR SELECT MAX(a) FROM ab]
WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
----
fetched: /ab/primary/5/b -> 50
fetched: /ab/primary/5 -> NULL
output row: [5]
query ITTT
EXPLAIN (EXPRS) SELECT v, COUNT(k) FROM kv GROUP BY v ORDER BY COUNT(k)
----
0 sort (empty) (empty)
0 (empty) order +"count(k)"
1 group (empty) (empty)
1 (empty) aggregate 0 v
1 (empty) aggregate 1 count(k)
2 render (empty) (empty)
2 (empty) render 0 v
2 (empty) render 1 k
3 scan (empty) (empty)
3 (empty) table kv@primary
3 (empty) spans ALL