Skip to content

Apache DataFusion SQL Query Engine

Simplify HashJoin memory accounting (#6170)

Raphael Taylor-Daviescd82cac5b7d0
diff --git a/datafusion/core/src/physical_plan/joins/hash_join.rs b/datafusion/core/src/physical_plan/joins/hash_join.rs
index 05a3c25243..d2759ac75b 100644
--- a/datafusion/core/src/physical_plan/joins/hash_join.rs
+++ b/datafusion/core/src/physical_plan/joins/hash_join.rs
@@ -51,16 +51,12 @@ use std::task::Poll;
use std::{any::Any, usize, vec};
use datafusion_common::cast::{as_dictionary_array, as_string_array};
+use datafusion_execution::memory_pool::MemoryReservation;
use crate::arrow::array::BooleanBufferBuilder;
use crate::arrow::datatypes::TimeUnit;
use crate::error::{DataFusionError, Result};
-use crate::execution::{
- context::TaskContext,
- memory_pool::{
- MemoryConsumer, SharedMemoryReservation, SharedOptionalMemoryReservation, TryGrow,
- },
-};
+use crate::execution::{context::TaskContext, memory_pool::MemoryConsumer};
use crate::logical_expr::JoinType;
use crate::physical_plan::joins::utils::{
adjust_indices_by_join_type, apply_join_filter_to_indices, build_batch_from_indices,
@@ -89,7 +85,7 @@ use super::{
};
use crate::physical_plan::joins::hash_join_utils::JoinHashMap;
-type JoinLeftData = (JoinHashMap, RecordBatch);
+type JoinLeftData = (JoinHashMap, RecordBatch, MemoryReservation);
/// Join execution plan executes partitions in parallel and combines them into a set of
/// partitions.
@@ -113,8 +109,6 @@ pub struct HashJoinExec {
schema: SchemaRef,
/// Build-side data
left_fut: OnceAsync<JoinLeftData>,
- /// Operator-level memory reservation for left data
- reservation: SharedOptionalMemoryReservation,
/// Shares the `RandomState` for the hashing algorithm
random_state: RandomState,
/// Partitioning mode to use
@@ -163,7 +157,6 @@ impl HashJoinExec {
join_type: *join_type,
schema: Arc::new(schema),
left_fut: Default::default(),
- reservation: Default::default(),
random_state,
mode: partition_mode,
metrics: ExecutionPlanMetricsSet::new(),
@@ -362,26 +355,10 @@ impl ExecutionPlan for HashJoinExec {
}
let join_metrics = BuildProbeJoinMetrics::new(partition, &self.metrics);
-
- // Initialization of operator-level reservation
- self.reservation
- .initialize("HashJoinExec", context.memory_pool());
-
- // Inititalization of stream-level reservation
- let reservation = SharedMemoryReservation::from(
- MemoryConsumer::new(format!("HashJoinStream[{partition}]"))
- .register(context.memory_pool()),
- );
-
- // Memory reservation for left-side data depends on PartitionMode:
- // - operator-level for `CollectLeft` mode
- // - stream-level for partitioned mode
- //
- // This approach allows to avoid cases when left data could potentially
- // outlive its memory reservation and rely on `MemoryReservation` destructors
- // for releasing memory in pool.
let left_fut = match self.mode {
PartitionMode::CollectLeft => self.left_fut.once(|| {
+ let reservation =
+ MemoryConsumer::new("HashJoinInput").register(context.memory_pool());
collect_left_input(
None,
self.random_state.clone(),
@@ -389,18 +366,24 @@ impl ExecutionPlan for HashJoinExec {
on_left.clone(),
context.clone(),
join_metrics.clone(),
- Arc::new(self.reservation.clone()),
+ reservation,
)
}),
- PartitionMode::Partitioned => OnceFut::new(collect_left_input(
- Some(partition),
- self.random_state.clone(),
- self.left.clone(),
- on_left.clone(),
- context.clone(),
- join_metrics.clone(),
- Arc::new(reservation.clone()),
- )),
+ PartitionMode::Partitioned => {
+ let reservation =
+ MemoryConsumer::new(format!("HashJoinInput[{partition}]"))
+ .register(context.memory_pool());
+
+ OnceFut::new(collect_left_input(
+ Some(partition),
+ self.random_state.clone(),
+ self.left.clone(),
+ on_left.clone(),
+ context.clone(),
+ join_metrics.clone(),
+ reservation,
+ ))
+ }
PartitionMode::Auto => {
return Err(DataFusionError::Plan(format!(
"Invalid HashJoinExec, unsupported PartitionMode {:?} in execute()",
@@ -409,6 +392,9 @@ impl ExecutionPlan for HashJoinExec {
}
};
+ let reservation = MemoryConsumer::new(format!("HashJoinStream[{partition}]"))
+ .register(context.memory_pool());
+
// we have the batches and the hash map with their keys. We can how create a stream
// over the right that uses this information to issue new batches.
let right_stream = self.right.execute(partition, context)?;
@@ -471,7 +457,7 @@ async fn collect_left_input(
on_left: Vec<Column>,
context: Arc<TaskContext>,
metrics: BuildProbeJoinMetrics,
- reservation: Arc<dyn TryGrow>,
+ reservation: MemoryReservation,
) -> Result<JoinLeftData> {
let schema = left.schema();
@@ -496,7 +482,7 @@ async fn collect_left_input(
// 1. creates a [JoinHashMap] of all batches from the stream
// 2. stores the batches in a vector.
let initial = (Vec::new(), 0, metrics, reservation);
- let (batches, num_rows, metrics, reservation) = stream
+ let (batches, num_rows, metrics, mut reservation) = stream
.try_fold(initial, |mut acc, batch| async {
let batch_size = batch.get_array_memory_size();
// Reserve memory for incoming batch
@@ -552,7 +538,7 @@ async fn collect_left_input(
// can directly index into the arrays
let single_batch = concat_batches(&schema, &batches, num_rows)?;
- Ok((hashmap, single_batch))
+ Ok((hashmap, single_batch, reservation))
}
/// Updates `hash` with new entries from [RecordBatch] evaluated against the expressions `on`,
@@ -621,7 +607,7 @@ struct HashJoinStream {
/// If null_equals_null is true, null == null else null != null
null_equals_null: bool,
/// Memory reservation
- reservation: SharedMemoryReservation,
+ reservation: MemoryReservation,
}
impl RecordBatchStream for HashJoinStream {
@@ -3054,7 +3040,7 @@ mod tests {
);
// Asserting that operator-level reservation attempting to overallocate
- assert_contains!(err.to_string(), "HashJoinExec");
+ assert_contains!(err.to_string(), "HashJoinInput");
}
Ok(())
@@ -3132,7 +3118,7 @@ mod tests {
);
// Asserting that stream-level reservation attempting to overallocate
- assert_contains!(err.to_string(), "HashJoinStream[1]");
+ assert_contains!(err.to_string(), "HashJoinInput[1]");
}
Ok(())
diff --git a/datafusion/core/tests/memory_limit.rs b/datafusion/core/tests/memory_limit.rs
index 71f7f30bf0..ae68b49e79 100644
--- a/datafusion/core/tests/memory_limit.rs
+++ b/datafusion/core/tests/memory_limit.rs
@@ -101,7 +101,7 @@ async fn join_by_key_multiple_partitions() {
"select t1.* from t t1 JOIN t t2 ON t1.service = t2.service",
vec![
"Resources exhausted: Failed to allocate additional",
- "HashJoinStream",
+ "HashJoinInput[0]",
],
1_000,
config,
@@ -116,7 +116,7 @@ async fn join_by_key_single_partition() {
"select t1.* from t t1 JOIN t t2 ON t1.service = t2.service",
vec![
"Resources exhausted: Failed to allocate additional",
- "HashJoinExec",
+ "HashJoinInput",
],
1_000,
config,