@@ -2109,7 +2109,7 @@ SET datafusion.execution.target_partitions = 4;
21092109# reused. Projection pushdown must not merge the outer projection into the file
21102110# scan when doing so would inline and duplicate the volatile expression.
21112111# Reproduces only against a file scan (not an in-memory table); if the volatile
2112- # expression is duplicated, the two references diverge and `x = y` is false .
2112+ # expression is duplicated, it is evaluated two times per row .
21132113#####################
21142114
21152115statement ok
@@ -2121,15 +2121,20 @@ statement ok
21212121CREATE EXTERNAL TABLE volatile_scan STORED AS PARQUET
21222122LOCATION 'test_files/scratch/projection_pushdown/volatile.parquet';
21232123
2124- # The two references to the aliased `random()` value must be equal on every
2125- # row: the expression is evaluated once and reused, not inlined twice.
2126- query B rowsort
2127- SELECT s.x = s.y
2128- FROM (SELECT r AS x, r AS y FROM (SELECT random( ) AS r FROM volatile_scan) AS t) AS s;
2124+ # The aliased volatile value is evaluated one time per row and reused, not
2125+ # inlined twice.
2126+ query II rowsort
2127+ SELECT s.x, s.y
2128+ FROM (SELECT r AS x, r AS y FROM (SELECT counted_volatile(id ) AS r FROM volatile_scan) AS t) AS s;
21292129----
2130- true
2131- true
2132- true
2130+ 1 1
2131+ 2 2
2132+ 3 3
2133+
2134+ query I
2135+ SELECT evaluations();
2136+ ----
2137+ 3
21332138
21342139#####################
21352140# Section: leaf expression extraction does not duplicate volatile expressions
@@ -2176,37 +2181,52 @@ logical_plan
21762181statement ok
21772182set datafusion.explain.logical_plan_only = false;
21782183
2179- # Every row's `field` is the `a` field of that row's own struct .
2180- query B
2181- SELECT bool_and(field = s['a'] )
2184+ # The struct is evaluated one time per row; `field` reads it .
2185+ query II
2186+ SELECT count(s), sum(field )
21822187FROM (
21832188 SELECT s, s['a'] AS field
2184- FROM (SELECT named_struct('a', random( )) AS s FROM generate_series(1, 1000 ))
2189+ FROM (SELECT named_struct('a', counted_volatile(value )) AS s FROM generate_series(1, 10 ))
21852190);
21862191----
2187- true
2192+ 10 55
2193+
2194+ query I
2195+ SELECT evaluations();
2196+ ----
2197+ 10
21882198
2189- # Same invariant when the extraction is routed into one side of a join.
2190- query B
2191- SELECT bool_and(field = s['a'] )
2199+ # Same when the extraction is routed into one side of a join.
2200+ query II
2201+ SELECT count(s), sum(field )
21922202FROM (
21932203 SELECT l.s, l.s['a'] AS field
2194- FROM (SELECT named_struct('a', random( )) AS s FROM generate_series(1, 1000 )) AS l
2204+ FROM (SELECT named_struct('a', counted_volatile(value )) AS s FROM generate_series(1, 10 )) AS l
21952205 INNER JOIN (SELECT 1 AS k) AS r ON true
21962206);
21972207----
2198- true
2208+ 10 55
2209+
2210+ query I
2211+ SELECT evaluations();
2212+ ----
2213+ 10
21992214
2200- # Every surviving row satisfies the predicate it was filtered on .
2201- query B
2202- SELECT bool_and(s['a'] > 0.5 )
2215+ # The filter tests the struct that is returned, not a second evaluation .
2216+ query I
2217+ SELECT count(s )
22032218FROM (
22042219 SELECT s
2205- FROM (SELECT named_struct('a', random( )) AS s FROM generate_series(1, 1000 ))
2206- WHERE s['a'] > 0. 5
2220+ FROM (SELECT named_struct('a', counted_volatile(value )) AS s FROM generate_series(1, 10 ))
2221+ WHERE s['a'] > 5
22072222);
22082223----
2209- true
2224+ 5
2225+
2226+ query I
2227+ SELECT evaluations();
2228+ ----
2229+ 10
22102230
22112231#####################
22122232# Section: expensive expressions are not re-inlined by projection pushdown
0 commit comments