From e9b8b0c3bc813aa4b3cc48b8bc67f685a5e0b1aa Mon Sep 17 00:00:00 2001 From: zhangfengcdt Date: Thu, 29 Oct 2026 10:32:02 -0700 Subject: [PATCH 1/9] fix: cap the rows per batch so a large string scalar fits 32-bit offsets A string scalar from a literal or a scalar subquery is expanded to the batch length by aggregates, the hash partitioner and string kernels. A 256 KiB string over an 8192-row batch needs 2 GiB of value bytes, past Arrow's offsets, and native execution panics where Spark succeeds. Add up the string and binary scalars while planning and lower the session batch size so their expansion stays within 1 GiB. Native sources read the batch size when they run, the range source now included, and the JVM-fed leaves slice wider batches to it. --- native/core/src/execution/jni_api.rs | 40 +++++++++ .../src/execution/operators/batch_slicer.rs | 90 +++++++++++++++++++ native/core/src/execution/operators/mod.rs | 1 + native/core/src/execution/operators/scan.rs | 28 ++++-- .../src/execution/operators/shuffle_scan.rs | 26 ++++-- native/core/src/execution/planner.rs | 46 +++++++++- native/operators/src/range.rs | 65 +++++++++++--- .../operators/large_scalar_string.sql | 60 +++++++++++++ 8 files changed, 326 insertions(+), 30 deletions(-) create mode 100644 native/core/src/execution/operators/batch_slicer.rs create mode 100644 spark/src/test/resources/sql-tests/operators/large_scalar_string.sql diff --git a/native/core/src/execution/jni_api.rs b/native/core/src/execution/jni_api.rs index 494e7acdff9..6263d2f3e95 100644 --- a/native/core/src/execution/jni_api.rs +++ b/native/core/src/execution/jni_api.rs @@ -1056,6 +1056,24 @@ fn configure_skip_partial_aggregation(config: &mut SessionConfig, plan: &Operato } } +/// The most value bytes a string or binary scalar may take once expanded to a batch's length. +/// Arrow's `Utf8` and `Binary` offsets are 32-bit, so 2 GiB is the hard limit; half of it leaves +/// room for kernels that append to the expanded value, such as `concat`, and matches what an +/// Arrow Java vector can hold before its reallocation fails. +const SCALAR_EXPANSION_BYTES: usize = 1 << 30; + +/// The rows per batch under which the plan's string and binary scalars, `scalar_bytes` in all, +/// expanded to the batch's length by an aggregate, a partitioner or a string kernel, stay within +/// `SCALAR_EXPANSION_BYTES`. Spark evaluates such scalars row by row and never materializes +/// the column, so this is what keeps a query with a large literal or scalar subquery result +/// running natively. +fn scalar_batch_rows(scalar_bytes: usize, batch_size: usize) -> usize { + if scalar_bytes == 0 { + return batch_size; + } + batch_size.min((SCALAR_EXPANSION_BYTES / scalar_bytes).max(1)) +} + /// Configure DataFusion session context. fn prepare_datafusion_session_context( batch_size: usize, @@ -1555,6 +1573,17 @@ fn execute_plan( exec_context.scans = scans; exec_context.shuffle_scans = shuffle_scans; + // The task context created below copies the session config, so every source and the + // JVM-fed leaves see the smaller batch size. + let batch_size = exec_context.session_ctx.copied_config().batch_size(); + let rows = scalar_batch_rows(planner.scalar_bytes(), batch_size); + if rows < batch_size { + let state = exec_context.session_ctx.state_ref(); + let mut state = state.write(); + let config = state.config_mut(); + *config = std::mem::take(config).with_batch_size(rows); + } + if exec_context.explain_native { let formatted_plan_str = DisplayableExecutionPlan::new(root_op.native_plan.as_ref()).indent(true); @@ -2538,6 +2567,17 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_columnarToRowClose( mod tests { use super::*; use crate::execution::operators::InputBatch; + + #[test] + fn scalar_batch_rows_keeps_the_batch_size_unless_a_scalar_would_overflow_it() { + // No string or binary scalar, or a small one: the batch size stands. + assert_eq!(scalar_batch_rows(0, 8192), 8192); + assert_eq!(scalar_batch_rows(1024, 8192), 8192); + // A 256 KiB scalar expanded to 8192 rows needs 2 GiB; 4096 rows keep it at 1 GiB. + assert_eq!(scalar_batch_rows(256 * 1024, 8192), 4096); + // A scalar past 1 GiB still gets one row per batch. + assert_eq!(scalar_batch_rows(2 * 1024 * 1024 * 1024, 8192), 1); + } use crate::execution::planner::TEST_EXEC_CONTEXT_ID; use arrow::array::{ArrayRef, Int32Array}; use arrow::datatypes::{DataType, Field, Schema}; diff --git a/native/core/src/execution/operators/batch_slicer.rs b/native/core/src/execution/operators/batch_slicer.rs new file mode 100644 index 00000000000..8b74a549424 --- /dev/null +++ b/native/core/src/execution/operators/batch_slicer.rs @@ -0,0 +1,90 @@ +// 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. + +use arrow::array::RecordBatch; + +/// Splits the batches a JVM-fed leaf receives so that none is wider than the session batch +/// size. The JVM sizes those batches itself, so this is the one place a smaller native batch +/// size takes effect on them; see `scalar_batch_rows` in `jni_api` for why it can be smaller. +/// Slices share the input's buffers, so splitting copies nothing. +pub(crate) struct BatchSlicer { + max_rows: usize, + rest: Option, +} + +impl BatchSlicer { + pub fn new(max_rows: usize) -> Self { + Self { + max_rows: max_rows.max(1), + rest: None, + } + } + + /// Takes a batch in and returns its first slice, holding the remainder for `pending`. + pub fn slice(&mut self, batch: RecordBatch) -> RecordBatch { + if batch.num_rows() <= self.max_rows { + return batch; + } + let head = batch.slice(0, self.max_rows); + self.rest = Some(batch.slice(self.max_rows, batch.num_rows() - self.max_rows)); + head + } + + /// The next slice of the batch last passed to `slice`, if any of it is left. + pub fn pending(&mut self) -> Option { + self.rest.take().map(|rest| self.slice(rest)) + } +} + +#[cfg(test)] +mod tests { + use super::BatchSlicer; + use arrow::array::{Int32Array, RecordBatch}; + use arrow::datatypes::{DataType, Field, Schema}; + use std::sync::Arc; + + fn batch(n: i32) -> RecordBatch { + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])); + RecordBatch::try_new(schema, vec![Arc::new(Int32Array::from_iter_values(0..n))]).unwrap() + } + + fn values(batch: &RecordBatch) -> Vec { + batch + .column(0) + .as_any() + .downcast_ref::() + .unwrap() + .values() + .to_vec() + } + + #[test] + fn passes_a_batch_within_the_limit_through() { + let mut slicer = BatchSlicer::new(4); + assert_eq!(values(&slicer.slice(batch(4))), vec![0, 1, 2, 3]); + assert!(slicer.pending().is_none()); + } + + #[test] + fn splits_a_wider_batch_into_slices_of_at_most_the_limit() { + let mut slicer = BatchSlicer::new(4); + assert_eq!(values(&slicer.slice(batch(10))), vec![0, 1, 2, 3]); + assert_eq!(values(&slicer.pending().unwrap()), vec![4, 5, 6, 7]); + assert_eq!(values(&slicer.pending().unwrap()), vec![8, 9]); + assert!(slicer.pending().is_none()); + } +} diff --git a/native/core/src/execution/operators/mod.rs b/native/core/src/execution/operators/mod.rs index 49669c58314..2e57365400f 100644 --- a/native/core/src/execution/operators/mod.rs +++ b/native/core/src/execution/operators/mod.rs @@ -24,6 +24,7 @@ pub use scan::*; #[cfg(feature = "python-udf")] mod arrow_python_udf; +mod batch_slicer; #[cfg(feature = "python-udf")] pub use arrow_python_udf::{ArrowPythonUdfExec, ArrowPythonUdfSpec}; mod dynamic_filter; diff --git a/native/core/src/execution/operators/scan.rs b/native/core/src/execution/operators/scan.rs index b3ec14f2e96..da630cb760a 100644 --- a/native/core/src/execution/operators/scan.rs +++ b/native/core/src/execution/operators/scan.rs @@ -15,7 +15,10 @@ // specific language governing permissions and limitations // under the License. -use crate::{errors::CometError, execution::planner::TEST_EXEC_CONTEXT_ID}; +use crate::{ + errors::CometError, + execution::{operators::batch_slicer::BatchSlicer, planner::TEST_EXEC_CONTEXT_ID}, +}; use arrow::array::{ArrayRef, RecordBatch, RecordBatchOptions}; use arrow::compute::{cast_with_options, CastOptions}; use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; @@ -232,13 +235,14 @@ impl ExecutionPlan for ScanExec { fn execute( &self, partition: usize, - _: Arc, + context: Arc, ) -> datafusion::common::Result { Ok(Box::pin(ScanStream::new( self.clone(), self.schema(), partition, self.baseline_metrics.clone(), + context.session_config().batch_size(), ))) } @@ -286,6 +290,8 @@ struct ScanStream<'a> { cast_options: CastOptions<'a>, /// elapsed time for casting columns to different data types during scan cast_time: Time, + /// Splits input batches wider than the session batch size + slicer: BatchSlicer, } impl ScanStream<'_> { @@ -294,6 +300,7 @@ impl ScanStream<'_> { schema: SchemaRef, partition: usize, baseline_metrics: BaselineMetrics, + batch_size: usize, ) -> Self { let cast_time = MetricBuilder::new(&scan.metrics).subset_time("cast_time", partition); Self { @@ -302,6 +309,7 @@ impl ScanStream<'_> { baseline_metrics, cast_options: CastOptions::default(), cast_time, + slicer: BatchSlicer::new(batch_size), } } @@ -365,21 +373,25 @@ impl Stream for ScanStream<'_> { type Item = DataFusionResult; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let mut timer = self.baseline_metrics.elapsed_compute().timer(); - let mut scan_batch = self.scan.batch.try_lock().unwrap(); + let this = self.get_mut(); + if let Some(batch) = this.slicer.pending() { + return Poll::Ready(Some(Ok(batch))); + } + let mut timer = this.baseline_metrics.elapsed_compute().timer(); + let mut scan_batch = this.scan.batch.try_lock().unwrap(); let result = match &*scan_batch { None => { - self.scan.waker.register(cx.waker()); + this.scan.waker.register(cx.waker()); Poll::Pending } // EOF stays buffered: a re-poll ends the stream again and `get_next_batch` has // nothing to pull. Some(InputBatch::EOF) => Poll::Ready(None), Some(InputBatch::Batch(columns, num_rows)) => { - self.baseline_metrics.record_output(*num_rows); - let maybe_batch = self.build_record_batch(columns, *num_rows); - Poll::Ready(Some(maybe_batch)) + this.baseline_metrics.record_output(*num_rows); + let maybe_batch = this.build_record_batch(columns, *num_rows); + Poll::Ready(Some(maybe_batch.map(|batch| this.slicer.slice(batch)))) } }; if matches!(result, Poll::Ready(Some(_))) { diff --git a/native/core/src/execution/operators/shuffle_scan.rs b/native/core/src/execution/operators/shuffle_scan.rs index b12aeafe3b4..09e2862dfe8 100644 --- a/native/core/src/execution/operators/shuffle_scan.rs +++ b/native/core/src/execution/operators/shuffle_scan.rs @@ -46,6 +46,7 @@ use std::{ task::{Context, Poll}, }; +use super::batch_slicer::BatchSlicer; use super::scan::InputBatch; /// ShuffleScanExec reads compressed shuffle blocks from JVM via JNI and decodes them natively. @@ -306,12 +307,13 @@ impl ExecutionPlan for ShuffleScanExec { fn execute( &self, partition: usize, - _: Arc, + context: Arc, ) -> datafusion::common::Result { Ok(Box::pin(ShuffleScanStream::new( self.clone(), partition, self.baseline_metrics.clone(), + context.session_config().batch_size(), ))) } @@ -352,6 +354,8 @@ struct ShuffleScanStream { shuffle_scan: ShuffleScanExec, /// Metrics. baseline_metrics: BaselineMetrics, + /// Splits input batches wider than the session batch size. + slicer: BatchSlicer, } impl ShuffleScanStream { @@ -359,10 +363,12 @@ impl ShuffleScanStream { shuffle_scan: ShuffleScanExec, _partition: usize, baseline_metrics: BaselineMetrics, + batch_size: usize, ) -> Self { Self { shuffle_scan, baseline_metrics, + slicer: BatchSlicer::new(batch_size), } } } @@ -371,30 +377,34 @@ impl Stream for ShuffleScanStream { type Item = DataFusionResult; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let mut timer = self.baseline_metrics.elapsed_compute().timer(); - let mut scan_batch = self.shuffle_scan.batch.try_lock().unwrap(); + let this = self.get_mut(); + if let Some(batch) = this.slicer.pending() { + return Poll::Ready(Some(Ok(batch))); + } + let mut timer = this.baseline_metrics.elapsed_compute().timer(); + let mut scan_batch = this.shuffle_scan.batch.try_lock().unwrap(); let result = match &*scan_batch { None => { - self.shuffle_scan.waker.register(cx.waker()); + this.shuffle_scan.waker.register(cx.waker()); Poll::Pending } // EOF stays buffered: a re-poll ends the stream again and `get_next_batch` has // nothing to pull. Some(InputBatch::EOF) => Poll::Ready(None), Some(InputBatch::Batch(columns, num_rows)) => { - self.baseline_metrics.record_output(*num_rows); + this.baseline_metrics.record_output(*num_rows); // Reconcile the decoded block with the catalyst-declared schema rather than // stamping it on, so that nested field nullability drift is absorbed here the way // `ScanExec` absorbs it at the FFI boundary. // See https://github.com/apache/datafusion-comet/issues/5137. let maybe_batch = cast_and_stamp_schema( - self.shuffle_scan.name(), - &self.shuffle_scan.schema, + this.shuffle_scan.name(), + &this.shuffle_scan.schema, columns.clone(), *num_rows, ); - Poll::Ready(Some(maybe_batch)) + Poll::Ready(Some(maybe_batch.map(|batch| this.slicer.slice(batch)))) } }; if matches!(result, Poll::Ready(Some(_))) { diff --git a/native/core/src/execution/planner.rs b/native/core/src/execution/planner.rs index 899d8ff3320..3645d6b8b07 100644 --- a/native/core/src/execution/planner.rs +++ b/native/core/src/execution/planner.rs @@ -99,6 +99,7 @@ use datafusion::physical_expr::expressions::Literal; use datafusion::physical_expr::LexOrdering; use crate::parquet::parquet_exec::init_datasource_exec; +use arrow::array::RecordBatch; use arrow::array::{ Array, ArrayRef, BinaryBuilder, BooleanArray, Date32Array, Decimal128Array, Float32Array, Float64Array, Int16Array, Int32Array, Int64Array, Int8Array, IntervalYearMonthArray, ListArray, @@ -106,6 +107,7 @@ use arrow::array::{ }; use arrow::buffer::{BooleanBuffer, NullBuffer, OffsetBuffer}; use datafusion::common::utils::SingleRowListArrayBuilder; +use datafusion::logical_expr::ColumnarValue; use datafusion::physical_plan::filter::FilterExec; use datafusion::physical_plan::limit::GlobalLimitExec; use datafusion_comet_proto::spark_expression::ListLiteral; @@ -127,7 +129,13 @@ use jni::objects::{Global, JObject}; use log::warn; use num::{BigInt, ToPrimitive}; use object_store::path::Path; -use std::{collections::HashMap, sync::Arc}; +use std::{ + collections::HashMap, + sync::{ + atomic::{AtomicUsize, Ordering}, + Arc, + }, +}; pub type PlanCreationResult = Result<(Vec, Vec, Arc), ExecutionError>; @@ -207,6 +215,11 @@ pub struct PhysicalPlanner { /// Whether a boundary that would cast a timestamp's timezone label to the declared one fails /// instead. Test-only; see `spark.comet.testing.strictTimestampLabels.enabled`. strict_timestamp_labels: bool, + /// The bytes of every string or binary scalar planned so far, literals and scalar subquery + /// results, added up so that a scalar used twice in one expression counts twice. Bounds the + /// rows per batch; see `scalar_batch_rows` in `jni_api`. Shared by the planner's clones, + /// which plan parts of the same plan. + scalar_bytes: Arc, } impl Default for PhysicalPlanner { @@ -229,9 +242,24 @@ impl PhysicalPlanner { output_stream: None, float_operands: FloatOperands::Normalize, strict_timestamp_labels: false, + scalar_bytes: Arc::new(AtomicUsize::new(0)), } } + /// The bytes of the string and binary scalars in the plans created so far, added up. + pub fn scalar_bytes(&self) -> usize { + self.scalar_bytes.load(Ordering::Relaxed) + } + + fn note_scalar_bytes(&self, value: &ScalarValue) { + let len = match value { + ScalarValue::Utf8(Some(s)) => s.len(), + ScalarValue::Binary(Some(b)) => b.len(), + _ => 0, + }; + self.scalar_bytes.fetch_add(len, Ordering::Relaxed); + } + /// Load the SQL text pool from the root operator of the plan about to be planned. Must be /// called with the *root* operator: the JVM only populates the pool there, and /// `QueryContext.sql_text_idx` values are indices into it. @@ -777,6 +805,7 @@ impl PhysicalPlanner { } } }; + self.note_scalar_bytes(&scalar_value); Ok(Arc::new(DataFusionLiteral::new(scalar_value))) } ExprStruct::Cast(expr) => { @@ -965,7 +994,18 @@ impl PhysicalPlanner { ExprStruct::Subquery(expr) => { let id = expr.id; let data_type = to_arrow_datatype(expr.datatype.as_ref().unwrap()); - Ok(Arc::new(Subquery::new(self.exec_context_id, id, data_type))) + let subquery = Subquery::new(self.exec_context_id, id, data_type); + // The JVM registers the result before the plan is built, so its size is known + // here and can bound the rows per batch. + if matches!(subquery.data_type, DataType::Utf8 | DataType::Binary) + && self.exec_context_id != TEST_EXEC_CONTEXT_ID + { + let no_rows = RecordBatch::new_empty(Arc::new(Schema::empty())); + if let ColumnarValue::Scalar(value) = subquery.evaluate(&no_rows)? { + self.note_scalar_bytes(&value); + } + } + Ok(Arc::new(subquery)) } ExprStruct::BloomFilterMightContain(expr) => { let bloom_filter_expr = self.create_expr( @@ -1758,7 +1798,7 @@ impl PhysicalPlanner { range.num_elements, range.num_slices, self.partition, - self.session_ctx.copied_config().batch_size(), + self.session_ctx.state_weak_ref(), )?); // The range's order is real, and Spark's `RangeExec` reports it, so declare it // for the operators above, a hash join in particular: see `SortedInputExec`. diff --git a/native/operators/src/range.rs b/native/operators/src/range.rs index 4ba4f6d6429..0e2ef4cffa7 100644 --- a/native/operators/src/range.rs +++ b/native/operators/src/range.rs @@ -19,17 +19,21 @@ use arrow::array::{Int64Array, RecordBatch}; use arrow::compute::SortOptions; use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; use datafusion::common::Result; +use datafusion::execution::session_state::SessionState; use datafusion::physical_expr::expressions::Column; use datafusion::physical_expr::PhysicalSortExpr; use datafusion::physical_plan::memory::{LazyBatchGenerator, LazyMemoryExec}; use datafusion::physical_plan::Partitioning; +use datafusion::prelude::SessionConfig; use parking_lot::RwLock; use std::any::Any; use std::fmt; -use std::sync::Arc; +use std::sync::{Arc, Weak}; /// Builds the plan for one Spark partition of `spark.range` or SQL `range()`: a single non-null -/// `BIGINT` column, produced `batch_size` rows at a time. +/// `BIGINT` column, produced at most a session batch size of rows at a time. The size is read +/// from `session` as each batch is made, because a plan holding a large string scalar lowers it +/// after planning (see `scalar_batch_rows` in comet's `jni_api`). /// /// Partition bounds follow Spark's `RangeExec`. The JVM side sends only ranges whose arithmetic /// cannot overflow in Spark, where both Spark's generated code and its interpreted path produce @@ -43,7 +47,7 @@ pub fn range_exec( num_elements: i64, num_slices: i32, partition: i32, - batch_size: usize, + session: Weak>, ) -> Result { let (partition_start, partition_elements) = partition_bounds(start, step, num_elements, num_slices, partition); @@ -53,7 +57,7 @@ pub fn range_exec( step, partition_start, partition_elements, - batch_size, + session, produced: 0, }; let mut exec = LazyMemoryExec::try_new(schema, vec![Arc::new(RwLock::new(generator))])?; @@ -122,11 +126,21 @@ struct RangeGenerator { step: i64, partition_start: i64, partition_elements: i64, - batch_size: usize, + /// The session whose batch size bounds each batch, read when the batch is made. + session: Weak>, /// The number of values produced so far. produced: i64, } +impl RangeGenerator { + fn batch_size(&self) -> usize { + self.session + .upgrade() + .map(|state| state.read().config().batch_size()) + .unwrap_or_else(|| SessionConfig::new().batch_size()) + } +} + impl fmt::Display for RangeGenerator { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( @@ -144,7 +158,7 @@ impl LazyBatchGenerator for RangeGenerator { fn generate_next_batch(&mut self) -> Result> { let first = self.produced; - let count = (self.partition_elements - first).min(self.batch_size as i64); + let count = (self.partition_elements - first).min(self.batch_size() as i64); if count <= 0 { return Ok(None); } @@ -162,8 +176,11 @@ impl LazyBatchGenerator for RangeGenerator { fn reset_state(&self) -> Arc> { Arc::new(RwLock::new(RangeGenerator { schema: Arc::clone(&self.schema), + step: self.step, + partition_start: self.partition_start, + partition_elements: self.partition_elements, + session: Weak::clone(&self.session), produced: 0, - ..*self })) } } @@ -172,7 +189,7 @@ impl LazyBatchGenerator for RangeGenerator { mod tests { use super::*; use datafusion::physical_plan::ExecutionPlan; - use datafusion::prelude::SessionContext; + use datafusion::prelude::{SessionConfig, SessionContext}; use futures::StreamExt; async fn collect( @@ -182,11 +199,18 @@ mod tests { num_slices: i32, batch_size: usize, ) -> Vec { - let ctx = SessionContext::new(); + let ctx = SessionContext::new_with_config(SessionConfig::new().with_batch_size(batch_size)); let mut values = Vec::new(); for partition in 0..num_slices { - let range = - range_exec(start, step, num_elements, num_slices, partition, batch_size).unwrap(); + let range = range_exec( + start, + step, + num_elements, + num_slices, + partition, + ctx.state_weak_ref(), + ) + .unwrap(); let mut stream = range.execute(0, ctx.task_ctx()).unwrap(); while let Some(batch) = stream.next().await { let batch = batch.unwrap(); @@ -239,6 +263,25 @@ mod tests { } } + /// `executePlan` lowers the session's batch size after planning when the plan holds a large + /// string scalar, so the batch size must be read as each batch is made. + #[tokio::test] + async fn reads_the_batch_size_the_session_has_when_a_batch_is_made() { + let ctx = SessionContext::new_with_config(SessionConfig::new().with_batch_size(8192)); + let range = range_exec(0, 1, 10, 1, 0, ctx.state_weak_ref()).unwrap(); + { + let state = ctx.state_ref(); + let mut state = state.write(); + *state.config_mut() = std::mem::take(state.config_mut()).with_batch_size(4); + } + let mut stream = range.execute(0, ctx.task_ctx()).unwrap(); + let mut rows = Vec::new(); + while let Some(batch) = stream.next().await { + rows.push(batch.unwrap().num_rows()); + } + assert_eq!(rows, vec![4, 4, 2]); + } + /// The cases from Spark's `DataFrameRangeSuite` whose bounds are at the edges of the `Long` /// range. #[tokio::test] diff --git a/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql new file mode 100644 index 00000000000..1fc7f056f53 --- /dev/null +++ b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql @@ -0,0 +1,60 @@ +-- 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. + +-- A string scalar, from a scalar subquery or a literal, is expanded to the batch length by the +-- aggregate, the hash partitioner and string kernels. A 256 KiB string over the default 8192-row +-- batch needs 2 GiB of value bytes, past Arrow's 32-bit offsets. Spark evaluates row by row. +-- The ids are written in one task so they arrive in one batch. Constant folding is on so that +-- `repeat('x', 262144)` reaches Comet as the literal it is in a Spark session. +-- See https://github.com/apache/datafusion-comet/issues/7247. +-- Config: spark.sql.adaptive.enabled=false +-- Config: spark.comet.exec.range.enabled=true +-- ConstantFolding: enabled + +statement +CREATE TABLE test_large_scalar_wide(s string) USING parquet + +statement +INSERT INTO test_large_scalar_wide SELECT repeat('x', 262144) + +statement +CREATE TABLE test_large_scalar_ids(k int) USING parquet + +statement +INSERT INTO test_large_scalar_ids SELECT /*+ COALESCE(1) */ CAST(id % 2 AS INT) FROM range(8192) + +-- count of the scalar in a grouped aggregate. The plan runs natively and panicked before the +-- fix; the answer-only mode is because the harness's missingInput check trips on a grouped +-- count of a scalar subquery whatever the string's size, which is a separate plan issue. +query spark_answer_only +SELECT k, count((SELECT max(s) FROM test_large_scalar_wide)) FROM test_large_scalar_ids GROUP BY k ORDER BY k + +-- the scalar as a hash partitioning key +query +SELECT count(*) FROM (SELECT k FROM test_large_scalar_ids DISTRIBUTE BY k, (SELECT max(s) FROM test_large_scalar_wide)) + +-- a string kernel whose output exceeds 2 GiB per 8192-row batch +query +SELECT sum(length(c)) FROM (SELECT concat((SELECT max(s) FROM test_large_scalar_wide), CAST(k AS STRING)) AS c FROM test_large_scalar_ids) + +-- the same with a literal instead of a subquery +query +SELECT sum(length(c)) FROM (SELECT concat(repeat('x', 262144), CAST(k AS STRING)) AS c FROM test_large_scalar_ids) + +-- the literal over the native range source, which sizes its own batches, in one slice +query +SELECT sum(length(concat(repeat('x', 262144), CAST(id AS STRING)))) FROM range(0, 8192, 1, 1) From 0e9745e39fa25ee0e123c3fe8229747db3b5fcca Mon Sep 17 00:00:00 2001 From: zhangfengcdt Date: Thu, 29 Oct 2026 20:09:34 -0700 Subject: [PATCH 2/9] fix: bound the batch under the operator that expands a large string scalar Rework after review. Capping the input batch size does not bound what reaches the expanding operator: a filter coalesces its output to its own batch size, a window emits its whole partition, and a scalar built from a subquery result can be wider than the result. The scan slicers also dropped a pending slice under LIMIT and double counted output_rows. A new CometBatchSplitExec, over DataFusion's BatchSplitStream, is placed right under a projection, filter, hash aggregate or hash shuffle writer when that operator's string and binary scalars, expanded to the session batch size, would exceed 1 GiB. The planner measures each column-free subtree of those types by evaluating it once on a one-row batch, so that repeat(, n) counts at its final width. IN list members are compared and never expanded, so they are skipped. The session batch size, the scan and shuffle scan operators and the range source are unchanged. --- native/core/src/execution/jni_api.rs | 40 --- .../src/execution/operators/batch_slicer.rs | 90 ------- native/core/src/execution/operators/mod.rs | 1 - .../src/execution/operators/projection.rs | 7 +- native/core/src/execution/operators/scan.rs | 28 +-- .../src/execution/operators/shuffle_scan.rs | 26 +- native/core/src/execution/planner.rs | 55 +---- .../core/src/execution/planner/aggregate.rs | 12 +- .../src/execution/planner/scalar_expansion.rs | 229 ++++++++++++++++++ native/core/src/execution/planner/shuffle.rs | 6 + native/operators/src/batch_split.rs | 174 +++++++++++++ native/operators/src/lib.rs | 2 + native/operators/src/range.rs | 65 +---- .../operators/large_scalar_string.sql | 22 +- 14 files changed, 477 insertions(+), 280 deletions(-) delete mode 100644 native/core/src/execution/operators/batch_slicer.rs create mode 100644 native/core/src/execution/planner/scalar_expansion.rs create mode 100644 native/operators/src/batch_split.rs diff --git a/native/core/src/execution/jni_api.rs b/native/core/src/execution/jni_api.rs index 6263d2f3e95..494e7acdff9 100644 --- a/native/core/src/execution/jni_api.rs +++ b/native/core/src/execution/jni_api.rs @@ -1056,24 +1056,6 @@ fn configure_skip_partial_aggregation(config: &mut SessionConfig, plan: &Operato } } -/// The most value bytes a string or binary scalar may take once expanded to a batch's length. -/// Arrow's `Utf8` and `Binary` offsets are 32-bit, so 2 GiB is the hard limit; half of it leaves -/// room for kernels that append to the expanded value, such as `concat`, and matches what an -/// Arrow Java vector can hold before its reallocation fails. -const SCALAR_EXPANSION_BYTES: usize = 1 << 30; - -/// The rows per batch under which the plan's string and binary scalars, `scalar_bytes` in all, -/// expanded to the batch's length by an aggregate, a partitioner or a string kernel, stay within -/// `SCALAR_EXPANSION_BYTES`. Spark evaluates such scalars row by row and never materializes -/// the column, so this is what keeps a query with a large literal or scalar subquery result -/// running natively. -fn scalar_batch_rows(scalar_bytes: usize, batch_size: usize) -> usize { - if scalar_bytes == 0 { - return batch_size; - } - batch_size.min((SCALAR_EXPANSION_BYTES / scalar_bytes).max(1)) -} - /// Configure DataFusion session context. fn prepare_datafusion_session_context( batch_size: usize, @@ -1573,17 +1555,6 @@ fn execute_plan( exec_context.scans = scans; exec_context.shuffle_scans = shuffle_scans; - // The task context created below copies the session config, so every source and the - // JVM-fed leaves see the smaller batch size. - let batch_size = exec_context.session_ctx.copied_config().batch_size(); - let rows = scalar_batch_rows(planner.scalar_bytes(), batch_size); - if rows < batch_size { - let state = exec_context.session_ctx.state_ref(); - let mut state = state.write(); - let config = state.config_mut(); - *config = std::mem::take(config).with_batch_size(rows); - } - if exec_context.explain_native { let formatted_plan_str = DisplayableExecutionPlan::new(root_op.native_plan.as_ref()).indent(true); @@ -2567,17 +2538,6 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_columnarToRowClose( mod tests { use super::*; use crate::execution::operators::InputBatch; - - #[test] - fn scalar_batch_rows_keeps_the_batch_size_unless_a_scalar_would_overflow_it() { - // No string or binary scalar, or a small one: the batch size stands. - assert_eq!(scalar_batch_rows(0, 8192), 8192); - assert_eq!(scalar_batch_rows(1024, 8192), 8192); - // A 256 KiB scalar expanded to 8192 rows needs 2 GiB; 4096 rows keep it at 1 GiB. - assert_eq!(scalar_batch_rows(256 * 1024, 8192), 4096); - // A scalar past 1 GiB still gets one row per batch. - assert_eq!(scalar_batch_rows(2 * 1024 * 1024 * 1024, 8192), 1); - } use crate::execution::planner::TEST_EXEC_CONTEXT_ID; use arrow::array::{ArrayRef, Int32Array}; use arrow::datatypes::{DataType, Field, Schema}; diff --git a/native/core/src/execution/operators/batch_slicer.rs b/native/core/src/execution/operators/batch_slicer.rs deleted file mode 100644 index 8b74a549424..00000000000 --- a/native/core/src/execution/operators/batch_slicer.rs +++ /dev/null @@ -1,90 +0,0 @@ -// 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. - -use arrow::array::RecordBatch; - -/// Splits the batches a JVM-fed leaf receives so that none is wider than the session batch -/// size. The JVM sizes those batches itself, so this is the one place a smaller native batch -/// size takes effect on them; see `scalar_batch_rows` in `jni_api` for why it can be smaller. -/// Slices share the input's buffers, so splitting copies nothing. -pub(crate) struct BatchSlicer { - max_rows: usize, - rest: Option, -} - -impl BatchSlicer { - pub fn new(max_rows: usize) -> Self { - Self { - max_rows: max_rows.max(1), - rest: None, - } - } - - /// Takes a batch in and returns its first slice, holding the remainder for `pending`. - pub fn slice(&mut self, batch: RecordBatch) -> RecordBatch { - if batch.num_rows() <= self.max_rows { - return batch; - } - let head = batch.slice(0, self.max_rows); - self.rest = Some(batch.slice(self.max_rows, batch.num_rows() - self.max_rows)); - head - } - - /// The next slice of the batch last passed to `slice`, if any of it is left. - pub fn pending(&mut self) -> Option { - self.rest.take().map(|rest| self.slice(rest)) - } -} - -#[cfg(test)] -mod tests { - use super::BatchSlicer; - use arrow::array::{Int32Array, RecordBatch}; - use arrow::datatypes::{DataType, Field, Schema}; - use std::sync::Arc; - - fn batch(n: i32) -> RecordBatch { - let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])); - RecordBatch::try_new(schema, vec![Arc::new(Int32Array::from_iter_values(0..n))]).unwrap() - } - - fn values(batch: &RecordBatch) -> Vec { - batch - .column(0) - .as_any() - .downcast_ref::() - .unwrap() - .values() - .to_vec() - } - - #[test] - fn passes_a_batch_within_the_limit_through() { - let mut slicer = BatchSlicer::new(4); - assert_eq!(values(&slicer.slice(batch(4))), vec![0, 1, 2, 3]); - assert!(slicer.pending().is_none()); - } - - #[test] - fn splits_a_wider_batch_into_slices_of_at_most_the_limit() { - let mut slicer = BatchSlicer::new(4); - assert_eq!(values(&slicer.slice(batch(10))), vec![0, 1, 2, 3]); - assert_eq!(values(&slicer.pending().unwrap()), vec![4, 5, 6, 7]); - assert_eq!(values(&slicer.pending().unwrap()), vec![8, 9]); - assert!(slicer.pending().is_none()); - } -} diff --git a/native/core/src/execution/operators/mod.rs b/native/core/src/execution/operators/mod.rs index 2e57365400f..49669c58314 100644 --- a/native/core/src/execution/operators/mod.rs +++ b/native/core/src/execution/operators/mod.rs @@ -24,7 +24,6 @@ pub use scan::*; #[cfg(feature = "python-udf")] mod arrow_python_udf; -mod batch_slicer; #[cfg(feature = "python-udf")] pub use arrow_python_udf::{ArrowPythonUdfExec, ArrowPythonUdfSpec}; mod dynamic_filter; diff --git a/native/core/src/execution/operators/projection.rs b/native/core/src/execution/operators/projection.rs index f22efc53c8e..7933c0dbfda 100644 --- a/native/core/src/execution/operators/projection.rs +++ b/native/core/src/execution/operators/projection.rs @@ -80,10 +80,11 @@ impl OperatorBuilder for ProjectionBuilder { } } } - let projection = Arc::new(ProjectionExec::try_new( - exprs.iter().cloned(), + let input = planner.bound_scalar_expansion( Arc::clone(&child.native_plan), - )?); + exprs.iter().map(|expr| &expr.expr), + ); + let projection = Arc::new(ProjectionExec::try_new(exprs.iter().cloned(), input)?); let spark_plan = if child.plan_id == spark_plan.plan_id { let mut additional_native_plans = vec![Arc::clone(&child.native_plan)]; diff --git a/native/core/src/execution/operators/scan.rs b/native/core/src/execution/operators/scan.rs index da630cb760a..b3ec14f2e96 100644 --- a/native/core/src/execution/operators/scan.rs +++ b/native/core/src/execution/operators/scan.rs @@ -15,10 +15,7 @@ // specific language governing permissions and limitations // under the License. -use crate::{ - errors::CometError, - execution::{operators::batch_slicer::BatchSlicer, planner::TEST_EXEC_CONTEXT_ID}, -}; +use crate::{errors::CometError, execution::planner::TEST_EXEC_CONTEXT_ID}; use arrow::array::{ArrayRef, RecordBatch, RecordBatchOptions}; use arrow::compute::{cast_with_options, CastOptions}; use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; @@ -235,14 +232,13 @@ impl ExecutionPlan for ScanExec { fn execute( &self, partition: usize, - context: Arc, + _: Arc, ) -> datafusion::common::Result { Ok(Box::pin(ScanStream::new( self.clone(), self.schema(), partition, self.baseline_metrics.clone(), - context.session_config().batch_size(), ))) } @@ -290,8 +286,6 @@ struct ScanStream<'a> { cast_options: CastOptions<'a>, /// elapsed time for casting columns to different data types during scan cast_time: Time, - /// Splits input batches wider than the session batch size - slicer: BatchSlicer, } impl ScanStream<'_> { @@ -300,7 +294,6 @@ impl ScanStream<'_> { schema: SchemaRef, partition: usize, baseline_metrics: BaselineMetrics, - batch_size: usize, ) -> Self { let cast_time = MetricBuilder::new(&scan.metrics).subset_time("cast_time", partition); Self { @@ -309,7 +302,6 @@ impl ScanStream<'_> { baseline_metrics, cast_options: CastOptions::default(), cast_time, - slicer: BatchSlicer::new(batch_size), } } @@ -373,25 +365,21 @@ impl Stream for ScanStream<'_> { type Item = DataFusionResult; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let this = self.get_mut(); - if let Some(batch) = this.slicer.pending() { - return Poll::Ready(Some(Ok(batch))); - } - let mut timer = this.baseline_metrics.elapsed_compute().timer(); - let mut scan_batch = this.scan.batch.try_lock().unwrap(); + let mut timer = self.baseline_metrics.elapsed_compute().timer(); + let mut scan_batch = self.scan.batch.try_lock().unwrap(); let result = match &*scan_batch { None => { - this.scan.waker.register(cx.waker()); + self.scan.waker.register(cx.waker()); Poll::Pending } // EOF stays buffered: a re-poll ends the stream again and `get_next_batch` has // nothing to pull. Some(InputBatch::EOF) => Poll::Ready(None), Some(InputBatch::Batch(columns, num_rows)) => { - this.baseline_metrics.record_output(*num_rows); - let maybe_batch = this.build_record_batch(columns, *num_rows); - Poll::Ready(Some(maybe_batch.map(|batch| this.slicer.slice(batch)))) + self.baseline_metrics.record_output(*num_rows); + let maybe_batch = self.build_record_batch(columns, *num_rows); + Poll::Ready(Some(maybe_batch)) } }; if matches!(result, Poll::Ready(Some(_))) { diff --git a/native/core/src/execution/operators/shuffle_scan.rs b/native/core/src/execution/operators/shuffle_scan.rs index 09e2862dfe8..b12aeafe3b4 100644 --- a/native/core/src/execution/operators/shuffle_scan.rs +++ b/native/core/src/execution/operators/shuffle_scan.rs @@ -46,7 +46,6 @@ use std::{ task::{Context, Poll}, }; -use super::batch_slicer::BatchSlicer; use super::scan::InputBatch; /// ShuffleScanExec reads compressed shuffle blocks from JVM via JNI and decodes them natively. @@ -307,13 +306,12 @@ impl ExecutionPlan for ShuffleScanExec { fn execute( &self, partition: usize, - context: Arc, + _: Arc, ) -> datafusion::common::Result { Ok(Box::pin(ShuffleScanStream::new( self.clone(), partition, self.baseline_metrics.clone(), - context.session_config().batch_size(), ))) } @@ -354,8 +352,6 @@ struct ShuffleScanStream { shuffle_scan: ShuffleScanExec, /// Metrics. baseline_metrics: BaselineMetrics, - /// Splits input batches wider than the session batch size. - slicer: BatchSlicer, } impl ShuffleScanStream { @@ -363,12 +359,10 @@ impl ShuffleScanStream { shuffle_scan: ShuffleScanExec, _partition: usize, baseline_metrics: BaselineMetrics, - batch_size: usize, ) -> Self { Self { shuffle_scan, baseline_metrics, - slicer: BatchSlicer::new(batch_size), } } } @@ -377,34 +371,30 @@ impl Stream for ShuffleScanStream { type Item = DataFusionResult; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let this = self.get_mut(); - if let Some(batch) = this.slicer.pending() { - return Poll::Ready(Some(Ok(batch))); - } - let mut timer = this.baseline_metrics.elapsed_compute().timer(); - let mut scan_batch = this.shuffle_scan.batch.try_lock().unwrap(); + let mut timer = self.baseline_metrics.elapsed_compute().timer(); + let mut scan_batch = self.shuffle_scan.batch.try_lock().unwrap(); let result = match &*scan_batch { None => { - this.shuffle_scan.waker.register(cx.waker()); + self.shuffle_scan.waker.register(cx.waker()); Poll::Pending } // EOF stays buffered: a re-poll ends the stream again and `get_next_batch` has // nothing to pull. Some(InputBatch::EOF) => Poll::Ready(None), Some(InputBatch::Batch(columns, num_rows)) => { - this.baseline_metrics.record_output(*num_rows); + self.baseline_metrics.record_output(*num_rows); // Reconcile the decoded block with the catalyst-declared schema rather than // stamping it on, so that nested field nullability drift is absorbed here the way // `ScanExec` absorbs it at the FFI boundary. // See https://github.com/apache/datafusion-comet/issues/5137. let maybe_batch = cast_and_stamp_schema( - this.shuffle_scan.name(), - &this.shuffle_scan.schema, + self.shuffle_scan.name(), + &self.shuffle_scan.schema, columns.clone(), *num_rows, ); - Poll::Ready(Some(maybe_batch.map(|batch| this.slicer.slice(batch)))) + Poll::Ready(Some(maybe_batch)) } }; if matches!(result, Poll::Ready(Some(_))) { diff --git a/native/core/src/execution/planner.rs b/native/core/src/execution/planner.rs index 3645d6b8b07..cf5fd4f6845 100644 --- a/native/core/src/execution/planner.rs +++ b/native/core/src/execution/planner.rs @@ -24,6 +24,7 @@ mod join; pub mod macros; mod merge_rows; pub mod operator_registry; +mod scalar_expansion; mod shuffle; mod window; mod write; @@ -99,7 +100,6 @@ use datafusion::physical_expr::expressions::Literal; use datafusion::physical_expr::LexOrdering; use crate::parquet::parquet_exec::init_datasource_exec; -use arrow::array::RecordBatch; use arrow::array::{ Array, ArrayRef, BinaryBuilder, BooleanArray, Date32Array, Decimal128Array, Float32Array, Float64Array, Int16Array, Int32Array, Int64Array, Int8Array, IntervalYearMonthArray, ListArray, @@ -107,7 +107,6 @@ use arrow::array::{ }; use arrow::buffer::{BooleanBuffer, NullBuffer, OffsetBuffer}; use datafusion::common::utils::SingleRowListArrayBuilder; -use datafusion::logical_expr::ColumnarValue; use datafusion::physical_plan::filter::FilterExec; use datafusion::physical_plan::limit::GlobalLimitExec; use datafusion_comet_proto::spark_expression::ListLiteral; @@ -129,13 +128,7 @@ use jni::objects::{Global, JObject}; use log::warn; use num::{BigInt, ToPrimitive}; use object_store::path::Path; -use std::{ - collections::HashMap, - sync::{ - atomic::{AtomicUsize, Ordering}, - Arc, - }, -}; +use std::{collections::HashMap, sync::Arc}; pub type PlanCreationResult = Result<(Vec, Vec, Arc), ExecutionError>; @@ -215,11 +208,6 @@ pub struct PhysicalPlanner { /// Whether a boundary that would cast a timestamp's timezone label to the declared one fails /// instead. Test-only; see `spark.comet.testing.strictTimestampLabels.enabled`. strict_timestamp_labels: bool, - /// The bytes of every string or binary scalar planned so far, literals and scalar subquery - /// results, added up so that a scalar used twice in one expression counts twice. Bounds the - /// rows per batch; see `scalar_batch_rows` in `jni_api`. Shared by the planner's clones, - /// which plan parts of the same plan. - scalar_bytes: Arc, } impl Default for PhysicalPlanner { @@ -242,24 +230,9 @@ impl PhysicalPlanner { output_stream: None, float_operands: FloatOperands::Normalize, strict_timestamp_labels: false, - scalar_bytes: Arc::new(AtomicUsize::new(0)), } } - /// The bytes of the string and binary scalars in the plans created so far, added up. - pub fn scalar_bytes(&self) -> usize { - self.scalar_bytes.load(Ordering::Relaxed) - } - - fn note_scalar_bytes(&self, value: &ScalarValue) { - let len = match value { - ScalarValue::Utf8(Some(s)) => s.len(), - ScalarValue::Binary(Some(b)) => b.len(), - _ => 0, - }; - self.scalar_bytes.fetch_add(len, Ordering::Relaxed); - } - /// Load the SQL text pool from the root operator of the plan about to be planned. Must be /// called with the *root* operator: the JVM only populates the pool there, and /// `QueryContext.sql_text_idx` values are indices into it. @@ -805,7 +778,6 @@ impl PhysicalPlanner { } } }; - self.note_scalar_bytes(&scalar_value); Ok(Arc::new(DataFusionLiteral::new(scalar_value))) } ExprStruct::Cast(expr) => { @@ -994,18 +966,7 @@ impl PhysicalPlanner { ExprStruct::Subquery(expr) => { let id = expr.id; let data_type = to_arrow_datatype(expr.datatype.as_ref().unwrap()); - let subquery = Subquery::new(self.exec_context_id, id, data_type); - // The JVM registers the result before the plan is built, so its size is known - // here and can bound the rows per batch. - if matches!(subquery.data_type, DataType::Utf8 | DataType::Binary) - && self.exec_context_id != TEST_EXEC_CONTEXT_ID - { - let no_rows = RecordBatch::new_empty(Arc::new(Schema::empty())); - if let ColumnarValue::Scalar(value) = subquery.evaluate(&no_rows)? { - self.note_scalar_bytes(&value); - } - } - Ok(Arc::new(subquery)) + Ok(Arc::new(Subquery::new(self.exec_context_id, id, data_type))) } ExprStruct::BloomFilterMightContain(expr) => { let bloom_filter_expr = self.create_expr( @@ -1745,10 +1706,10 @@ impl PhysicalPlanner { let predicate = self.create_expr(filter.predicate.as_ref().unwrap(), child.schema())?; - let filter: Arc = Arc::new(FilterExec::try_new( - predicate, - Arc::clone(&child.native_plan), - )?); + let input = + self.bound_scalar_expansion(Arc::clone(&child.native_plan), [&predicate]); + let filter: Arc = + Arc::new(FilterExec::try_new(predicate, input)?); Ok(( scans, @@ -1798,7 +1759,7 @@ impl PhysicalPlanner { range.num_elements, range.num_slices, self.partition, - self.session_ctx.state_weak_ref(), + self.session_ctx.copied_config().batch_size(), )?); // The range's order is real, and Spark's `RangeExec` reports it, so declare it // for the operators above, a hash join in particular: see `SortedInputExec`. diff --git a/native/core/src/execution/planner/aggregate.rs b/native/core/src/execution/planner/aggregate.rs index 03dc75e35d2..9ed338ec01c 100644 --- a/native/core/src/execution/planner/aggregate.rs +++ b/native/core/src/execution/planner/aggregate.rs @@ -275,13 +275,21 @@ impl OperatorBuilder for HashAggBuilder { mode }; + let filter_exprs = filter_exprs?; + let input_exprs: Vec> = group_by + .input_exprs() + .into_iter() + .chain(aggr_expr.iter().flat_map(|expr| expr.expressions())) + .chain(filter_exprs.iter().flatten().cloned()) + .collect(); + let input = planner.bound_scalar_expansion(Arc::clone(&child.native_plan), &input_exprs); let aggregate: Arc = Arc::new( datafusion::physical_plan::aggregates::AggregateExec::try_new( mode, group_by, aggr_expr, - filter_exprs?, - Arc::clone(&child.native_plan), + filter_exprs, + input, Arc::clone(&schema), )?, ); diff --git a/native/core/src/execution/planner/scalar_expansion.rs b/native/core/src/execution/planner/scalar_expansion.rs new file mode 100644 index 00000000000..ebadd8d4911 --- /dev/null +++ b/native/core/src/execution/planner/scalar_expansion.rs @@ -0,0 +1,229 @@ +// 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. + +//! Bounds the rows per batch under an operator that expands a large string scalar. +//! +//! An aggregate, a hash partitioner and most string kernels expand a scalar argument to the +//! batch's length: a 256 KiB string over the default 8192-row batch needs 2 GiB of value bytes, +//! past the 32-bit offsets of Arrow's `Utf8` and `Binary` arrays. Spark evaluates such scalars +//! row by row and never materializes the column. See +//! . +//! +//! The bound is enforced right under the operator that evaluates the expressions, with a +//! [`BatchSplitExec`], because the operators below do not keep the batch size they were handed: +//! a filter coalesces its output up to its own size and a window emits its whole partition. + +use std::sync::Arc; + +use arrow::datatypes::{DataType, Schema}; +use arrow::record_batch::{RecordBatch, RecordBatchOptions}; +use datafusion::common::ScalarValue; +use datafusion::logical_expr::ColumnarValue; +use datafusion::physical_expr::expressions::{Column, InListExpr}; +use datafusion::physical_expr::PhysicalExpr; +use datafusion::physical_plan::ExecutionPlan; +use datafusion_comet_operators::BatchSplitExec; +use datafusion_comet_spark_expr::jvm_udf::JvmScalarUdfExpr; +use datafusion_comet_spark_expr::Subquery; + +use super::{PhysicalPlanner, TEST_EXEC_CONTEXT_ID}; + +/// The most value bytes the string and binary scalars of one operator may take once expanded to +/// a batch's length. Arrow's 32-bit offsets make 2 GiB the hard limit; half of it leaves room for +/// kernels that append to the expanded value, such as `concat`, and matches what an Arrow Java +/// vector can hold before its reallocation fails. +const SCALAR_EXPANSION_BYTES: usize = 1 << 30; + +/// The rows per batch under which `scalar_bytes`, expanded to the batch's length, stay within +/// `SCALAR_EXPANSION_BYTES`. +fn scalar_batch_rows(scalar_bytes: usize, batch_size: usize) -> usize { + if scalar_bytes == 0 { + return batch_size; + } + batch_size.min((SCALAR_EXPANSION_BYTES / scalar_bytes).max(1)) +} + +impl PhysicalPlanner { + /// Wraps `input` so that the batches it emits keep the string and binary scalars in `exprs`, + /// expanded to a batch's length, within `SCALAR_EXPANSION_BYTES`. Returns `input` as is when + /// the session's batch size already does. + pub(crate) fn bound_scalar_expansion<'a>( + &self, + input: Arc, + exprs: impl IntoIterator>, + ) -> Arc { + let schema = input.schema(); + let bytes: usize = exprs + .into_iter() + .map(|expr| self.expanded_scalar_bytes(expr, &schema)) + .sum(); + let batch_size = self.session_ctx.copied_config().batch_size(); + let rows = scalar_batch_rows(bytes, batch_size); + if rows < batch_size { + Arc::new(BatchSplitExec::new(input, rows)) + } else { + input + } + } + + /// The bytes of the string and binary scalars that evaluating `expr` expands to the batch's + /// length, added up, so that a scalar used twice counts twice. + /// + /// A scalar is any subtree without a column reference, evaluated here on a one-row batch: + /// `repeat(, 8)` is wider than the subquery result it is built from. A subtree + /// that fails to evaluate counts nothing, so that its error surfaces where Spark's would, + /// at execution. The members of an `IN` list are compared and never expanded. + fn expanded_scalar_bytes(&self, expr: &Arc, schema: &Schema) -> usize { + if let Some(in_list) = expr.downcast_ref::() { + return self.expanded_scalar_bytes(in_list.expr(), schema); + } + if !is_column_free(expr) { + return expr + .children() + .into_iter() + .map(|child| self.expanded_scalar_bytes(child, schema)) + .sum(); + } + if !matches!( + expr.data_type(schema), + Ok(DataType::Utf8 | DataType::Binary) + ) { + return 0; + } + // A JVM UDF is the user's code, which may have side effects or be slow, and a planner + // unit test has no JVM to read a subquery result from. + if contains(expr, &|e| e.is::()) + || (self.exec_context_id == TEST_EXEC_CONTEXT_ID + && contains(expr, &|e| e.is::())) + { + return 0; + } + // One row, so that a kernel that expands its scalar arguments still yields one value. + let Ok(one_row) = RecordBatch::try_new_with_options( + Arc::new(Schema::empty()), + vec![], + &RecordBatchOptions::new().with_row_count(Some(1)), + ) else { + return 0; + }; + let value = match expr.evaluate(&one_row) { + Ok(ColumnarValue::Scalar(value)) => value, + Ok(ColumnarValue::Array(array)) if array.len() == 1 => { + match ScalarValue::try_from_array(&array, 0) { + Ok(value) => value, + Err(_) => return 0, + } + } + _ => return 0, + }; + match value { + ScalarValue::Utf8(Some(s)) => s.len(), + ScalarValue::Binary(Some(b)) => b.len(), + _ => 0, + } + } +} + +fn is_column_free(expr: &Arc) -> bool { + !expr.is::() && expr.children().iter().all(|child| is_column_free(child)) +} + +fn contains(expr: &Arc, is: &dyn Fn(&Arc) -> bool) -> bool { + is(expr) || expr.children().iter().any(|child| contains(child, is)) +} + +#[cfg(test)] +mod tests { + use super::*; + use arrow::datatypes::Field; + use datafusion::logical_expr::Operator; + use datafusion::physical_expr::expressions::{binary, col, in_list, lit}; + + fn schema() -> Schema { + Schema::new(vec![ + Field::new("k", DataType::Int32, false), + Field::new("s", DataType::Utf8, false), + ]) + } + + fn bytes(expr: Arc) -> usize { + PhysicalPlanner::default().expanded_scalar_bytes(&expr, &schema()) + } + + #[test] + fn scalar_batch_rows_keeps_the_batch_size_unless_a_scalar_would_overflow_it() { + assert_eq!(scalar_batch_rows(0, 8192), 8192); + assert_eq!(scalar_batch_rows(1024, 8192), 8192); + // A 256 KiB scalar expanded to 8192 rows needs 2 GiB; 4096 rows keep it at 1 GiB. + assert_eq!(scalar_batch_rows(256 * 1024, 8192), 4096); + // A scalar past 1 GiB still gets one row per batch. + assert_eq!(scalar_batch_rows(2 * 1024 * 1024 * 1024, 8192), 1); + } + + #[test] + fn counts_string_scalars_and_not_columns() { + assert_eq!(bytes(lit("abcd")), 4); + assert_eq!(bytes(lit(7)), 0); + assert_eq!(bytes(col("s", &schema()).unwrap()), 0); + } + + #[test] + fn counts_a_scalar_built_by_an_expression_at_its_final_width() { + let schema = schema(); + let built = binary(lit("abcd"), Operator::StringConcat, lit("efgh"), &schema).unwrap(); + assert_eq!(bytes(Arc::clone(&built)), 8); + // Under a column reference the scalar subtree still counts, once per use. + let over_column = binary( + Arc::clone(&built), + Operator::StringConcat, + col("s", &schema).unwrap(), + &schema, + ) + .unwrap(); + assert_eq!(bytes(Arc::clone(&over_column)), 8); + let twice = binary(over_column, Operator::StringConcat, built, &schema).unwrap(); + assert_eq!(bytes(twice), 16); + } + + #[test] + fn skips_the_members_of_an_in_list() { + let schema = schema(); + let expr = in_list( + col("s", &schema).unwrap(), + vec![lit("abcd"), lit("efgh")], + &false, + &schema, + ) + .unwrap(); + assert_eq!(bytes(expr), 0); + } + + #[test] + fn wraps_the_input_only_when_the_scalars_need_a_smaller_batch() { + use datafusion::physical_plan::empty::EmptyExec; + let planner = PhysicalPlanner::default(); + let input: Arc = Arc::new(EmptyExec::new(Arc::new(schema()))); + let small = planner.bound_scalar_expansion(Arc::clone(&input), [&lit("abcd")]); + assert_eq!("EmptyExec", small.name()); + let wide = lit("x".repeat(256 * 1024)); + let bounded = planner.bound_scalar_expansion(input, [&wide, &wide]); + let split = bounded + .downcast_ref::() + .expect("a split under the expanding operator"); + assert_eq!(2048, split.batch_size()); + } +} diff --git a/native/core/src/execution/planner/shuffle.rs b/native/core/src/execution/planner/shuffle.rs index f5fb1f869ac..cd2f6801380 100644 --- a/native/core/src/execution/planner/shuffle.rs +++ b/native/core/src/execution/planner/shuffle.rs @@ -85,6 +85,12 @@ impl OperatorBuilder for ShuffleWriterBuilder { let partitioning = planner .create_partitioning(writer.partitioning.as_ref().unwrap(), writer_input.schema())?; + let writer_input = match &partitioning { + CometPartitioning::Hash(exprs, _) => { + planner.bound_scalar_expansion(writer_input, exprs) + } + _ => writer_input, + }; let codec = match writer.codec.try_into() { Ok(SparkCompressionCodec::None) => Ok(CompressionCodec::None), diff --git a/native/operators/src/batch_split.rs b/native/operators/src/batch_split.rs new file mode 100644 index 00000000000..81aef57a7e9 --- /dev/null +++ b/native/operators/src/batch_split.rs @@ -0,0 +1,174 @@ +// 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. + +//! An operator that bounds the rows per batch its parent receives. + +use datafusion::common::tree_node::TreeNodeRecursion; +use datafusion::common::Result; +use datafusion::execution::TaskContext; +use datafusion::physical_expr::PhysicalExpr; +use datafusion::physical_plan::metrics::{ExecutionPlanMetricsSet, MetricsSet, SplitMetrics}; +use datafusion::physical_plan::stream::BatchSplitStream; +use datafusion::physical_plan::{ + DisplayAs, DisplayFormatType, ExecutionPlan, ExecutionPlanProperties, PlanProperties, + SendableRecordBatchStream, +}; +use std::fmt::Formatter; +use std::sync::Arc; + +/// Passes its input through in batches of at most `batch_size` rows, slicing wider ones without +/// copying. The planner puts it under an operator whose expressions expand a large string or +/// binary scalar to the batch's length, so that the expanded value fits Arrow's 32-bit offsets +/// whatever batch size the operators below settle on. +#[derive(Debug)] +pub struct BatchSplitExec { + input: Arc, + batch_size: usize, + metrics: ExecutionPlanMetricsSet, + cache: Arc, +} + +impl BatchSplitExec { + pub fn new(input: Arc, batch_size: usize) -> Self { + // Slicing keeps every row in place, so the input's ordering and partitioning stand. + let cache = Arc::new(PlanProperties::new( + input.equivalence_properties().clone(), + input.output_partitioning().clone(), + input.pipeline_behavior(), + input.boundedness(), + )); + Self { + input, + batch_size: batch_size.max(1), + metrics: ExecutionPlanMetricsSet::new(), + cache, + } + } + + pub fn batch_size(&self) -> usize { + self.batch_size + } +} + +impl DisplayAs for BatchSplitExec { + fn fmt_as(&self, t: DisplayFormatType, f: &mut Formatter) -> std::fmt::Result { + match t { + DisplayFormatType::Default | DisplayFormatType::Verbose => { + write!(f, "CometBatchSplitExec: batch_size={}", self.batch_size) + } + DisplayFormatType::TreeRender => unimplemented!(), + } + } +} + +impl ExecutionPlan for BatchSplitExec { + fn name(&self) -> &str { + "CometBatchSplitExec" + } + + fn properties(&self) -> &Arc { + &self.cache + } + + fn children(&self) -> Vec<&Arc> { + vec![&self.input] + } + + fn maintains_input_order(&self) -> Vec { + vec![true] + } + + fn apply_expressions( + &self, + _f: &mut dyn FnMut(&Arc) -> Result, + ) -> Result { + Ok(TreeNodeRecursion::Continue) + } + + fn with_new_children( + self: Arc, + children: Vec>, + ) -> Result> { + Ok(Arc::new(BatchSplitExec::new( + Arc::clone(&children[0]), + self.batch_size, + ))) + } + + fn execute( + &self, + partition: usize, + context: Arc, + ) -> Result { + let input = self.input.execute(partition, context)?; + Ok(Box::pin(BatchSplitStream::new( + input, + self.batch_size, + SplitMetrics::new(&self.metrics, partition), + ))) + } + + fn metrics(&self) -> Option { + Some(self.metrics.clone_inner()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use arrow::array::{Int32Array, RecordBatch}; + use arrow::datatypes::{DataType, Field, Schema}; + use datafusion::datasource::memory::MemorySourceConfig; + use datafusion::datasource::source::DataSourceExec; + use datafusion::prelude::SessionContext; + use futures::StreamExt; + + fn batch(values: Vec) -> RecordBatch { + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)])); + RecordBatch::try_new(schema, vec![Arc::new(Int32Array::from(values))]).unwrap() + } + + /// The rows of each batch the operator emits for `batches`, split at `batch_size`. + async fn split(batches: Vec, batch_size: usize) -> Vec> { + let schema = batches[0].schema(); + let config = MemorySourceConfig::try_new(&[batches], schema, None).unwrap(); + let input = Arc::new(DataSourceExec::new(Arc::new(config))); + let exec = Arc::new(BatchSplitExec::new(input, batch_size)); + let ctx = SessionContext::new(); + let mut stream = exec.execute(0, ctx.task_ctx()).unwrap(); + let mut out = Vec::new(); + while let Some(batch) = stream.next().await { + let batch = batch.unwrap(); + let column = batch + .column(0) + .as_any() + .downcast_ref::() + .unwrap(); + out.push(column.values().to_vec()); + } + out + } + + #[tokio::test] + async fn splits_a_wider_batch_and_passes_narrower_ones_through() { + let out = split(vec![batch((0..10).collect()), batch(vec![10, 11])], 4).await; + assert_eq!( + out, + vec![vec![0, 1, 2, 3], vec![4, 5, 6, 7], vec![8, 9], vec![10, 11]] + ); + } +} diff --git a/native/operators/src/lib.rs b/native/operators/src/lib.rs index 0a9fa33b94a..9b46c958d3a 100644 --- a/native/operators/src/lib.rs +++ b/native/operators/src/lib.rs @@ -25,6 +25,7 @@ // The lint makes easier for code reader/reviewer separate references clones from more heavyweight ones #![deny(clippy::clone_on_ref_ptr)] +mod batch_split; mod expand; mod explode; mod filter; @@ -34,6 +35,7 @@ mod range; mod rank_limit; mod sample; +pub use batch_split::BatchSplitExec; pub use expand::ExpandExec; pub use explode::ExplodeExec; pub use filter::CometFilterExec; diff --git a/native/operators/src/range.rs b/native/operators/src/range.rs index 0e2ef4cffa7..4ba4f6d6429 100644 --- a/native/operators/src/range.rs +++ b/native/operators/src/range.rs @@ -19,21 +19,17 @@ use arrow::array::{Int64Array, RecordBatch}; use arrow::compute::SortOptions; use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; use datafusion::common::Result; -use datafusion::execution::session_state::SessionState; use datafusion::physical_expr::expressions::Column; use datafusion::physical_expr::PhysicalSortExpr; use datafusion::physical_plan::memory::{LazyBatchGenerator, LazyMemoryExec}; use datafusion::physical_plan::Partitioning; -use datafusion::prelude::SessionConfig; use parking_lot::RwLock; use std::any::Any; use std::fmt; -use std::sync::{Arc, Weak}; +use std::sync::Arc; /// Builds the plan for one Spark partition of `spark.range` or SQL `range()`: a single non-null -/// `BIGINT` column, produced at most a session batch size of rows at a time. The size is read -/// from `session` as each batch is made, because a plan holding a large string scalar lowers it -/// after planning (see `scalar_batch_rows` in comet's `jni_api`). +/// `BIGINT` column, produced `batch_size` rows at a time. /// /// Partition bounds follow Spark's `RangeExec`. The JVM side sends only ranges whose arithmetic /// cannot overflow in Spark, where both Spark's generated code and its interpreted path produce @@ -47,7 +43,7 @@ pub fn range_exec( num_elements: i64, num_slices: i32, partition: i32, - session: Weak>, + batch_size: usize, ) -> Result { let (partition_start, partition_elements) = partition_bounds(start, step, num_elements, num_slices, partition); @@ -57,7 +53,7 @@ pub fn range_exec( step, partition_start, partition_elements, - session, + batch_size, produced: 0, }; let mut exec = LazyMemoryExec::try_new(schema, vec![Arc::new(RwLock::new(generator))])?; @@ -126,21 +122,11 @@ struct RangeGenerator { step: i64, partition_start: i64, partition_elements: i64, - /// The session whose batch size bounds each batch, read when the batch is made. - session: Weak>, + batch_size: usize, /// The number of values produced so far. produced: i64, } -impl RangeGenerator { - fn batch_size(&self) -> usize { - self.session - .upgrade() - .map(|state| state.read().config().batch_size()) - .unwrap_or_else(|| SessionConfig::new().batch_size()) - } -} - impl fmt::Display for RangeGenerator { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( @@ -158,7 +144,7 @@ impl LazyBatchGenerator for RangeGenerator { fn generate_next_batch(&mut self) -> Result> { let first = self.produced; - let count = (self.partition_elements - first).min(self.batch_size() as i64); + let count = (self.partition_elements - first).min(self.batch_size as i64); if count <= 0 { return Ok(None); } @@ -176,11 +162,8 @@ impl LazyBatchGenerator for RangeGenerator { fn reset_state(&self) -> Arc> { Arc::new(RwLock::new(RangeGenerator { schema: Arc::clone(&self.schema), - step: self.step, - partition_start: self.partition_start, - partition_elements: self.partition_elements, - session: Weak::clone(&self.session), produced: 0, + ..*self })) } } @@ -189,7 +172,7 @@ impl LazyBatchGenerator for RangeGenerator { mod tests { use super::*; use datafusion::physical_plan::ExecutionPlan; - use datafusion::prelude::{SessionConfig, SessionContext}; + use datafusion::prelude::SessionContext; use futures::StreamExt; async fn collect( @@ -199,18 +182,11 @@ mod tests { num_slices: i32, batch_size: usize, ) -> Vec { - let ctx = SessionContext::new_with_config(SessionConfig::new().with_batch_size(batch_size)); + let ctx = SessionContext::new(); let mut values = Vec::new(); for partition in 0..num_slices { - let range = range_exec( - start, - step, - num_elements, - num_slices, - partition, - ctx.state_weak_ref(), - ) - .unwrap(); + let range = + range_exec(start, step, num_elements, num_slices, partition, batch_size).unwrap(); let mut stream = range.execute(0, ctx.task_ctx()).unwrap(); while let Some(batch) = stream.next().await { let batch = batch.unwrap(); @@ -263,25 +239,6 @@ mod tests { } } - /// `executePlan` lowers the session's batch size after planning when the plan holds a large - /// string scalar, so the batch size must be read as each batch is made. - #[tokio::test] - async fn reads_the_batch_size_the_session_has_when_a_batch_is_made() { - let ctx = SessionContext::new_with_config(SessionConfig::new().with_batch_size(8192)); - let range = range_exec(0, 1, 10, 1, 0, ctx.state_weak_ref()).unwrap(); - { - let state = ctx.state_ref(); - let mut state = state.write(); - *state.config_mut() = std::mem::take(state.config_mut()).with_batch_size(4); - } - let mut stream = range.execute(0, ctx.task_ctx()).unwrap(); - let mut rows = Vec::new(); - while let Some(batch) = stream.next().await { - rows.push(batch.unwrap().num_rows()); - } - assert_eq!(rows, vec![4, 4, 2]); - } - /// The cases from Spark's `DataFrameRangeSuite` whose bounds are at the edges of the `Long` /// range. #[tokio::test] diff --git a/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql index 1fc7f056f53..30c2d03bf2d 100644 --- a/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql +++ b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql @@ -37,11 +37,11 @@ CREATE TABLE test_large_scalar_ids(k int) USING parquet statement INSERT INTO test_large_scalar_ids SELECT /*+ COALESCE(1) */ CAST(id % 2 AS INT) FROM range(8192) --- count of the scalar in a grouped aggregate. The plan runs natively and panicked before the --- fix; the answer-only mode is because the harness's missingInput check trips on a grouped --- count of a scalar subquery whatever the string's size, which is a separate plan issue. -query spark_answer_only -SELECT k, count((SELECT max(s) FROM test_large_scalar_wide)) FROM test_large_scalar_ids GROUP BY k ORDER BY k +-- count of the scalar in a grouped aggregate, which expands it to the batch length. A literal +-- rather than the subquery: the harness's missingInput check trips on a grouped count of a +-- scalar subquery whatever the string's size, which is a separate plan issue. +query +SELECT k, count(repeat('x', 262144)) FROM test_large_scalar_ids GROUP BY k ORDER BY k -- the scalar as a hash partitioning key query @@ -58,3 +58,15 @@ SELECT sum(length(c)) FROM (SELECT concat(repeat('x', 262144), CAST(k AS STRING) -- the literal over the native range source, which sizes its own batches, in one slice query SELECT sum(length(concat(repeat('x', 262144), CAST(id AS STRING)))) FROM range(0, 8192, 1, 1) + +-- a filter under the projection coalesces its output to its own batch size +query +SELECT sum(length(c)) FROM (SELECT concat((SELECT max(s) FROM test_large_scalar_wide), CAST(k AS STRING)) AS c FROM test_large_scalar_ids WHERE k >= 0) + +-- a window under the aggregate emits its whole partition +query +SELECT sum(length(concat(repeat('x', 262144), CAST(tile AS STRING)))) FROM (SELECT ntile(2) OVER (ORDER BY id) AS tile FROM range(0, 8192, 1, 1)) + +-- a scalar built from the subquery result is wider than the result itself +query +SELECT sum(length(c)) FROM (SELECT concat(repeat((SELECT max(s) FROM test_large_scalar_wide), 2), CAST(k AS STRING)) AS c FROM test_large_scalar_ids) From 7448cd15e87ee16e2298ee5b9d80ae3fda0b2e93 Mon Sep 17 00:00:00 2001 From: zhangfengcdt Date: Fri, 30 Oct 2026 08:29:11 -0700 Subject: [PATCH 3/9] fix: never measure a volatile expression for the scalar expansion bound uuid(0) and randstr(10, 0) are column-free string expressions, so the planner evaluated them once at plan time to measure their width, which drew the first value from their seeded generator. Execution then produced Spark's second value onward and the bit-for-bit tests failed. A subtree with a volatile expression yields a value per row rather than a scalar, so it is never measured: Comet's stateful expressions and any DataFusion scalar function declared volatile. --- .../src/execution/planner/scalar_expansion.rs | 54 +++++++++++++++---- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/native/core/src/execution/planner/scalar_expansion.rs b/native/core/src/execution/planner/scalar_expansion.rs index ebadd8d4911..74b2e659718 100644 --- a/native/core/src/execution/planner/scalar_expansion.rs +++ b/native/core/src/execution/planner/scalar_expansion.rs @@ -32,13 +32,16 @@ use std::sync::Arc; use arrow::datatypes::{DataType, Schema}; use arrow::record_batch::{RecordBatch, RecordBatchOptions}; use datafusion::common::ScalarValue; -use datafusion::logical_expr::ColumnarValue; +use datafusion::logical_expr::{ColumnarValue, Volatility}; use datafusion::physical_expr::expressions::{Column, InListExpr}; -use datafusion::physical_expr::PhysicalExpr; +use datafusion::physical_expr::{PhysicalExpr, ScalarFunctionExpr}; use datafusion::physical_plan::ExecutionPlan; use datafusion_comet_operators::BatchSplitExec; use datafusion_comet_spark_expr::jvm_udf::JvmScalarUdfExpr; -use datafusion_comet_spark_expr::Subquery; +use datafusion_comet_spark_expr::monotonically_increasing_id::MonotonicallyIncreasingId; +use datafusion_comet_spark_expr::{ + RandExpr, RandStrExpr, RandnExpr, ShuffleExpr, Subquery, UuidExpr, +}; use super::{PhysicalPlanner, TEST_EXEC_CONTEXT_ID}; @@ -83,10 +86,11 @@ impl PhysicalPlanner { /// The bytes of the string and binary scalars that evaluating `expr` expands to the batch's /// length, added up, so that a scalar used twice counts twice. /// - /// A scalar is any subtree without a column reference, evaluated here on a one-row batch: - /// `repeat(, 8)` is wider than the subquery result it is built from. A subtree - /// that fails to evaluate counts nothing, so that its error surfaces where Spark's would, - /// at execution. The members of an `IN` list are compared and never expanded. + /// A scalar is any subtree without a column reference or a volatile function, evaluated + /// here on a one-row batch: `repeat(, 8)` is wider than the subquery result it is + /// built from. A subtree that fails to evaluate counts nothing, so that its error surfaces + /// where Spark's would, at execution. The members of an `IN` list are compared and never + /// expanded. fn expanded_scalar_bytes(&self, expr: &Arc, schema: &Schema) -> usize { if let Some(in_list) = expr.downcast_ref::() { return self.expanded_scalar_bytes(in_list.expr(), schema); @@ -104,9 +108,10 @@ impl PhysicalPlanner { ) { return 0; } - // A JVM UDF is the user's code, which may have side effects or be slow, and a planner - // unit test has no JVM to read a subquery result from. - if contains(expr, &|e| e.is::()) + // A volatile function yields a value per row and advances a seeded generator when + // evaluated. A JVM UDF is the user's code, which may have side effects or be slow, and a + // planner unit test has no JVM to read a subquery result from. + if contains(expr, &|e| is_volatile(e) || e.is::()) || (self.exec_context_id == TEST_EXEC_CONTEXT_ID && contains(expr, &|e| e.is::())) { @@ -142,6 +147,18 @@ fn is_column_free(expr: &Arc) -> bool { !expr.is::() && expr.children().iter().all(|child| is_column_free(child)) } +fn is_volatile(expr: &Arc) -> bool { + expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr + .downcast_ref::() + .is_some_and(|f| f.fun().signature().volatility == Volatility::Volatile) +} + fn contains(expr: &Arc, is: &dyn Fn(&Arc) -> bool) -> bool { is(expr) || expr.children().iter().any(|child| contains(child, is)) } @@ -199,6 +216,23 @@ mod tests { assert_eq!(bytes(twice), 16); } + #[test] + fn measuring_a_seeded_generator_does_not_advance_it() { + use datafusion_comet_spark_expr::UuidExpr; + let one_row = RecordBatch::try_new_with_options( + Arc::new(Schema::empty()), + vec![], + &RecordBatchOptions::new().with_row_count(Some(1)), + ) + .unwrap(); + let first_draw = + |expr: &Arc| expr.evaluate(&one_row).unwrap().into_array(1).unwrap(); + let measured: Arc = Arc::new(UuidExpr::new(0)); + assert_eq!(bytes(Arc::clone(&measured)), 0); + let fresh: Arc = Arc::new(UuidExpr::new(0)); + assert_eq!(*first_draw(&measured), *first_draw(&fresh)); + } + #[test] fn skips_the_members_of_an_in_list() { let schema = schema(); From 9e4b6b05fde44141eea95686267a8e7160bf78c8 Mon Sep 17 00:00:00 2001 From: zhangfengcdt Date: Sat, 31 Oct 2026 17:19:10 -0700 Subject: [PATCH 4/9] fix: install the batch split whenever an upstream batch can outgrow the scalar cap The split was installed only when the scalar cap fell below the session batch size. With spark.comet.batchSize=256 a 256 KiB scalar allows 4096 rows, so nothing was installed, but DataFusion's filter coalesces toward its own default of 8192 rows whatever the session says, and the projection above it overflowed the 32-bit offsets. The decision now compares the cap against the larger of the session batch size and DataFusion's default, and splits at the smaller of the cap and the session size. A new fixture runs the filter case with a 256-row session. --- .../src/execution/planner/scalar_expansion.rs | 54 +++++++++++++------ .../large_scalar_string_small_batch.sql | 33 ++++++++++++ 2 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 spark/src/test/resources/sql-tests/operators/large_scalar_string_small_batch.sql diff --git a/native/core/src/execution/planner/scalar_expansion.rs b/native/core/src/execution/planner/scalar_expansion.rs index 74b2e659718..db260f4f0e9 100644 --- a/native/core/src/execution/planner/scalar_expansion.rs +++ b/native/core/src/execution/planner/scalar_expansion.rs @@ -24,8 +24,9 @@ //! . //! //! The bound is enforced right under the operator that evaluates the expressions, with a -//! [`BatchSplitExec`], because the operators below do not keep the batch size they were handed: -//! a filter coalesces its output up to its own size and a window emits its whole partition. +//! [`BatchSplitExec`], because the operators below do not keep the session's batch size: a +//! filter coalesces its output toward DataFusion's default size and a window emits its whole +//! partition. use std::sync::Arc; @@ -36,6 +37,7 @@ use datafusion::logical_expr::{ColumnarValue, Volatility}; use datafusion::physical_expr::expressions::{Column, InListExpr}; use datafusion::physical_expr::{PhysicalExpr, ScalarFunctionExpr}; use datafusion::physical_plan::ExecutionPlan; +use datafusion::prelude::SessionConfig; use datafusion_comet_operators::BatchSplitExec; use datafusion_comet_spark_expr::jvm_udf::JvmScalarUdfExpr; use datafusion_comet_spark_expr::monotonically_increasing_id::MonotonicallyIncreasingId; @@ -51,19 +53,19 @@ use super::{PhysicalPlanner, TEST_EXEC_CONTEXT_ID}; /// vector can hold before its reallocation fails. const SCALAR_EXPANSION_BYTES: usize = 1 << 30; -/// The rows per batch under which `scalar_bytes`, expanded to the batch's length, stay within -/// `SCALAR_EXPANSION_BYTES`. -fn scalar_batch_rows(scalar_bytes: usize, batch_size: usize) -> usize { +/// The most rows per batch at which `scalar_bytes`, expanded to the batch's length, stay within +/// `SCALAR_EXPANSION_BYTES`; unbounded when there is nothing to expand. +fn scalar_rows_cap(scalar_bytes: usize) -> usize { if scalar_bytes == 0 { - return batch_size; + return usize::MAX; } - batch_size.min((SCALAR_EXPANSION_BYTES / scalar_bytes).max(1)) + (SCALAR_EXPANSION_BYTES / scalar_bytes).max(1) } impl PhysicalPlanner { /// Wraps `input` so that the batches it emits keep the string and binary scalars in `exprs`, /// expanded to a batch's length, within `SCALAR_EXPANSION_BYTES`. Returns `input` as is when - /// the session's batch size already does. + /// the widest batch it can emit already does. pub(crate) fn bound_scalar_expansion<'a>( &self, input: Arc, @@ -74,10 +76,13 @@ impl PhysicalPlanner { .into_iter() .map(|expr| self.expanded_scalar_bytes(expr, &schema)) .sum(); + let cap = scalar_rows_cap(bytes); let batch_size = self.session_ctx.copied_config().batch_size(); - let rows = scalar_batch_rows(bytes, batch_size); - if rows < batch_size { - Arc::new(BatchSplitExec::new(input, rows)) + // A filter below coalesces toward DataFusion's default batch size whatever the session's + // is, so a session batch size under the default does not bound what arrives here. + let widest_input = batch_size.max(SessionConfig::default().batch_size()); + if cap < widest_input { + Arc::new(BatchSplitExec::new(input, cap.min(batch_size))) } else { input } @@ -182,13 +187,13 @@ mod tests { } #[test] - fn scalar_batch_rows_keeps_the_batch_size_unless_a_scalar_would_overflow_it() { - assert_eq!(scalar_batch_rows(0, 8192), 8192); - assert_eq!(scalar_batch_rows(1024, 8192), 8192); + fn scalar_rows_cap_bounds_the_rows_by_the_expanded_bytes() { + assert_eq!(scalar_rows_cap(0), usize::MAX); + assert_eq!(scalar_rows_cap(1024), 1024 * 1024); // A 256 KiB scalar expanded to 8192 rows needs 2 GiB; 4096 rows keep it at 1 GiB. - assert_eq!(scalar_batch_rows(256 * 1024, 8192), 4096); + assert_eq!(scalar_rows_cap(256 * 1024), 4096); // A scalar past 1 GiB still gets one row per batch. - assert_eq!(scalar_batch_rows(2 * 1024 * 1024 * 1024, 8192), 1); + assert_eq!(scalar_rows_cap(2 * 1024 * 1024 * 1024), 1); } #[test] @@ -233,6 +238,23 @@ mod tests { assert_eq!(*first_draw(&measured), *first_draw(&fresh)); } + #[test] + fn wraps_the_input_when_a_small_session_batch_fits_but_the_filter_default_does_not() { + use datafusion::execution::context::SessionContext; + use datafusion::physical_plan::empty::EmptyExec; + let session = SessionContext::new_with_config(SessionConfig::new().with_batch_size(256)); + let planner = PhysicalPlanner::new(Arc::new(session), 0); + let input: Arc = Arc::new(EmptyExec::new(Arc::new(schema()))); + // A 256 KiB scalar allows 4096 rows, more than the session's 256, but a filter below + // coalesces toward DataFusion's default of 8192 rows whatever the session says. + let wide = lit("x".repeat(256 * 1024)); + let bounded = planner.bound_scalar_expansion(input, [&wide]); + let split = bounded + .downcast_ref::() + .expect("a split under the expanding operator"); + assert_eq!(256, split.batch_size()); + } + #[test] fn skips_the_members_of_an_in_list() { let schema = schema(); diff --git a/spark/src/test/resources/sql-tests/operators/large_scalar_string_small_batch.sql b/spark/src/test/resources/sql-tests/operators/large_scalar_string_small_batch.sql new file mode 100644 index 00000000000..737ecba8f10 --- /dev/null +++ b/spark/src/test/resources/sql-tests/operators/large_scalar_string_small_batch.sql @@ -0,0 +1,33 @@ +-- 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. + +-- The filter case of large_scalar_string.sql with a session batch size that a 256 KiB scalar +-- fits as is. DataFusion's filter coalesces toward its own default of 8192 rows whatever the +-- session says, so the projection above it still needs its input bounded. +-- See https://github.com/apache/datafusion-comet/issues/7247. +-- Config: spark.sql.adaptive.enabled=false +-- Config: spark.comet.batchSize=256 +-- ConstantFolding: enabled + +statement +CREATE TABLE test_large_scalar_small_batch(k int) USING parquet + +statement +INSERT INTO test_large_scalar_small_batch SELECT /*+ COALESCE(1) */ CAST(id % 2 AS INT) FROM range(8192) + +query +SELECT sum(length(c)) FROM (SELECT concat(repeat('x', 262144), CAST(k AS STRING)) AS c FROM test_large_scalar_small_batch WHERE k >= 0) From 74e7aeea514baf21fb8cae6ee434cc99bef9a4d8 Mon Sep 17 00:00:00 2001 From: zhangfengcdt Date: Mon, 2 Nov 2026 09:15:32 -0800 Subject: [PATCH 5/9] fix: split the input of every operator that expands a string scalar The split was installed only when the scalar cap fell below the widest batch assumed to arrive, the larger of the session batch size and DataFusion's default. A window emits its whole partition and a broadcast arrives as a few wide batches, so no width can be assumed: a 64 KiB scalar over a 32768-row window partition skipped the split and overflowed the 32-bit offsets. An operator that expands any string or binary scalar now has its input split at the smaller of the scalar cap and the session batch size. A plan without such a scalar is unchanged. The fixture excludes NullPropagation so that the grouped count keeps the wide literal as its argument instead of being rewritten to count(1), and adds the window case above. --- .../src/execution/planner/scalar_expansion.rs | 52 ++++++++++--------- .../operators/large_scalar_string.sql | 8 ++- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/native/core/src/execution/planner/scalar_expansion.rs b/native/core/src/execution/planner/scalar_expansion.rs index db260f4f0e9..58bce209b57 100644 --- a/native/core/src/execution/planner/scalar_expansion.rs +++ b/native/core/src/execution/planner/scalar_expansion.rs @@ -25,8 +25,9 @@ //! //! The bound is enforced right under the operator that evaluates the expressions, with a //! [`BatchSplitExec`], because the operators below do not keep the session's batch size: a -//! filter coalesces its output toward DataFusion's default size and a window emits its whole -//! partition. +//! filter coalesces its output toward DataFusion's default size, a window emits its whole +//! partition and a broadcast arrives as a few wide batches. No width can be assumed, so the +//! split is installed whenever the operator expands a string or binary scalar at all. use std::sync::Arc; @@ -37,7 +38,6 @@ use datafusion::logical_expr::{ColumnarValue, Volatility}; use datafusion::physical_expr::expressions::{Column, InListExpr}; use datafusion::physical_expr::{PhysicalExpr, ScalarFunctionExpr}; use datafusion::physical_plan::ExecutionPlan; -use datafusion::prelude::SessionConfig; use datafusion_comet_operators::BatchSplitExec; use datafusion_comet_spark_expr::jvm_udf::JvmScalarUdfExpr; use datafusion_comet_spark_expr::monotonically_increasing_id::MonotonicallyIncreasingId; @@ -64,8 +64,8 @@ fn scalar_rows_cap(scalar_bytes: usize) -> usize { impl PhysicalPlanner { /// Wraps `input` so that the batches it emits keep the string and binary scalars in `exprs`, - /// expanded to a batch's length, within `SCALAR_EXPANSION_BYTES`. Returns `input` as is when - /// the widest batch it can emit already does. + /// expanded to a batch's length, within `SCALAR_EXPANSION_BYTES` and the session's batch + /// size. Returns `input` as is when `exprs` expand no scalar. pub(crate) fn bound_scalar_expansion<'a>( &self, input: Arc, @@ -76,16 +76,14 @@ impl PhysicalPlanner { .into_iter() .map(|expr| self.expanded_scalar_bytes(expr, &schema)) .sum(); - let cap = scalar_rows_cap(bytes); - let batch_size = self.session_ctx.copied_config().batch_size(); - // A filter below coalesces toward DataFusion's default batch size whatever the session's - // is, so a session batch size under the default does not bound what arrives here. - let widest_input = batch_size.max(SessionConfig::default().batch_size()); - if cap < widest_input { - Arc::new(BatchSplitExec::new(input, cap.min(batch_size))) - } else { - input + if bytes == 0 { + return input; } + let batch_size = self.session_ctx.copied_config().batch_size(); + Arc::new(BatchSplitExec::new( + input, + scalar_rows_cap(bytes).min(batch_size), + )) } /// The bytes of the string and binary scalars that evaluating `expr` expands to the batch's @@ -242,11 +240,12 @@ mod tests { fn wraps_the_input_when_a_small_session_batch_fits_but_the_filter_default_does_not() { use datafusion::execution::context::SessionContext; use datafusion::physical_plan::empty::EmptyExec; + use datafusion::prelude::SessionConfig; let session = SessionContext::new_with_config(SessionConfig::new().with_batch_size(256)); let planner = PhysicalPlanner::new(Arc::new(session), 0); let input: Arc = Arc::new(EmptyExec::new(Arc::new(schema()))); - // A 256 KiB scalar allows 4096 rows, more than the session's 256, but a filter below - // coalesces toward DataFusion's default of 8192 rows whatever the session says. + // A 256 KiB scalar allows 4096 rows, more than the session's 256, which a filter below + // coalescing toward DataFusion's default of 8192 rows would exceed. let wide = lit("x".repeat(256 * 1024)); let bounded = planner.bound_scalar_expansion(input, [&wide]); let split = bounded @@ -269,17 +268,22 @@ mod tests { } #[test] - fn wraps_the_input_only_when_the_scalars_need_a_smaller_batch() { + fn wraps_the_input_at_the_session_batch_size_or_the_scalar_cap() { use datafusion::physical_plan::empty::EmptyExec; let planner = PhysicalPlanner::default(); let input: Arc = Arc::new(EmptyExec::new(Arc::new(schema()))); - let small = planner.bound_scalar_expansion(Arc::clone(&input), [&lit("abcd")]); - assert_eq!("EmptyExec", small.name()); + let none = planner.bound_scalar_expansion(Arc::clone(&input), [&lit(7)]); + assert_eq!("EmptyExec", none.name()); + let split_at = |exprs: &[Arc]| { + planner + .bound_scalar_expansion(Arc::clone(&input), exprs) + .downcast_ref::() + .expect("a split under the expanding operator") + .batch_size() + }; + // A small scalar is bounded by the session's batch size, whatever arrives from below. + assert_eq!(8192, split_at(&[lit("abcd")])); let wide = lit("x".repeat(256 * 1024)); - let bounded = planner.bound_scalar_expansion(input, [&wide, &wide]); - let split = bounded - .downcast_ref::() - .expect("a split under the expanding operator"); - assert_eq!(2048, split.batch_size()); + assert_eq!(2048, split_at(&[Arc::clone(&wide), wide])); } } diff --git a/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql index 30c2d03bf2d..434b3e86e7d 100644 --- a/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql +++ b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql @@ -19,11 +19,13 @@ -- aggregate, the hash partitioner and string kernels. A 256 KiB string over the default 8192-row -- batch needs 2 GiB of value bytes, past Arrow's 32-bit offsets. Spark evaluates row by row. -- The ids are written in one task so they arrive in one batch. Constant folding is on so that --- `repeat('x', 262144)` reaches Comet as the literal it is in a Spark session. +-- `repeat('x', 262144)` reaches Comet as the literal it is in a Spark session. Null propagation +-- is off so that the grouped count keeps the literal as its argument instead of `count(1)`. -- See https://github.com/apache/datafusion-comet/issues/7247. -- Config: spark.sql.adaptive.enabled=false -- Config: spark.comet.exec.range.enabled=true -- ConstantFolding: enabled +-- ExcludeRules: org.apache.spark.sql.catalyst.optimizer.NullPropagation statement CREATE TABLE test_large_scalar_wide(s string) USING parquet @@ -67,6 +69,10 @@ SELECT sum(length(c)) FROM (SELECT concat((SELECT max(s) FROM test_large_scalar_ query SELECT sum(length(concat(repeat('x', 262144), CAST(tile AS STRING)))) FROM (SELECT ntile(2) OVER (ORDER BY id) AS tile FROM range(0, 8192, 1, 1)) +-- a partition wider than the session batch size, with a scalar that fits the session batch size +query +SELECT sum(length(concat(repeat('x', 65536), CAST(tile AS STRING)))) FROM (SELECT ntile(2) OVER (ORDER BY id) AS tile FROM range(0, 32768, 1, 1)) + -- a scalar built from the subquery result is wider than the result itself query SELECT sum(length(c)) FROM (SELECT concat(repeat((SELECT max(s) FROM test_large_scalar_wide), 2), CAST(k AS STRING)) AS c FROM test_large_scalar_ids) From 30ee2c7a51527f1ce7a717bd9e26b9bc1cdecc87 Mon Sep 17 00:00:00 2001 From: zhangfengcdt Date: Wed, 4 Nov 2026 12:48:31 -0800 Subject: [PATCH 6/9] fix: count scalars beside per-row nodes and bound range partitioning keys The plan-time measurement stopped at the first volatile expression, so a column-free subtree such as concat(repeat('x', 262144), uuid()) was measured as zero bytes and left unbounded. A column, a volatile function, a JVM UDF or a subquery is now a boundary rather than a reason to give up: the scalars among its siblings are still counted, and the volatile node itself is never evaluated. The shuffle writer only bounded hash partitioning keys. Range partitioning evaluates its ordering expressions in the exchange too, so those are bounded the same way. --- .../src/execution/planner/scalar_expansion.rs | 62 +++++++++++++------ native/core/src/execution/planner/shuffle.rs | 3 + .../operators/large_scalar_string.sql | 4 ++ .../comet/exec/CometNativeShuffleSuite.scala | 19 +++++- 4 files changed, 68 insertions(+), 20 deletions(-) diff --git a/native/core/src/execution/planner/scalar_expansion.rs b/native/core/src/execution/planner/scalar_expansion.rs index 3a4e77cf891..c0544ffb86d 100644 --- a/native/core/src/execution/planner/scalar_expansion.rs +++ b/native/core/src/execution/planner/scalar_expansion.rs @@ -91,14 +91,15 @@ impl PhysicalPlanner { /// /// A scalar is any subtree without a column reference or a volatile function, evaluated /// here on a one-row batch: `repeat(, 8)` is wider than the subquery result it is - /// built from. A subtree that fails to evaluate counts nothing, so that its error surfaces - /// where Spark's would, at execution. The members of an `IN` list are compared and never - /// expanded. + /// built from. A subtree that is not a scalar is evaluated per row, so only the scalars + /// among its children count. A subtree that fails to evaluate counts nothing, so that its + /// error surfaces where Spark's would, at execution. The members of an `IN` list are + /// compared and never expanded. fn expanded_scalar_bytes(&self, expr: &Arc, schema: &Schema) -> usize { if let Some(in_list) = expr.downcast_ref::() { return self.expanded_scalar_bytes(in_list.expr(), schema); } - if !is_column_free(expr) { + if !self.is_scalar(expr) { return expr .children() .into_iter() @@ -111,15 +112,6 @@ impl PhysicalPlanner { ) { return 0; } - // A volatile function yields a value per row and advances a seeded generator when - // evaluated. A JVM UDF is the user's code, which may have side effects or be slow, and a - // planner unit test has no JVM to read a subquery result from. - if contains(expr, &|e| is_volatile(e) || e.is::()) - || (self.exec_context_id == TEST_EXEC_CONTEXT_ID - && contains(expr, &|e| e.is::())) - { - return 0; - } // One row, so that a kernel that expands its scalar arguments still yields one value. let Ok(one_row) = RecordBatch::try_new_with_options( Arc::new(Schema::empty()), @@ -146,8 +138,19 @@ impl PhysicalPlanner { } } -fn is_column_free(expr: &Arc) -> bool { - !expr.is::() && expr.children().iter().all(|child| is_column_free(child)) +impl PhysicalPlanner { + /// Whether `expr` yields one value for a batch that can be computed at plan time. A column + /// reference and a volatile function yield a value per row, and evaluating a volatile + /// function advances its seeded generator. A JVM UDF is the user's code, which may have + /// side effects or be slow, and a planner unit test has no JVM to read a subquery result + /// from. + fn is_scalar(&self, expr: &Arc) -> bool { + let per_row = expr.is::() + || is_volatile(expr) + || expr.is::() + || (self.exec_context_id == TEST_EXEC_CONTEXT_ID && expr.is::()); + !per_row && expr.children().iter().all(|child| self.is_scalar(child)) + } } fn is_volatile(expr: &Arc) -> bool { @@ -162,10 +165,6 @@ fn is_volatile(expr: &Arc) -> bool { .is_some_and(|f| f.fun().signature().volatility == Volatility::Volatile) } -fn contains(expr: &Arc, is: &dyn Fn(&Arc) -> bool) -> bool { - is(expr) || expr.children().iter().any(|child| contains(child, is)) -} - #[cfg(test)] mod tests { use super::*; @@ -254,6 +253,31 @@ mod tests { assert_eq!(256, split.batch_size()); } + #[test] + fn counts_a_scalar_beside_a_volatile_sibling_without_evaluating_the_sibling() { + use datafusion_comet_spark_expr::UuidExpr; + let schema = schema(); + let one_row = RecordBatch::try_new_with_options( + Arc::new(Schema::empty()), + vec![], + &RecordBatchOptions::new().with_row_count(Some(1)), + ) + .unwrap(); + let first_draw = + |expr: &Arc| expr.evaluate(&one_row).unwrap().into_array(1).unwrap(); + let uuid: Arc = Arc::new(UuidExpr::new(0)); + let mixed = binary( + lit("x".repeat(256 * 1024)), + Operator::StringConcat, + Arc::clone(&uuid), + &schema, + ) + .unwrap(); + assert_eq!(bytes(mixed), 256 * 1024); + let fresh: Arc = Arc::new(UuidExpr::new(0)); + assert_eq!(*first_draw(&uuid), *first_draw(&fresh)); + } + #[test] fn skips_the_members_of_an_in_list() { let schema = schema(); diff --git a/native/core/src/execution/planner/shuffle.rs b/native/core/src/execution/planner/shuffle.rs index 17cc4aaa934..3646d304182 100644 --- a/native/core/src/execution/planner/shuffle.rs +++ b/native/core/src/execution/planner/shuffle.rs @@ -89,6 +89,9 @@ impl OperatorBuilder for ShuffleWriterBuilder { CometPartitioning::Hash(exprs, _) => { planner.bound_scalar_expansion(writer_input, exprs) } + CometPartitioning::RangePartitioning(ordering, ..) => { + planner.bound_scalar_expansion(writer_input, ordering.iter().map(|sort| &sort.expr)) + } _ => writer_input, }; diff --git a/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql index 434b3e86e7d..8f45b8977e8 100644 --- a/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql +++ b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql @@ -73,6 +73,10 @@ SELECT sum(length(concat(repeat('x', 262144), CAST(tile AS STRING)))) FROM (SELE query SELECT sum(length(concat(repeat('x', 65536), CAST(tile AS STRING)))) FROM (SELECT ntile(2) OVER (ORDER BY id) AS tile FROM range(0, 32768, 1, 1)) +-- a scalar beside a volatile expression, which is not evaluated at plan time +query +SELECT sum(length(c)) FROM (SELECT concat(repeat('x', 262144), uuid()) AS c FROM range(0, 8192, 1, 1)) + -- a scalar built from the subquery result is wider than the result itself query SELECT sum(length(c)) FROM (SELECT concat(repeat((SELECT max(s) FROM test_large_scalar_wide), 2), CAST(k AS STRING)) AS c FROM test_large_scalar_ids) diff --git a/spark/src/test/scala/org/apache/comet/exec/CometNativeShuffleSuite.scala b/spark/src/test/scala/org/apache/comet/exec/CometNativeShuffleSuite.scala index bb06a09fbf3..094a7d37750 100644 --- a/spark/src/test/scala/org/apache/comet/exec/CometNativeShuffleSuite.scala +++ b/spark/src/test/scala/org/apache/comet/exec/CometNativeShuffleSuite.scala @@ -50,7 +50,7 @@ import org.apache.spark.sql.execution.{FileSourceScanExec, LocalTableScanExec, S import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanExec, AdaptiveSparkPlanHelper, AQEShuffleReadExec, ShuffleQueryStageExec} import org.apache.spark.sql.execution.aggregate.ObjectHashAggregateExec import org.apache.spark.sql.execution.exchange.{ReusedExchangeExec, ShuffleExchangeExec} -import org.apache.spark.sql.functions.{broadcast, col, count, countDistinct, spark_partition_id, sum} +import org.apache.spark.sql.functions.{broadcast, col, count, countDistinct, expr, spark_partition_id, sum} import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.types.{ArrayType, DataType, IntegerType, LongType, MapType, StringType, StructField, StructType} import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector} @@ -534,6 +534,23 @@ class CometNativeShuffleSuite extends CometTestBase with AdaptiveSparkPlanHelper } } + test("range shuffle bounds a large string scalar in its ordering expression") { + // A 256 KiB scalar expanded to an 8192-row batch needs 2 GiB of value bytes, past Arrow's + // 32-bit offsets. The expression is evaluated by the exchange itself, with no projection + // between it and the range source. See https://github.com/apache/datafusion-comet/issues/7247. + withSQLConf( + CometConf.COMET_SHUFFLE_MODE.key -> "native", + CometConf.COMET_SHUFFLE_NATIVE_RANGE_PARTITIONING_ENABLED.key -> "true", + CometConf.COMET_EXEC_RANGE_ENABLED.key -> "true", + SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false") { + val df = spark + .range(0, 8192, 1, 1) + .repartitionByRange(2, expr("concat(repeat('x', 262144), cast(id AS string))").asc) + .toDF() + checkSparkAnswerAndOperator(df, Seq(classOf[CometShuffleExchangeExec])) + } + } + test("range shuffle reports current child failure reasons instead of stale sort-order tags") { withSQLConf(CometConf.COMET_SHUFFLE_NATIVE_RANGE_PARTITIONING_ENABLED.key -> "true") { val attr = AttributeReference("key", LongType)() From e19824a690b6d0be6c35a0bb3af114f8ba1d86cf Mon Sep 17 00:00:00 2001 From: zhangfengcdt Date: Fri, 6 Nov 2026 06:59:45 -0800 Subject: [PATCH 7/9] fix: measure per-row IN list members and keep the split transparent to join runtime filters The measurement only looked at the value an IN list tests, so a list member computed per row, such as concat(repeat('x', 262144), CAST(id AS STRING)), expanded its scalar unbounded. The scalars inside such a member now count; a constant member is still compared without expansion and skipped. The split that bounds a filter's input sat between the filter and its Spark child, which broke the pointer alignment the join planner uses to carry a filter's runtime-filter pushdown permission, and stopped reader attachment on its way to the Parquet scan. Both now look through the split and rebuild it over the prepared or filtered child, since slicing batches discards no rows. --- .../operators/dynamic_filter/join/tests.rs | 50 ++++++- .../dynamic_filter/parquet_reader.rs | 11 +- native/core/src/execution/planner/join.rs | 129 +++++++++++++++--- .../src/execution/planner/scalar_expansion.rs | 37 ++++- native/operators/src/batch_split.rs | 4 + .../operators/large_scalar_string.sql | 5 + .../apache/comet/exec/CometJoinSuite.scala | 83 ++++++----- 7 files changed, 259 insertions(+), 60 deletions(-) diff --git a/native/core/src/execution/operators/dynamic_filter/join/tests.rs b/native/core/src/execution/operators/dynamic_filter/join/tests.rs index 54ae6737edf..ee1594d8603 100644 --- a/native/core/src/execution/operators/dynamic_filter/join/tests.rs +++ b/native/core/src/execution/operators/dynamic_filter/join/tests.rs @@ -46,7 +46,7 @@ use datafusion::physical_plan::projection::ProjectionExec; use datafusion::physical_plan::statistics::{StatisticsArgs, StatisticsContext}; use datafusion::physical_plan::{ChildrenPropertiesMode, Distribution, ReplaceChildrenOptions}; use datafusion::prelude::{SessionConfig, SessionContext}; -use datafusion_comet_operators::CometFilterExec; +use datafusion_comet_operators::{BatchSplitExec, CometFilterExec}; use datafusion_comet_spark_expr::RandExpr; use datafusion_datasource::file::FileSource; use parquet::arrow::ArrowWriter; @@ -1108,6 +1108,54 @@ async fn run_parquet_join(values: Vec, enabled: bool) -> (usize, usize, usi ) } +#[tokio::test] +async fn reader_filter_attaches_through_a_batch_split() { + // The planner puts a CometBatchSplitExec under a filter whose predicate expands a string + // scalar. It only slices batches, so a reader filter below it discards the same rows. + let mut config = SessionConfig::new() + .with_target_partitions(1) + .with_parquet_page_index_pruning(false); + config.options_mut().execution.parquet.pushdown_filters = false; + let session = Arc::new(SessionContext::new_with_config(config)); + let (_file, scan) = parquet_probe((0..400).collect(), &session, 100); + let split: Arc = Arc::new(BatchSplitExec::new( + Arc::clone(&scan) as Arc, + 7, + )); + let predicate: Arc = + Arc::new(IsNotNullExpr::new(Arc::new(Column::new("key", 0)))); + let probe = Arc::new(CometFilterExec::from_datafusion( + FilterExec::try_new(predicate, split).unwrap(), + )); + let join = single_key_join_plans(two_batch_build(), probe, PartitionMode::Partitioned); + let wrapper = + DynamicFilterJoinExec::new(&join, session.copied_config().options().as_ref().clone()) + .unwrap(); + let runtime = wrapper.build_runtime_join().unwrap(); + assert!(runtime.reader_filter_attached); + let consumer = runtime + .join + .right() + .downcast_ref::() + .unwrap(); + let filter = consumer.children()[0] + .downcast_ref::() + .unwrap(); + let split = filter.input().downcast_ref::().unwrap(); + assert_eq!(split.batch_size(), 7); + let reader = split.children()[0] + .downcast_ref::() + .unwrap(); + let (_, source) = reader.downcast_to_file_source::().unwrap(); + let reader_filter = source.filter().unwrap(); + let reader_filter = find_dynamic_filter(&reader_filter).unwrap(); + let join_filter = produced_join_filter(&runtime.join); + assert_eq!( + reader_filter.expression_id().unwrap(), + join_filter.expression_id().unwrap() + ); +} + #[tokio::test] async fn broadcast_filter_reaches_parquet_reader_after_complete_build() { let mut config = SessionConfig::new() diff --git a/native/core/src/execution/operators/dynamic_filter/parquet_reader.rs b/native/core/src/execution/operators/dynamic_filter/parquet_reader.rs index 6afd49cb226..dfe6d3777a6 100644 --- a/native/core/src/execution/operators/dynamic_filter/parquet_reader.rs +++ b/native/core/src/execution/operators/dynamic_filter/parquet_reader.rs @@ -32,7 +32,7 @@ use datafusion::physical_expr::PhysicalExpr; use datafusion::physical_plan::ExecutionPlan; use crate::parquet::file_error_context::ParquetErrorContext; -use datafusion_comet_operators::CometFilterExec; +use datafusion_comet_operators::{BatchSplitExec, CometFilterExec}; mod schema_adapter; @@ -98,6 +98,15 @@ pub(super) fn try_attach_parquet_reader_filter( .map(|reader| filter.with_execution_input(reader)), ); } + // The batch split that bounds a scalar expansion only slices batches, so a + // reader filter below it discards the same rows. + if let Some(split) = input.downcast_ref::() { + return Ok( + try_attach_parquet_reader_filter(split.input(), predicate, config)?.map(|reader| { + Arc::new(BatchSplitExec::new(reader, split.batch_size())) as Arc + }), + ); + } // Spark inserts IS NOT NULL residuals above equijoin inputs, including AND // chains of inferred null checks. A reader predicate can cross those direct // checks because both operations only discard rows. Broader deterministic diff --git a/native/core/src/execution/planner/join.rs b/native/core/src/execution/planner/join.rs index b4ed291eaf2..cbd1db7f547 100644 --- a/native/core/src/execution/planner/join.rs +++ b/native/core/src/execution/planner/join.rs @@ -45,7 +45,7 @@ use datafusion::{ }, }; use datafusion_comet_operators::{ - CometFilterExec, CometProjectionExec, MembershipKeyReductionExec, + BatchSplitExec, CometFilterExec, CometProjectionExec, MembershipKeyReductionExec, }; use datafusion_comet_proto::{ spark_expression::Expr, @@ -420,6 +420,16 @@ impl OperatorBuilder for BroadcastNestedLoopJoinBuilder { } } +/// `plan`, or its input when it is the batch split that bounds a scalar expansion. The +/// planner puts that split between an operator and its child, and the Spark tree has no +/// node for it. +fn through_batch_split(plan: &Arc) -> &Arc { + match plan.downcast_ref::() { + Some(split) => split.input(), + None => plan, + } +} + impl PhysicalPlanner { /// Keep Spark metric identities for the small set of nodes whose children /// runtime-filter placement can replace. Stop at native/Spark tree boundaries. @@ -429,22 +439,22 @@ impl PhysicalPlanner { ) -> Result, ExecutionError> { // Walk the serialized and Spark trees together. A mismatch must not lend // another filter's permission to this node or any of its descendants. - let source = source.filter(|source| { - source.plan_id == plan.plan_id - && source.children.len() == plan.children.len() - && plan.children.len() == plan.native_plan.children().len() - && plan - .native_plan - .children() - .iter() - .zip(&plan.children) - .all(|(native, spark)| Arc::ptr_eq(native, &spark.native_plan)) - && match &source.op_struct { - Some(OpStruct::Filter(_)) => plan.native_plan.is::(), - Some(OpStruct::Projection(_)) => plan.native_plan.is::(), - _ => false, - } - }); + let source = + source.filter(|source| { + source.plan_id == plan.plan_id + && source.children.len() == plan.children.len() + && plan.children.len() == plan.native_plan.children().len() + && plan.native_plan.children().iter().zip(&plan.children).all( + |(native, spark)| { + Arc::ptr_eq(through_batch_split(native), &spark.native_plan) + }, + ) + && match &source.op_struct { + Some(OpStruct::Filter(_)) => plan.native_plan.is::(), + Some(OpStruct::Projection(_)) => plan.native_plan.is::(), + _ => false, + } + }); let mut native: Arc = if let Some(filter) = plan.native_plan.downcast_ref::() { let allowed = matches!( @@ -462,13 +472,23 @@ impl PhysicalPlanner { }; let mut prepared = plan.as_ref().clone(); if let ([child], [only]) = (plan.children.as_slice(), native.children().as_slice()) { - if Arc::ptr_eq(only, &child.native_plan) { + if Arc::ptr_eq(through_batch_split(only), &child.native_plan) { + let split_size = only + .downcast_ref::() + .map(|split| split.batch_size()); let child = Self::prepare_probe_for_runtime_filters( Arc::clone(child), source.and_then(|source| source.children.first()), )?; + let input = match split_size { + Some(size) => { + Arc::new(BatchSplitExec::new(Arc::clone(&child.native_plan), size)) + as Arc + } + None => Arc::clone(&child.native_plan), + }; native = native.replace_children( - vec![Arc::clone(&child.native_plan)], + vec![input], ReplaceChildrenOptions::new(ChildrenPropertiesMode::Recompute), )?; prepared.children = vec![child]; @@ -885,6 +905,7 @@ fn rewrite_physical_expr( #[cfg(test)] mod tests { use super::*; + use datafusion_comet_proto::spark_expression::expr::ExprStruct; use datafusion_comet_proto::{spark_expression, spark_operator}; fn column() -> Expr { @@ -921,6 +942,57 @@ mod tests { } } + fn string_type() -> spark_expression::DataType { + spark_expression::DataType { + type_id: spark_expression::data_type::DataTypeId::String as i32, + type_info: None, + } + } + + fn string_scan() -> Operator { + Operator { + op_struct: Some(OpStruct::Scan(spark_operator::Scan { + fields: vec![string_type()], + ..Default::default() + })), + ..Default::default() + } + } + + /// A filter on `column = 'x'`: the string scalar puts a batch split under the filter. + fn string_filter(child: Operator, allowed: bool) -> Operator { + let column = Expr { + expr_struct: Some(ExprStruct::Bound(spark_expression::BoundReference { + index: 0, + datatype: Some(string_type()), + })), + ..Default::default() + }; + let literal = Expr { + expr_struct: Some(ExprStruct::Literal(spark_expression::Literal { + value: Some(spark_expression::literal::Value::StringVal("x".to_string())), + datatype: Some(string_type()), + is_null: false, + })), + ..Default::default() + }; + Operator { + plan_id: child.plan_id + 1, + children: vec![child], + op_struct: Some(OpStruct::Filter(spark_operator::Filter { + predicate: Some(Expr { + expr_struct: Some(ExprStruct::Eq(Box::new(spark_expression::BinaryExpr { + left: Some(Box::new(column)), + right: Some(Box::new(literal)), + }))), + ..Default::default() + }), + allow_runtime_filter_pushdown: allowed, + })), + ..Default::default() + } + } + fn projection(child: Operator) -> Operator { Operator { plan_id: child.plan_id + 1, @@ -962,6 +1034,25 @@ mod tests { } } + #[test] + fn probe_filter_permission_survives_a_scalar_batch_split() { + for permission in [false, true] { + let source = string_filter(string_scan(), permission); + let planned = plan(&source); + assert!(planned.native_plan.children()[0].is::()); + let prepared = + PhysicalPlanner::prepare_probe_for_runtime_filters(planned, Some(&source)).unwrap(); + assert_eq!(allowed(&prepared), permission); + let split = prepared.native_plan.children()[0] + .downcast_ref::() + .unwrap(); + assert!(Arc::ptr_eq( + split.children()[0], + &prepared.children[0].native_plan + )); + } + } + #[test] fn stacked_probe_filters_keep_independent_permissions() { for outer in [false, true] { diff --git a/native/core/src/execution/planner/scalar_expansion.rs b/native/core/src/execution/planner/scalar_expansion.rs index c0544ffb86d..be3d0186c87 100644 --- a/native/core/src/execution/planner/scalar_expansion.rs +++ b/native/core/src/execution/planner/scalar_expansion.rs @@ -93,11 +93,19 @@ impl PhysicalPlanner { /// here on a one-row batch: `repeat(, 8)` is wider than the subquery result it is /// built from. A subtree that is not a scalar is evaluated per row, so only the scalars /// among its children count. A subtree that fails to evaluate counts nothing, so that its - /// error surfaces where Spark's would, at execution. The members of an `IN` list are - /// compared and never expanded. + /// error surfaces where Spark's would, at execution. A constant member of an `IN` list is + /// compared and never expanded; a member computed per row counts the scalars it contains. fn expanded_scalar_bytes(&self, expr: &Arc, schema: &Schema) -> usize { if let Some(in_list) = expr.downcast_ref::() { - return self.expanded_scalar_bytes(in_list.expr(), schema); + // A member that IN compares as a constant is not expanded. One computed per row is, + // along with the scalars it contains. + let members: usize = in_list + .list() + .iter() + .filter(|member| !self.is_scalar(member)) + .map(|member| self.expanded_scalar_bytes(member, schema)) + .sum(); + return self.expanded_scalar_bytes(in_list.expr(), schema) + members; } if !self.is_scalar(expr) { return expr @@ -291,6 +299,29 @@ mod tests { assert_eq!(bytes(expr), 0); } + #[test] + fn counts_the_scalar_in_a_list_member_computed_per_row() { + use datafusion_comet_spark_expr::spark_in_list; + let schema = schema(); + let per_row = binary( + lit("x".repeat(256 * 1024)), + Operator::StringConcat, + col("s", &schema).unwrap(), + &schema, + ) + .unwrap(); + // The per-row member expands its scalar for every input row; the literal is compared. + let expr = spark_in_list( + col("s", &schema).unwrap(), + vec![per_row, lit("foo")], + false, + &schema, + ) + .unwrap(); + assert!(expr.is::()); + assert_eq!(bytes(expr), 256 * 1024); + } + #[test] fn wraps_the_input_at_the_session_batch_size_or_the_scalar_cap() { use datafusion::physical_plan::empty::EmptyExec; diff --git a/native/operators/src/batch_split.rs b/native/operators/src/batch_split.rs index 81aef57a7e9..ec8ff7b528d 100644 --- a/native/operators/src/batch_split.rs +++ b/native/operators/src/batch_split.rs @@ -59,6 +59,10 @@ impl BatchSplitExec { } } + pub fn input(&self) -> &Arc { + &self.input + } + pub fn batch_size(&self) -> usize { self.batch_size } diff --git a/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql index 8f45b8977e8..0828dd342a9 100644 --- a/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql +++ b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql @@ -80,3 +80,8 @@ SELECT sum(length(c)) FROM (SELECT concat(repeat('x', 262144), uuid()) AS c FROM -- a scalar built from the subquery result is wider than the result itself query SELECT sum(length(c)) FROM (SELECT concat(repeat((SELECT max(s) FROM test_large_scalar_wide), 2), CAST(k AS STRING)) AS c FROM test_large_scalar_ids) + +-- A list member computed per row expands the scalar it contains; the literal member does not +query +SELECT sum(CASE WHEN CAST(id AS STRING) IN (concat(repeat('x', 262144), CAST(id AS STRING)), 'foo') THEN 1 ELSE 0 END) +FROM range(0, 8192, 1, 1) diff --git a/spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala b/spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala index b76f52330ac..6e91a367362 100644 --- a/spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala +++ b/spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala @@ -659,42 +659,53 @@ class CometJoinSuite extends CometTestBase { } else { "dynamic_residual_probe p JOIN dynamic_residual_build b" } - val query = "SELECT /*+ BROADCAST(b) */ p.probe_key, p.payload FROM " + from + - " ON p.probe_key = b._1 WHERE pmod(p.payload, 7) > 0" - var unfilteredBytes = 0L - for (allowed <- Seq(false, true)) { - withSQLConf( - CometConf.COMET_EXEC_JOIN_DYNAMIC_FILTER_ALLOW_DETERMINISTIC_FILTER_PUSHDOWN.key -> allowed.toString) { - val (_, plan) = checkSparkAnswerAndOperator( - sql(query), - Seq(classOf[CometBroadcastHashJoinExec], classOf[CometNativeScanExec])) - checkAnswer(sql(query), Seq(Row(2500, 2500L))) - val joins = collect(plan) { case join: CometBroadcastHashJoinExec => join } - assert(joins.size == 1) - val filters = collect(plan) { - case filter: CometFilterExec if filter.output.exists(_.name == "probe_key") => - filter - } - assert(filters.size == 1, s"Expected one native residual filter:\n$plan") - assert(filters.head.condition.deterministic) - assert(filters.head.nativeOp.getFilter.getAllowRuntimeFilterPushdown == allowed) - val scans = collect(plan) { - case scan: CometNativeScanExec if scan.output.exists(_.name == "probe_key") => - scan - } - assert(scans.size == 1) - val metrics = scans.head.metrics - val bytes = metrics("bytes_scanned").value - assert(joins.head.metrics("output_rows").value == 1L) - assert(filters.head.metrics("output_rows").value > 0L) - if (allowed) { - assert(joins.head.metrics("dynamic_filter_join_filters_attached").value > 0L) - assert(metrics("row_groups_pruned_statistics").value > 0L) - assert(bytes < unfilteredBytes) - } else { - unfilteredBytes = bytes - assert(joins.head.metrics("dynamic_filter_join_filters_attached").value == 0L) - assert(joins.head.metrics("dynamic_filter_join_filters_skipped").value > 0L) + // A string scalar in the residual puts a batch split under the native filter, + // which must keep the filter's permission and let the reader filter through. + for (residual <- Seq( + "pmod(p.payload, 7) > 0", + "CAST(p.payload AS STRING) <> '2506'")) { + val query = "SELECT /*+ BROADCAST(b) */ p.probe_key, p.payload FROM " + from + + s" ON p.probe_key = b._1 WHERE $residual" + var unfilteredBytes = 0L + for (allowed <- Seq(false, true)) { + withSQLConf( + CometConf.COMET_EXEC_JOIN_DYNAMIC_FILTER_ALLOW_DETERMINISTIC_FILTER_PUSHDOWN.key -> allowed.toString) { + val (_, plan) = checkSparkAnswerAndOperator( + sql(query), + Seq(classOf[CometBroadcastHashJoinExec], classOf[CometNativeScanExec])) + checkAnswer(sql(query), Seq(Row(2500, 2500L))) + val joins = collect(plan) { case join: CometBroadcastHashJoinExec => join } + assert(joins.size == 1) + val filters = collect(plan) { + case filter: CometFilterExec + if filter.output.exists(_.name == "probe_key") => + filter + } + assert(filters.size == 1, s"Expected one native residual filter:\n$plan") + assert(filters.head.condition.deterministic) + assert( + filters.head.nativeOp.getFilter.getAllowRuntimeFilterPushdown == allowed) + val scans = collect(plan) { + case scan: CometNativeScanExec + if scan.output.exists(_.name == "probe_key") => + scan + } + assert(scans.size == 1) + val metrics = scans.head.metrics + val bytes = metrics("bytes_scanned").value + assert(joins.head.metrics("output_rows").value == 1L) + assert(filters.head.metrics("output_rows").value > 0L) + if (allowed) { + assert( + joins.head.metrics("dynamic_filter_join_filters_attached").value > 0L) + assert(metrics("row_groups_pruned_statistics").value > 0L) + assert(bytes < unfilteredBytes) + } else { + unfilteredBytes = bytes + assert( + joins.head.metrics("dynamic_filter_join_filters_attached").value == 0L) + assert(joins.head.metrics("dynamic_filter_join_filters_skipped").value > 0L) + } } } } From 29ce8c786edce6307b44ab19122651dea5577cd2 Mon Sep 17 00:00:00 2001 From: zhangfengcdt Date: Mon, 9 Nov 2026 06:07:24 -0800 Subject: [PATCH 8/9] fix: skip a scalar a comparison takes as a Datum and bound the sort keys A scalar operand of a comparison is compared as a Datum and never expanded, so `payload = 'x'` no longer installs a split; an operand computed per row still counts the scalars it contains. The sort bounds its input with its keys, which covers the TopK under a limit. --- native/core/src/execution/planner.rs | 87 ++++++++++++++++-- native/core/src/execution/planner/join.rs | 22 +++-- .../src/execution/planner/scalar_expansion.rs | 90 ++++++++++++++++--- .../operators/large_scalar_string.sql | 4 + .../apache/comet/exec/CometJoinSuite.scala | 6 +- 5 files changed, 181 insertions(+), 28 deletions(-) diff --git a/native/core/src/execution/planner.rs b/native/core/src/execution/planner.rs index 93520be998a..4ac80ad71bf 100644 --- a/native/core/src/execution/planner.rs +++ b/native/core/src/execution/planner.rs @@ -1816,19 +1816,21 @@ impl PhysicalPlanner { let (scans, shuffle_scans, child) = self.create_plan(&children[0], inputs, partition_count)?; - let exprs: Result, ExecutionError> = sort + let exprs: Vec = sort .sort_orders .iter() .map(|expr| self.create_sort_expr(expr, child.schema())) - .collect(); + .collect::>()?; let fetch = sort.fetch.map(|num| num as usize); - let sort_plan = SortExec::new( - LexOrdering::new(exprs?).unwrap(), + // The sort evaluates its keys on each batch it receives. + let input = self.bound_scalar_expansion( Arc::clone(&child.native_plan), - ) - .with_fetch(fetch); + exprs.iter().map(|sort| &sort.expr), + ); + let sort_plan = + SortExec::new(LexOrdering::new(exprs).unwrap(), input).with_fetch(fetch); let mut sort_exec: Arc = if sort.dynamic_filter_enabled { match TopKReaderFilterExec::try_new( &sort_plan, @@ -3187,7 +3189,7 @@ mod tests { use crate::execution::shuffle::CometPartitioning; use crate::parquet::parquet_support::SparkParquetOptions; use crate::parquet::schema_adapter::SparkPhysicalExprAdapterFactory; - use datafusion_comet_operators::{PartitionedRankLimitExec, WindowFnKind}; + use datafusion_comet_operators::{BatchSplitExec, PartitionedRankLimitExec, WindowFnKind}; use datafusion_comet_proto::spark_expression::expr::ExprStruct; use datafusion_comet_proto::spark_expression::ListLiteral; use datafusion_comet_proto::{ @@ -4315,6 +4317,77 @@ mod tests { ); } + #[test] + fn sort_bounds_the_scalar_its_keys_expand() { + // A sort evaluates its keys on each batch it receives, and a TopK on each batch it + // inserts, expanding a string scalar in a key to the batch's length. The keys get the + // bound a projection's expressions get. + let string_type = spark_expression::DataType { + type_id: spark_expression::data_type::DataTypeId::String as i32, + type_info: None, + }; + let scan = Operator { + op_struct: Some(OpStruct::Scan(spark_operator::Scan { + fields: vec![string_type.clone()], + source: String::new(), + })), + ..Default::default() + }; + let wide = Expr { + expr_struct: Some(Literal(spark_expression::Literal { + value: Some(literal::Value::StringVal("x".repeat(256 * 1024))), + datatype: Some(string_type.clone()), + is_null: false, + })), + ..Default::default() + }; + let column = Expr { + expr_struct: Some(Bound(spark_expression::BoundReference { + index: 0, + datatype: Some(string_type.clone()), + })), + ..Default::default() + }; + let key = Expr { + expr_struct: Some(ScalarFunc(spark_expression::ScalarFunc { + func: "concat".to_string(), + args: vec![wide, column], + return_type: Some(string_type), + ..Default::default() + })), + ..Default::default() + }; + for fetch in [None, Some(1)] { + let sort = Operator { + children: vec![scan.clone()], + op_struct: Some(OpStruct::Sort(spark_operator::Sort { + sort_orders: vec![Expr { + expr_struct: Some(SortOrder(Box::new(spark_expression::SortOrder { + child: Some(Box::new(key.clone())), + direction: spark_expression::SortDirection::Ascending as i32, + null_ordering: spark_expression::NullOrdering::NullsFirst as i32, + }))), + ..Default::default() + }], + fetch, + ..Default::default() + })), + ..Default::default() + }; + let (_, _, planned) = PhysicalPlanner::default() + .create_plan(&sort, &mut vec![], 1) + .unwrap(); + let sort = planned.native_plan.downcast_ref::().unwrap(); + assert_eq!(fetch.map(|n| n as usize), sort.fetch()); + let split = sort + .input() + .downcast_ref::() + .expect("a split under the sort"); + // A 256 KiB scalar expanded to 4096 rows takes 1 GiB. + assert_eq!(4096, split.batch_size()); + } + } + #[test] fn declared_order_reaches_a_scan_of_one_file_group() { // A scan of files reads the files of its one file group one after the other, so the order diff --git a/native/core/src/execution/planner/join.rs b/native/core/src/execution/planner/join.rs index cef8c097162..4db1c294f9b 100644 --- a/native/core/src/execution/planner/join.rs +++ b/native/core/src/execution/planner/join.rs @@ -1002,7 +1002,8 @@ mod tests { } } - /// A filter on `column = 'x'`: the string scalar puts a batch split under the filter. + /// A filter on `concat(column, 'x') = 'yx'`: the scalar inside the concatenation puts a + /// batch split under the filter. A scalar compared directly is never expanded. fn string_filter(child: Operator, allowed: bool) -> Operator { let column = Expr { expr_struct: Some(ExprStruct::Bound(spark_expression::BoundReference { @@ -1011,22 +1012,33 @@ mod tests { })), ..Default::default() }; - let literal = Expr { + let literal = |value: &str| Expr { expr_struct: Some(ExprStruct::Literal(spark_expression::Literal { - value: Some(spark_expression::literal::Value::StringVal("x".to_string())), + value: Some(spark_expression::literal::Value::StringVal( + value.to_string(), + )), datatype: Some(string_type()), is_null: false, })), ..Default::default() }; + let concatenated = Expr { + expr_struct: Some(ExprStruct::ScalarFunc(spark_expression::ScalarFunc { + func: "concat".to_string(), + args: vec![column, literal("x")], + return_type: Some(string_type()), + ..Default::default() + })), + ..Default::default() + }; Operator { plan_id: child.plan_id + 1, children: vec![child], op_struct: Some(OpStruct::Filter(spark_operator::Filter { predicate: Some(Expr { expr_struct: Some(ExprStruct::Eq(Box::new(spark_expression::BinaryExpr { - left: Some(Box::new(column)), - right: Some(Box::new(literal)), + left: Some(Box::new(concatenated)), + right: Some(Box::new(literal("yx"))), }))), ..Default::default() }), diff --git a/native/core/src/execution/planner/scalar_expansion.rs b/native/core/src/execution/planner/scalar_expansion.rs index be3d0186c87..84fb28babb7 100644 --- a/native/core/src/execution/planner/scalar_expansion.rs +++ b/native/core/src/execution/planner/scalar_expansion.rs @@ -34,8 +34,8 @@ use std::sync::Arc; use arrow::datatypes::{DataType, Schema}; use arrow::record_batch::{RecordBatch, RecordBatchOptions}; use datafusion::common::ScalarValue; -use datafusion::logical_expr::{ColumnarValue, Volatility}; -use datafusion::physical_expr::expressions::{Column, InListExpr}; +use datafusion::logical_expr::{ColumnarValue, Operator, Volatility}; +use datafusion::physical_expr::expressions::{BinaryExpr, Column, InListExpr}; use datafusion::physical_expr::{PhysicalExpr, ScalarFunctionExpr}; use datafusion::physical_plan::ExecutionPlan; use datafusion_comet_operators::BatchSplitExec; @@ -93,19 +93,24 @@ impl PhysicalPlanner { /// here on a one-row batch: `repeat(, 8)` is wider than the subquery result it is /// built from. A subtree that is not a scalar is evaluated per row, so only the scalars /// among its children count. A subtree that fails to evaluate counts nothing, so that its - /// error surfaces where Spark's would, at execution. A constant member of an `IN` list is - /// compared and never expanded; a member computed per row counts the scalars it contains. + /// error surfaces where Spark's would, at execution. A scalar that a comparison or an `IN` + /// list takes as a constant is compared and never expanded; an operand computed per row + /// counts the scalars it contains. fn expanded_scalar_bytes(&self, expr: &Arc, schema: &Schema) -> usize { if let Some(in_list) = expr.downcast_ref::() { - // A member that IN compares as a constant is not expanded. One computed per row is, - // along with the scalars it contains. - let members: usize = in_list - .list() - .iter() - .filter(|member| !self.is_scalar(member)) - .map(|member| self.expanded_scalar_bytes(member, schema)) - .sum(); - return self.expanded_scalar_bytes(in_list.expr(), schema) + members; + // The tested value is expanded to the batch's length. A member that IN compares as + // a constant is not; one computed per row is, along with the scalars it contains. + return self.expanded_scalar_bytes(in_list.expr(), schema) + + self.per_row_operand_bytes(in_list.list().iter(), schema); + } + if let Some(comparison) = expr + .downcast_ref::() + .filter(|binary| compares_as_datum(binary.op())) + { + return self.per_row_operand_bytes( + [comparison.left(), comparison.right()].into_iter(), + schema, + ); } if !self.is_scalar(expr) { return expr @@ -144,6 +149,39 @@ impl PhysicalPlanner { _ => 0, } } + + /// The bytes of the scalars inside the `operands` computed per row. An operand that is a + /// scalar is compared as it is. + fn per_row_operand_bytes<'a>( + &self, + operands: impl Iterator>, + schema: &Schema, + ) -> usize { + operands + .filter(|operand| !self.is_scalar(operand)) + .map(|operand| self.expanded_scalar_bytes(operand, schema)) + .sum() + } +} + +/// Whether DataFusion's `BinaryExpr` evaluates `op` with `apply_cmp`, which takes a scalar +/// operand as a `Datum` instead of expanding it to the batch's length. +fn compares_as_datum(op: &Operator) -> bool { + matches!( + op, + Operator::Eq + | Operator::NotEq + | Operator::Lt + | Operator::LtEq + | Operator::Gt + | Operator::GtEq + | Operator::IsDistinctFrom + | Operator::IsNotDistinctFrom + | Operator::LikeMatch + | Operator::ILikeMatch + | Operator::NotLikeMatch + | Operator::NotILikeMatch + ) } impl PhysicalPlanner { @@ -341,4 +379,30 @@ mod tests { let wide = lit("x".repeat(256 * 1024)); assert_eq!(2048, split_at(&[Arc::clone(&wide), wide])); } + + #[test] + fn skips_a_scalar_that_a_comparison_takes_as_a_datum() { + let schema = schema(); + let wide = lit("x".repeat(256 * 1024)); + for op in [ + Operator::Eq, + Operator::NotEq, + Operator::Lt, + Operator::IsNotDistinctFrom, + ] { + let direct = + binary(col("s", &schema).unwrap(), op, Arc::clone(&wide), &schema).unwrap(); + assert_eq!(bytes(direct), 0, "{op}"); + // An operand computed per row still expands the scalar it contains. + let per_row = binary( + Arc::clone(&wide), + Operator::StringConcat, + col("s", &schema).unwrap(), + &schema, + ) + .unwrap(); + let compared = binary(col("s", &schema).unwrap(), op, per_row, &schema).unwrap(); + assert_eq!(bytes(compared), 256 * 1024, "{op}"); + } + } } diff --git a/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql index 0828dd342a9..1c8851ac85a 100644 --- a/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql +++ b/spark/src/test/resources/sql-tests/operators/large_scalar_string.sql @@ -85,3 +85,7 @@ SELECT sum(length(c)) FROM (SELECT concat(repeat((SELECT max(s) FROM test_large_ query SELECT sum(CASE WHEN CAST(id AS STRING) IN (concat(repeat('x', 262144), CAST(id AS STRING)), 'foo') THEN 1 ELSE 0 END) FROM range(0, 8192, 1, 1) + +-- a sort key evaluated per batch by the TopK under the limit +query +SELECT id FROM range(0, 8192, 1, 1) ORDER BY concat(repeat('x', 262144), CAST(id AS STRING)) LIMIT 1 diff --git a/spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala b/spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala index 443ef7aca15..10637d82814 100644 --- a/spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala +++ b/spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala @@ -678,11 +678,11 @@ class CometJoinSuite extends CometTestBase { } else { "dynamic_residual_probe p JOIN dynamic_residual_build b" } - // A string scalar in the residual puts a batch split under the native filter, - // which must keep the filter's permission and let the reader filter through. + // A string scalar that the residual expands puts a batch split under the native + // filter, which must keep the filter's permission and let the reader filter through. for (residual <- Seq( "pmod(p.payload, 7) > 0", - "CAST(p.payload AS STRING) <> '2506'")) { + "concat(CAST(p.payload AS STRING), '!') <> '2506!'")) { val query = "SELECT /*+ BROADCAST(b) */ p.probe_key, p.payload FROM " + from + s" ON p.probe_key = b._1 WHERE $residual" var unfilteredBytes = 0L From a5708d07ed9a4db42aa1e39e06520ebd6b3a34e5 Mon Sep 17 00:00:00 2001 From: Feng Zhang Date: Thu, 19 Nov 2026 11:04:51 -0800 Subject: [PATCH 9/9] fix: split only under a sort with a limit and pull JVM input on demand A full sort concatenates its input before evaluating its keys, so the split did not bound it, and DataFusion's ExternalSorter reserves each slice of a shared buffer as if it owned the buffer (apache/datafusion#25804). executePlan no longer pulls the next JVM batch on every call. It pulls only when the plan waits for input, so a limit can finish on rows a split still holds without reading another batch. --- native/core/src/execution/jni_api.rs | 6 +- native/core/src/execution/planner.rs | 40 ++++++++----- .../CometExecIteratorLifecycleSuite.scala | 58 ++++++++++++++++++- 3 files changed, 84 insertions(+), 20 deletions(-) diff --git a/native/core/src/execution/jni_api.rs b/native/core/src/execution/jni_api.rs index 0437ed9dbff..2006b5ceb06 100644 --- a/native/core/src/execution/jni_api.rs +++ b/native/core/src/execution/jni_api.rs @@ -1641,10 +1641,10 @@ fn execute_plan( exec_context.stream = Some(stream); } exec_context.root_op = Some(root_op); - } else { - // Pull input batches - pull_input_batches(exec_context)?; } + // JVM input is pulled only when the stream waits for it, in `next_batch` below. An operator + // above a scan, such as a `BatchSplitExec`, can still hold rows of the last batch it took, + // and a limit can finish on them without the JVM producing another batch. // Wait no later than when metrics are due, so a plan that runs long before its first batch, // such as a native shuffle write, still publishes on the interval. A publish while waiting diff --git a/native/core/src/execution/planner.rs b/native/core/src/execution/planner.rs index 8908b13a5f4..5665fa56129 100644 --- a/native/core/src/execution/planner.rs +++ b/native/core/src/execution/planner.rs @@ -1864,11 +1864,18 @@ impl PhysicalPlanner { let fetch = sort.fetch.map(|num| num as usize); - // The sort evaluates its keys on each batch it receives. - let input = self.bound_scalar_expansion( - Arc::clone(&child.native_plan), - exprs.iter().map(|sort| &sort.expr), - ); + // A TopK evaluates its keys on each batch it receives. A full sort concatenates + // its buffered input before evaluating them, so a split would not bound it, and + // DataFusion's `ExternalSorter` reserves each slice of a shared buffer as if it + // owned the buffer (apache/datafusion#25804). + let input = if fetch.is_some() { + self.bound_scalar_expansion( + Arc::clone(&child.native_plan), + exprs.iter().map(|sort| &sort.expr), + ) + } else { + Arc::clone(&child.native_plan) + }; let sort_plan = SortExec::new(LexOrdering::new(exprs).unwrap(), input).with_fetch(fetch); let mut sort_exec: Arc = if sort.dynamic_filter_enabled { @@ -4375,9 +4382,9 @@ mod tests { #[test] fn sort_bounds_the_scalar_its_keys_expand() { - // A sort evaluates its keys on each batch it receives, and a TopK on each batch it - // inserts, expanding a string scalar in a key to the batch's length. The keys get the - // bound a projection's expressions get. + // A TopK evaluates its keys on each batch it inserts, expanding a string scalar in a key + // to the batch's length, so the keys get the bound a projection's expressions get. A full + // sort gets no split: it evaluates its keys after concatenating its buffered input. let string_type = spark_expression::DataType { type_id: spark_expression::data_type::DataTypeId::String as i32, type_info: None, @@ -4435,12 +4442,17 @@ mod tests { .unwrap(); let sort = planned.native_plan.downcast_ref::().unwrap(); assert_eq!(fetch.map(|n| n as usize), sort.fetch()); - let split = sort - .input() - .downcast_ref::() - .expect("a split under the sort"); - // A 256 KiB scalar expanded to 4096 rows takes 1 GiB. - assert_eq!(4096, split.batch_size()); + match fetch { + None => assert!(!sort.input().is::()), + Some(_) => { + let split = sort + .input() + .downcast_ref::() + .expect("a split under the TopK"); + // A 256 KiB scalar expanded to 4096 rows takes 1 GiB. + assert_eq!(4096, split.batch_size()); + } + } } } diff --git a/spark/src/test/scala/org/apache/spark/CometExecIteratorLifecycleSuite.scala b/spark/src/test/scala/org/apache/spark/CometExecIteratorLifecycleSuite.scala index 1caaec887fa..a9f3d4b5cc7 100644 --- a/spark/src/test/scala/org/apache/spark/CometExecIteratorLifecycleSuite.scala +++ b/spark/src/test/scala/org/apache/spark/CometExecIteratorLifecycleSuite.scala @@ -29,18 +29,18 @@ import scala.reflect.ClassTag import org.apache.arrow.c.ArrowArrayStream import org.apache.arrow.memory.RootAllocator -import org.apache.arrow.vector.BigIntVector +import org.apache.arrow.vector.{BigIntVector, VarCharVector} import org.apache.arrow.vector.ipc.ArrowReader import org.apache.arrow.vector.types.pojo.{ArrowType, Field, Schema} import org.apache.spark.executor.TaskMetrics import org.apache.spark.memory.{TaskMemoryManager, TestMemoryManager} import org.apache.spark.rdd.RDD import org.apache.spark.sql.CometTestBase -import org.apache.spark.sql.catalyst.expressions.PrettyAttribute +import org.apache.spark.sql.catalyst.expressions.{Alias, AttributeReference, Concat, Literal, PrettyAttribute} import org.apache.spark.sql.comet.{CometExec, CometExecUtils, CometMetricNode, CometNativeScanExec, CometProjectExec, CometSortExec} import org.apache.spark.sql.comet.execution.arrow.CometArrowStream import org.apache.spark.sql.functions.{col, udf} -import org.apache.spark.sql.types.{LongType, StructField, StructType} +import org.apache.spark.sql.types.{LongType, StringType, StructField, StructType} import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector} import org.apache.spark.util.JsonProtocol @@ -293,6 +293,58 @@ class CometExecIteratorLifecycleSuite extends CometTestBase { } } + test("a limit finishes on the rows a split holds without pulling the next input batch") { + // `concat(s, '!')` expands a string scalar, so the planner splits the scan's batches to the + // batch size of 2 under the projection. The limit of 3 is met by the first input batch's + // second slice, so native must not pull the input again, which here would throw. + withSQLConf(CometConf.COMET_BATCH_SIZE.key -> "2") { + val allocator = CometArrowAllocator.newChildAllocator("split-limit", 0, Long.MaxValue) + try { + withTaskContext(4650000L) { + val vector = new VarCharVector("s", allocator) + vector.allocateNew(10) + (0 until 10).foreach(i => vector.setSafe(i, s"r$i".getBytes("UTF-8"))) + vector.setValueCount(10) + val first = new ColumnarBatch(Array[ColumnVector](new CometPlainVector(vector)), 10) + val input = Iterator.single(first) ++ new Iterator[ColumnarBatch] { + override def hasNext: Boolean = throw new IllegalStateException("read ahead") + override def next(): ColumnarBatch = throw new IllegalStateException("read ahead") + } + val stream = CometArrowStream.fromColumnarBatchIter( + input, + StructType(Seq(StructField("s", StringType, nullable = false))), + CometArrowStream.NATIVE_TIMEZONE, + "split-limit") + val s = AttributeReference("s", StringType, nullable = false)() + val limit = CometExecUtils.getLimitNativePlan(Seq(s), 3).get + val projection = CometExecUtils + .getProjectionNativePlan( + Seq(Alias(Concat(Seq(s, Literal("!"))), "c")()), + Seq(s), + limit.getChildren(0)) + .get + val iter = CometExec.getCometIterator( + Array(stream.asInstanceOf[Object]), + 1, + limit.toBuilder.setChildren(0, projection).build(), + CometMetricNode(Map.empty), + 1, + 0, + None, + Seq.empty, + None) + var rows = 0 + while (iter.hasNext) { + rows += iter.next().numRows() + } + assert(rows == 3) + } + } finally { + allocator.close() + } + } + } + test("a plan fed only by native scans returns its memory when its consumer stops early") { // See issue #2453. The sort keeps its sorted runs in memory, and the merge that reads them // spawns a Tokio task for each run.