@@ -1208,14 +1208,19 @@ impl OptimizerRule for PushDownFilter {
12081208 // determine if we can push any predicates down past the extension node
12091209
12101210 // each element is true for push, false to keep
1211+ //
1212+ // A volatile predicate must stay above the extension node:
1213+ // pushing it into the inputs evaluates it once per input row
1214+ // (or once per input) instead of once for the filtered result.
12111215 let predicate_push_or_keep: Vec < bool > =
12121216 split_conjunction ( & filter. predicate )
12131217 . iter ( )
12141218 . map ( |expr| {
1215- !expr
1216- . column_refs ( )
1217- . iter ( )
1218- . any ( |c| prevent_cols. contains ( & c. name ) )
1219+ !expr. is_volatile ( )
1220+ && !expr
1221+ . column_refs ( )
1222+ . iter ( )
1223+ . any ( |c| prevent_cols. contains ( & c. name ) )
12191224 } )
12201225 . collect ( ) ;
12211226
@@ -1439,7 +1444,7 @@ mod tests {
14391444 use std:: cmp:: Ordering ;
14401445 use std:: fmt:: { Debug , Formatter } ;
14411446
1442- use arrow:: datatypes:: { Field , Schema , SchemaRef } ;
1447+ use arrow:: datatypes:: { DataType , Field , Schema , SchemaRef } ;
14431448 use async_trait:: async_trait;
14441449
14451450 use datafusion_common:: { DFSchemaRef , DataFusionError , ScalarValue } ;
@@ -2189,6 +2194,203 @@ mod tests {
21892194 )
21902195 }
21912196
2197+ /// returns a "random" function that is marked volatile (aka each invocation
2198+ /// returns a different value)
2199+ ///
2200+ /// Does not use datafusion_functions::rand to avoid introducing a
2201+ /// dependency on that crate.
2202+ fn rand_func ( ) -> ScalarUDF {
2203+ ScalarUDF :: new_from_impl ( RandomStub :: new ( ) )
2204+ }
2205+
2206+ #[ derive( Debug , PartialEq , Eq , Hash ) ]
2207+ struct RandomStub {
2208+ signature : Signature ,
2209+ }
2210+
2211+ impl RandomStub {
2212+ fn new ( ) -> Self {
2213+ Self {
2214+ signature : Signature :: exact ( vec ! [ ] , Volatility :: Volatile ) ,
2215+ }
2216+ }
2217+ }
2218+ impl ScalarUDFImpl for RandomStub {
2219+ fn name ( & self ) -> & str {
2220+ "random"
2221+ }
2222+
2223+ fn signature ( & self ) -> & Signature {
2224+ & self . signature
2225+ }
2226+
2227+ fn return_type ( & self , _arg_types : & [ DataType ] ) -> Result < DataType > {
2228+ Ok ( DataType :: Float64 )
2229+ }
2230+
2231+ fn invoke_with_args ( & self , _args : ScalarFunctionArgs ) -> Result < ColumnarValue > {
2232+ panic ! ( "dummy - not implemented" )
2233+ }
2234+ }
2235+
2236+ #[ test]
2237+ fn user_defined_plan_volatile_filters ( ) -> Result < ( ) > {
2238+ let table_scan = test_table_scan ( ) ?;
2239+ let rand = rand_func ( ) . call ( vec ! [ ] ) ;
2240+
2241+ // A volatile predicate that references no column stays above the
2242+ // extension node: pushing it into the inputs would evaluate it one
2243+ // extra time per input.
2244+ let custom_plan = LogicalPlan :: Extension ( Extension {
2245+ node : Arc :: new ( NoopPlan {
2246+ input : vec ! [ table_scan. clone( ) ] ,
2247+ schema : Arc :: clone ( table_scan. schema ( ) ) ,
2248+ } ) ,
2249+ } ) ;
2250+ let plan = LogicalPlanBuilder :: from ( custom_plan)
2251+ . filter ( rand. clone ( ) . lt ( lit ( 0.5f64 ) ) ) ?
2252+ . build ( ) ?;
2253+
2254+ assert_optimized_plan_equal ! (
2255+ plan,
2256+ @r"
2257+ Filter: random() < Float64(0.5)
2258+ NoopPlan
2259+ TableScan: test
2260+ "
2261+ ) ?;
2262+
2263+ // A volatile predicate on a column that would otherwise be pushable
2264+ // also stays above the extension node.
2265+ let custom_plan = LogicalPlan :: Extension ( Extension {
2266+ node : Arc :: new ( NoopPlan {
2267+ input : vec ! [ table_scan. clone( ) ] ,
2268+ schema : Arc :: clone ( table_scan. schema ( ) ) ,
2269+ } ) ,
2270+ } ) ;
2271+ let plan = LogicalPlanBuilder :: from ( custom_plan)
2272+ . filter ( col ( "a" ) . eq ( rand. clone ( ) ) ) ?
2273+ . build ( ) ?;
2274+
2275+ assert_optimized_plan_equal ! (
2276+ plan,
2277+ @r"
2278+ Filter: test.a = random()
2279+ NoopPlan
2280+ TableScan: test
2281+ "
2282+ ) ?;
2283+
2284+ // Mixed conjunction: the deterministic predicate is pushed below the
2285+ // NoopPlan while the volatile predicate is kept above it.
2286+ let custom_plan = LogicalPlan :: Extension ( Extension {
2287+ node : Arc :: new ( NoopPlan {
2288+ input : vec ! [ table_scan. clone( ) ] ,
2289+ schema : Arc :: clone ( table_scan. schema ( ) ) ,
2290+ } ) ,
2291+ } ) ;
2292+ let plan = LogicalPlanBuilder :: from ( custom_plan)
2293+ . filter ( col ( "a" ) . eq ( lit ( 1i64 ) ) . and ( rand. clone ( ) . lt ( lit ( 0.5f64 ) ) ) ) ?
2294+ . build ( ) ?;
2295+
2296+ assert_optimized_plan_equal ! (
2297+ plan,
2298+ @r"
2299+ Filter: random() < Float64(0.5)
2300+ NoopPlan
2301+ TableScan: test, full_filters=[test.a = Int64(1)]
2302+ "
2303+ ) ?;
2304+
2305+ // Deterministic control: a predicate on a pushable column is still
2306+ // pushed below the NoopPlan.
2307+ let custom_plan = LogicalPlan :: Extension ( Extension {
2308+ node : Arc :: new ( NoopPlan {
2309+ input : vec ! [ table_scan. clone( ) ] ,
2310+ schema : Arc :: clone ( table_scan. schema ( ) ) ,
2311+ } ) ,
2312+ } ) ;
2313+ let plan = LogicalPlanBuilder :: from ( custom_plan)
2314+ . filter ( col ( "a" ) . eq ( lit ( 1i64 ) ) ) ?
2315+ . build ( ) ?;
2316+
2317+ assert_optimized_plan_equal ! (
2318+ plan,
2319+ @r"
2320+ NoopPlan
2321+ TableScan: test, full_filters=[test.a = Int64(1)]
2322+ "
2323+ )
2324+ }
2325+
2326+ #[ test]
2327+ fn aggregate_volatile_filters ( ) -> Result < ( ) > {
2328+ let rand = rand_func ( ) . call ( vec ! [ ] ) ;
2329+
2330+ // A volatile predicate that references no column stays above the
2331+ // aggregate: evaluating it per input row changes which rows each group
2332+ // aggregates instead of selecting whole groups.
2333+ let plan = LogicalPlanBuilder :: from ( test_table_scan ( ) ?)
2334+ . aggregate ( vec ! [ col( "a" ) ] , vec ! [ sum( col( "c" ) ) ] ) ?
2335+ . filter ( rand. clone ( ) . lt ( lit ( 0.5f64 ) ) ) ?
2336+ . build ( ) ?;
2337+
2338+ assert_optimized_plan_equal ! (
2339+ plan,
2340+ @r"
2341+ Filter: random() < Float64(0.5)
2342+ Aggregate: groupBy=[[test.a]], aggr=[[sum(test.c)]]
2343+ TableScan: test
2344+ "
2345+ ) ?;
2346+
2347+ // A volatile predicate on a grouping key stays above the aggregate.
2348+ let plan = LogicalPlanBuilder :: from ( test_table_scan ( ) ?)
2349+ . aggregate ( vec ! [ col( "a" ) ] , vec ! [ sum( col( "c" ) ) ] ) ?
2350+ . filter ( col ( "a" ) . eq ( rand. clone ( ) ) ) ?
2351+ . build ( ) ?;
2352+
2353+ assert_optimized_plan_equal ! (
2354+ plan,
2355+ @r"
2356+ Filter: test.a = random()
2357+ Aggregate: groupBy=[[test.a]], aggr=[[sum(test.c)]]
2358+ TableScan: test
2359+ "
2360+ ) ?;
2361+
2362+ // Mixed conjunction: the deterministic predicate on the grouping key
2363+ // is pushed below the aggregate, the volatile predicate is kept above.
2364+ let plan = LogicalPlanBuilder :: from ( test_table_scan ( ) ?)
2365+ . aggregate ( vec ! [ col( "a" ) ] , vec ! [ sum( col( "c" ) ) ] ) ?
2366+ . filter ( col ( "a" ) . eq ( lit ( 1i64 ) ) . and ( rand. clone ( ) . lt ( lit ( 0.5f64 ) ) ) ) ?
2367+ . build ( ) ?;
2368+
2369+ assert_optimized_plan_equal ! (
2370+ plan,
2371+ @r"
2372+ Filter: random() < Float64(0.5)
2373+ Aggregate: groupBy=[[test.a]], aggr=[[sum(test.c)]]
2374+ TableScan: test, full_filters=[test.a = Int64(1)]
2375+ "
2376+ ) ?;
2377+
2378+ // Deterministic control: a predicate on the grouping key is still
2379+ // pushed below the aggregate.
2380+ let plan = LogicalPlanBuilder :: from ( test_table_scan ( ) ?)
2381+ . aggregate ( vec ! [ col( "a" ) ] , vec ! [ sum( col( "c" ) ) ] ) ?
2382+ . filter ( col ( "a" ) . eq ( lit ( 1i64 ) ) ) ?
2383+ . build ( ) ?;
2384+
2385+ assert_optimized_plan_equal ! (
2386+ plan,
2387+ @r"
2388+ Aggregate: groupBy=[[test.a]], aggr=[[sum(test.c)]]
2389+ TableScan: test, full_filters=[test.a = Int64(1)]
2390+ "
2391+ )
2392+ }
2393+
21922394 /// verifies that when two filters apply after an aggregation that only allows one to be pushed, one is pushed
21932395 /// and the other not.
21942396 #[ test]
0 commit comments