sunchao opened a new issue, #7785: URL: https://github.com/apache/datafusion-comet/issues/7785
### Describe the bug Spark evaluates the divisor first for `/`, `DIV` and `%`. A NULL divisor skips the dividend. A zero divisor also skips it when division by zero yields NULL. Comet evaluates a stateful dividend over the whole batch, advancing its state on the skipped rows. This changes the results on later rows with valid divisors. [Spark interpreted evaluation](https://github.com/apache/spark/blob/v4.1.3/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala#L665-L672) · [Spark generated evaluation](https://github.com/apache/spark/blob/v4.1.3/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala#L741-L778) · [Comet counter](https://github.com/apache/datafusion-comet/blob/f67d51d24aa9cac69a428df2a7b6655e4b042456/native/spark-expr/src/nondetermenistic_funcs/monotonically_increasing_id.rs#L68-L74) ### Steps to reproduce Compare these projections with Comet enabled and disabled. The single input partition makes the initial counter value zero. The first two rows have a NULL and a zero divisor. ```sql SET spark.sql.ansi.enabled=false; SET spark.comet.batchSize=2; CREATE TABLE dividends (id BIGINT, d BIGINT) USING parquet; INSERT INTO dividends SELECT id, CASE WHEN id = 0 THEN NULL WHEN id = 1 THEN 0 ELSE 3 END FROM range(0, 4, 1, 1); SELECT id, monotonically_increasing_id() / d FROM dividends; SELECT id, monotonically_increasing_id() DIV d FROM dividends; SELECT id, monotonically_increasing_id() % d FROM dividends; ``` ### Expected behavior In each query, Spark returns NULL for rows 0 and 1 and evaluates the counter for the first time on row 2. Rows 2 and 3 therefore use dividends 0 and 1. Evaluating the counter on every row gives dividends 2 and 3, which changes the quotient and remainder on row 2 and the integral quotient on row 3. These expectations follow from the implementations above and need verification in a regression test. ### Additional context This came up in [the review of #7730](https://github.com/apache/datafusion-comet/pull/7730#discussion_r4703070930), which fixes repeated evaluation of the divisor. #7456 already proposes evaluating the divisor first for #7246. Check the stateful dividend cases against that work, including `try_divide` and a NULL divisor with ANSI enabled. -- 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]
