@@ -30,7 +30,6 @@ use datafusion_common::tree_node::{Transformed, TransformedResult, TreeNode};
30
30
use datafusion_common:: JoinType ;
31
31
use datafusion_physical_expr_common:: physical_expr:: format_physical_expr_list;
32
32
33
- #[ derive( Debug , Clone ) ]
34
33
/// A structure representing a expression known to be constant in a physical execution plan.
35
34
///
36
35
/// The `ConstExpr` struct encapsulates an expression that is constant during the execution
@@ -41,9 +40,10 @@ use datafusion_physical_expr_common::physical_expr::format_physical_expr_list;
41
40
///
42
41
/// - `expr`: Constant expression for a node in the physical plan.
43
42
///
44
- /// - `across_partitions`: A boolean flag indicating whether the constant expression is
45
- /// valid across partitions. If set to `true`, the constant expression has same value for all partitions.
46
- /// If set to `false`, the constant expression may have different values for different partitions.
43
+ /// - `across_partitions`: A boolean flag indicating whether the constant
44
+ /// expression is the same across partitions. If set to `true`, the constant
45
+ /// expression has same value for all partitions. If set to `false`, the
46
+ /// constant expression may have different values for different partitions.
47
47
///
48
48
/// # Example
49
49
///
@@ -56,11 +56,22 @@ use datafusion_physical_expr_common::physical_expr::format_physical_expr_list;
56
56
/// // create a constant expression from a physical expression
57
57
/// let const_expr = ConstExpr::from(col);
58
58
/// ```
59
+ #[ derive( Debug , Clone ) ]
59
60
pub struct ConstExpr {
61
+ /// The expression that is known to be constant (e.g. a `Column`)
60
62
expr : Arc < dyn PhysicalExpr > ,
63
+ /// Does the constant have the same value across all partitions? See
64
+ /// struct docs for more details
61
65
across_partitions : bool ,
62
66
}
63
67
68
+ impl PartialEq for ConstExpr {
69
+ fn eq ( & self , other : & Self ) -> bool {
70
+ self . across_partitions == other. across_partitions
71
+ && self . expr . eq ( other. expr . as_any ( ) )
72
+ }
73
+ }
74
+
64
75
impl ConstExpr {
65
76
/// Create a new constant expression from a physical expression.
66
77
///
@@ -74,11 +85,17 @@ impl ConstExpr {
74
85
}
75
86
}
76
87
88
+ /// Set the `across_partitions` flag
89
+ ///
90
+ /// See struct docs for more details
77
91
pub fn with_across_partitions ( mut self , across_partitions : bool ) -> Self {
78
92
self . across_partitions = across_partitions;
79
93
self
80
94
}
81
95
96
+ /// Is the expression the same across all partitions?
97
+ ///
98
+ /// See struct docs for more details
82
99
pub fn across_partitions ( & self ) -> bool {
83
100
self . across_partitions
84
101
}
@@ -101,6 +118,31 @@ impl ConstExpr {
101
118
across_partitions : self . across_partitions ,
102
119
} )
103
120
}
121
+
122
+ /// Returns true if this constant expression is equal to the given expression
123
+ pub fn eq_expr ( & self , other : impl AsRef < dyn PhysicalExpr > ) -> bool {
124
+ self . expr . eq ( other. as_ref ( ) . as_any ( ) )
125
+ }
126
+
127
+ /// Returns a [`Display`]able list of `ConstExpr`.
128
+ pub fn format_list ( input : & [ ConstExpr ] ) -> impl Display + ' _ {
129
+ struct DisplayableList < ' a > ( & ' a [ ConstExpr ] ) ;
130
+ impl < ' a > Display for DisplayableList < ' a > {
131
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
132
+ let mut first = true ;
133
+ for const_expr in self . 0 {
134
+ if first {
135
+ first = false ;
136
+ } else {
137
+ write ! ( f, "," ) ?;
138
+ }
139
+ write ! ( f, "{}" , const_expr) ?;
140
+ }
141
+ Ok ( ( ) )
142
+ }
143
+ }
144
+ DisplayableList ( input)
145
+ }
104
146
}
105
147
106
148
/// Display implementation for `ConstExpr`
0 commit comments