@@ -883,9 +883,11 @@ impl EquivalenceProperties {
883883 if self . is_expr_constant ( source)
884884 && !const_exprs_contains ( & projected_constants, target)
885885 {
886+ let across_partitions = self . is_expr_constant_accross_partitions ( source) ;
886887 // Expression evaluates to single value
887- projected_constants
888- . push ( ConstExpr :: from ( target) . with_across_partitions ( true ) ) ;
888+ projected_constants. push (
889+ ConstExpr :: from ( target) . with_across_partitions ( across_partitions) ,
890+ ) ;
889891 }
890892 }
891893 projected_constants
@@ -1014,6 +1016,37 @@ impl EquivalenceProperties {
10141016 is_constant_recurse ( & normalized_constants, & normalized_expr)
10151017 }
10161018
1019+ /// This function determines whether the provided expression is constant
1020+ /// across partitions based on the known constants.
1021+ ///
1022+ /// # Arguments
1023+ ///
1024+ /// - `expr`: A reference to a `Arc<dyn PhysicalExpr>` representing the
1025+ /// expression to be checked.
1026+ ///
1027+ /// # Returns
1028+ ///
1029+ /// Returns `true` if the expression is constant across all partitions according
1030+ /// to equivalence group, `false` otherwise.
1031+ pub fn is_expr_constant_accross_partitions (
1032+ & self ,
1033+ expr : & Arc < dyn PhysicalExpr > ,
1034+ ) -> bool {
1035+ // As an example, assume that we know columns `a` and `b` are constant.
1036+ // Then, `a`, `b` and `a + b` will all return `true` whereas `c` will
1037+ // return `false`.
1038+ let const_exprs = self . constants . iter ( ) . flat_map ( |const_expr| {
1039+ if const_expr. across_partitions ( ) {
1040+ Some ( Arc :: clone ( const_expr. expr ( ) ) )
1041+ } else {
1042+ None
1043+ }
1044+ } ) ;
1045+ let normalized_constants = self . eq_group . normalize_exprs ( const_exprs) ;
1046+ let normalized_expr = self . eq_group . normalize_expr ( Arc :: clone ( expr) ) ;
1047+ is_constant_recurse ( & normalized_constants, & normalized_expr)
1048+ }
1049+
10171050 /// Retrieves the properties for a given physical expression.
10181051 ///
10191052 /// This function constructs an [`ExprProperties`] object for the given
0 commit comments