Repository navigation
Commit 334f293
authored
perf: Add an adaptive integer membership prefilter for hash joins (apache#25363)
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->
- Closes #.
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
Please explain the problem you are trying to solve in terms of the
user-visible
behavior, rather than the implementation.
For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of
the
implementation. "COUNT(DISTINCT) returns wrong results when the column
contains
nulls" is the user-visible problem.
-->
Hash joins can spend substantial time looking up probe keys that do not
exist on the build side. For a single integer key with a bounded range,
an exact membership bitmap can reject these keys cheaply before hash
table lookup. This helps when many nonmatching keys reach the join,
including when dynamic filters cannot be pushed down to the input.
For example, in the measured TPC-H SF1 plans, the `RightSemi` join on
`part.p_partkey = lineitem.l_partkey` receives approximately 6 million
probe rows. With memtable inputs or the default Parquet configuration:
- **Q8:** Only **0.73%** of probe rows match the filtered build side,
resulting in approximately 5.96 million unsuccessful hash table lookups.
- **Q9:** Only **5.32%** of probe rows match the filtered build side,
resulting in approximately 5.68 million unsuccessful hash table lookups.
These low probe hit rates leave substantial room to avoid unsuccessful
lookups, reflected in both the query-latency and operator-level
measurements below.
The extra membership check is less useful when most keys match. An
adaptive policy pauses filtering when the observed pruning ratio is low
and periodically samples again as the input changes.
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here, but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
- Build an optional integer membership bitmap for eligible single-key
HashMap joins in both CollectLeft and Partitioned modes. ArrayMap and
null-aware joins retain their existing paths.
- Generate a lookup mask for each probe batch, skipping hash table
lookups for absent keys. Preserve the original probe rows and NULL
handling so outer, anti, and mark join semantics remain unchanged. Hash
computation is unchanged.
- Track pruning independently in each probe stream. Unproductive
sampling windows trigger progressively longer pauses; productive samples
reset the pause interval.
- Account for bitmap and mask memory through the memory pool, falling
back to normal lookup when an optional reservation cannot be satisfied.
- Add configuration, documentation, creation/pruning metrics, and the
`hash_join_probe` microbenchmark.
## What is the testing strategy for this PR?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
Briefly describe how this PR is tested, and point to the specific tests
you added. For example: 'This new feature is covered by the
`sqllogictest` cases added in `foo.slt`'.
If this PR does not add tests, explain why. For example, if the change
is already covered by existing tests, please mention it.
You should also check the `codecov` bot reply on this PR to confirm the
changed code is exercised.
-->
- Unit tests cover membership and integer boundaries, range overflow,
bitmap boundaries, adaptive sampling/backoff, and sliced validity masks.
- HashJoin tests cover fallback paths, memory reservation failures, and
independent probe-stream adaptation with a shared build bitmap.
- `hash_join_integer_prefilter.slt` covers duplicate matches, unmatched
rows, NULL equality, inner/full/semi/anti joins, null-aware IN/NOT IN
behavior, range limits, and creation/pruning metrics.
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
The feature is opt-in and disabled by default. It can be enabled with:
```sql
SET datafusion.execution.enable_join_integer_prefilter = true;
```
Additional execution options control the maximum key range (default
`262144`), minimum pruning ratio (`0.5`), and sampling window (`12288`
input rows). The metrics `integer_prefilter_created_count` and
`probe_prefilter_rows_pruned` expose whether the prefilter was created
and how many probe rows skipped lookup.
Query results are unchanged.
## Benchmarks
### Microbench: `hash_join_probe`
| Workload | Main | Off | On | On vs main |
|---|---:|---:|---:|---:|
| RightSemi, 10% interleaved hits | 5.335 | 5.293 | 3.985 | -25.3% |
| RightAnti, 10% interleaved hits | 10.849 | 10.933 | 9.479 | -12.6% |
| Inner, 10% interleaved hits | 6.114 | 6.164 | 4.802 | -21.4% |
| RightSemi, 10% clustered hits | 4.286 | 4.338 | 3.549 | -17.2% |
| RightAnti, 10% clustered hits | 10.172 | 11.633 | 8.969 | -11.8% |
| RightSemi, all hits | 13.066 | 12.897 | 13.061 | -0.0% |
| RightAnti, all hits | 5.920 | 5.924 | 6.112 | +3.2% |
| Inner, all hits | 19.235 | 19.567 | 19.565 | +1.7% |
| RightSemi, high-to-low hit rate | 9.096 | 9.405 | 9.203 | +1.2% |
| RightAnti, high-to-low hit rate | 8.480 | 8.478 | 8.408 | -0.8% |
| RightSemi, single probe batch, all hits | 0.205 | 0.217 | 0.258 |
+25.9% |
| RightSemi, dense 100K build, all hits | 10.172 | 10.264 | 10.107 |
-0.6% |
| RightSemi, sparse 100K build, all hits | 15.579 | 15.568 | 15.567 |
-0.1% |
### TPC-H with memtable
| Query | Main | Off | On | On vs main |
|---|---:|---:|---:|---:|
| Q8 | 25.332 | 23.381 | 16.080 | -36.5% |
| Q9 | 48.990 | 46.381 | 41.653 | -15.0% |
| Q17 | 67.344 | 66.039 | 43.156 | -35.9% |
### TPC-H with parquet
with the default `datafusion.execution.parquet.pushdown_filters=false`
| Query | Main | Off | On | On vs main |
| ----- | -----: | -----: | -----: | ---------: |
| Q8 | 63.736 | 63.243 | 58.271 | -8.6% |
| Q9 | 76.014 | 74.103 | 70.175 | -7.7% |
### Target HashJoin `join_time`
The following table measures the `CollectLeft` / `RightSemi` HashMap
join on `part.p_partkey = lineitem.l_partkey` described above. It
reports **operator `join_time`, summed across the four probe
partitions**, separately from end-to-end query latency.
| Input | Query | Main | Off | On | On vs main |
| ----------------- | ----- | -----: | -----: | -----: | ---------: |
| MemTable | Q8 | 29.330 | 29.625 | 12.045 | -58.9% |
| MemTable | Q9 | 39.985 | 40.010 | 26.415 | -33.9% |
| Parquet (default) | Q8 | 29.685 | 29.820 | 11.190 | -62.3% |
| Parquet (default) | Q9 | 35.830 | 35.305 | 20.525 | -42.7% |1 parent 4e6e564 commit 334f293
12 files changed
Lines changed: 1450 additions & 6 deletions
File tree
- datafusion
- common/src
- physical-plan
- benches
- src/joins
- hash_join
- exec
- prepared
- sqllogictest/test_files
- docs/source/user-guide
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
925 | 925 | | |
926 | 926 | | |
927 | 927 | | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
928 | 951 | | |
929 | 952 | | |
930 | 953 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
176 | 181 | | |
177 | 182 | | |
178 | 183 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
0 commit comments