@@ -220,6 +220,9 @@ set datafusion.execution.batch_size = 4;
220220
221221# Inserting into nullable table with batch_size specified above
222222# to prevent creation on single in-memory batch
223+
224+ # Test error case: the error case is expected here, as the table c5 is not nullable
225+ # and the query tries to insert a nullable value
223226statement ok
224227CREATE TABLE aggregate_test_100_null (
225228 c2 TINYINT NOT NULL,
@@ -237,6 +240,27 @@ SELECT
237240 CASE WHEN c1 = 'a' THEN NULL ELSE c11 END as c11
238241FROM aggregate_test_100;
239242
243+ statement ok
244+ drop table aggregate_test_100_null;
245+
246+ # Test successful insert case which c5 is nullable
247+ statement ok
248+ CREATE TABLE aggregate_test_100_null (
249+ c2 TINYINT NOT NULL,
250+ c5 INT,
251+ c3 SMALLINT,
252+ c11 FLOAT
253+ );
254+
255+ statement ok
256+ INSERT INTO aggregate_test_100_null
257+ SELECT
258+ c2,
259+ c5,
260+ CASE WHEN c1 = 'e' THEN NULL ELSE c3 END as c3,
261+ CASE WHEN c1 = 'a' THEN NULL ELSE c11 END as c11
262+ FROM aggregate_test_100;
263+
240264# Test count varchar / int / float
241265query IIII
242266SELECT c2, count(c1), count(c5), count(c11) FROM aggregate_test_100 GROUP BY c2 ORDER BY c2;
@@ -301,11 +325,21 @@ SELECT c2, approx_distinct(c1), approx_distinct(c5) FROM aggregate_test_100 GROU
301325query III
302326SELECT c2, count(c3), count(c11) FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
303327----
328+ 1 19 17
329+ 2 17 19
330+ 3 15 13
331+ 4 16 19
332+ 5 12 11
304333
305334# Test min / max with nullable fields
306335query IIIRR
307336SELECT c2, min(c3), max(c3), min(c11), max(c11) FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
308337----
338+ 1 -99 125 0.064453244 0.89651865
339+ 2 -117 122 0.09683716 0.8315913
340+ 3 -101 123 0.034291923 0.94669616
341+ 4 -117 123 0.028003037 0.7085086
342+ 5 -101 118 0.12559289 0.87989986
309343
310344# Test sum with nullable fields
311345query IIR
@@ -321,16 +355,31 @@ SELECT c2, sum(c3), sum(c11) FROM aggregate_test_100 GROUP BY c2 ORDER BY c2;
321355query IIR
322356SELECT c2, median(c3), median(c11) FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
323357----
358+ 1 12 0.6067944
359+ 2 1 0.46076488
360+ 3 14 0.40154034
361+ 4 -17 0.48515016
362+ 5 -35 0.5536642
324363
325364# Test approx_median with nullable fields
326365query IIR
327366SELECT c2, approx_median(c3), approx_median(c11) FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
328367----
368+ 1 12 0.6067944
369+ 2 1 0.46076488
370+ 3 14 0.40154034
371+ 4 -7 0.48515016
372+ 5 -39 0.5536642
329373
330374# Test approx_distinct with nullable fields
331375query II
332376SELECT c2, approx_distinct(c3) FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
333377----
378+ 1 19
379+ 2 16
380+ 3 13
381+ 4 16
382+ 5 12
334383
335384# Test avg for tinyint / float
336385query TRR
@@ -467,6 +516,11 @@ SELECT c2,
467516 COUNT(c11) FILTER(WHERE c5 > 0)
468517FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
469518----
519+ 1 11 6
520+ 2 6 6
521+ 3 8 6
522+ 4 11 14
523+ 5 8 7
470524
471525# Test avg for tinyint / float
472526query TRR
@@ -489,6 +543,11 @@ SELECT c2,
489543 COUNT(c11) FILTER(WHERE c3 > 0)
490544FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
491545----
546+ 1 10 9
547+ 2 7 8
548+ 3 3 6
549+ 4 3 7
550+ 5 6 3
492551
493552# Test min / max with nullable fields and filter
494553query IIIRR
@@ -499,6 +558,11 @@ SELECT c2,
499558 MAX(c11) FILTER (WHERE c5 < 0)
500559FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
501560----
561+ 1 -99 103 0.2578469 0.89651865
562+ 2 -48 93 0.09683716 0.8315913
563+ 3 -76 123 0.034291923 0.94669616
564+ 4 -117 123 0.06563997 0.57360977
565+ 5 -94 68 0.12559289 0.75173044
502566
503567# Test min / max with nullable fields and nullable filter
504568query III
@@ -507,6 +571,11 @@ SELECT c2,
507571 MAX(c3) FILTER (WHERE c11 > 0.5)
508572FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
509573----
574+ 1 -99 125
575+ 2 -106 122
576+ 3 -76 73
577+ 4 -117 47
578+ 5 -82 118
510579
511580# Test sum with nullable field and nullable / non-nullable filters
512581query IIIRR
@@ -517,20 +586,35 @@ SELECT c2,
517586 SUM(c11) FILTER (WHERE c3 > 0)
518587FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
519588----
589+ 1 -3 77 7.214695632458 5.085060358047
590+ 2 100 77 6.197732746601 3.150197088718
591+ 3 109 211 2.80575042963 2.80632930994
592+ 4 -171 56 2.10740506649 1.939846396446
593+ 5 -86 -76 1.8741710186 1.600569307804
520594
521595# Test approx_distinct with nullable fields and filter
522596query II
523597SELECT c2,
524598 approx_distinct(c3) FILTER (WHERE c5 > 0)
525599FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
526600----
601+ 1 11
602+ 2 6
603+ 3 6
604+ 4 11
605+ 5 8
527606
528607# Test approx_distinct with nullable fields and nullable filter
529608query II
530609SELECT c2,
531610 approx_distinct(c3) FILTER (WHERE c11 > 0.5)
532611FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
533612----
613+ 1 10
614+ 2 6
615+ 3 3
616+ 4 3
617+ 5 6
534618
535619# Test median with nullable fields and filter
536620query IIR
@@ -539,13 +623,23 @@ SELECT c2,
539623 median(c11) FILTER (WHERE c5 < 0)
540624FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
541625----
626+ 1 -5 0.6623719
627+ 2 15 0.52930677
628+ 3 13 0.32792538
629+ 4 -38 0.49774808
630+ 5 -18 0.49842384
542631
543632# Test min / max with nullable fields and nullable filter
544633query II
545634SELECT c2,
546635 median(c3) FILTER (WHERE c11 > 0.5)
547636FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
548637----
638+ 1 33
639+ 2 -29
640+ 3 22
641+ 4 -90
642+ 5 -22
549643
550644# Test approx_median with nullable fields and filter
551645query IIR
@@ -554,13 +648,23 @@ SELECT c2,
554648 approx_median(c11) FILTER (WHERE c5 < 0)
555649FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
556650----
651+ 1 -5 0.6623719
652+ 2 12 0.52930677
653+ 3 13 0.32792538
654+ 4 -38 0.49774808
655+ 5 -21 0.47652745
557656
558657# Test approx_median with nullable fields and nullable filter
559658query II
560659SELECT c2,
561660 approx_median(c3) FILTER (WHERE c11 > 0.5)
562661FROM aggregate_test_100_null GROUP BY c2 ORDER BY c2;
563662----
663+ 1 35
664+ 2 -29
665+ 3 22
666+ 4 -90
667+ 5 -32
564668
565669statement ok
566670DROP TABLE aggregate_test_100_null;
0 commit comments