Apache DataFusion SQL Query Engine
Reuse spill_record_batch_by_size function (#12389)
张林伟09f7592bd6eb
diff --git a/datafusion/physical-plan/src/aggregates/mod.rs b/datafusion/physical-plan/src/aggregates/mod.rsindex ad0e43631a..c3bc7b042e 100644--- a/datafusion/physical-plan/src/aggregates/mod.rs+++ b/datafusion/physical-plan/src/aggregates/mod.rs@@ -1136,12 +1136,12 @@ fn evaluate_optional(/// Evaluate a group by expression against a `RecordBatch`////// Arguments:-/// `group_by`: the expression to evaluate-/// `batch`: the `RecordBatch` to evaluate against+/// - `group_by`: the expression to evaluate+/// - `batch`: the `RecordBatch` to evaluate against////// Returns: A Vec of Vecs of Array of results-/// The outer Vect appears to be for grouping sets-/// The inner Vect contains the results per expression+/// The outer Vec appears to be for grouping sets+/// The inner Vec contains the results per expression/// The inner-inner Array contains the results per rowpub(crate) fn evaluate_group_by(group_by: &PhysicalGroupBy,diff --git a/datafusion/physical-plan/src/aggregates/row_hash.rs b/datafusion/physical-plan/src/aggregates/row_hash.rsindex fd2d26d9b4..0332131d4b 100644--- a/datafusion/physical-plan/src/aggregates/row_hash.rs+++ b/datafusion/physical-plan/src/aggregates/row_hash.rs@@ -27,11 +27,10 @@ use crate::aggregates::{evaluate_group_by, evaluate_many, evaluate_optional, group_schema, AggregateMode,PhysicalGroupBy,};-use crate::common::IPCWriter;use crate::metrics::{BaselineMetrics, MetricBuilder, RecordOutput};use crate::sorts::sort::sort_batch;use crate::sorts::streaming_merge;-use crate::spill::read_spill_as_stream;+use crate::spill::{read_spill_as_stream, spill_record_batch_by_size};use crate::stream::RecordBatchStreamAdapter;use crate::{aggregates, metrics, ExecutionPlan, PhysicalExpr};use crate::{RecordBatchStream, SendableRecordBatchStream};@@ -905,19 +904,13 @@ impl GroupedHashAggregateStream {let emit = self.emit(EmitTo::All, true)?;let sorted = sort_batch(&emit, &self.spill_state.spill_expr, None)?;let spillfile = self.runtime.disk_manager.create_tmp_file("HashAggSpill")?;- let mut writer = IPCWriter::new(spillfile.path(), &emit.schema())?;// TODO: slice large `sorted` and write to multiple files in parallel- let mut offset = 0;- let total_rows = sorted.num_rows();-- while offset < total_rows {- let length = std::cmp::min(total_rows - offset, self.batch_size);- let batch = sorted.slice(offset, length);- offset += batch.num_rows();- writer.write(&batch)?;- }-- writer.finish()?;+ spill_record_batch_by_size(+ &sorted,+ spillfile.path().into(),+ sorted.schema(),+ self.batch_size,+ )?;self.spill_state.spills.push(spillfile);Ok(())}diff --git a/datafusion/physical-plan/src/spill.rs b/datafusion/physical-plan/src/spill.rsindex 21ca58fa0a..de85a7c6f0 100644--- a/datafusion/physical-plan/src/spill.rs+++ b/datafusion/physical-plan/src/spill.rs@@ -88,7 +88,6 @@ fn read_spill(sender: Sender<Result<RecordBatch>>, path: &Path) -> Result<()> {/// Spill the `RecordBatch` to disk as smaller batches/// split by `batch_size_rows`-/// Return `total_rows` what is spilledpub fn spill_record_batch_by_size(batch: &RecordBatch,path: PathBuf,