comphead commented on code in PR #7741:
URL: https://github.com/apache/datafusion-comet/pull/7741#discussion_r4669875626
##########
native/spark-expr/src/conditional_funcs/lazy_logical.rs:
##########
@@ -104,17 +104,18 @@ impl LazyLogical {
///
/// 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.
+/// is then evaluated only for the rows that the ones before it have not
matched, and no candidate
+/// is evaluated for a row whose value is NULL, which Spark returns NULL for
at once. The value is
+/// evaluated once per term, so this requires it to be deterministic.
+/// This returns `None` when no candidate after the first can fail, when the
value is not
+/// deterministic, and when a candidate has another type than the value.
pub fn lazy_in_list(
value: &Arc<dyn PhysicalExpr>,
candidates: &[Arc<dyn PhysicalExpr>],
negated: bool,
schema: &Schema,
) -> Result<Option<Arc<dyn PhysicalExpr>>> {
- if candidates.iter().skip(1).all(|c| cannot_fail(c, schema)) ||
!is_infallible(value, schema) {
+ if candidates.iter().skip(1).all(|c| cannot_fail(c, schema)) ||
!is_deterministic(value) {
Review Comment:
`is_deterministic` does not list `GetStructField`, which `infallible` in
`case_when.rs` has accepted since
https://github.com/apache/datafusion-comet/pull/7538. So a struct field value,
which takes the lazy chain on `main`, now returns `None` here and goes back to
the eager `InListExpr`. That is the case `case_when_struct_field.sql:109` pins:
`s.n.x IN (9223372036854775807, s.n.x div s.n.y)` with ANSI on, where row 1
matches the first candidate and has `y = 0`. Spark returns true there, and the
eager path raises `DIVIDE_BY_ZERO`.
Adding `|| expr.is::<GetStructField>()` to `is_deterministic` should restore
it. A struct field is deterministic, so that is also safe for the speculation
check in `LazyLogical::try_new`, which is the other caller.
##########
native/spark-expr/src/conditional_funcs/lazy_logical.rs:
##########
@@ -104,17 +104,18 @@ impl LazyLogical {
///
/// 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.
+/// is then evaluated only for the rows that the ones before it have not
matched, and no candidate
+/// is evaluated for a row whose value is NULL, which Spark returns NULL for
at once. The value is
+/// evaluated once per term, so this requires it to be deterministic.
+/// This returns `None` when no candidate after the first can fail, when the
value is not
Review Comment:
Spark evaluates no candidate at all for a NULL value, but `skip(1)` on line
118 still treats the first candidate as evaluated for every row. So `v IN
(CAST(s AS INT), 1)` and `v IN (CAST(s AS INT))` return `None`, and
DataFusion's `InListExpr` casts `s` on the `(NULL, 'bad')` row, where Spark
returns NULL. That is part of
https://github.com/apache/datafusion-comet/issues/7675 too. Skipping the first
candidate only for a value that cannot be NULL would cover both shapes and
match what this comment says:
```rust
let nullable = value.nullable(schema)?;
// Spark evaluates no candidate for a row whose value is NULL
let skipped = candidates.iter().skip(usize::from(!nullable));
if skipped.all(|c| cannot_fail(c, schema)) || !is_deterministic(value) {
```
with `nullable` reused for the guard below. `SELECT v, v IN (CAST(s AS INT),
1) FROM test_lazy_in WHERE v IS NULL OR s != 'bad'` would pin it. The filter
keeps out `(0, 'bad')`, whose cast Spark does evaluate.
##########
native/spark-expr/src/conditional_funcs/lazy_logical.rs:
##########
@@ -147,7 +148,11 @@ pub fn lazy_in_list(
)?
});
}
- let chain = balanced_or(&terms, schema)?;
+ let mut chain = balanced_or(&terms, schema)?;
+ if value.nullable(schema)? {
+ let value_is_set: Arc<dyn PhysicalExpr> =
Arc::new(IsNotNullExpr::new(Arc::clone(value)));
+ chain = Arc::new(CaseWhenExpr::try_new(vec![(value_is_set, chain)],
None)?);
+ }
Review Comment:
This evaluates the value once more, on top of once per term. Before,
`is_infallible` also kept the value cheap, as the old comment here said, while
`is_deterministic` admits a cast that parses a string and any immutable
function. So `CAST(s AS INT) IN (CAST(a AS INT), CAST(b AS INT))` now parses
`s` three times per batch, where `main` parses it once on the eager path.
https://github.com/apache/datafusion-comet/issues/7675 suggested evaluating
the value once and building the chain over that column, the way
`NullShortCircuit` evaluates its arguments into a batch for `body`. That would
also make the determinism check unnecessary, and the `GetStructField` gap with
it. Could you do that here, or limit the value to cheap expressions such as
checked arithmetic over columns and give the measured cost in the description?
##########
spark/src/test/resources/sql-tests/expressions/conditional/logical_lazy_ansi.sql:
##########
@@ -72,3 +72,21 @@ 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
+
+-- IN is NULL for a NULL value without evaluating any candidate, and a value
that can fail, such
+-- as an ANSI addition, is still evaluated lazily
+statement
+CREATE TABLE test_lazy_in(v int, s string) USING parquet
+
+statement
+INSERT INTO test_lazy_in SELECT /*+ REPARTITION(1) */ * FROM VALUES
+ (0, 'bad'), (NULL, 'bad'), (2, '2') AS t(v, s)
Review Comment:
`test_lazy_in` has the same columns as `test_lazy_logical`. Adding `(NULL,
'bad')` to the existing table changes none of its queries, since each one
filters out a NULL `v` or a `'bad'` `s`, or never reads `s`. Then dropping
`WHERE v IS NOT NULL` from `SELECT v, v IN (0, CAST(s AS INT))` at line 60
covers the NULL value with the query that is already there, which
https://github.com/apache/datafusion-comet/issues/7675 pointed out. The `NOT
IN` and `(v + 1)` queries can read the same table.
##########
native/spark-expr/src/conditional_funcs/lazy_logical.rs:
##########
@@ -485,6 +490,39 @@ mod tests {
Ok(())
}
+ #[test]
+ fn in_list_skips_candidates_for_a_null_value() -> Result<()> {
Review Comment:
This is `in_list_evaluates_a_candidate_for_unmatched_rows` with the NULL
row's string changed from `"1"` to `"bad"`. Changing that one string there
covers the NULL value without a second test, and its expected results stay the
same. `in_list_of_a_checked_arithmetic_value` below is the `(v + 1)` query in
`logical_lazy_ansi.sql`, which also checks the answer against Spark, so I would
drop it too.
--
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]