Commit 9d311ee
fix: Only use null-aware anti join for NOT IN, not NOT EXISTS
The previous implementation incorrectly applied null-aware semantics to ALL
LeftAnti joins, including NOT EXISTS subqueries. This was wrong because:
- **NOT IN**: Uses three-valued logic (TRUE/FALSE/UNKNOWN), requires null-aware
- **NOT EXISTS**: Uses two-valued logic (TRUE/FALSE), should NOT be null-aware
```sql
-- Setup: customers has (1, 2, 3, NULL), banned has (2, NULL)
-- NOT IN - Correctly returns empty (null-aware)
SELECT * FROM customers WHERE id NOT IN (SELECT id FROM banned);
-- Result: Empty (correct - NULL in subquery makes all comparisons UNKNOWN)
-- NOT EXISTS - Was incorrectly returning empty (bug)
SELECT * FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM banned b WHERE c.id = b.id);
-- Expected: (1, 3, NULL) - NULL=NULL is FALSE, so no matches for these rows
-- Actual (buggy): Empty - incorrectly using null-aware semantics
```
In `decorrelate_predicate_subquery.rs`, line 424:
```rust
let null_aware = matches!(join_type, JoinType::LeftAnti);
```
This set `null_aware=true` for ALL LeftAnti joins, but it should only be
true for NOT IN (InSubquery), not NOT EXISTS (Exists).
The `SubqueryInfo` struct already distinguishes between them:
- **NOT IN**: Created with `new_with_in_expr()` → `in_predicate_opt` is `Some(...)`
- **NOT EXISTS**: Created with `new()` → `in_predicate_opt` is `None`
Fixed by checking both conditions:
```rust
let null_aware = matches!(join_type, JoinType::LeftAnti)
&& in_predicate_opt.is_some(); // Only NOT IN, not NOT EXISTS
```
**File**: `datafusion/optimizer/src/decorrelate_predicate_subquery.rs`
- Updated null_aware detection to only apply to NOT IN (lines 420-426)
- Added comprehensive comments explaining the distinction
- Check `in_predicate_opt.is_some()` to distinguish NOT IN from NOT EXISTS
**File**: `datafusion/sqllogictest/test_files/null_aware_anti_join.slt`
Added 5 new test scenarios (Tests 14-18):
**Test 14**: Direct comparison of NOT IN vs NOT EXISTS with NULLs
- NOT IN with NULL → empty result (null-aware)
- NOT EXISTS with NULL → returns non-matching rows (NOT null-aware)
- EXPLAIN verification
**Test 15**: NOT EXISTS with no NULLs
**Test 16**: NOT EXISTS with correlated subquery
**Test 17**: NOT EXISTS with all-NULL subquery
- Shows that NOT EXISTS returns all rows (NULL=NULL is FALSE)
- Compares with NOT IN which correctly returns empty
**Test 18**: Nested NOT EXISTS and NOT IN
- Verifies correct interaction between the two
```bash
cargo test -p datafusion-sqllogictest --test sqllogictests -- null_aware_anti_join
cargo test -p datafusion-sqllogictest --test sqllogictests subquery.slt
cargo test -p datafusion-optimizer --lib
cargo test -p datafusion-physical-plan --lib hash_join
```
This fix ensures DataFusion correctly implements SQL semantics:
- NOT IN subqueries now correctly use null-aware anti join (three-valued logic)
- NOT EXISTS subqueries now correctly use regular anti join (two-valued logic)
Users can now reliably use both NOT IN and NOT EXISTS with confidence that
NULL handling follows SQL standards.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 8d2f08f commit 9d311ee
File tree
2 files changed
+159
-4
lines changed- datafusion
- optimizer/src
- sqllogictest/test_files
2 files changed
+159
-4
lines changedLines changed: 6 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
418 | 418 | | |
419 | 419 | | |
420 | 420 | | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
425 | 427 | | |
426 | 428 | | |
427 | 429 | | |
| |||
Lines changed: 153 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
229 | 229 | | |
230 | 230 | | |
231 | 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 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
232 | 367 | | |
233 | 368 | | |
234 | 369 | | |
| |||
259 | 394 | | |
260 | 395 | | |
261 | 396 | | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
0 commit comments