snmvaughan opened a new pull request, #7833:
URL: https://github.com/apache/datafusion-comet/pull/7833

   ## Which issue does this PR close?
   
   Backport of #7730 to `branch-1.2`. Closes #7671 on this branch.
   
   ## Rationale for this change
   
   Comet guards a non-ANSI divisor with `IF(x = 0, NULL, x)` and a logarithm 
argument with `IF(x <= 0, NULL, x)`. The condition evaluates `x` over the 
batch, and the value branch evaluates it again on the rows that passed. For a 
stateful expression such as `monotonically_increasing_id()`, the two 
evaluations can assign different values to the same row, so a divisor that 
passed the check can be zero when the arithmetic kernel uses it. Non-ANSI `/`, 
`div` and `%`, and `ln`, `log10` and `log2`, then return wrong results (#7671, 
`priority:critical`). `branch-1.2` has the same guards (`nullIfWhenPrimitive` 
and `nullIfNegative`), so it evaluates these operands twice. See #7730 for the 
full analysis.
   
   ## What changes are included in this PR?
   
   Cherry-pick of c7a048060 (`git cherry-pick -x`) with two adaptations, both 
in imports and comments. Otherwise the diff against the source commit is 
identical apart from line numbers and blob hashes: 13 of its 15 files have 
exactly the same changed lines.
   
   It replaces the guards with a unary native function that evaluates the 
operand once, masks zeros (non-ANSI `/`, `div` and `%`) or non-positive values 
(`ln`, `log10` and `log2`), and hands the checked values to the math kernel. It 
comes with the source PR's regression tests: a SQL file test over the six 
operators with a stateful divisor, and a `CometCodegenSuite` test that counts 
the calls of a nondeterministic UDF.
   
   Adaptations:
   
   - `native/core/src/execution/planner.rs`: the `use 
datafusion_comet_spark_expr::{...}` list gains `create_null_if_zero_expr`. On 
`main` the list also has `MakeDateExpr`, which `branch-1.2` does not have, so 
the list wraps differently.
   - `native/spark-expr/src/conditional_funcs/case_when.rs`: the source 
commit's import line and the doc comment of `infallible` also mention 
`GetStructField` and "struct fields", which `branch-1.2` does not have. The 
backport keeps this branch's lines and applies only what the source commit 
changes there: the `NumericGuard` import, and "every function" becoming 
"general functions" in the doc comment.
   
   No serde's compatibility notes, reasons or support level change, so the 
frozen pages under `docs/source/user-guide/latest/` need no update.
   
   Other release branches: `branch-1.2` is the newest release branch, so there 
is no newer one to port to. Whether an older branch needs the fix is not part 
of this PR. The follow-up #7807 (more Rust tests for the guards) and the 
separate dividend-side bug #7785 are not part of it either.
   
   ## How are these changes tested?
   
   On this branch (`branch-1.2` at 8afafe4f4), with JDK 17:
   
   - `cargo fmt --all -- --check` is clean, and `cargo check --all-targets` 
passes for `datafusion-comet` and `datafusion-comet-spark-expr`.
   - `cargo test` passes for the `math_funcs` and `conditional_funcs` unit 
tests of `datafusion-comet-spark-expr` (240 tests, including the source 
commit's new assertion that guarded division over columns is safe to evaluate 
eagerly) and for the planner tests of `datafusion-comet` (118 tests).
   - `./mvnw test-compile`, which runs spotless and scalastyle, succeeds for 
the default profile (Spark 4.1) and for `-Pspark-3.4`, `-Pspark-3.5`, 
`-Pspark-4.0` and `-Pspark-4.2`.
   - The new SQL file test `nondeterministic_operand.sql` and the new 
`CometCodegenSuite` test (`math domain checks evaluate a nondeterministic 
operand once per row (#7671)`) pass on the default profile (Spark 4.1.3) with 
the native library built from this branch.
   - With `branch-1.2`'s own code (8afafe4f4, with the native library built 
from it) and only the source PR's two test files added, both tests fail. The 
SQL file test fails on its first query (`id / monotonically_increasing_id()`, 
`id DIV ...`, `id % ...`) with `Results do not match`, and the 
`CometCodegenSuite` test fails with `27 did not equal 18`: the UDF runs 27 
times for 3 rows instead of 18. So the branch has the bug and the tests catch 
it.
   
   


-- 
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]

Reply via email to