@@ -463,26 +463,34 @@ physical_plan
46346301)ProjectionExec: expr=[coalesce(nullif(floor(random() * 2), 0), -1) as c]
46446402)--PlaceholderRowExec
465465
466- # With two draws, about 25% of rows were NULL (and Arrow rejected them because
467- # the `-1` fallback makes the result non-nullable). With one draw, every row is
468- # either 1 or -1.
469- statement ok
470- create table volatile_t as select * from generate_series(1, 100000) as t(i);
466+ # A volatile first argument is evaluated one time per row, not once for the
467+ # null test and again for the value.
468+ query II
469+ select
470+ count(coalesce(nullif(counted_volatile(value) % 2, 0), -1)),
471+ sum(coalesce(nullif(counted_volatile(value) % 2, 0), -1))
472+ from generate_series(1, 10);
473+ ----
474+ 10 0
475+
476+ query I
477+ select evaluations();
478+ ----
479+ 20
471480
472- query III
481+ query II
473482select
474- count(coalesce(nullif(floor(random() * 2), 0), -1)),
475- count(nvl(nullif(floor(random() * 2), 0), -1)),
476- count(*) filter (where coalesce(nullif(floor(random() * 2), 0), -1) not in (1, -1))
477- from volatile_t;
483+ count(nvl(nullif(counted_volatile(value) % 2, 0), -1)),
484+ sum(nvl(nullif(counted_volatile(value) % 2, 0), -1))
485+ from generate_series(1, 10);
478486----
479- 100000 100000 0
487+ 10 0
480488
481- statement ok
482- drop table volatile_t;
489+ query I
490+ select evaluations();
491+ ----
492+ 20
483493
484- # `case when random() >= 0 then x end` is volatile but always equals `x`, which
485- # makes the results of the volatile code path deterministic.
486494statement ok
487495create table volatile_args(a int, b int, c int, s varchar, x int, y int) as values
488496 (1, 9, 8, null, 0, 1),
@@ -492,25 +500,37 @@ create table volatile_args(a int, b int, c int, s varchar, x int, y int) as valu
492500
493501query IIIIT
494502select
495- coalesce(case when random() >= 0 then a end , b, c),
496- coalesce(case when random() >= 0 then a end , -1),
497- coalesce(case when random() >= 0 then a end , null::int),
498- coalesce(a, case when random() >= 0 then b end , c),
499- coalesce(case when random() >= 0 then s end , 'none')
503+ coalesce(counted_volatile(a) , b, c),
504+ coalesce(counted_volatile(a) , -1),
505+ coalesce(counted_volatile(a) , null::int),
506+ coalesce(a, counted_volatile(b) , c),
507+ coalesce(counted_volatile(s) , 'none')
500508from volatile_args;
501509----
5025101 1 1 1 none
5035112 -1 NULL 2 b
5045123 -1 NULL 3 none
505513NULL -1 NULL NULL none
506514
515+ # Four rows for each of the first three and the last call, and three for
516+ # `counted_volatile(b)`, which is reached only where `a` is null.
517+ query I
518+ select evaluations();
519+ ----
520+ 19
521+
507522query TI
508523select
509- coalesce(case when random() >= 0 then null::varchar end , 'datafusion', 'unused'),
510- coalesce(case when random() >= 0 then null::int end , null::int);
524+ coalesce(counted_volatile( null::varchar) , 'datafusion', 'unused'),
525+ coalesce(counted_volatile( null::int) , null::int);
511526----
512527datafusion NULL
513528
529+ query I
530+ select evaluations();
531+ ----
532+ 2
533+
514534# Arguments before the first volatile one keep the CASE rewrite; only the rest
515535# is left to `coalesce`.
516536query TT
0 commit comments