comphead commented on code in PR #7696:
URL: https://github.com/apache/datafusion-comet/pull/7696#discussion_r4789939304
##########
native/core/src/execution/memory_pools/fair_pool.rs:
##########
@@ -448,6 +448,169 @@ mod tests {
(0..n).map(|_| tasks.start()).collect()
}
+ /// Sorts 3000 batches of 60 wide rows in `pool`, optionally combining
them first. Returns
+ /// how many rows came out, how many times the sort spilled, and how many
input batches were
+ /// combined.
+ async fn sort_small_batches(
Review Comment:
`sort_small_batches` and `sort_int_batches` rebuild what `sort_on` and
`try_collect_with` in this test module already do, and most of their local
`use` lines repeat the module's imports. Could one helper on top of those two
take the input, the config and the coalesce flag, and return the sorted batches
with the spill and combine counts? The share test could then also compare the
combined output with the plain one. That would cover what
`a_spilling_sort_gives_the_same_result_from_combined_input` and its multi-key
twin check in `sort_input_coalesce.rs`, on Comet's pool instead of DataFusion's
`GreedyMemoryPool`, so those two and their `spilling_sort` and
`check_spilling_sort` helpers could go. The second sort key does not change
what the coalescer does.
##########
native/operators/src/sort_input_coalesce.rs:
##########
@@ -0,0 +1,1168 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Combines small input batches before a native sort.
+//!
+//! On its spill path DataFusion's sort merges one stream per buffered batch,
so thousands of
+//! tiny shuffle blocks make it very slow. Below the in-place threshold the
sort concatenates its
+//! input itself, so batches pass through until a partition's input reaches
that threshold. The
+//! gate never closes again, so a sort whose share is below the threshold gets
a copy it does not
+//! need.
+//!
+//! This sits in front of the sort rather than in the shuffle reader because
only the sort's merge
+//! cost grows with the number of batches. Arrow's `BatchCoalescer` has no
byte limit and copies
+//! as batches arrive, and `DecodedBatchCoalescer` takes only flat types, so
neither lets the held
+//! memory be reserved exactly for any input.
+
+use std::fmt::Formatter;
+use std::sync::Arc;
+
+use arrow::compute::concat_batches;
+use arrow::datatypes::DataType;
+use arrow::record_batch::RecordBatch;
+use datafusion::common::tree_node::TreeNodeRecursion;
+use datafusion::common::utils::memory::get_record_batch_memory_size;
+use datafusion::common::{DataFusionError, Result};
+use datafusion::execution::memory_pool::{MemoryConsumer, MemoryReservation};
+use datafusion::execution::{async_try_stream, TaskContext};
+use datafusion::physical_expr::PhysicalExpr;
+use datafusion::physical_plan::execution_plan::CardinalityEffect;
+use datafusion::physical_plan::metrics::{
+ BaselineMetrics, Count, ExecutionPlanMetricsSet, MetricBuilder,
MetricsSet, RecordOutput,
+};
+use datafusion::physical_plan::stream::RecordBatchStreamAdapter;
+use datafusion::physical_plan::{
+ DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties,
SendableRecordBatchStream,
+};
+use futures::TryStreamExt;
+
+/// The cap is half the in-place threshold because the sort reserves about
twice a batch's size.
+/// The bounds also keep string and binary offsets far below the i32 limit.
+const MIN_GROUP_BYTES: usize = 64 * 1024;
+const MAX_GROUP_BYTES: usize = 4 * 1024 * 1024;
+
+/// In `sort_small_batches`, combining 512-row batches made a spilling
single-key sort slower,
+/// while 384 rows or fewer was never slower.
+const MAX_SMALL_BATCH_ROWS: usize = 512;
+
+/// Small batches passed through after a refusal, so a pool under pressure is
not asked again for
+/// each one.
+const REFUSED_BACKOFF_BATCHES: usize = 64;
+
+#[derive(Debug)]
+pub struct SortInputCoalesceExec {
+ input: Arc<dyn ExecutionPlan>,
+ target_rows: usize,
+ gate_bytes: usize,
+ max_bytes: usize,
+ metrics: ExecutionPlanMetricsSet,
+}
+
+impl SortInputCoalesceExec {
+ /// `gate_bytes` is the sort's in-place threshold.
+ pub fn new(input: Arc<dyn ExecutionPlan>, target_rows: usize, gate_bytes:
usize) -> Self {
+ Self {
+ input,
+ target_rows: target_rows.max(1),
+ gate_bytes,
+ max_bytes: (gate_bytes / 2).clamp(MIN_GROUP_BYTES,
MAX_GROUP_BYTES),
+ metrics: ExecutionPlanMetricsSet::new(),
+ }
+ }
+
+ #[cfg(test)]
+ fn with_max_bytes(mut self, max_bytes: usize) -> Self {
+ self.max_bytes = max_bytes;
+ self
+ }
+
+ pub fn input(&self) -> &Arc<dyn ExecutionPlan> {
+ &self.input
+ }
+
+ /// The plan below a coalescer, or `plan` itself when it is not one.
+ pub fn unwrap_input(plan: &Arc<dyn ExecutionPlan>) -> &Arc<dyn
ExecutionPlan> {
+ plan.downcast_ref::<Self>().map_or(plan, Self::input)
+ }
+}
+
+impl DisplayAs for SortInputCoalesceExec {
+ fn fmt_as(&self, _t: DisplayFormatType, f: &mut Formatter) ->
std::fmt::Result {
+ write!(
+ f,
+ "CometSortInputCoalesceExec: target_rows={}, gate_bytes={},
max_bytes={}",
+ self.target_rows, self.gate_bytes, self.max_bytes
+ )
+ }
+}
+
+impl ExecutionPlan for SortInputCoalesceExec {
+ fn name(&self) -> &str {
+ "CometSortInputCoalesceExec"
+ }
+
+ fn properties(&self) -> &Arc<PlanProperties> {
+ self.input.properties()
+ }
+
+ fn maintains_input_order(&self) -> Vec<bool> {
+ vec![true]
+ }
+
+ fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
+ vec![&self.input]
+ }
+
+ fn apply_expressions(
+ &self,
+ _f: &mut dyn FnMut(&Arc<dyn PhysicalExpr>) ->
Result<TreeNodeRecursion>,
+ ) -> Result<TreeNodeRecursion> {
+ Ok(TreeNodeRecursion::Continue)
+ }
+
+ fn cardinality_effect(&self) -> CardinalityEffect {
+ CardinalityEffect::Equal
+ }
+
+ fn with_new_children(
+ self: Arc<Self>,
+ children: Vec<Arc<dyn ExecutionPlan>>,
+ ) -> Result<Arc<dyn ExecutionPlan>> {
+ let [input]: [_; 1] = children.try_into().map_err(|_| {
+ DataFusionError::Internal("Sort input coalescing requires one
child".into())
+ })?;
+ Ok(Arc::new(Self {
+ input,
+ target_rows: self.target_rows,
+ gate_bytes: self.gate_bytes,
+ max_bytes: self.max_bytes,
+ // Shared, so the copy reports through the node the Spark plan
registered for its
+ // metrics if a rewrite rebuilds the sort's input.
+ metrics: self.metrics.clone(),
+ }))
+ }
+
+ fn execute(
+ &self,
+ partition: usize,
+ context: Arc<TaskContext>,
+ ) -> Result<SendableRecordBatchStream> {
+ let mut input = self.input.execute(partition, Arc::clone(&context))?;
+ let reservation =
MemoryConsumer::new(format!("SortInputCoalesceExec[{partition}]"))
+ .register(context.memory_pool());
+ let baseline = BaselineMetrics::new(&self.metrics, partition);
+ let mut grouper = Grouper::new(self, reservation, partition);
+ let stream = async_try_stream(|mut out| async move {
+ while let Some(batch) = input.try_next().await? {
+ // A group that has to go out first is emitted before the new
batch is held, so
+ // the sort reserves it while this operator holds nothing.
+ let (step, flushed) = {
+ let _timer = baseline.elapsed_compute().timer();
+ grouper.prepare(&batch)?
+ };
+ if let Some(batch) = flushed {
+ (&batch).record_output(&baseline);
+ out.emit(batch).await;
+ }
+ let ready = {
+ let _timer = baseline.elapsed_compute().timer();
+ grouper.add(batch, step)?
+ };
+ for batch in ready.into_iter().flatten() {
+ (&batch).record_output(&baseline);
+ out.emit(batch).await;
+ }
+ }
+ let last = {
+ let _timer = baseline.elapsed_compute().timer();
+ grouper.flush()?
+ };
+ if let Some(batch) = last {
+ (&batch).record_output(&baseline);
+ out.emit(batch).await;
+ }
+ Ok(())
+ });
+ Ok(Box::pin(RecordBatchStreamAdapter::new(
+ self.schema(),
+ stream,
+ )))
+ }
+
+ fn metrics(&self) -> Option<MetricsSet> {
+ Some(self.metrics.clone_inner())
+ }
+}
+
+/// Concatenating dictionaries merges their values, which can overflow a small
key type where
+/// the batches on their own fit. Dictionaries nested in structs, lists and
maps count too.
+fn has_small_dictionary_keys(batch: &RecordBatch) -> bool {
+ fn small(data_type: &DataType) -> bool {
+ match data_type {
+ DataType::Dictionary(key, value) => {
+ key.primitive_width().is_some_and(|width| width < 4) ||
small(value)
+ }
+ DataType::Struct(fields) => fields.iter().any(|f|
small(f.data_type())),
+ DataType::List(f)
+ | DataType::LargeList(f)
+ | DataType::ListView(f)
+ | DataType::LargeListView(f)
+ | DataType::FixedSizeList(f, _)
+ | DataType::Map(f, _) => small(f.data_type()),
+ // Run ends count rows, so a small run-end type can overflow too.
+ DataType::RunEndEncoded(run_ends, values) => {
+ run_ends
+ .data_type()
+ .primitive_width()
+ .is_some_and(|width| width < 4)
+ || small(values.data_type())
+ }
+ _ => false,
+ }
+ }
+ batch.schema().fields().iter().any(|f| small(f.data_type()))
+}
+
+/// The bytes the batch's rows take, without unused buffer capacity, as
DataFusion's sort counts
+/// it.
+fn sliced_size(batch: &RecordBatch) -> Result<usize> {
+ batch.columns().iter().try_fold(0usize, |total, column| {
+ Ok(total + column.to_data().get_slice_memory_size()?)
+ })
+}
+
+/// `held` is what a batch keeps alive, counting a shared buffer once. `copy`
is what concatenating
+/// it writes, which counts a buffer once per column that uses it.
+#[derive(Clone, Copy)]
+struct Size {
+ held: usize,
+ copy: usize,
+}
+
+enum Step {
+ Skip,
+ PassThrough,
+ Hold(Size),
+}
+
+/// Groups whole input batches in arrival order and concatenates each group.
Inputs are never
+/// split, so the reservation always covers the batches being held.
+struct Grouper {
+ target_rows: usize,
+ small_rows: usize,
+ gate_bytes: usize,
+ max_bytes: usize,
+ seen_bytes: usize,
+ past_gate: bool,
+ pending: Vec<RecordBatch>,
+ rows: usize,
+ held: usize,
+ copy: usize,
+ reservation: MemoryReservation,
+ backoff: usize,
+ input_batches: Count,
+ output_batches: Count,
+}
+
+impl Grouper {
+ fn new(exec: &SortInputCoalesceExec, reservation: MemoryReservation,
partition: usize) -> Self {
+ let metrics = MetricBuilder::new(&exec.metrics);
+ Self {
+ target_rows: exec.target_rows,
+ small_rows: exec.target_rows.div_ceil(2).min(MAX_SMALL_BATCH_ROWS),
+ gate_bytes: exec.gate_bytes,
+ max_bytes: exec.max_bytes,
+ seen_bytes: 0,
+ past_gate: exec.gate_bytes == 0,
+ pending: Vec::new(),
+ rows: 0,
+ held: 0,
+ copy: 0,
+ reservation,
+ backoff: 0,
+ input_batches: metrics
+ .clone()
+ .counter("coalesced_input_batches", partition),
+ output_batches: metrics.counter("coalesced_output_batches",
partition),
+ }
+ }
+
+ /// Decides what `add` does with `batch`. Returns the held group too when
it has to go out
+ /// first: before a batch that passes through, or before one that does not
fit in the group.
+ fn prepare(&mut self, batch: &RecordBatch) -> Result<(Step,
Option<RecordBatch>)> {
+ let rows = batch.num_rows();
+ if rows == 0 {
+ return Ok((Step::Skip, None));
+ }
+ // A small slice of a large batch is held by its whole buffer, so it
passes through.
+ let size = if !self.past_gate {
+ // Counted as DataFusion's `get_reserved_bytes_for_record_batch`
counts it, which the
+ // sort compares with the threshold.
+ self.seen_bytes = self
+ .seen_bytes
+ .saturating_add(get_record_batch_memory_size(batch) +
sliced_size(batch)?);
+ self.past_gate = self.seen_bytes >= self.gate_bytes;
+ None
+ } else if rows >= self.small_rows || has_small_dictionary_keys(batch) {
+ None
+ } else if self.backoff > 0 {
+ self.backoff -= 1;
+ None
+ } else {
+ Some(Size {
+ held: get_record_batch_memory_size(batch),
+ copy: sliced_size(batch)?,
+ })
+ };
+ match size.filter(|size| size.held.max(size.copy) <= self.max_bytes) {
+ Some(size) => {
+ // A batch with another schema starts a new group, since the
concat needs one.
+ // The cap applies to what the group keeps alive as well as to
its copy, since
+ // small slices of separate large buffers hold far more than
they copy.
+ let fits = rows <= self.target_rows - self.rows
+ && size.copy <= self.max_bytes - self.copy
+ && size.held <= self.max_bytes - self.held
+ && self
+ .pending
+ .first()
+ .is_none_or(|held| held.schema() == batch.schema());
+ let flushed = if fits { None } else { self.flush()? };
+ Ok((Step::Hold(size), flushed))
+ }
+ None => Ok((Step::PassThrough, self.flush()?)),
+ }
+ }
+
+ /// Returns what is ready, in input order: a flushed group, then `batch`
when it is not held.
+ fn add(&mut self, batch: RecordBatch, step: Step) ->
Result<[Option<RecordBatch>; 2]> {
+ let size = match step {
+ Step::Skip => return Ok([None, None]),
+ Step::PassThrough => return Ok([None, Some(batch)]),
+ Step::Hold(size) => size,
+ };
+ let rows = batch.num_rows();
+ if !self.reserve(size)? {
+ // The sort can spill and this cannot, so hand it everything.
+ self.backoff = REFUSED_BACKOFF_BATCHES;
+ return Ok([self.flush()?, Some(batch)]);
+ }
+ self.pending.push(batch);
+ self.rows += rows;
+ self.held += size.held;
+ self.copy += size.copy;
+ if self.rows == self.target_rows
+ || self.copy == self.max_bytes
+ || self.held == self.max_bytes
+ {
+ return Ok([self.flush()?, None]);
+ }
+ Ok([None, None])
+ }
+
+ #[cfg(test)]
+ fn push(&mut self, batch: RecordBatch) -> Result<Vec<RecordBatch>> {
+ let (step, flushed) = self.prepare(&batch)?;
+ let mut ready: Vec<_> = flushed.into_iter().collect();
+ ready.extend(self.add(batch, step)?.into_iter().flatten());
+ Ok(ready)
+ }
+
+ /// Reserves the held batches and the combined copy with `size` added, and
no more: a consumer
+ /// that cannot spill is checked only against the whole pool, so asking
ahead could make Spark
+ /// spill another consumer for memory never used. Returns false when
refused.
+ fn reserve(&mut self, size: Size) -> Result<bool> {
+ let needed = self.held + size.held + self.copy + size.copy;
+ let size = self.reservation.size();
+ if needed <= size {
+ return Ok(true);
+ }
+ match self.reservation.try_grow(needed - size) {
+ Ok(()) => Ok(true),
+ Err(DataFusionError::ResourcesExhausted(_)) => Ok(false),
+ Err(error) => Err(error),
+ }
+ }
+
+ fn flush(&mut self) -> Result<Option<RecordBatch>> {
+ let out = match self.pending.len() {
+ 0 => None,
+ 1 => self.pending.pop(),
+ n => {
+ // The inputs' own schema, which a batch that passes through
also keeps.
+ let combined = concat_batches(&self.pending[0].schema(),
&self.pending)?;
+ self.pending.clear();
+ self.input_batches.add(n);
+ self.output_batches.add(1);
+ Some(combined)
+ }
+ };
+ // The sort reserves what it buffers, so hold nothing between groups.
+ self.reservation.free();
+ self.rows = 0;
+ self.held = 0;
+ self.copy = 0;
+ Ok(out)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use arrow::array::{
+ Array, ArrayRef, AsArray, Date32Array, Decimal128Array, Int32Array,
Int64Array, ListArray,
+ MapArray, StringArray, StringDictionaryBuilder, StructArray,
TimestampMicrosecondArray,
+ };
+ use arrow::datatypes::{DataType, Field, Fields, Int32Type, Int64Type,
Schema, SchemaRef};
+ use datafusion::datasource::memory::MemorySourceConfig;
+ use datafusion::execution::memory_pool::{GreedyMemoryPool, MemoryPool};
+ use datafusion::execution::runtime_env::RuntimeEnvBuilder;
+ use datafusion::physical_plan::common::collect;
+ use datafusion::prelude::{SessionConfig, SessionContext};
+
+ fn ints(values: std::ops::Range<i32>) -> RecordBatch {
+ RecordBatch::try_from_iter([(
+ "k",
+ Arc::new(Int32Array::from_iter_values(values)) as ArrayRef,
+ )])
+ .unwrap()
+ }
+
+ fn ten_row_batches(n: i32) -> Vec<RecordBatch> {
+ (0..n).map(|i| ints(i * 10..i * 10 + 10)).collect()
+ }
+
+ fn run_with(
+ input: Vec<RecordBatch>,
+ schema: SchemaRef,
+ exec: impl FnOnce(Arc<dyn ExecutionPlan>) -> SortInputCoalesceExec,
+ ctx: SessionContext,
+ ) -> (Vec<RecordBatch>, MetricsSet) {
+ let source = MemorySourceConfig::try_new_exec(&[input], schema,
None).unwrap();
+ let exec = exec(source);
+ let rt = tokio::runtime::Builder::new_current_thread()
+ .build()
+ .unwrap();
+ let output = rt
+ .block_on(collect(exec.execute(0, ctx.task_ctx()).unwrap()))
+ .unwrap();
+ (output, exec.metrics().unwrap())
+ }
+
+ fn run(
+ input: Vec<RecordBatch>,
+ target_rows: usize,
+ gate_bytes: usize,
+ max_bytes: usize,
+ ) -> (Vec<RecordBatch>, MetricsSet) {
+ let schema = input[0].schema();
+ run_with(
+ input,
+ schema,
+ |source| {
+ SortInputCoalesceExec::new(source, target_rows, gate_bytes)
+ .with_max_bytes(max_bytes)
+ },
+ SessionContext::new(),
+ )
+ }
+
+ fn row_counts(batches: &[RecordBatch]) -> Vec<usize> {
+ batches.iter().map(|b| b.num_rows()).collect()
+ }
+
+ fn same_rows(output: &[RecordBatch], input: &[RecordBatch]) {
+ let schema = input[0].schema();
+ assert_eq!(
+ concat_batches(&schema, output).unwrap(),
+ concat_batches(&schema, input).unwrap()
+ );
+ }
+
+ fn metric(metrics: &MetricsSet, name: &str) -> usize {
+ metrics.sum_by_name(name).map(|v| v.as_usize()).unwrap_or(0)
+ }
+
+ #[test]
+ fn small_partitions_pass_through_without_a_copy() {
+ let input = ten_row_batches(10);
+ let (output, _) = run(input.clone(), 1024, usize::MAX,
MAX_GROUP_BYTES);
+ assert_eq!(output, input);
+ for (out, inp) in output.iter().zip(&input) {
+ assert!(Arc::ptr_eq(out.column(0), inp.column(0)));
+ }
+ }
+
+ #[test]
+ fn combines_small_batches_after_the_gate_in_input_order() {
+ let input = ten_row_batches(10);
+ // A zero gate starts combining with the first batch.
+ let (output, metrics) = run(input.clone(), 40, 0, MAX_GROUP_BYTES);
+ assert_eq!(row_counts(&output), [40, 40, 20]);
+ same_rows(&output, &input);
+ assert_eq!(metric(&metrics, "coalesced_input_batches"), 10);
+ assert_eq!(metric(&metrics, "coalesced_output_batches"), 3);
+ assert_eq!(metrics.output_rows(), Some(100));
+ }
+
+ #[test]
+ fn a_rebuilt_coalescer_reports_through_the_original() {
+ let input = ten_row_batches(10);
+ let schema = input[0].schema();
+ let source = MemorySourceConfig::try_new_exec(&[input], schema,
None).unwrap();
+ let original = Arc::new(SortInputCoalesceExec::new(
+ Arc::clone(&source) as Arc<dyn ExecutionPlan>,
+ 40,
+ 0,
+ ));
+ let rebuilt = (Arc::clone(&original) as Arc<dyn ExecutionPlan>)
+ .replace_children(
+ vec![source],
+ datafusion::physical_plan::ReplaceChildrenOptions::new(
+
datafusion::physical_plan::ChildrenPropertiesMode::Recompute,
+ ),
+ )
+ .unwrap();
+ let rt = tokio::runtime::Builder::new_current_thread()
+ .build()
+ .unwrap();
+ rt.block_on(collect(
+ rebuilt
+ .execute(0, SessionContext::new().task_ctx())
+ .unwrap(),
+ ))
+ .unwrap();
+ let metrics = original.metrics().unwrap();
+ assert_eq!(metric(&metrics, "coalesced_input_batches"), 10);
+ }
+
+ #[test]
+ fn the_gate_counts_bytes_before_combining() {
+ let input = ten_row_batches(10);
+ // The gate counts each batch as the sort does: memory size plus
sliced size.
+ let one = get_record_batch_memory_size(&input[0]) +
sliced_size(&input[0]).unwrap();
+ // The first three batches pass through; the third one crosses the
gate.
+ let (output, _) = run(input.clone(), 40, one * 2 + 1, MAX_GROUP_BYTES);
+ assert_eq!(row_counts(&output), [10, 10, 10, 40, 30]);
+ same_rows(&output, &input);
+ }
+
+ #[test]
+ fn byte_cap_flushes_before_the_row_target() {
+ let input = ten_row_batches(6);
+ let one = get_record_batch_memory_size(&input[0]);
+ let (output, _) = run(input.clone(), 1000, 0, one * 2);
+ assert_eq!(row_counts(&output), [20, 20, 20]);
+ same_rows(&output, &input);
+ }
+
+ #[test]
+ fn large_batches_pass_through_after_flushing_the_group() {
+ let small = ints(0..10);
+ let large = ints(100..150);
+ let input = vec![small.clone(), small.clone(), large.clone(),
small.clone()];
+ let (output, _) = run(input.clone(), 100, 0, MAX_GROUP_BYTES);
+ assert_eq!(row_counts(&output), [20, 50, 10]);
+ assert!(Arc::ptr_eq(output[1].column(0), large.column(0)));
+ same_rows(&output, &input);
+ }
+
+ #[test]
+ fn batches_of_512_rows_or_more_pass_through_at_the_default_target() {
+ let mid = ints(0..MAX_SMALL_BATCH_ROWS as i32);
+ let small = ints(0..(MAX_SMALL_BATCH_ROWS - 1) as i32);
+ let input = vec![mid.clone(), mid.clone(), small.clone(),
small.clone()];
+ let (output, _) = run(input.clone(), 8192, 0, MAX_GROUP_BYTES);
+ assert_eq!(row_counts(&output), [512, 512, 1022]);
+ assert!(Arc::ptr_eq(output[0].column(0), mid.column(0)));
+ same_rows(&output, &input);
+ }
+
+ #[test]
+ fn empty_input_and_empty_batches() {
+ let schema = ints(0..1).schema();
+ let (output, _) = run_with(
+ vec![],
+ Arc::clone(&schema),
+ |source| SortInputCoalesceExec::new(source, 8, 0),
+ SessionContext::new(),
+ );
+ assert!(output.is_empty());
+
+ let input = vec![ints(0..0), ints(0..3), ints(0..0), ints(3..5)];
+ let (output, _) = run(input.clone(), 8, 0, MAX_GROUP_BYTES);
+ assert_eq!(row_counts(&output), [5]);
+ same_rows(&output, &input);
+ }
+
+ #[test]
+ fn passes_through_when_memory_is_short() {
+ let input = ten_row_batches(4);
+ let schema = input[0].schema();
+ let pool: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(1));
+ let runtime = RuntimeEnvBuilder::new()
+ .with_memory_pool(Arc::clone(&pool))
+ .build_arc()
+ .unwrap();
+ let ctx = SessionContext::new_with_config_rt(SessionConfig::new(),
runtime);
+ let (output, _) = run_with(
+ input.clone(),
+ schema,
+ |source| SortInputCoalesceExec::new(source, 40, 0),
+ ctx,
+ );
+ assert_eq!(output, input);
+ assert_eq!(pool.reserved(), 0);
+ }
+
+ #[test]
+ fn the_group_cap_is_half_the_in_place_threshold() {
+ let cap = |gate| {
+ let source =
+ MemorySourceConfig::try_new_exec(&[vec![]],
ints(0..1).schema(), None).unwrap();
+ SortInputCoalesceExec::new(source, 8192, gate).max_bytes
+ };
+ assert_eq!(cap(1024 * 1024), 512 * 1024);
+ assert_eq!(cap(0), MIN_GROUP_BYTES);
+ assert_eq!(cap(usize::MAX), MAX_GROUP_BYTES);
+ }
+
+ #[test]
+ fn reservation_covers_a_group_and_is_released_after_it() {
+ let input = ten_row_batches(3);
+ let pool: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(1 <<
30));
+ let source = MemorySourceConfig::try_new_exec(&[vec![]],
input[0].schema(), None).unwrap();
+ let exec = SortInputCoalesceExec::new(source, 1000, 0);
+ let mut grouper = Grouper::new(&exec,
MemoryConsumer::new("test").register(&pool), 0);
+ // The reservation covers exactly the held batches and their copy.
+ let mut needed = 0;
+ for batch in &input {
+ needed += get_record_batch_memory_size(batch) +
sliced_size(batch).unwrap();
+ assert!(grouper.push(batch.clone()).unwrap().is_empty());
+ assert_eq!(pool.reserved(), needed);
+ }
+ let combined = grouper.flush().unwrap().unwrap();
+ assert_eq!(
+ combined,
+ concat_batches(&input[0].schema(), &input).unwrap()
+ );
+ // The sort reserves the combined batch itself.
+ assert_eq!(pool.reserved(), 0);
+ assert!(grouper.flush().unwrap().is_none());
+ }
+
+ fn run_in_pool(
+ input: Vec<RecordBatch>,
+ target_rows: usize,
+ pool_bytes: usize,
+ ) -> Vec<RecordBatch> {
+ let schema = input[0].schema();
+ let pool: Arc<dyn MemoryPool> =
Arc::new(GreedyMemoryPool::new(pool_bytes));
+ let runtime = RuntimeEnvBuilder::new()
+ .with_memory_pool(Arc::clone(&pool))
+ .build_arc()
+ .unwrap();
+ let ctx = SessionContext::new_with_config_rt(SessionConfig::new(),
runtime);
+ let (output, _) = run_with(
+ input,
+ schema,
+ |source| SortInputCoalesceExec::new(source, target_rows, 0),
+ ctx,
+ );
+ assert_eq!(pool.reserved(), 0);
+ output
+ }
+
+ #[test]
+ fn combines_when_the_pool_holds_the_group_but_not_a_full_one() {
Review Comment:
This pins what `reservation_covers_a_group_and_is_released_after_it` already
checks after every push, and what the last two asserts of
`a_copy_of_shared_columns_is_reserved_and_capped` check at the exact boundary
(held plus copy combines, one byte less does not). Could it go? Also, six
places in this module build the same pool, runtime and `SessionContext`.
`run_in_pool` already covers the body of `passes_through_when_memory_is_short`,
a `max_bytes` argument would let it replace `run_capped`, and a small context
helper would cover the three stream tests.
##########
docs/source/contributor-guide/memory_management.md:
##########
@@ -583,8 +600,10 @@ unbounded, so nobody charged for them at allocation time.
Whether they are charg
on who holds them. DataFusion's `ExternalSorter` reserves
`get_reserved_bytes_for_record_batch` for
every batch it retains, and the hash join build side reserves
`get_record_batch_memory_size` for
each incoming batch; both read buffer sizes off the `ArrayData` and apply
equally to imported
-buffers. So an imported batch that a sort or join has buffered _is_ reserved
in Comet's pool, and
-through a unified pool is charged to Spark. The same batch held by an operator
that does not reserve
+buffers. The coalescer that can sit in front of a native sort also reserves
the batches it holds,
+at twice their size to cover the combined copy. So an imported batch that a
sort, a join or that
Review Comment:
"At twice their size" does not match what the coalescer reserves. It
reserves what the held batches keep alive plus the copy counted per column, so
a batch whose columns share a buffer reserves more than twice its size, and a
small slice of a larger buffer less. Could this say it reserves the batches it
holds and their combined copy, as the new bullet under "The unified pools" does?
##########
spark/src/main/scala/org/apache/spark/sql/comet/operators.scala:
##########
@@ -1655,7 +1656,13 @@ case class CometSortExec(
CometMetricNode.baselineMetrics(sparkContext) ++
Map(
"spill_count" -> SQLMetrics.createMetric(sparkContext, "number of
spills"),
- "spilled_bytes" -> SQLMetrics.createSizeMetric(sparkContext, "total
spilled bytes"))
+ "spilled_bytes" -> SQLMetrics.createSizeMetric(sparkContext, "total
spilled bytes"),
+ "coalesced_input_batches" -> SQLMetrics.createMetric(
+ sparkContext,
+ "number of small input batches combined into larger ones"),
+ "coalesced_output_batches" -> SQLMetrics.createMetric(
+ sparkContext,
+ "number of batches produced by combining small input batches"))
Review Comment:
These two are declared on every `CometSort`, including with the config off,
which is the default and leaves them at zero. Other opt-in features declare
theirs only when the serialized operator enables them, as
`CometSortMergeJoinExec` does with `getRightKeyReductionEnabled` and
`CometHashJoinExec` with `getDynamicFilterEnabled`. Could these be added only
when `nativeOp.getSort.getCoalesceInput` is set? The `CometSort` section in
`metrics.md` would then say that the last two rows appear only with
`spark.comet.exec.sort.coalesceInput.enabled=true`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]