Skip to content

Apache DataFusion SQL Query Engine

Unimplement `PartialEq`, `PartialOrd` from `ToRepartition`, `RePartition` (#17441)

Piotr Findeisen9b82a1e5f1e9
diff --git a/datafusion/datasource/src/file_groups.rs b/datafusion/datasource/src/file_groups.rs
index 8bfadbef77..f1f248c8c8 100644
--- a/datafusion/datasource/src/file_groups.rs
+++ b/datafusion/datasource/src/file_groups.rs
@@ -20,11 +20,11 @@
use crate::{FileRange, PartitionedFile};
use datafusion_common::Statistics;
use itertools::Itertools;
-use std::cmp::min;
+use std::cmp::{min, Ordering};
use std::collections::BinaryHeap;
use std::iter::repeat_with;
use std::mem;
-use std::ops::{Index, IndexMut};
+use std::ops::{Deref, DerefMut, Index, IndexMut};
use std::sync::Arc;
/// Repartition input files into `target_partitions` partitions, if total file size exceed
@@ -304,6 +304,7 @@ impl FileGroupPartitioner {
None
}
})
+ .map(CompareByRangeSize)
.collect();
// No files can be redistributed
@@ -337,7 +338,7 @@ impl FileGroupPartitioner {
source_index,
file_size,
new_groups,
- } = to_repartition;
+ } = to_repartition.into_inner();
assert_eq!(file_groups[source_index].len(), 1);
let original_file = file_groups[source_index].pop().unwrap();
@@ -503,7 +504,7 @@ impl Default for FileGroup {
}
/// Tracks how a individual file will be repartitioned
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Debug, Clone)]
struct ToRepartition {
/// the index from which the original file will be taken
source_index: usize,
@@ -520,16 +521,38 @@ impl ToRepartition {
}
}
-impl PartialOrd for ToRepartition {
- fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
+struct CompareByRangeSize(ToRepartition);
+impl CompareByRangeSize {
+ fn into_inner(self) -> ToRepartition {
+ self.0
+ }
+}
+impl Ord for CompareByRangeSize {
+ fn cmp(&self, other: &Self) -> Ordering {
+ self.0.range_size().cmp(&other.0.range_size())
+ }
+}
+impl PartialOrd for CompareByRangeSize {
+ fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
-
-/// Order based on individual range
-impl Ord for ToRepartition {
- fn cmp(&self, other: &Self) -> std::cmp::Ordering {
- self.range_size().cmp(&other.range_size())
+impl PartialEq for CompareByRangeSize {
+ fn eq(&self, other: &Self) -> bool {
+ // PartialEq must be consistent with PartialOrd
+ self.cmp(other) == Ordering::Equal
+ }
+}
+impl Eq for CompareByRangeSize {}
+impl Deref for CompareByRangeSize {
+ type Target = ToRepartition;
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+impl DerefMut for CompareByRangeSize {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.0
}
}
diff --git a/datafusion/datasource/src/memory.rs b/datafusion/datasource/src/memory.rs
index f83af7aa06..564033438f 100644
--- a/datafusion/datasource/src/memory.rs
+++ b/datafusion/datasource/src/memory.rs
@@ -20,6 +20,7 @@ use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::fmt;
use std::fmt::Debug;
+use std::ops::Deref;
use std::sync::Arc;
use crate::sink::DataSink;
@@ -498,7 +499,7 @@ impl MemorySourceConfig {
// by count of rows.
let mut max_heap = BinaryHeap::with_capacity(target_partitions);
for rep in to_repartition {
- max_heap.push(rep);
+ max_heap.push(CompareByRowCount(rep));
}
// Split the largest partitions into smaller partitions. Maintaining the output
@@ -514,10 +515,10 @@ impl MemorySourceConfig {
};
// Split the partition. The new partitions will be ordered with idx and idx+1.
- let mut new_partitions = to_split.split();
+ let mut new_partitions = to_split.into_inner().split();
if new_partitions.len() > 1 {
for new_partition in new_partitions {
- max_heap.push(new_partition);
+ max_heap.push(CompareByRowCount(new_partition));
}
// Successful repartition. Break inner loop, and return to outer `cnt_to_repartition` loop.
break;
@@ -526,7 +527,10 @@ impl MemorySourceConfig {
}
}
}
- let mut partitions = max_heap.drain().collect_vec();
+ let mut partitions = max_heap
+ .drain()
+ .map(CompareByRowCount::into_inner)
+ .collect_vec();
partitions.extend(cannot_split_further);
// Finally, sort all partitions by the output ordering.
@@ -648,26 +652,6 @@ impl RePartition {
}
}
-impl PartialOrd for RePartition {
- fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
- Some(self.cmp(other))
- }
-}
-
-impl Ord for RePartition {
- fn cmp(&self, other: &Self) -> Ordering {
- self.row_count.cmp(&other.row_count)
- }
-}
-
-impl PartialEq for RePartition {
- fn eq(&self, other: &Self) -> bool {
- self.row_count.eq(&other.row_count)
- }
-}
-
-impl Eq for RePartition {}
-
impl fmt::Display for RePartition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
@@ -680,6 +664,36 @@ impl fmt::Display for RePartition {
}
}
+struct CompareByRowCount(RePartition);
+impl CompareByRowCount {
+ fn into_inner(self) -> RePartition {
+ self.0
+ }
+}
+impl Ord for CompareByRowCount {
+ fn cmp(&self, other: &Self) -> Ordering {
+ self.0.row_count.cmp(&other.0.row_count)
+ }
+}
+impl PartialOrd for CompareByRowCount {
+ fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+ Some(self.cmp(other))
+ }
+}
+impl PartialEq for CompareByRowCount {
+ fn eq(&self, other: &Self) -> bool {
+ // PartialEq must be consistent with PartialOrd
+ self.cmp(other) == Ordering::Equal
+ }
+}
+impl Eq for CompareByRowCount {}
+impl Deref for CompareByRowCount {
+ type Target = RePartition;
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
/// Type alias for partition data
pub type PartitionData = Arc<RwLock<Vec<RecordBatch>>>;