@@ -56,7 +56,7 @@ use datafusion_common::{
5656use datafusion_execution:: TaskContext ;
5757use datafusion_expr:: Operator ;
5858use datafusion_physical_expr:: equivalence:: ProjectionMapping ;
59- use datafusion_physical_expr:: expressions:: { BinaryExpr , Column , lit} ;
59+ use datafusion_physical_expr:: expressions:: { BinaryExpr , Column , Literal , lit} ;
6060use datafusion_physical_expr:: intervals:: utils:: check_support;
6161use datafusion_physical_expr:: utils:: { collect_columns, reassign_expr_columns} ;
6262use datafusion_physical_expr:: {
@@ -358,18 +358,37 @@ impl FilterExec {
358358 if let Some ( binary) = conjunction. as_any ( ) . downcast_ref :: < BinaryExpr > ( )
359359 && binary. op ( ) == & Operator :: Eq
360360 {
361- // Filter evaluates to single value for all partitions
362- if input_eqs. is_expr_constant ( binary. left ( ) ) . is_some ( ) {
363- let across = input_eqs
364- . is_expr_constant ( binary. right ( ) )
365- . unwrap_or_default ( ) ;
361+ // Check if either side is constant — either already known
362+ // constant from the input equivalence properties, or a literal
363+ // value (which is inherently constant across all partitions).
364+ let left_const =
365+ input_eqs. is_expr_constant ( binary. left ( ) ) . or_else ( || {
366+ binary
367+ . left ( )
368+ . as_any ( )
369+ . downcast_ref :: < Literal > ( )
370+ . map ( |l| AcrossPartitions :: Uniform ( Some ( l. value ( ) . clone ( ) ) ) )
371+ } ) ;
372+ let right_const =
373+ input_eqs. is_expr_constant ( binary. right ( ) ) . or_else ( || {
374+ binary
375+ . right ( )
376+ . as_any ( )
377+ . downcast_ref :: < Literal > ( )
378+ . map ( |l| AcrossPartitions :: Uniform ( Some ( l. value ( ) . clone ( ) ) ) )
379+ } ) ;
380+
381+ if let Some ( left_across) = left_const {
382+ // LEFT is constant, so RIGHT must also be constant.
383+ // Use RIGHT's known across value if available, otherwise
384+ // propagate LEFT's (e.g. Uniform from a literal).
385+ let across = right_const. unwrap_or ( left_across) ;
366386 res_constants
367387 . push ( ConstExpr :: new ( Arc :: clone ( binary. right ( ) ) , across) ) ;
368- } else if input_eqs. is_expr_constant ( binary. right ( ) ) . is_some ( ) {
369- let across = input_eqs
370- . is_expr_constant ( binary. left ( ) )
371- . unwrap_or_default ( ) ;
372- res_constants. push ( ConstExpr :: new ( Arc :: clone ( binary. left ( ) ) , across) ) ;
388+ } else if let Some ( right_across) = right_const {
389+ // RIGHT is constant, so LEFT must also be constant.
390+ res_constants
391+ . push ( ConstExpr :: new ( Arc :: clone ( binary. left ( ) ) , right_across) ) ;
373392 }
374393 }
375394 }
@@ -977,6 +996,18 @@ fn collect_columns_from_predicate_inner(
977996 let predicates = split_conjunction ( predicate) ;
978997 predicates. into_iter ( ) . for_each ( |p| {
979998 if let Some ( binary) = p. as_any ( ) . downcast_ref :: < BinaryExpr > ( ) {
999+ // Only extract pairs where at least one side is a Column reference.
1000+ // Pairs like `complex_expr = literal` should not create equivalence
1001+ // classes — the literal could appear in many unrelated expressions
1002+ // (e.g. sort keys), and normalize_expr's deep traversal would
1003+ // replace those occurrences with the complex expression, corrupting
1004+ // sort orderings. Constant propagation for such pairs is handled
1005+ // separately by `extend_constants`.
1006+ let has_column = binary. left ( ) . as_any ( ) . downcast_ref :: < Column > ( ) . is_some ( )
1007+ || binary. right ( ) . as_any ( ) . downcast_ref :: < Column > ( ) . is_some ( ) ;
1008+ if !has_column {
1009+ return ;
1010+ }
9801011 match binary. op ( ) {
9811012 Operator :: Eq => {
9821013 eq_predicate_columns. push ( ( binary. left ( ) , binary. right ( ) ) )
@@ -2011,4 +2042,46 @@ mod tests {
20112042
20122043 Ok ( ( ) )
20132044 }
2045+
2046+ /// Regression test for https://github.com/apache/datafusion/issues/20194
2047+ ///
2048+ /// `collect_columns_from_predicate_inner` should only extract equality
2049+ /// pairs where at least one side is a Column. Pairs like
2050+ /// `complex_expr = literal` must not create equivalence classes because
2051+ /// `normalize_expr`'s deep traversal would replace the literal inside
2052+ /// unrelated expressions (e.g. sort keys) with the complex expression.
2053+ #[ tokio:: test]
2054+ async fn test_collect_columns_skips_non_column_pairs ( ) -> Result < ( ) > {
2055+ let schema = test:: aggr_test_schema ( ) ;
2056+
2057+ // Simulate: nvl(c2, 0) = 0 → (c2 IS DISTINCT FROM 0) = 0
2058+ // Neither side is a Column, so this should NOT be extracted.
2059+ let complex_expr: Arc < dyn PhysicalExpr > = binary (
2060+ col ( "c2" , & schema) ?,
2061+ Operator :: IsDistinctFrom ,
2062+ lit ( 0u32 ) ,
2063+ & schema,
2064+ ) ?;
2065+ let predicate: Arc < dyn PhysicalExpr > =
2066+ binary ( complex_expr, Operator :: Eq , lit ( 0u32 ) , & schema) ?;
2067+
2068+ let ( equal_pairs, _) = collect_columns_from_predicate_inner ( & predicate) ;
2069+ assert_eq ! (
2070+ 0 ,
2071+ equal_pairs. len( ) ,
2072+ "Should not extract equality pairs where neither side is a Column"
2073+ ) ;
2074+
2075+ // But col = literal should still be extracted
2076+ let predicate: Arc < dyn PhysicalExpr > =
2077+ binary ( col ( "c2" , & schema) ?, Operator :: Eq , lit ( 0u32 ) , & schema) ?;
2078+ let ( equal_pairs, _) = collect_columns_from_predicate_inner ( & predicate) ;
2079+ assert_eq ! (
2080+ 1 ,
2081+ equal_pairs. len( ) ,
2082+ "Should extract equality pairs where one side is a Column"
2083+ ) ;
2084+
2085+ Ok ( ( ) )
2086+ }
20142087}
0 commit comments