From 4caad780ffa6ae44650cd8d7301854de40832ca7 Mon Sep 17 00:00:00 2001 From: Andy Grove Date: Sat, 31 Oct 2026 12:15:25 +0000 Subject: [PATCH] fix: evaluate the right operand of AND, OR and IN only for rows the left side has not decided (#7326) * fix: evaluate the right operand of AND, OR and IN only for rows the left side has not decided * refactor: share masked evaluation with NullShortCircuit and build AND/OR in spark_comparison * bench: add micro-benchmarks for lazy AND, OR and IN * perf: try the right operand over the whole batch when most rows need it, and keep runs of IN candidates as one IN * fix: keep an expensive IN value out of the lazy rewrite and build the OR tree balanced (cherry picked from commit 7deeba21a42e4a9cd34f43e7ed6e960e4e851b26) --- native/spark-expr/Cargo.toml | 4 + native/spark-expr/benches/lazy_logical.rs | 217 +++++++ .../src/array_funcs/nested_comparison.rs | 16 +- .../src/conditional_funcs/case_when.rs | 4 +- .../src/conditional_funcs/lazy_logical.rs | 581 ++++++++++++++++++ .../spark-expr/src/conditional_funcs/mod.rs | 2 + .../conditional_funcs/null_short_circuit.rs | 27 +- .../conditional/logical_lazy_ansi.sql | 74 +++ 8 files changed, 912 insertions(+), 13 deletions(-) create mode 100644 native/spark-expr/benches/lazy_logical.rs create mode 100644 native/spark-expr/src/conditional_funcs/lazy_logical.rs create mode 100644 spark/src/test/resources/sql-tests/expressions/conditional/logical_lazy_ansi.sql diff --git a/native/spark-expr/Cargo.toml b/native/spark-expr/Cargo.toml index 563c4410bcd..a99a6891a59 100644 --- a/native/spark-expr/Cargo.toml +++ b/native/spark-expr/Cargo.toml @@ -514,3 +514,7 @@ harness = false [[bench]] name = "at_least_n_non_nulls" harness = false + +[[bench]] +name = "lazy_logical" +harness = false diff --git a/native/spark-expr/benches/lazy_logical.rs b/native/spark-expr/benches/lazy_logical.rs new file mode 100644 index 00000000000..81ada348c6b --- /dev/null +++ b/native/spark-expr/benches/lazy_logical.rs @@ -0,0 +1,217 @@ +// 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. + +//! Benchmarks for `AND`, `OR` and `IN` whose later operands can fail under ANSI mode. +//! +//! Each shape runs as `eager`, the way a plain `BinaryExpr` or DataFusion `InListExpr` evaluates +//! every operand over the whole batch, and as `lazy`, the way Comet plans it now. The data is +//! valid in every row, so both forms succeed and the difference is the cost of evaluating the +//! right operand only for the rows the left one has not decided, which `pending` sets for `AND`: +//! the percentage of rows where the left operand is true. + +use arrow::array::{ArrayRef, Int32Array, Int64Array, StringArray}; +use arrow::datatypes::{DataType, Field, Schema}; +use arrow::record_batch::RecordBatch; +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; +use datafusion::logical_expr::Operator; +use datafusion::physical_expr::expressions::{in_list, lit, BinaryExpr, Column}; +use datafusion::physical_expr::PhysicalExpr; +use datafusion_comet_spark_expr::{spark_in_list, Cast, EvalMode, LazyLogical, SparkCastOptions}; +use rand::rngs::StdRng; +use rand::{RngExt, SeedableRng}; +use std::hint::black_box; +use std::sync::Arc; + +const NUM_ROWS: usize = 8192; + +type Expr = Arc; + +/// A right operand: its name, and how to build it +type Right = (&'static str, fn() -> Expr); + +/// `g` an int in `0..100`, `n` a long in `0..1000`, and `s` an integer written as a string, with +/// `null_density` of each column NULL. +fn make_batch(null_density: f32) -> RecordBatch { + let mut rng = StdRng::seed_from_u64(42); + let valid = |rng: &mut StdRng| rng.random::() >= null_density; + let g: Int32Array = (0..NUM_ROWS) + .map(|_| valid(&mut rng).then(|| rng.random_range(0..100))) + .collect(); + let n: Int64Array = (0..NUM_ROWS) + .map(|_| valid(&mut rng).then(|| rng.random_range(0..1000))) + .collect(); + let s: StringArray = (0..NUM_ROWS) + .map(|_| valid(&mut rng).then(|| rng.random_range(0..1_000_000).to_string())) + .collect(); + let columns: Vec = vec![Arc::new(g), Arc::new(n), Arc::new(s)]; + RecordBatch::try_new(Arc::new(schema()), columns).unwrap() +} + +fn schema() -> Schema { + Schema::new(vec![ + Field::new("g", DataType::Int32, true), + Field::new("n", DataType::Int64, true), + Field::new("s", DataType::Utf8, true), + ]) +} + +fn column(name: &str, index: usize) -> Expr { + Arc::new(Column::new(name, index)) +} + +fn ansi_cast(child: Expr, to: DataType) -> Expr { + Arc::new(Cast::new( + child, + to, + SparkCastOptions::new_without_timezone(EvalMode::Ansi), + None, + None, + )) +} + +/// `CAST(n AS INT) > 1`, which fails if `n` overflows an int +fn narrowing_cast() -> Expr { + let cast = ansi_cast(column("n", 1), DataType::Int32); + Arc::new(BinaryExpr::new(cast, Operator::Gt, lit(1))) +} + +/// `CAST(s AS INT) > 1`, which fails if `s` is not an integer +fn parsing_cast() -> Expr { + let cast = ansi_cast(column("s", 2), DataType::Int32); + Arc::new(BinaryExpr::new(cast, Operator::Gt, lit(1))) +} + +fn g_cmp(op: Operator, value: i32) -> Expr { + Arc::new(BinaryExpr::new(column("g", 0), op, lit(value))) +} + +fn bench_logical(c: &mut Criterion) { + let schema = schema(); + let mut group = c.benchmark_group("lazy_logical"); + group.throughput(Throughput::Elements(NUM_ROWS as u64)); + + for (nulls, null_density) in [("no_nulls", 0.0), ("with_nulls", 0.1)] { + let batch = make_batch(null_density); + let rights: [Right; 2] = [ + ("narrowing_cast", narrowing_cast), + ("parsing_cast", parsing_cast), + ]; + for (right_name, right) in rights { + for pending in [0, 10, 50, 90, 100] { + // `g < pending` is true for about `pending` percent of the rows + let left = || g_cmp(Operator::Lt, pending); + let id = format!("and_{right_name}_{nulls}"); + let eager: Expr = Arc::new(BinaryExpr::new(left(), Operator::And, right())); + let lazy = LazyLogical::wrap(left(), Operator::And, right(), &schema).unwrap(); + for (form, expr) in [("eager", eager), ("lazy", lazy)] { + group.bench_with_input( + BenchmarkId::new(format!("{id}/{form}"), format!("pending_{pending}")), + &batch, + |b, batch| b.iter(|| black_box(expr.evaluate(black_box(batch)).unwrap())), + ); + } + } + } + } + + // OR is the mirror image: the right operand is pending where the left one is not true + let batch = make_batch(0.0); + let left = || g_cmp(Operator::GtEq, 50); + let eager: Expr = Arc::new(BinaryExpr::new(left(), Operator::Or, parsing_cast())); + let lazy = LazyLogical::wrap(left(), Operator::Or, parsing_cast(), &schema).unwrap(); + for (form, expr) in [("eager", eager), ("lazy", lazy)] { + group.bench_with_input( + BenchmarkId::new(format!("or_parsing_cast_no_nulls/{form}"), "pending_50"), + &batch, + |b, batch| b.iter(|| black_box(expr.evaluate(black_box(batch)).unwrap())), + ); + } + group.finish(); +} + +fn bench_in(c: &mut Criterion) { + let schema = schema(); + let batch = make_batch(0.0); + let mut group = c.benchmark_group("lazy_in"); + group.throughput(Throughput::Elements(NUM_ROWS as u64)); + + for candidates in [2, 8, 64] { + // `g IN (0, 1, ..., CAST(s AS INT))`: the literals match `candidates - 1` percent of rows + let list = |fallible: bool| -> Vec { + let mut list: Vec = (0..candidates - 1).map(lit).collect(); + list.push(if fallible { + ansi_cast(column("s", 2), DataType::Int32) + } else { + lit(-1) + }); + list + }; + let value = || column("g", 0); + // DataFusion evaluates every candidate over the whole batch + let eager = in_list(value(), list(true), &false, &schema).unwrap(); + // Comet evaluates the cast only for the rows no literal matched + let lazy = spark_in_list(value(), list(true), false, &schema).unwrap(); + // All candidates are constants, so nothing is lazy and DataFusion's static filter is used + let constants = spark_in_list(value(), list(false), false, &schema).unwrap(); + for (form, expr) in [ + ("eager", eager), + ("lazy", lazy), + ("all_constants", constants), + ] { + group.bench_with_input( + BenchmarkId::new(format!("in_{form}"), format!("candidates_{candidates}")), + &batch, + |b, batch| b.iter(|| black_box(expr.evaluate(black_box(batch)).unwrap())), + ); + } + } + + // A value that parses a string is dear to evaluate again for every candidate, so it keeps + // DataFusion's `IN`, which evaluates it once + for candidates in [8, 64] { + let list = || -> Vec { + (0..candidates) + .map(|_| ansi_cast(column("n", 1), DataType::Int32)) + .collect() + }; + let value = || -> Expr { + Arc::new(Cast::new( + column("s", 2), + DataType::Int32, + SparkCastOptions::new_without_timezone(EvalMode::Try), + None, + None, + )) + }; + let eager = in_list(value(), list(), &false, &schema).unwrap(); + let lazy = spark_in_list(value(), list(), false, &schema).unwrap(); + for (form, expr) in [("eager", eager), ("lazy", lazy)] { + group.bench_with_input( + BenchmarkId::new( + format!("in_parsing_value_{form}"), + format!("candidates_{candidates}"), + ), + &batch, + |b, batch| b.iter(|| black_box(expr.evaluate(black_box(batch)).unwrap())), + ); + } + } + group.finish(); +} + +criterion_group!(benches, bench_logical, bench_in); +criterion_main!(benches); diff --git a/native/spark-expr/src/array_funcs/nested_comparison.rs b/native/spark-expr/src/array_funcs/nested_comparison.rs index 62f5c1eca58..490d76e6296 100644 --- a/native/spark-expr/src/array_funcs/nested_comparison.rs +++ b/native/spark-expr/src/array_funcs/nested_comparison.rs @@ -24,6 +24,7 @@ use crate::float_semantics::{ normalize_floats, normalize_nested_floats, spark_comparator_ignoring_nulls, spark_equality_ignoring_nulls, NormalizeNestedFloats, }; +use crate::{lazy_in_list, LazyLogical}; use arrow::array::{Array, ArrayRef, AsArray, BooleanArray}; use arrow::buffer::{BooleanBuffer, Buffer, NullBuffer}; use arrow::compute::{not, or_kleene}; @@ -494,7 +495,8 @@ pub enum FloatOperands { /// nested values with `spark_comparator`. Nested `=` and `<>` compare with `spark_equality`, which /// also serves `IN`. A literal is normalized while planning. Operands of any other type, such as /// dictionary-encoded floats, go through [`normalize_comparison_operand`] and a plain -/// [`BinaryExpr`], and so does any other operator, such as `AND`. +/// [`BinaryExpr`], and so does any other operator. `AND` and `OR` are a [`LazyLogical`], which +/// evaluates a right operand that can fail only for the rows that the left one has not decided. /// /// The planner reconciles the nullability of nested operands before calling this. pub fn spark_comparison( @@ -509,7 +511,13 @@ pub fn spark_comparison( op, Eq | NotEq | Lt | LtEq | Gt | GtEq | IsDistinctFrom | IsNotDistinctFrom ) { - return Ok(Arc::new(BinaryExpr::new(left, op, right))); + // AND and OR evaluate the right operand only for the rows that the left one has not + // decided + return if matches!(op, And | Or) { + LazyLogical::wrap(left, op, right, schema) + } else { + Ok(Arc::new(BinaryExpr::new(left, op, right))) + }; } // An operand whose type does not resolve against this schema falls back to the plain // comparison, the way `reconcile_nested_comparison_types` already leaves such operands alone. @@ -652,6 +660,10 @@ pub fn spark_in_list( negated: bool, schema: &Schema, ) -> Result> { + // A candidate that can fail is evaluated only for the rows that no earlier one has matched + if let Some(lazy) = lazy_in_list(&value, &candidates, negated, schema)? { + return Ok(lazy); + } if !is_nested_with_float_leaf(&value.data_type(schema)?) || candidates.is_empty() { // DataFusion takes a candidate as a constant when it returns a scalar for an empty // batch, so an expression that reads a column must return an array there diff --git a/native/spark-expr/src/conditional_funcs/case_when.rs b/native/spark-expr/src/conditional_funcs/case_when.rs index 6eed2a951a2..1737a953de5 100644 --- a/native/spark-expr/src/conditional_funcs/case_when.rs +++ b/native/spark-expr/src/conditional_funcs/case_when.rs @@ -444,8 +444,8 @@ pub(super) fn is_infallible(expr: &Arc, input_schema: &Schema) /// Whether `expr` can be evaluated for rows that Spark would not evaluate it for, at any cost: what /// [`is_infallible`] accepts, and casts that [`Cast::cannot_fail`] accepts beyond it, such as a /// LEGACY or TRY cast from a string to an integer, which returns NULL for a string it cannot parse. -/// `NullShortCircuit` uses this, since it masks an argument only to keep it from failing on rows -/// that Spark skips. +/// `NullShortCircuit` and `LazyLogical` use this, since they mask an operand only to keep it from +/// failing on rows that Spark skips. pub(super) fn cannot_fail(expr: &Arc, input_schema: &Schema) -> bool { infallible(expr, input_schema, true) } diff --git a/native/spark-expr/src/conditional_funcs/lazy_logical.rs b/native/spark-expr/src/conditional_funcs/lazy_logical.rs new file mode 100644 index 00000000000..1f3e9d0cbcd --- /dev/null +++ b/native/spark-expr/src/conditional_funcs/lazy_logical.rs @@ -0,0 +1,581 @@ +// 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 super::case_when::{cannot_fail, is_infallible}; +use super::null_short_circuit::{evaluate_projected, project}; +use crate::{ + spark_comparison, spark_in_list, CaseWhenExpr, Cast, CheckedBinaryExpr, FloatOperands, IfExpr, + NormalizeNaNAndZero, SparkComparison, WideDecimalBinaryExpr, +}; +use arrow::array::{Array, BooleanArray}; +use arrow::compute::{and_kleene, or_kleene}; +use arrow::datatypes::{DataType, Schema}; +use arrow::record_batch::RecordBatch; +use datafusion::common::cast::as_boolean_array; +use datafusion::common::{internal_datafusion_err, Result}; +use datafusion::logical_expr::Volatility; +use datafusion::logical_expr::{ColumnarValue, Operator}; +use datafusion::physical_expr::expressions::{ + BinaryExpr, Column, IsNotNullExpr, IsNullExpr, Literal, NotExpr, +}; +use datafusion::physical_expr::{PhysicalExpr, ScalarFunctionExpr}; +use std::fmt::{Display, Formatter}; +use std::hash::{Hash, Hasher}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; + +/// Spark's `AND` and `OR`, which evaluate the right operand only for the rows that the left one +/// has not decided: the rows where it is not false for `AND`, and not true for `OR`. +/// +/// DataFusion's `BinaryExpr` evaluates both operands over the whole batch, so a right operand that +/// can fail, such as an ANSI division or cast, would fail on a row that Spark never evaluates it +/// for, such as `v != 0 AND 100 / v > 1` on a row where `v = 0`. +#[derive(Debug)] +pub struct LazyLogical { + left: Arc, + op: Operator, + right: Arc, + /// The input columns that `right` reads + projection: Vec, + /// `right` reading those columns from a batch of just them, so that skipping rows filters no + /// other column. It is evaluated in place of `right` for every batch, since rebuilding it gave + /// it its own copy of any state kept across batches + projected: Arc, + /// Whether to try `right` over the whole batch first when most rows need it, which costs less + /// than selecting rows. That is wrong for an expression whose result depends on the rows it + /// has seen, so it requires a deterministic `right`. If that fails on a row that Spark skips, + /// the rows are selected instead, here and for the batches after it. + speculate: AtomicBool, +} + +impl LazyLogical { + /// Creates `left op right`, where `op` is `AND` or `OR`, over Boolean operands. It evaluates `right` + /// lazily if `right` could fail on a row that `left` decides, and is a plain [`BinaryExpr`] + /// otherwise. + pub fn wrap( + left: Arc, + op: Operator, + right: Arc, + input_schema: &Schema, + ) -> Result> { + let booleans = left.data_type(input_schema).ok() == Some(DataType::Boolean) + && right.data_type(input_schema).ok() == Some(DataType::Boolean); + if !booleans || cannot_fail(&right, input_schema) { + return Ok(Arc::new(BinaryExpr::new(left, op, right))); + } + Ok(Arc::new(Self::try_new(left, op, right)?)) + } + + fn try_new( + left: Arc, + op: Operator, + right: Arc, + ) -> Result { + let (projection, projected) = project(&right)?; + let speculate = AtomicBool::new(is_deterministic(&right)); + Ok(Self { + left, + op, + right, + projection, + projected, + speculate, + }) + } +} + +/// Creates `value IN (candidates)` as the chain of equalities that Spark evaluates it as, each +/// candidate that can fail only for the rows that no earlier one has matched, or `None` if it has +/// no such form. A run of candidates that cannot fail is one `IN`, so that it keeps a hash set. +/// +/// Spark's `In` returns true at the first candidate that equals the value, and NULL if none does +/// and one is NULL, which is that of `value = c1 OR value = c2 OR ...`. A candidate that can fail +/// is then evaluated only for the rows that the ones before it have not matched. The value is +/// evaluated once per term, so this requires it to be a cheap expression that cannot fail. +/// This returns `None` when no candidate after the first can fail, when the value cannot be +/// evaluated again, and when a candidate has another type than the value. +pub fn lazy_in_list( + value: &Arc, + candidates: &[Arc], + negated: bool, + schema: &Schema, +) -> Result>> { + if candidates.iter().skip(1).all(|c| cannot_fail(c, schema)) || !is_infallible(value, schema) { + return Ok(None); + } + let value_type = value.data_type(schema)?; + if !candidates + .iter() + .all(|c| c.data_type(schema).is_ok_and(|t| t == value_type)) + { + return Ok(None); + } + // A run of candidates that cannot fail is matched at once, as the IN it would be alone. The + // chain evaluates a candidate that can fail only for the rows that the ones before it did not + // match. + let mut terms = vec![]; + let mut rest = candidates; + while !rest.is_empty() { + let run = rest.iter().take_while(|c| cannot_fail(c, schema)).count(); + terms.push(if run > 0 { + let (run, remaining) = rest.split_at(run); + rest = remaining; + spark_in_list(Arc::clone(value), run.to_vec(), false, schema)? + } else { + let candidate = &rest[0]; + rest = &rest[1..]; + spark_comparison( + Arc::clone(value), + Operator::Eq, + Arc::clone(candidate), + schema, + FloatOperands::Normalize, + )? + }); + } + let chain = balanced_or(&terms, schema)?; + Ok(Some(if negated { + Arc::new(NotExpr::new(chain)) + } else { + chain + })) +} + +/// `terms` joined by `OR` in a balanced tree, so that a long list is no deeper than its logarithm. +/// Each term is still evaluated only for the rows that the ones before it have not matched, since +/// `OR` is associative. +fn balanced_or(terms: &[Arc], schema: &Schema) -> Result> { + match terms { + [term] => Ok(Arc::clone(term)), + _ => { + let (left, right) = terms.split_at(terms.len() / 2); + LazyLogical::wrap( + balanced_or(left, schema)?, + Operator::Or, + balanced_or(right, schema)?, + schema, + ) + } + } +} + +/// Whether `expr` returns the same for a row whichever rows it has been evaluated for. That holds +/// for the expressions that Comet plans from deterministic ones, and is not assumed of any other. +fn is_deterministic(expr: &Arc) -> bool { + let node_is_deterministic = expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr.is::() + || expr + .downcast_ref::() + .is_some_and(|f| f.fun().signature().volatility == Volatility::Immutable); + node_is_deterministic && expr.children().into_iter().all(is_deterministic) +} + +impl Hash for LazyLogical { + fn hash(&self, state: &mut H) { + self.left.hash(state); + self.op.hash(state); + self.right.hash(state); + } +} + +impl PartialEq for LazyLogical { + fn eq(&self, other: &Self) -> bool { + self.left.eq(&other.left) && self.op == other.op && self.right.eq(&other.right) + } +} + +impl Eq for LazyLogical {} + +impl Display for LazyLogical { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{} {} {}", self.left, self.op, self.right) + } +} + +impl PhysicalExpr for LazyLogical { + fn data_type(&self, _input_schema: &Schema) -> Result { + Ok(DataType::Boolean) + } + + fn nullable(&self, input_schema: &Schema) -> Result { + Ok(self.left.nullable(input_schema)? || self.right.nullable(input_schema)?) + } + + fn evaluate(&self, batch: &RecordBatch) -> Result { + let num_rows = batch.num_rows(); + let left = self.left.evaluate(batch)?.into_array(num_rows)?; + let left = as_boolean_array(&left)?; + // The rows that the left operand has not decided: not false for AND, not true for OR + let pending = match (self.op, left.nulls()) { + (Operator::And, None) => left.values().clone(), + (Operator::And, Some(nulls)) => left.values() | &!nulls.inner(), + (_, None) => !left.values(), + (_, Some(nulls)) => !&(left.values() & nulls.inner()), + }; + let pending_rows = pending.count_set_bits(); + if pending_rows == 0 { + // No row needs the right operand, and the left one is the result on every row + return Ok(ColumnarValue::Array(Arc::new(left.clone()))); + } + let mut right = None; + if pending_rows * 2 > num_rows && self.speculate.load(Ordering::Relaxed) { + // Whatever the right operand returns for a row that the left one decides is ignored + // below, so it only matters if it fails there + right = evaluate_projected(&self.projection, &self.projected, batch, None).ok(); + if right.is_none() { + self.speculate.store(false, Ordering::Relaxed); + } + } + let right = match right { + Some(right) => right, + None => { + let pending = BooleanArray::new(pending, None); + evaluate_projected(&self.projection, &self.projected, batch, Some(&pending))? + } + } + .into_array(num_rows)?; + let right = as_boolean_array(&right)?; + // The right operand is NULL on the rows it was skipped for, which the left one decides + let result: BooleanArray = match self.op { + Operator::And => and_kleene(left, right)?, + _ => or_kleene(left, right)?, + }; + Ok(ColumnarValue::Array(Arc::new(result))) + } + + fn children(&self) -> Vec<&Arc> { + vec![&self.left, &self.right] + } + + fn with_new_children( + self: Arc, + children: Vec>, + ) -> Result> { + let [left, right] = <[_; 2]>::try_from(children).map_err(|children| { + internal_datafusion_err!("LazyLogical expects 2 children, got {}", children.len()) + })?; + Ok(Arc::new(Self::try_new(left, self.op, right)?)) + } + + fn fmt_sql(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "(")?; + self.left.fmt_sql(f)?; + write!(f, " {} ", self.op)?; + self.right.fmt_sql(f)?; + write!(f, ")") + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{Cast, EvalMode, SparkCastOptions}; + use arrow::array::{ArrayRef, Float64Array, Int32Array, StringArray}; + use arrow::datatypes::Field; + use datafusion::common::ScalarValue; + use datafusion::physical_expr::expressions::{col, lit, Literal}; + + fn batch(v: Vec>, s: Vec<&str>) -> RecordBatch { + let schema = Schema::new(vec![ + Field::new("v", DataType::Int32, true), + Field::new("s", DataType::Utf8, false), + ]); + RecordBatch::try_new( + Arc::new(schema), + vec![ + Arc::new(Int32Array::from(v)), + Arc::new(StringArray::from(s)), + ], + ) + .unwrap() + } + + /// An ANSI cast, which fails on a string that is not a number + fn ansi_cast(child: Arc, to: DataType) -> Arc { + Arc::new(Cast::new( + child, + to, + SparkCastOptions::new_without_timezone(EvalMode::Ansi), + None, + None, + )) + } + + /// `CAST(s AS INT) > 1` under ANSI + fn parsed_gt_one(schema: &Schema) -> Arc { + let cast = ansi_cast(col("s", schema).unwrap(), DataType::Int32); + Arc::new(BinaryExpr::new(cast, Operator::Gt, lit(1))) + } + + fn v_cmp(schema: &Schema, op: Operator, value: i32) -> Arc { + Arc::new(BinaryExpr::new(col("v", schema).unwrap(), op, lit(value))) + } + + /// `v value`, then `op`, then `CAST(s AS INT) > 1` + fn guarded( + schema: &Schema, + guard: Operator, + value: i32, + op: Operator, + ) -> Arc { + let lazy = LazyLogical::wrap( + v_cmp(schema, guard, value), + op, + parsed_gt_one(schema), + schema, + ); + lazy.unwrap() + } + + fn evaluate(expr: &Arc, batch: &RecordBatch) -> Result>> { + let array: ArrayRef = expr.evaluate(batch)?.into_array(batch.num_rows())?; + Ok(as_boolean_array(&array)?.iter().collect()) + } + + #[test] + fn skips_rows_the_left_operand_decides() -> Result<()> { + let batch = batch(vec![Some(0), Some(1), Some(0)], vec!["bad", "5", "worse"]); + let schema = batch.schema(); + let and = guarded(&schema, Operator::NotEq, 0, Operator::And); + assert!(and.is::()); + assert_eq!( + evaluate(&and, &batch)?, + vec![Some(false), Some(true), Some(false)] + ); + let or = guarded(&schema, Operator::Eq, 0, Operator::Or); + assert_eq!( + evaluate(&or, &batch)?, + vec![Some(true), Some(true), Some(true)] + ); + Ok(()) + } + + #[test] + fn null_left_operand_evaluates_the_right_one() -> Result<()> { + let failing = batch(vec![None], vec!["bad"]); + let batch = batch(vec![None, Some(0), None], vec!["5", "bad", "0"]); + let schema = batch.schema(); + let and = guarded(&schema, Operator::NotEq, 0, Operator::And); + // NULL AND true is NULL, false AND (not evaluated) is false, NULL AND false is false + assert_eq!( + evaluate(&and, &batch)?, + vec![None, Some(false), Some(false)] + ); + // A row with a NULL left operand is evaluated, so its right operand can fail + assert!(evaluate(&and, &failing).is_err()); + Ok(()) + } + + #[test] + fn evaluates_every_row_when_none_is_decided() -> Result<()> { + let batch = batch(vec![Some(1), Some(2)], vec!["5", "0"]); + let schema = batch.schema(); + let and = guarded(&schema, Operator::NotEq, 0, Operator::And); + assert_eq!(evaluate(&and, &batch)?, vec![Some(true), Some(false)]); + // An empty batch evaluates nothing + assert!(evaluate(&and, &batch.slice(0, 0))?.is_empty()); + Ok(()) + } + + #[test] + fn infallible_right_operand_stays_a_binary_expr() -> Result<()> { + let batch = batch(vec![Some(1)], vec!["5"]); + let schema = batch.schema(); + let expr = LazyLogical::wrap( + v_cmp(&schema, Operator::NotEq, 0), + Operator::And, + v_cmp(&schema, Operator::Lt, 5), + &schema, + )?; + assert!(expr.is::()); + Ok(()) + } + + #[test] + fn scalar_left_operand() -> Result<()> { + let batch = batch(vec![Some(1), None], vec!["bad", "worse"]); + let schema = batch.schema(); + let lazy = LazyLogical::wrap( + Arc::new(Literal::new(ScalarValue::Boolean(Some(false)))), + Operator::And, + parsed_gt_one(&schema), + &schema, + )?; + assert_eq!(evaluate(&lazy, &batch)?, vec![Some(false); 2]); + Ok(()) + } + + #[test] + fn right_operand_that_reads_no_column() -> Result<()> { + // CAST('bad' AS INT) > 1 fails for any row it is evaluated for + let batch = batch(vec![Some(0), Some(1)], vec!["a", "b"]); + let schema = batch.schema(); + let failing: Arc = Arc::new(BinaryExpr::new( + ansi_cast(lit("bad"), DataType::Int32), + Operator::Gt, + lit(1), + )); + let or = |value| { + LazyLogical::wrap( + v_cmp(&schema, Operator::Eq, value), + Operator::Or, + Arc::clone(&failing), + &schema, + ) + }; + // Row 0 is decided by `v = 0`, and row 1 is not + assert!(evaluate(&or(0)?, &batch).is_err()); + // Every row is decided + let decided = LazyLogical::wrap( + v_cmp(&schema, Operator::Lt, 5), + Operator::Or, + Arc::clone(&failing), + &schema, + )?; + assert_eq!(evaluate(&decided, &batch)?, vec![Some(true); 2]); + Ok(()) + } + + #[test] + fn in_list_evaluates_a_candidate_for_unmatched_rows() -> Result<()> { + // v IN (0, CAST(s AS INT)): the second candidate is skipped where v = 0 + let batch = batch( + vec![Some(0), Some(5), None, Some(7)], + vec!["bad", "5", "1", "8"], + ); + let schema = batch.schema(); + let candidates = vec![lit(0), ansi_cast(col("s", &schema)?, DataType::Int32)]; + let value = col("v", &schema)?; + let expr = crate::spark_in_list(Arc::clone(&value), candidates.clone(), false, &schema)?; + assert_eq!( + evaluate(&expr, &batch)?, + vec![Some(true), Some(true), None, Some(false)] + ); + let expr = crate::spark_in_list(value, candidates, true, &schema)?; + assert_eq!( + evaluate(&expr, &batch)?, + vec![Some(false), Some(false), None, Some(true)] + ); + Ok(()) + } + + #[test] + fn in_list_of_floats_matches_nan() -> Result<()> { + let d = Float64Array::from(vec![Some(f64::NAN), Some(1.5), Some(0.0), None]); + let s = StringArray::from(vec!["bad", "bad", "2.5", "1"]); + let schema = Schema::new(vec![ + Field::new("d", DataType::Float64, true), + Field::new("s", DataType::Utf8, false), + ]); + let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(d), Arc::new(s)])?; + let schema = batch.schema(); + let candidates: Vec> = vec![ + lit(f64::NAN), + lit(1.5f64), + ansi_cast(col("s", &schema)?, DataType::Float64), + ]; + // The rows with 'bad' match an earlier candidate, so the cast is never evaluated for them + let expr = crate::spark_in_list(col("d", &schema)?, candidates, false, &schema)?; + assert_eq!( + evaluate(&expr, &batch)?, + vec![Some(true), Some(true), Some(false), None] + ); + Ok(()) + } + + #[test] + fn speculative_evaluation_falls_back_to_selected_rows() -> Result<()> { + // Most rows are pending, so the right operand is tried over the whole batch first, and + // fails on the row that the left operand decides + let failing = batch(vec![Some(1); 4], vec!["5", "5", "bad", "5"]); + let mut values = vec![Some(1); 9]; + values.push(Some(0)); + let mut strings = vec!["5"; 9]; + strings.push("bad"); + let batch = batch(values, strings); + let schema = batch.schema(); + let and = guarded(&schema, Operator::NotEq, 0, Operator::And); + let mut expected = vec![Some(true); 9]; + expected.push(Some(false)); + assert_eq!(evaluate(&and, &batch)?, expected); + // Later batches select rows from the start + assert_eq!(evaluate(&and, &batch)?, expected); + // A row that is pending still fails + assert!(evaluate(&and, &failing).is_err()); + Ok(()) + } + + #[test] + fn in_list_matches_runs_of_candidates_at_once() -> Result<()> { + // v IN (0, 1, CAST(s AS INT), 5, 6) + let batch = batch( + vec![Some(0), Some(1), Some(2), Some(5), Some(9), None], + vec!["bad", "bad", "2", "8", "8", "1"], + ); + let schema = batch.schema(); + let candidates = vec![ + lit(0), + lit(1), + ansi_cast(col("s", &schema)?, DataType::Int32), + lit(5), + lit(6), + ]; + let expr = crate::spark_in_list(col("v", &schema)?, candidates, false, &schema)?; + assert_eq!( + evaluate(&expr, &batch)?, + vec![ + Some(true), + Some(true), + Some(true), + Some(true), + Some(false), + None + ] + ); + Ok(()) + } + + #[test] + fn long_in_list_does_not_deepen_the_stack() { + // 4096 candidates that can fail, on a stack too small for a call per candidate + let handle = std::thread::Builder::new() + .stack_size(256 * 1024) + .spawn(|| -> Result<()> { + let batch = batch(vec![Some(7), Some(9)], vec!["1", "2"]); + let schema = batch.schema(); + let candidates = (0..4096) + .map(|_| ansi_cast(col("s", &schema).unwrap(), DataType::Int32)) + .collect::>(); + let expr = crate::spark_in_list(col("v", &schema)?, candidates, false, &schema)?; + assert_eq!(evaluate(&expr, &batch)?, vec![Some(false); 2]); + Ok(()) + }) + .unwrap(); + handle.join().unwrap().unwrap(); + } +} diff --git a/native/spark-expr/src/conditional_funcs/mod.rs b/native/spark-expr/src/conditional_funcs/mod.rs index e4741cb8f0c..7a3093f6503 100644 --- a/native/spark-expr/src/conditional_funcs/mod.rs +++ b/native/spark-expr/src/conditional_funcs/mod.rs @@ -17,8 +17,10 @@ mod case_when; mod if_expr; +mod lazy_logical; mod null_short_circuit; pub use case_when::{coerce_to_common_type, create_case_when, create_if_expr, CaseWhenExpr}; pub use if_expr::IfExpr; +pub use lazy_logical::{lazy_in_list, LazyLogical}; pub use null_short_circuit::NullShortCircuit; diff --git a/native/spark-expr/src/conditional_funcs/null_short_circuit.rs b/native/spark-expr/src/conditional_funcs/null_short_circuit.rs index 232b9725cf6..1687f15b3f7 100644 --- a/native/spark-expr/src/conditional_funcs/null_short_circuit.rs +++ b/native/spark-expr/src/conditional_funcs/null_short_circuit.rs @@ -269,19 +269,28 @@ impl Arg { let Some((projection, projected)) = &self.masked else { return self.expr.evaluate(batch); }; - let batch = batch.project(projection)?; - match nulls { - Some(nulls) => { - let rows = BooleanArray::new(nulls.inner().clone(), None); - projected.evaluate_selection(&batch, &rows) - } - None => projected.evaluate(&batch), - } + let rows = nulls.map(|nulls| BooleanArray::new(nulls.inner().clone(), None)); + evaluate_projected(projection, projected, batch, rows.as_ref()) + } +} + +/// Evaluates `projected`, an expression reading the `projection` columns of `batch` from a batch +/// of just them, for only the rows selected by `rows` if given, returning a value for every row. +pub(super) fn evaluate_projected( + projection: &[usize], + projected: &Arc, + batch: &RecordBatch, + rows: Option<&BooleanArray>, +) -> Result { + let batch = batch.project(projection)?; + match rows { + Some(rows) => projected.evaluate_selection(&batch, rows), + None => projected.evaluate(&batch), } } /// The input columns that `expr` reads, and `expr` reading them from a batch of just those columns. -fn project(expr: &Arc) -> Result<(Vec, Arc)> { +pub(super) fn project(expr: &Arc) -> Result<(Vec, Arc)> { let mut projection = collect_columns(expr) .iter() .map(Column::index) diff --git a/spark/src/test/resources/sql-tests/expressions/conditional/logical_lazy_ansi.sql b/spark/src/test/resources/sql-tests/expressions/conditional/logical_lazy_ansi.sql new file mode 100644 index 00000000000..e2f50587a3c --- /dev/null +++ b/spark/src/test/resources/sql-tests/expressions/conditional/logical_lazy_ansi.sql @@ -0,0 +1,74 @@ +-- 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. + +-- AND, OR and IN evaluate their later operands only for the rows that the earlier ones have not +-- decided, so an operand that fails under ANSI mode does not fail on a row that Spark skips. +-- Every row is in one batch. + +-- Config: spark.sql.ansi.enabled=true + +statement +CREATE TABLE test_lazy_logical(v int, s string) USING parquet + +statement +INSERT INTO test_lazy_logical SELECT /*+ REPARTITION(1) */ * FROM VALUES + (0, 'bad'), (1, '5'), (2, '1'), (NULL, '3'), (4, '7') AS t(v, s) + +query +SELECT v, v != 0 AND 100 / v > 1 FROM test_lazy_logical WHERE v IS NOT NULL + +query +SELECT v, v = 0 OR 100 / v > 1 FROM test_lazy_logical WHERE v IS NOT NULL + +query +SELECT v FROM test_lazy_logical WHERE v != 0 AND 100 / v > 1 + +query +SELECT v FROM test_lazy_logical WHERE v = 0 OR 100 / v > 1 + +-- A chain, which is rebalanced +query +SELECT v, v != 0 AND v < 10 AND 100 / v > 1 AND CAST(s AS INT) > 0 +FROM test_lazy_logical WHERE v IS NOT NULL + +-- Nested under another operator +query +SELECT v, NOT (v = 0 OR 100 / v > 1), (v != 0 AND 200 / v > 1) IS NULL +FROM test_lazy_logical WHERE v IS NOT NULL + +query +SELECT v, v IN (0, 100 / v) FROM test_lazy_logical WHERE v IS NOT NULL + +query +SELECT v, v NOT IN (0, 100 / v) FROM test_lazy_logical WHERE v IS NOT NULL + +query +SELECT v, v IN (0, CAST(s AS INT)) FROM test_lazy_logical WHERE v IS NOT NULL + +-- A NULL candidate does not stop the later ones from being evaluated +query +SELECT v, v IN (0, CAST(s AS INT), NULL) FROM test_lazy_logical WHERE s != 'bad' + +-- A row that no earlier operand decides still fails +query expect_error(DIVIDE_BY_ZERO) +SELECT v != 1 AND 100 / v > 1 FROM test_lazy_logical WHERE v IS NOT NULL + +query expect_error(DIVIDE_BY_ZERO) +SELECT v = 1 OR 100 / v > 1 FROM test_lazy_logical WHERE v IS NOT NULL + +query expect_error(CAST_INVALID_INPUT) +SELECT v, v IN (1, CAST(s AS INT)) FROM test_lazy_logical WHERE v IS NOT NULL