haohuaijin opened a new issue, #8927:
URL: https://github.com/apache/arrow-datafusion/issues/8927

   ### Describe the bug
   
   In our current implementation, the sub-expression within the short-circuited 
expression is always evaluated even when they don't need to be evaluated. We 
first evaluate all expressions recursively and then perform the corresponding 
operation or function.
   
   The related code is below
   the behavior of `COALESCE` function is wrong
   
https://github.com/apache/arrow-datafusion/blob/b7e13a0af711477ad41450566c14430089edd3f2/datafusion/physical-expr/src/scalar_function.rs#L152-L156
   the behavior of `And` and `Or` operation is wrong
   
https://github.com/apache/arrow-datafusion/blob/b7e13a0af711477ad41450566c14430089edd3f2/datafusion/physical-expr/src/expressions/binary.rs#L262-L263
   
   the behavior of `CASE ... WHEN ...` is correct
   
https://github.com/apache/arrow-datafusion/blob/b7e13a0af711477ad41450566c14430089edd3f2/datafusion/physical-expr/src/expressions/case.rs#L136-L140
   
   
   
   ### To Reproduce
   
   ```
   DataFusion CLI v34.0.0
   ❯ create table t(x int, y int) as values (1,1), (2,2), (3,3), (0,0), (4,0);
   0 rows in set. Query took 0.004 seconds.
   ❯ select y > 0 and 1/y < 1 from t;
   Arrow error: Divide by zero error
   ❯ select y = 0 or 1/y < 1 from t;
   Arrow error: Divide by zero error
   ❯ select COALESCE(1, 1/y) from t;
   Arrow error: Divide by zero error
   ❯ select case 1 when 2 then 1/y end from t;
   +--------------------------------------------------------------------+
   | CASE Int64(1) WHEN Int64(2) THEN Int64(1) / CAST(t.y AS Int64) END |
   +--------------------------------------------------------------------+
   |                                                                    |
   |                                                                    |
   |                                                                    |
   |                                                                    |
   |                                                                    |
   +--------------------------------------------------------------------+
   5 rows in set. Query took 0.003 seconds.
   ```
   
   ### Expected behavior
   
   The short-circuited expressions should have the correct results.
   I also check in postgres
   ```
   postgres=# create table t(x int, y int); insert into t values (1,1), (2,2), 
(3,3), (0,0), (4,0);
   CREATE TABLE
   INSERT 0 5
   postgres=# select y > 0 and 1/y < 1 from t;
    ?column? 
   ----------
    f
    t
    t
    f
    f
   (5 rows)
   
   postgres=# select y = 0 or 1/y < 1 from t;
    ?column? 
   ----------
    f
    t
    t
    t
    t
   (5 rows)
   
   postgres=# select COALESCE(1, 1/y) from t;
    coalesce 
   ----------
           1
           1
           1
           1
           1
   (5 rows)
   ```
   
   ### Additional context
   
   _No response_


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

Reply via email to