tlfua commented on code in PR #7456:
URL: https://github.com/apache/datafusion-comet/pull/7456#discussion_r4713067158
##########
native/spark-expr/src/conditional_funcs/case_when.rs:
##########
@@ -467,6 +476,26 @@ fn infallible(expr: &Arc<dyn PhysicalExpr>, input_schema:
&Schema, parsing: bool
} else {
cast.is_infallible(input_schema)
}
+ } else if let Some(check) = expr.downcast_ref::<CheckOverflow>() {
+ // Without `fail_on_error` a decimal that overflows the precision
becomes NULL
+ parsing
+ && !check.fail_on_error
+ && matches!(check.data_type, DataType::Decimal128(_, _))
+ && matches!(
+ check.child.data_type(input_schema),
+ Ok(DataType::Decimal128(_, _))
+ )
+ } else if let Some(function) = expr.downcast_ref::<ScalarFunctionExpr>() {
+ // Named as the planner names the functions it builds from Spark's, in
+ // `create_scalar_function_expr`
+ parsing && STRING_FUNCTIONS_THAT_CANNOT_FAIL.contains(&function.name())
Review Comment:
@rich7420 Done in aa9cb705b. `cannot_fail` is back to exactly what it is on
`main`, and `LazyLogical::wrap` and `lazy_in_list` still use it, so `x AND
substring(s, 2, 4) = '2345'` is evaluated lazily again. The additions moved to
a new `cannot_fail_at_any_cost`, and `Cast::cannot_fail_at_any_cost` for the
casts, which only `NullShortCircuit` uses. Each consumer now picks its check by
cost, as CASE WHEN already does with `is_infallible`
(https://github.com/apache/datafusion-comet/pull/6716 ).
I added your shape to `benches/lazy_logical.rs`. µs per 8192-row batch with
10% of `x` NULL, best of two rounds on one GitHub runner
([run](https://github.com/tlfua/datafusion-comet/actions/runs/45868304846)):
| pending | `main` (f67d51d24) | previous head (d54d9a469) | this head
(aa9cb705b) |
|---|---|---|---|
| 0% | 29.2 | 172.9 | 29.5 |
| 10% | 49.4 | 172.7 | 50.0 |
| 50% | 170.2 | 173.6 | 172.7 |
The other `lazy_logical` and `lazy_in` shapes are within noise of `main` or
faster. `costlier_right_operand_that_cannot_fail_is_evaluated_lazily` in
`lazy_logical.rs` checks that `AND` with a `substring`, an int to
`decimal(12,2)` cast or an int to string cast on the right is still planned as
`LazyLogical`, and that `lazy_in_list` still masks a `substring` candidate. It
fails on the previous head.
@andygrove @coderfender this keeps what you asked for: `NullShortCircuit`
still doesn't wrap those casts, the non-failing `CheckOverflow`, the narrow
decimal arithmetic or the six string functions, and no benchmark shape changes
whether it is wrapped.
##########
native/spark-expr/benches/null_short_circuit.rs:
##########
@@ -0,0 +1,273 @@
+// 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 arithmetic and `concat` whose second operand can fail.
+//!
+//! Each shape runs as `eager`, evaluating both operands over the whole batch,
and as `lazy`, the
+//! way Comet plans it now, evaluating the second operand only for the rows
where the first one is
+//! not NULL unless it cannot fail. The data is valid in every row, so both
forms succeed, and the
+//! difference is the cost of skipping the rows Spark skips.
+
+use arrow::array::{ArrayRef, Decimal128Array, Int64Array, StringArray};
+use arrow::datatypes::{DataType, Field, Schema};
+use arrow::record_batch::RecordBatch;
+use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion,
Throughput};
+use datafusion::common::ScalarValue;
+use datafusion::config::ConfigOptions;
+use datafusion::logical_expr::{Operator, ScalarUDF};
+use datafusion::physical_expr::expressions::{BinaryExpr, Column, Literal};
+use datafusion::physical_expr::{PhysicalExpr, ScalarFunctionExpr};
+use datafusion::prelude::SessionContext;
+use datafusion_comet_spark_expr::{
+ create_comet_physical_fun_with_eval_mode, Cast, CheckOverflow, EvalMode,
NullShortCircuit,
+ SparkCastOptions, WideDecimalBinaryExpr, WideDecimalOp,
+};
+use datafusion_spark::function::string::concat::SparkConcat;
+use rand::rngs::StdRng;
+use rand::{RngExt, SeedableRng};
+use std::hint::black_box;
+use std::sync::Arc;
+
+const NUM_ROWS: usize = 8192;
+
+const DECIMAL: DataType = DataType::Decimal128(12, 2);
+
+type Expr = Arc<dyn PhysicalExpr>;
+
+/// An expression: its name, how to build it, and whether Comet wraps it in
`NullShortCircuit`
+type Shape = (&'static str, fn() -> Expr, bool);
+
+/// `a` a long with `null_density` of it NULL, `p` the same as a decimal and
`q` as a string, and
+/// `b`, `c`, `s`, a long written as a string, and `d`, a decimal, never NULL.
+fn make_batch(null_density: f32) -> RecordBatch {
+ let mut rng = StdRng::seed_from_u64(42);
+ let a: Int64Array = (0..NUM_ROWS)
+ .map(|_| (rng.random::<f32>() >= null_density).then(||
rng.random_range(0..1000)))
Review Comment:
@rich7420 Done in aa9cb705b. `make_batch` now makes exactly
`round(null_density * 8192)` rows NULL, choosing them by shuffling a mask with
the same seed, so `nulls_50` has 4096 NULLs and the operand is needed by
exactly half the rows, where `NullShortCircuit` starts to speculate. At that
row the wrapped `a + CAST(s AS BIGINT)` now costs the same as eager (92.1
against 93.6 µs), rather than the 80.9 µs the 4108-NULL batch showed. The full
table is in the PR description
([run](https://github.com/tlfua/datafusion-comet/actions/runs/45868304846)).
--
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]