Skip to content

Potential optimization for CASE WHEN for protecting against divide by zero #11570

@andygrove

Description

@andygrove

Is your feature request related to a problem or challenge?

A common usage of CASE WHEN (particularly in the TPC-DS benchmark) is to protect against divide by zero errors. For example:

CASE WHEN y > 0 THEN x / y ELSE null END

The CaseExpr implementation is quite expensive and we could replace this whole expression with a divide kernel that returns null if the right hand side is zero (Rust has a div_checked function that already provides this functionality).

arrow-rs already has the following code in arrow-array/src/arithmetic.rs, which is really close to what we need. I think we just need a version that returns an Option::None instead of an Err.

#[inline]
fn div_checked(self, rhs: Self) -> Result<Self, ArrowError> {
    if rhs.is_zero() {
        Err(ArrowError::DivideByZero)
    } else {
        self.checked_div(rhs).ok_or_else(|| {
            ArrowError::ComputeError(format!(
                "Overflow happened on: {:?} / {:?}",
                self, rhs
            ))
        })
    }
}

Describe the solution you'd like

No response

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions