Skip to content

Commit f6974e1

Browse files
adriangbclaude
andcommitted
refactor: count evaluations in the volatile coalesce tests
The tests for apache#25477 ran random() over 100000 rows, or wrapped arguments in `case when random() >= 0 then x end` to make a volatile call with a known value. Use counted_volatile, which does both, and check the evaluation count. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 200101d commit f6974e1

1 file changed

Lines changed: 42 additions & 22 deletions

File tree

‎datafusion/sqllogictest/test_files/coalesce.slt‎

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -463,26 +463,34 @@ physical_plan
463463
01)ProjectionExec: expr=[coalesce(nullif(floor(random() * 2), 0), -1) as c]
464464
02)--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
473482
select
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.
486494
statement ok
487495
create 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

493501
query IIIIT
494502
select
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')
500508
from volatile_args;
501509
----
502510
1 1 1 1 none
503511
2 -1 NULL 2 b
504512
3 -1 NULL 3 none
505513
NULL -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+
507522
query TI
508523
select
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
----
512527
datafusion 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`.
516536
query TT

0 commit comments

Comments
 (0)