apache / apache/datafusion

New Null Handling Behavior in Joins: Null Matches Everything

Open
#15,891 0 comments 0 reactions 0 assignees View on GitHub
enhancement
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?

Building a system that works with graph-like data on DataFusion will stumble upon the need to join the intermediate results of graph patterns. However, null handling is a bit different in these systems compared to SQL.

Usually you combine two intermediary results based on a notion of compatibility instead of strict equality. In these semantics, `NULL` is compatible with everything. Here is a small table that demonstrates this behavior on a single value:

| Lhs | Rhs | Matches? |
|--------|--------|--------|
| `NULL` | `NULL` | Yes |
| "A" | `NULL` | Yes |
| NULL | "A" | Yes |
| `"A"` | `"A"` | Yes |
| `"A"` | `"B"` | No |

Currently, we use UDFs to check for compatibility which can be implemented using a `NestedLoopJoinExec` as we do not have a "native" equal join condition. Having access to the HashJoin etc. implementation of DataFusion would be great, as we would not have to re-invent the join infrastructure.

Is this something that you'd be interested in having in DF?

### Describe the solution you'd like

I propose addressing this problem in three steps:
1. Replace `Join::null_equals_null` with an enum `JoinNullBehavior` (or similar).
2. Add an additional variant `JoinNullBehavior::NullMatchesEverything`.
3. Extending join implementations one-by-one by checking in the planner whether a join implementation is available for the given `JoinNullBehavior`.

### Describe alternatives you've considered

1. Sticking with `NestedLoopJoins`
2. Leaving this behavior downstream

### Additional context

Definition of Solution Compatibility in SPARQL 1.1:
- https://www.w3.org/TR/sparql11-query/#defn_algCompatibleMapping (`NULL` above represents unbound)

This could also be helpful for SQL/PGQ or GQL implementations based on DF.
Related Issues:
- https://github.com/apache/datafusion/issues/13545

Contributor guide

Open the contributing guide

Research direction

Start by reading Join::null_equals_null and the existing NestedLoopJoinExec, HashJoin, and planner entry points mentioned in the issue. Trace how join implementations select and apply null behavior, then determine what tests are needed to show that NullMatchesEverything works across the supported join implementations.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.