Skip to content

COALESCE expr in datafusion should perform lazy evaluation of the operands #17322

Closed

Description

@coderfender

Describe the bug

We observed that datafusion-spark's coalesce should perform lazy evaluation of the operands from left to right similar to spark . Current implementation throws an exception in case any operand throws an error which is not in alignment with spark which does lazy evaluation to return first non NULL values

To Reproduce

select coalesce(100, 1/0) from tbl

Expected behavior

result : 100 but throws an exception

Additional context

No response

Activity

  1. coderfender commented on Aug 25, 2025

    @coderfender
    ContributorAuthor
  2. andygrove commented on Aug 25, 2025

    @andygrove
    Member

    For reference, this is the behavior in Postgres:

    postgres=# select coalesce(1, 1/0);
     coalesce 
    ----------
            1
    (1 row)
    
    postgres=# select coalesce(1/0, 1);
    ERROR:  division by zero
    

    This differs from DataFusion's behavior:

    DataFusion CLI v48.0.0
    > select coalesce(1, 1/0);
    
    Arrow error: Divide by zero error
    
  3. andygrove commented on Aug 25, 2025

    @andygrove
    Member

    @coderfender This issue is for DataFusion's coalesce expression and is not specific to datafusion-spark as far as I know.

  4. changed the title [-]COALESCE expr in datafusion spark crate should perform lazy evaluation of the operands[/-] [+]COALESCE expr in datafusion should perform lazy evaluation of the operands[/+] on Aug 25, 2025
  5. coderfender commented on Aug 25, 2025

    @coderfender
    ContributorAuthor

    Ah that explains it (fixed the title) . Perhaps we are adhering to postgreSQL 's coalesce implementation ?

  6. coderfender commented on Aug 26, 2025

    @coderfender
    ContributorAuthor

    I can work on this once the comet's side impl is fixed .

  7. alamb commented on Nov 9, 2025

    @alamb
    Contributor

    We merged a fix for this in the 51 release, but we have found as subtle problem so I am planning to revert it for 51 (the fix should appear in 52):

  8. coderfender commented on Feb 8, 2026

    @coderfender
    ContributorAuthor

    Reviving this thread to update comet side of things once we bump DF version to 52 @comphead , @andygrove

  9. alamb commented on Feb 9, 2026

    @alamb
    Contributor

    This query works well now in datafusion 52

    DataFusion CLI v52.1.0
    > select coalesce(100, 1/0)  from values (1), (2);
    +------------------------------------------+
    | coalesce(Int64(100),Int64(1) / Int64(0)) |
    +------------------------------------------+
    | 100                                      |
    | 100                                      |
    +------------------------------------------+
    2 row(s) fetched.
    Elapsed 0.038 seconds.
  10. coderfender commented on Mar 5, 2026

    @coderfender
    ContributorAuthor

    Thank you for the update @alamb . I will revert comet's fix to use DF's coalesce function then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions