Apache DataFusion SQL Query Engine
Unimplement PartialOrd for TDigest's Centroid (#17440)
Piotr Findeisendf5a09c75b00
diff --git a/datafusion/functions-aggregate-common/src/merge_arrays.rs b/datafusion/functions-aggregate-common/src/merge_arrays.rsindex 0cfea66249..c6989bc010 100644--- a/datafusion/functions-aggregate-common/src/merge_arrays.rs+++ b/datafusion/functions-aggregate-common/src/merge_arrays.rs@@ -67,6 +67,7 @@ impl<'a> CustomElement<'a> {// - When used inside `BinaryHeap` it is a min-heap.impl Ord for CustomElement<'_> {fn cmp(&self, other: &Self) -> Ordering {+ // TODO Ord/PartialOrd is not consistent with PartialEq; PartialOrd contract is violated// Compares according to custom orderingself.ordering(&self.ordering, &other.ordering)// Convert max heap to min heapdiff --git a/datafusion/functions-aggregate-common/src/tdigest.rs b/datafusion/functions-aggregate-common/src/tdigest.rsindex 378fc8c42b..38a9292cea 100644--- a/datafusion/functions-aggregate-common/src/tdigest.rs+++ b/datafusion/functions-aggregate-common/src/tdigest.rs@@ -103,20 +103,6 @@ pub struct Centroid {weight: f64,}-impl PartialOrd for Centroid {- fn partial_cmp(&self, other: &Centroid) -> Option<Ordering> {- Some(self.cmp(other))- }-}--impl Eq for Centroid {}--impl Ord for Centroid {- fn cmp(&self, other: &Centroid) -> Ordering {- self.mean.total_cmp(&other.mean)- }-}-impl Centroid {pub fn new(mean: f64, weight: f64) -> Self {Centroid { mean, weight }@@ -139,6 +125,10 @@ impl Centroid {self.mean = new_sum / new_weight;new_sum}++ pub fn cmp_mean(&self, other: &Self) -> Ordering {+ self.mean.total_cmp(&other.mean)+ }}impl Default for Centroid {@@ -331,7 +321,7 @@ impl TDigest {result.sum += curr.add(sums_to_merge, weights_to_merge);compressed.push(curr);compressed.shrink_to_fit();- compressed.sort();+ compressed.sort_by(|a, b| a.cmp_mean(b));result.centroids = compressed;result@@ -349,7 +339,7 @@ impl TDigest {let mut j = middle;while i < middle && j < last {- match centroids[i].cmp(¢roids[j]) {+ match centroids[i].cmp_mean(¢roids[j]) {Ordering::Less => {result.push(centroids[i].clone());i += 1;@@ -466,7 +456,7 @@ impl TDigest {result.sum += curr.add(sums_to_merge, weights_to_merge);compressed.push(curr.clone());compressed.shrink_to_fit();- compressed.sort();+ compressed.sort_by(|a, b| a.cmp_mean(b));result.count = count;result.min = min;diff --git a/datafusion/physical-plan/src/topk/mod.rs b/datafusion/physical-plan/src/topk/mod.rsindex 6c0479a108..ce977b0706 100644--- a/datafusion/physical-plan/src/topk/mod.rs+++ b/datafusion/physical-plan/src/topk/mod.rs@@ -940,6 +940,7 @@ impl Eq for TopKRow {}impl PartialOrd for TopKRow {fn partial_cmp(&self, other: &Self) -> Option<Ordering> {+ // TODO PartialOrd is not consistent with PartialEq; PartialOrd contract is violatedSome(self.cmp(other))}}