@@ -34,7 +34,7 @@ use crate::{
3434
3535use arrow_schema:: { SchemaRef , SortOptions } ;
3636use 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 } ;
3838use datafusion_expr:: interval_arithmetic:: Interval ;
3939use datafusion_expr:: sort_properties:: { ExprProperties , SortProperties } ;
4040use 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