apache / apache/datafusion

Potential optimization for CASE WHEN for protecting against divide by zero

Open
#11,570 9 comments 0 reactions 1 assignee Claimed by @shinzoxD View on GitHub
enhancement good first issue optimizer performance
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### 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:

```sql
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`.

```rust
#[inline]
fn div_checked(self, rhs: Self) -> Result {
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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.