|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::{ |
| 19 | + CaseWhenExpr, Cast, CheckedBinaryExpr, CreateNamedStruct, GetStructField, IfExpr, ListExtract, |
| 20 | + NegativeExpr, NullShortCircuit, RLike, SparkComparison, Subquery, ToJson, |
| 21 | +}; |
| 22 | +use arrow::array::{new_empty_array, Array, ArrayData, ArrayRef}; |
| 23 | +use arrow::compute::concat; |
| 24 | +use arrow::datatypes::{DataType, FieldRef, Schema}; |
| 25 | +use arrow::record_batch::RecordBatch; |
| 26 | +use datafusion::common::{exec_err, internal_err, Result, ScalarValue}; |
| 27 | +use datafusion::logical_expr::{ColumnarValue, Volatility}; |
| 28 | +use datafusion::physical_expr::expressions::{ |
| 29 | + BinaryExpr, CastExpr, Column, InListExpr, IsNotNullExpr, IsNullExpr, LikeExpr, Literal, NotExpr, |
| 30 | +}; |
| 31 | +use datafusion::physical_expr::{PhysicalExpr, ScalarFunctionExpr}; |
| 32 | +use std::fmt::{Display, Formatter}; |
| 33 | +use std::hash::{Hash, Hasher}; |
| 34 | +use std::sync::Arc; |
| 35 | + |
| 36 | +/// Evaluates scalar-struct consumers before repeating large variable-width fields. |
| 37 | +/// |
| 38 | +/// GetStructField retains its batch-sized array contract. Constant consumers run on one row; |
| 39 | +/// pure row-dependent consumers run on bounded slices before their results are concatenated. |
| 40 | +/// Nothing is evaluated during planning, preserving conditional and ANSI NULL short-circuiting. |
| 41 | +#[derive(Debug, Eq)] |
| 42 | +pub struct ScalarSubqueryConsumer { |
| 43 | + inner: Arc<dyn PhysicalExpr>, |
| 44 | + constant: bool, |
| 45 | + max_rows: usize, |
| 46 | +} |
| 47 | + |
| 48 | +// Limit repeated scalar input payload, not the size of the user's final output. A single scalar |
| 49 | +// can exceed this budget; in that case consume one row at a time. |
| 50 | +const SCALAR_INPUT_BYTES: usize = 1024 * 1024; |
| 51 | + |
| 52 | +impl ScalarSubqueryConsumer { |
| 53 | + pub fn wrap(inner: Arc<dyn PhysicalExpr>) -> Arc<dyn PhysicalExpr> { |
| 54 | + // A field projection must still materialize its output. Wrap its consumer instead. |
| 55 | + if inner.is::<GetStructField>() || inner.is::<Self>() { |
| 56 | + return inner; |
| 57 | + } |
| 58 | + if let Some(properties) = consumer_properties(&inner) { |
| 59 | + if properties.has_field && (properties.constant || properties.bytes != 0) { |
| 60 | + return Arc::new(Self { |
| 61 | + inner, |
| 62 | + constant: properties.constant, |
| 63 | + max_rows: (SCALAR_INPUT_BYTES / properties.bytes.max(1)).max(1), |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + inner |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +struct ConsumerProperties { |
| 72 | + constant: bool, |
| 73 | + has_field: bool, |
| 74 | + bytes: usize, |
| 75 | +} |
| 76 | + |
| 77 | +/// Explicitly recognize pure row-local trees. Unknown nodes and volatile functions cannot safely |
| 78 | +/// be evaluated on a different batch. Columns require sliced evaluation. |
| 79 | +fn consumer_properties(expr: &Arc<dyn PhysicalExpr>) -> Option<ConsumerProperties> { |
| 80 | + if let Some(value) = expr |
| 81 | + .downcast_ref::<GetStructField>() |
| 82 | + .and_then(GetStructField::literal_field) |
| 83 | + { |
| 84 | + return Some(ConsumerProperties { |
| 85 | + constant: true, |
| 86 | + has_field: true, |
| 87 | + bytes: variable_bytes(&value.to_data()), |
| 88 | + }); |
| 89 | + } |
| 90 | + if let Some(literal) = expr.downcast_ref::<Literal>() { |
| 91 | + let bytes = match literal.value() { |
| 92 | + ScalarValue::Utf8(Some(value)) | ScalarValue::LargeUtf8(Some(value)) => value.len(), |
| 93 | + ScalarValue::Binary(Some(value)) | ScalarValue::LargeBinary(Some(value)) => value.len(), |
| 94 | + ScalarValue::Struct(value) => variable_bytes(&value.to_data()), |
| 95 | + _ => 0, |
| 96 | + }; |
| 97 | + return Some(ConsumerProperties { |
| 98 | + constant: true, |
| 99 | + has_field: false, |
| 100 | + bytes, |
| 101 | + }); |
| 102 | + } |
| 103 | + // Spark materializes and registers this execution-context value before native execution. |
| 104 | + // Lookup still happens at runtime; it is independent of the current input row or batch. |
| 105 | + if expr.is::<Subquery>() { |
| 106 | + return Some(ConsumerProperties { |
| 107 | + constant: true, |
| 108 | + has_field: false, |
| 109 | + bytes: 0, |
| 110 | + }); |
| 111 | + } |
| 112 | + if expr.is::<Column>() { |
| 113 | + return Some(ConsumerProperties { |
| 114 | + constant: false, |
| 115 | + has_field: false, |
| 116 | + bytes: 0, |
| 117 | + }); |
| 118 | + } |
| 119 | + if let Some(expr) = expr.downcast_ref::<ScalarSubqueryConsumer>() { |
| 120 | + return consumer_properties(&expr.inner); |
| 121 | + } |
| 122 | + if let Some(expr) = expr.downcast_ref::<NullShortCircuit>() { |
| 123 | + return consumer_properties(expr.inner()); |
| 124 | + } |
| 125 | + if let Some(fun) = expr.downcast_ref::<ScalarFunctionExpr>() { |
| 126 | + if fun.fun().signature().volatility != Volatility::Immutable { |
| 127 | + return None; |
| 128 | + } |
| 129 | + } else if !(expr.is::<GetStructField>() |
| 130 | + || expr.is::<CastExpr>() |
| 131 | + || expr.is::<Cast>() |
| 132 | + || expr.is::<BinaryExpr>() |
| 133 | + || expr.is::<CheckedBinaryExpr>() |
| 134 | + || expr.is::<NegativeExpr>() |
| 135 | + || expr.is::<SparkComparison>() |
| 136 | + || expr.is::<IsNullExpr>() |
| 137 | + || expr.is::<IsNotNullExpr>() |
| 138 | + || expr.is::<NotExpr>() |
| 139 | + || expr.is::<InListExpr>() |
| 140 | + || expr.is::<LikeExpr>() |
| 141 | + || expr.is::<RLike>() |
| 142 | + || expr.is::<IfExpr>() |
| 143 | + || expr.is::<CaseWhenExpr>() |
| 144 | + || expr.is::<CreateNamedStruct>() |
| 145 | + || expr.is::<ListExtract>() |
| 146 | + || expr.is::<ToJson>()) |
| 147 | + { |
| 148 | + return None; |
| 149 | + } |
| 150 | + let mut properties = ConsumerProperties { |
| 151 | + constant: true, |
| 152 | + has_field: false, |
| 153 | + bytes: 0, |
| 154 | + }; |
| 155 | + for child in expr.children() { |
| 156 | + let child = consumer_properties(child)?; |
| 157 | + properties.constant &= child.constant; |
| 158 | + properties.has_field |= child.has_field; |
| 159 | + properties.bytes = properties.bytes.saturating_add(child.bytes); |
| 160 | + } |
| 161 | + properties.has_field |= expr.is::<GetStructField>() && properties.constant; |
| 162 | + Some(properties) |
| 163 | +} |
| 164 | + |
| 165 | +fn variable_bytes(data: &ArrayData) -> usize { |
| 166 | + match data.data_type() { |
| 167 | + DataType::Utf8 | DataType::LargeUtf8 | DataType::Binary | DataType::LargeBinary => { |
| 168 | + data.buffers()[1].len() |
| 169 | + } |
| 170 | + DataType::List(_) |
| 171 | + | DataType::LargeList(_) |
| 172 | + | DataType::FixedSizeList(_, _) |
| 173 | + | DataType::Map(_, _) => data.get_buffer_memory_size(), |
| 174 | + _ => data |
| 175 | + .child_data() |
| 176 | + .iter() |
| 177 | + .map(variable_bytes) |
| 178 | + .fold(0, usize::saturating_add), |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +impl Hash for ScalarSubqueryConsumer { |
| 183 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 184 | + self.inner.hash(state); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +impl PartialEq for ScalarSubqueryConsumer { |
| 189 | + fn eq(&self, other: &Self) -> bool { |
| 190 | + self.inner.eq(&other.inner) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +impl Display for ScalarSubqueryConsumer { |
| 195 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 196 | + write!(f, "ScalarSubqueryConsumer({})", self.inner) |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +impl PhysicalExpr for ScalarSubqueryConsumer { |
| 201 | + fn fmt_sql(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 202 | + self.inner.fmt_sql(f) |
| 203 | + } |
| 204 | + |
| 205 | + fn data_type(&self, input_schema: &Schema) -> Result<DataType> { |
| 206 | + self.inner.data_type(input_schema) |
| 207 | + } |
| 208 | + |
| 209 | + fn nullable(&self, input_schema: &Schema) -> Result<bool> { |
| 210 | + self.inner.nullable(input_schema) |
| 211 | + } |
| 212 | + |
| 213 | + fn return_field(&self, input_schema: &Schema) -> Result<FieldRef> { |
| 214 | + self.inner.return_field(input_schema) |
| 215 | + } |
| 216 | + |
| 217 | + fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> { |
| 218 | + let rows = batch.num_rows(); |
| 219 | + if rows == 0 { |
| 220 | + return Ok(ColumnarValue::Array(new_empty_array( |
| 221 | + &self.data_type(batch.schema_ref())?, |
| 222 | + ))); |
| 223 | + } |
| 224 | + if !self.constant { |
| 225 | + if rows <= self.max_rows { |
| 226 | + return self.inner.evaluate(batch); |
| 227 | + } |
| 228 | + let mut arrays: Vec<ArrayRef> = Vec::new(); |
| 229 | + for offset in (0..rows).step_by(self.max_rows) { |
| 230 | + let len = self.max_rows.min(rows - offset); |
| 231 | + let array = self |
| 232 | + .inner |
| 233 | + .evaluate(&batch.slice(offset, len))? |
| 234 | + .into_array(len)?; |
| 235 | + if array.len() != len { |
| 236 | + return internal_err!( |
| 237 | + "Scalar subquery consumer returned {} rows, expected {len}", |
| 238 | + array.len() |
| 239 | + ); |
| 240 | + } |
| 241 | + arrays.push(array); |
| 242 | + } |
| 243 | + let arrays: Vec<&dyn Array> = arrays.iter().map(|array| array.as_ref()).collect(); |
| 244 | + return Ok(ColumnarValue::Array(concat(&arrays)?)); |
| 245 | + } |
| 246 | + let value = match self.inner.evaluate(&batch.slice(0, 1))? { |
| 247 | + ColumnarValue::Scalar(value) => value, |
| 248 | + ColumnarValue::Array(array) => { |
| 249 | + if array.len() != 1 { |
| 250 | + return internal_err!("Scalar subquery consumer returned {} rows", array.len()); |
| 251 | + } |
| 252 | + if rows == 1 { |
| 253 | + return Ok(ColumnarValue::Array(array)); |
| 254 | + } |
| 255 | + ScalarValue::try_from_array(array.as_ref(), 0)? |
| 256 | + } |
| 257 | + }; |
| 258 | + // A genuinely oversized final string/binary column cannot use 32-bit Arrow offsets. |
| 259 | + // Report it as an execution error rather than panicking in new_repeated. |
| 260 | + let bytes = match &value { |
| 261 | + ScalarValue::Utf8(Some(value)) => value.len(), |
| 262 | + ScalarValue::Binary(Some(value)) => value.len(), |
| 263 | + _ => 0, |
| 264 | + }; |
| 265 | + if bytes |
| 266 | + .checked_mul(rows) |
| 267 | + .is_none_or(|n| n > i32::MAX as usize) |
| 268 | + { |
| 269 | + return exec_err!( |
| 270 | + "Scalar subquery consumer result exceeds Arrow's 32-bit offset limit" |
| 271 | + ); |
| 272 | + } |
| 273 | + Ok(ColumnarValue::Array(value.to_array_of_size(rows)?)) |
| 274 | + } |
| 275 | + |
| 276 | + fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> { |
| 277 | + vec![&self.inner] |
| 278 | + } |
| 279 | + |
| 280 | + fn with_new_children( |
| 281 | + self: Arc<Self>, |
| 282 | + children: Vec<Arc<dyn PhysicalExpr>>, |
| 283 | + ) -> Result<Arc<dyn PhysicalExpr>> { |
| 284 | + let [inner]: [Arc<dyn PhysicalExpr>; 1] = children.try_into().map_err(|_| { |
| 285 | + datafusion::common::internal_datafusion_err!("Expected one scalar subquery consumer") |
| 286 | + })?; |
| 287 | + // Recheck after rewrites: constant and row-dependent consumers need different evaluation. |
| 288 | + Ok(Self::wrap(inner)) |
| 289 | + } |
| 290 | +} |
0 commit comments