sunchao commented on code in PR #7316: URL: https://github.com/apache/datafusion-comet/pull/7316#discussion_r4433043526
########## native/core/src/execution/planner/scalar_expansion.rs: ########## @@ -0,0 +1,263 @@ +// 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 +//! <https://github.com/apache/datafusion-comet/issues/7247>. +//! +//! 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, Volatility}; +use datafusion::physical_expr::expressions::{Column, InListExpr}; +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::monotonically_increasing_id::MonotonicallyIncreasingId; +use datafusion_comet_spark_expr::{ + RandExpr, RandStrExpr, RandnExpr, ShuffleExpr, Subquery, UuidExpr, +}; + +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<dyn ExecutionPlan>, + exprs: impl IntoIterator<Item = &'a Arc<dyn PhysicalExpr>>, + ) -> Arc<dyn ExecutionPlan> { + 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 { Review Comment: With `spark.comet.batchSize=256` and a 256 KiB string scalar, this returns the input unchanged because 256 rows fit the budget. The upstream `FilterExec` on this head still coalesces toward 8,192 rows. An all-passing filter can therefore feed the projection 8,192 copies of that scalar, reaching 2 GiB of string data and exceeding the signed 32-bit Arrow offset limit. Could expansion cap the batches it actually receives even when the calculated cap equals the session setting? A small-session version of the filter regression would cover this branch. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
