Apache DataFusion SQL Query Engine
Fix `PartialOrd` for logical plan nodes and expressions (#17438)
Piotr Findeisen0c7d8307a8c4
diff --git a/datafusion/expr/src/expr.rs b/datafusion/expr/src/expr.rsindex f2493abbde..88024066c6 100644--- a/datafusion/expr/src/expr.rs+++ b/datafusion/expr/src/expr.rs@@ -748,7 +748,10 @@ impl PartialOrd for Alias {let Some(Ordering::Equal) = cmp else {return cmp;};- self.name.partial_cmp(&other.name)+ self.name+ .partial_cmp(&other.name)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}diff --git a/datafusion/expr/src/logical_plan/ddl.rs b/datafusion/expr/src/logical_plan/ddl.rsindex 827e2812ec..d477ca1ad0 100644--- a/datafusion/expr/src/logical_plan/ddl.rs+++ b/datafusion/expr/src/logical_plan/ddl.rs@@ -292,7 +292,10 @@ impl PartialOrd for CreateExternalTable {unbounded: &other.unbounded,constraints: &other.constraints,};- comparable_self.partial_cmp(&comparable_other)+ comparable_self+ .partial_cmp(&comparable_other)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -348,6 +351,8 @@ impl PartialOrd for CreateCatalog {Some(Ordering::Equal) => self.if_not_exists.partial_cmp(&other.if_not_exists),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -369,6 +374,8 @@ impl PartialOrd for CreateCatalogSchema {Some(Ordering::Equal) => self.if_not_exists.partial_cmp(&other.if_not_exists),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -390,6 +397,8 @@ impl PartialOrd for DropTable {Some(Ordering::Equal) => self.if_exists.partial_cmp(&other.if_exists),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -411,6 +420,8 @@ impl PartialOrd for DropView {Some(Ordering::Equal) => self.if_exists.partial_cmp(&other.if_exists),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -437,6 +448,8 @@ impl PartialOrd for DropCatalogSchema {},cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -486,7 +499,10 @@ impl PartialOrd for CreateFunction {return_type: &other.return_type,params: &other.params,};- comparable_self.partial_cmp(&comparable_other)+ comparable_self+ .partial_cmp(&comparable_other)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -566,6 +582,8 @@ impl PartialOrd for DropFunction {Some(Ordering::Equal) => self.if_exists.partial_cmp(&other.if_exists),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -608,7 +626,10 @@ impl PartialOrd for CreateIndex {unique: &other.unique,if_not_exists: &other.if_not_exists,};- comparable_self.partial_cmp(&comparable_other)+ comparable_self+ .partial_cmp(&comparable_other)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}diff --git a/datafusion/expr/src/logical_plan/dml.rs b/datafusion/expr/src/logical_plan/dml.rsindex 369b91e204..b8448a5da6 100644--- a/datafusion/expr/src/logical_plan/dml.rs+++ b/datafusion/expr/src/logical_plan/dml.rs@@ -81,6 +81,8 @@ impl PartialOrd for CopyTo {},cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -217,6 +219,8 @@ impl PartialOrd for DmlStatement {},cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}diff --git a/datafusion/expr/src/logical_plan/extension.rs b/datafusion/expr/src/logical_plan/extension.rsindex e10b74098b..a8ee788564 100644--- a/datafusion/expr/src/logical_plan/extension.rs+++ b/datafusion/expr/src/logical_plan/extension.rs@@ -150,7 +150,7 @@ pub trait UserDefinedLogicalNode: fmt::Debug + Send + Sync {/// directly because it must remain object safe.fn dyn_hash(&self, state: &mut dyn Hasher);- /// Compare `other`, respecting requirements from [std::cmp::Eq].+ /// Compare `other`, respecting requirements from [Eq].////// Note: consider using [`UserDefinedLogicalNodeCore`] instead of/// [`UserDefinedLogicalNode`] directly.@@ -188,6 +188,9 @@ pub trait UserDefinedLogicalNode: fmt::Debug + Send + Sync {/// Note: [`UserDefinedLogicalNode`] is not constrained by [`Eq`]/// directly because it must remain object safe.fn dyn_eq(&self, other: &dyn UserDefinedLogicalNode) -> bool;++ /// Compare `other`, respecting requirements from [PartialOrd].+ /// Must return `Some(Equal)` if and only if `self.dyn_eq(other)`.fn dyn_ord(&self, other: &dyn UserDefinedLogicalNode) -> Option<Ordering>;/// Returns `true` if a limit can be safely pushed down through this@@ -312,7 +315,7 @@ pub trait UserDefinedLogicalNodeCore:}/// Automatically derive UserDefinedLogicalNode to `UserDefinedLogicalNode`-/// to avoid boiler plate for implementing `as_any`, `Hash` and `PartialEq`+/// to avoid boiler plate for implementing `as_any`, `Hash`, `PartialEq` and `PartialOrd`.impl<T: UserDefinedLogicalNodeCore> UserDefinedLogicalNode for T {fn as_any(&self) -> &dyn Any {selfdiff --git a/datafusion/expr/src/logical_plan/plan.rs b/datafusion/expr/src/logical_plan/plan.rsindex b4f2902cc4..03d60084ee 100644--- a/datafusion/expr/src/logical_plan/plan.rs+++ b/datafusion/expr/src/logical_plan/plan.rs@@ -2060,7 +2060,10 @@ pub struct EmptyRelation {// Manual implementation needed because of `schema` field. Comparison excludes this field.impl PartialOrd for EmptyRelation {fn partial_cmp(&self, other: &Self) -> Option<Ordering> {- self.produce_one_row.partial_cmp(&other.produce_one_row)+ self.produce_one_row+ .partial_cmp(&other.produce_one_row)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -2114,7 +2117,10 @@ pub struct Values {// Manual implementation needed because of `schema` field. Comparison excludes this field.impl PartialOrd for Values {fn partial_cmp(&self, other: &Self) -> Option<Ordering> {- self.values.partial_cmp(&other.values)+ self.values+ .partial_cmp(&other.values)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -2139,6 +2145,8 @@ impl PartialOrd for Projection {Some(Ordering::Equal) => self.input.partial_cmp(&other.input),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -2249,6 +2257,8 @@ impl PartialOrd for SubqueryAlias {Some(Ordering::Equal) => self.alias.partial_cmp(&other.alias),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -2606,7 +2616,10 @@ impl PartialOrd for TableScan {filters: &other.filters,fetch: &other.fetch,};- comparable_self.partial_cmp(&comparable_other)+ comparable_self+ .partial_cmp(&comparable_other)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -2929,7 +2942,10 @@ impl Union {// Manual implementation needed because of `schema` field. Comparison excludes this field.impl PartialOrd for Union {fn partial_cmp(&self, other: &Self) -> Option<Ordering> {- self.inputs.partial_cmp(&other.inputs)+ self.inputs+ .partial_cmp(&other.inputs)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -3210,7 +3226,10 @@ impl PartialOrd for Explain {stringified_plans: &other.stringified_plans,logical_optimization_succeeded: &other.logical_optimization_succeeded,};- comparable_self.partial_cmp(&comparable_other)+ comparable_self+ .partial_cmp(&comparable_other)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -3233,6 +3252,8 @@ impl PartialOrd for Analyze {Some(Ordering::Equal) => self.input.partial_cmp(&other.input),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -3460,7 +3481,10 @@ impl PartialOrd for DistinctOn {sort_expr: &other.sort_expr,input: &other.input,};- comparable_self.partial_cmp(&comparable_other)+ comparable_self+ .partial_cmp(&comparable_other)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -3647,6 +3671,8 @@ impl PartialOrd for Aggregate {}cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -3919,7 +3945,10 @@ impl PartialOrd for Join {join_constraint: &other.join_constraint,null_equality: &other.null_equality,};- comparable_self.partial_cmp(&comparable_other)+ comparable_self+ .partial_cmp(&comparable_other)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -4084,7 +4113,10 @@ impl PartialOrd for Unnest {dependency_indices: &other.dependency_indices,options: &other.options,};- comparable_self.partial_cmp(&comparable_other)+ comparable_self+ .partial_cmp(&comparable_other)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}diff --git a/datafusion/expr/src/udaf.rs b/datafusion/expr/src/udaf.rsindex 9b6e97bef9..2243682cfd 100644--- a/datafusion/expr/src/udaf.rs+++ b/datafusion/expr/src/udaf.rs@@ -766,16 +766,14 @@ impl PartialEq for dyn AggregateUDFImpl {}}-// TODO (https://github.com/apache/datafusion/issues/17064) PartialOrd is not consistent with PartialEq for `dyn AggregateUDFImpl` and it should be-// Manual implementation of `PartialOrd`-// There might be some wackiness with it, but this is based on the impl of eq for AggregateUDFImpl-// https://users.rust-lang.org/t/how-to-compare-two-trait-objects-for-equality/88063/5impl PartialOrd for dyn AggregateUDFImpl {fn partial_cmp(&self, other: &Self) -> Option<Ordering> {match self.name().partial_cmp(other.name()) {Some(Ordering::Equal) => self.signature().partial_cmp(other.signature()),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}diff --git a/datafusion/expr/src/udwf.rs b/datafusion/expr/src/udwf.rsindex 29bf379f60..a7b4302dd8 100644--- a/datafusion/expr/src/udwf.rs+++ b/datafusion/expr/src/udwf.rs@@ -434,13 +434,14 @@ impl PartialEq for dyn WindowUDFImpl {}}-// TODO (https://github.com/apache/datafusion/issues/17064) PartialOrd is not consistent with PartialEq for `dyn WindowUDFImpl` and it should beimpl PartialOrd for dyn WindowUDFImpl {fn partial_cmp(&self, other: &Self) -> Option<Ordering> {match self.name().partial_cmp(other.name()) {Some(Ordering::Equal) => self.signature().partial_cmp(other.signature()),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}diff --git a/datafusion/optimizer/src/optimize_projections/mod.rs b/datafusion/optimizer/src/optimize_projections/mod.rsindex 97402c990b..1e9c40a5d7 100644--- a/datafusion/optimizer/src/optimize_projections/mod.rs+++ b/datafusion/optimizer/src/optimize_projections/mod.rs@@ -902,6 +902,8 @@ mod tests {Some(Ordering::Equal) => self.input.partial_cmp(&other.input),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -989,6 +991,8 @@ mod tests {}cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}diff --git a/datafusion/optimizer/src/push_down_filter.rs b/datafusion/optimizer/src/push_down_filter.rsindex 27c2499c8a..5a3d57d65a 100644--- a/datafusion/optimizer/src/push_down_filter.rs+++ b/datafusion/optimizer/src/push_down_filter.rs@@ -1966,7 +1966,10 @@ mod tests {// Manual implementation needed because of `schema` field. Comparison excludes this field.impl PartialOrd for NoopPlan {fn partial_cmp(&self, other: &Self) -> Option<Ordering> {- self.input.partial_cmp(&other.input)+ self.input+ .partial_cmp(&other.input)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}diff --git a/datafusion/optimizer/src/push_down_limit.rs b/datafusion/optimizer/src/push_down_limit.rsindex ec042dd350..c5a2e65788 100644--- a/datafusion/optimizer/src/push_down_limit.rs+++ b/datafusion/optimizer/src/push_down_limit.rs@@ -312,7 +312,10 @@ mod test {// Manual implementation needed because of `schema` field. Comparison excludes this field.impl PartialOrd for NoopPlan {fn partial_cmp(&self, other: &Self) -> Option<Ordering> {- self.input.partial_cmp(&other.input)+ self.input+ .partial_cmp(&other.input)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}@@ -365,7 +368,10 @@ mod test {// Manual implementation needed because of `schema` field. Comparison excludes this field.impl PartialOrd for NoLimitNoopPlan {fn partial_cmp(&self, other: &Self) -> Option<Ordering> {- self.input.partial_cmp(&other.input)+ self.input+ .partial_cmp(&other.input)+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}diff --git a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rsindex 606d32ee4d..616dc917ef 100644--- a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs+++ b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs@@ -92,6 +92,8 @@ impl PartialOrd for MockUserDefinedLogicalPlan {Some(Ordering::Equal) => self.inputs.partial_cmp(&other.inputs),cmp => cmp,}+ // TODO (https://github.com/apache/datafusion/issues/17477) avoid recomparing all fields+ .filter(|cmp| *cmp != Ordering::Equal || self == other)}}