apache / apache/arrow-rs

Evaluate Kernel under Selection / Short-Circuiting Filter Evaluation

Open
#3,620 15 comments 3 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
3.6k
Forks
1.3k
Avg merge
2d 18h
Merged PRs (30d)
169

Description

**Is your feature request related to a problem or challenge? Please describe what you are trying to do.**

Currently for common expressions such as `AND` or `OR`, we don't apply any short-circuiting and therefore the same columnar batch needs to be fully evaluated on every predicate.

For instance, consider the following example:
```sql
a = 'FOO' AND b = 42
```

We would evaluate each batch on both predicates, and apply bit-and on the result `BooleanArray` from both side. Similarly for `OR`.

This would not be efficient in many cases, nor correct (see https://github.com/apache/arrow-datafusion/issues/5093 for a bug report). A more efficient approach, is perhaps to only apply the second predicate on the **remaining rows** from the evaluation of the first predicate. This could be especially effective if the first predicate has low selectivity.

Note, sometimes it would still be beneficial to evaluate the full batch to take advantage of SIMD. For a detailed analysis, please check https://dl.acm.org/doi/abs/10.1145/3465998.3466009.

This approach has been adopted by other popular engines such as Velox, Databricks Photon, etc.

**Describe the solution you'd like**

Implement the short-circuiting logic in both `arrow-rs` and `arrow-datafusion`. This could introduce a lot of API changes since we may need to introduce an extra parameter `SelectivityVector` for related compute kernels (e.g., `is_null`). We also need to change `arrow-datafusion`'s [`PhysicalExpr::evaluate`](https://github.com/apache/arrow-datafusion/blob/master/datafusion/physical-expr/src/physical_expr.rs#L47) to take the `SelectivityVector` into account. Note similar features has been done for `CASE WHEN` with the introduction of a separate `evaluate_selection` method. See https://github.com/apache/arrow-datafusion/pull/2068 for more details.

**Describe alternatives you've considered**

N/A

**Additional context**

N/A

Contributor guide

Open the contributing guide

Research direction

Start with the arrow-rs compute kernels and arrow-datafusion's PhysicalExpr::evaluate entry point, then compare the CASE WHEN evaluate_selection work described in PR 2068. Trace how a SelectivityVector would affect predicates and related kernels such as is_null; done means short-circuiting works in both projects with the required API and tests updated.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
data-engineering
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.