Skip to content

Commit 642a812

Browse files
authored
Minor: avoid clone while calculating union equivalence properties (#12722)
* Minor: avoid clone while calculating union equivalence properties * Update datafusion/physical-expr/src/equivalence/properties.rs * fmt
1 parent 77f330c commit 642a812

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

datafusion/physical-expr/src/equivalence/properties.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::{
3434

3535
use arrow_schema::{SchemaRef, SortOptions};
3636
use datafusion_common::tree_node::{Transformed, TransformedResult, TreeNode};
37-
use datafusion_common::{plan_err, JoinSide, JoinType, Result};
37+
use datafusion_common::{internal_err, plan_err, JoinSide, JoinType, Result};
3838
use datafusion_expr::interval_arithmetic::Interval;
3939
use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
4040
use datafusion_physical_expr_common::utils::ExprPropertiesNode;
@@ -1677,14 +1677,22 @@ pub fn calculate_union(
16771677
) -> Result<EquivalenceProperties> {
16781678
// TODO: In some cases, we should be able to preserve some equivalence
16791679
// classes. Add support for such cases.
1680-
let mut init = eqps[0].clone();
1680+
let mut iter = eqps.into_iter();
1681+
let Some(mut acc) = iter.next() else {
1682+
return internal_err!(
1683+
"Cannot calculate EquivalenceProperties for a union with no inputs"
1684+
);
1685+
};
1686+
16811687
// Harmonize the schema of the init with the schema of the union:
1682-
if !init.schema.eq(&schema) {
1683-
init = init.with_new_schema(schema)?;
1688+
if !acc.schema.eq(&schema) {
1689+
acc = acc.with_new_schema(schema)?;
1690+
}
1691+
// Fold in the rest of the EquivalenceProperties:
1692+
for props in iter {
1693+
acc = calculate_union_binary(acc, props)?;
16841694
}
1685-
eqps.into_iter()
1686-
.skip(1)
1687-
.try_fold(init, calculate_union_binary)
1695+
Ok(acc)
16881696
}
16891697

16901698
#[cfg(test)]

0 commit comments

Comments
 (0)