Repository navigation
fix: backport null-aware anti-join, timestamp-to-time pruning and spill fixes - #42
Conversation
WenyXu
left a comment
There was a problem hiding this comment.
LGTM. Reviewed the 8 commits at the executor level; no correctness findings.
What was verified:
#25076/#25339/#26578+42cd9bed9: tracker and all four consumers read together and checked against SQL 3-valued logic (uncorrelated probe NULL/empty probe/build logical NULL, correlated per-build-row marking via the residual filter, LeftMark rejection, multi-key rejection).probe_completion.rsis byte-identical to5747e87e9. Confirmedmark_null_candidates_for_probe_batchgates onlogical_null_count()and enumerates witharrow::compute::is_null, which is itself logical in arrow 59.2, so dictionary keys pointing at NULL values are covered on both cases; a correlated dictionary repro returns the expected rows. Cross-product cost when there are no scope keys matches upstream.#24891: only the two documented cosmetic deltas from09a2aff72; reader/writer contract (no lost wakeup, no lost/duplicated batch, EOF ordering, error propagation, single-writer FIFO, disk release) checked state by state, plus the 400-seed fuzz run.#24675: gate matchesc4910e076;need_produce_result_in_finalcovers exactly the bitmap-dependent types, so the fallback now errors withResourcesExhaustedinstead of emitting wrong rows.#26452: production diff identical to6110f74f5; guard covers CAST/TRY_CAST, all time units, dictionary unwrapping in both directions andNegativeExpr, and rejection degrades to no pruning rather than an error.
Tests run at 42cd9bed9 with the pinned target dir: datafusion-physical-plan --lib 1740 passed (1706 at base, +34 new), null_aware 45, datafusion-pruning 90, null_aware_anti_join.slt and push_down_filter_parquet.slt 1/1 each, core_integration memory_limit::repartition_mem_limit 2/2, spill pool 20/20 with DATAFUSION_SPILL_POOL_FUZZ_ITERATIONS=200 (plus 400 extra seeds), loom 4/4, fmt, cargo check --offline --locked.
Two nits, PR description only: the physical-plan counts are 1706 -> 1740 here, not 1720 -> 1734; and the clippy claim needs --no-deps under a host clippy 1.98, which flags untouched datafusion/common/src/utils/hex.rs:156.
Merge-order note for the fork: this and #39 both rewrite the same state in hash_join/exec.rs (JoinLeftData/JoinBuildData, collect_left_input, the visited-bitmap allocation, the null-aware block in stream.rs). The correct resolution is #39's layout plus this PR's probe state; report_probe_completed ends up defined twice, and two shortcuts (_visited_reservation: reservation.new_empty(), or restoring #39's flag-based stream.rs block) compile while silently dropping one side's fix. Merging #39 first and rebasing this one is cheaper.
## Which issue does this PR close? - Closes apache#26451. ## Rationale for this change Casting timestamp statistics to time-of-day values does not preserve ordering across midnight. This can invert the minimum and maximum used by statistics pruning and incorrectly discard files or row groups that contain matching rows. ## What changes are included in this PR? - Prevent timestamp-to-`Time32` and timestamp-to-`Time64` casts from being used for statistics pruning. - Add pruning-expression coverage for both `CAST` and `TRY_CAST` across all supported time units. - Add a Parquet regression test whose timestamp bounds cross midnight. ## What is the testing strategy for this PR? The regression test verifies equality and range predicates for `Time32` and `Time64`, including the expected row counts and pruning metrics. The full extended workspace test suite also passes. ## Are there any user-facing changes? Queries that cast timestamps to time-of-day values may perform less statistics pruning, but they no longer risk incorrect results when timestamp bounds cross a day boundary. (backport of apache#26452, 6110f74) Signed-off-by: discord9 <discord9@163.com> (cherry picked from commit 17f5b0ba54232e76d6bb45d1e4b77b9ade691fd7)
…joins with a multi-partition probe side (apache#24675) The memory-limited fallback returns wrong rows for join types whose final emission reads the visited-left bitmap (LEFT, LEFT SEMI, LEFT ANTI, LEFT MARK) when the probe side has more than one partition. The fallback rebuilds a per-partition `JoinLeftData` per left chunk with `probe_threads_counter == 1` and a per-partition bitmap, so every partition believes it is the last to finish probing and emits its own unmatched set, from a bitmap that only saw its own right rows. Unmatched rows are emitted once per partition, and rows matched only in another partition are emitted as unmatched. The existing gate covers only FULL. This extends it to every `need_produce_result_in_final` type, so an over-budget build side fails with `ResourcesExhausted` instead of returning wrong rows. Right-side emission types (RIGHT, RIGHT SEMI, RIGHT ANTI, RIGHT MARK) keep the fallback: each partition owns its right rows exclusively, so their bitmaps are complete per partition. Proper cross-partition coordination of the left bitmap, which would re-enable the fallback for these types, is apache#22038. Changes: - `NestedLoopJoinExec::execute`: `full_join_multi_partition` becomes `left_emission_multi_partition = need_produce_result_in_final(join_type) && right_partition_count > 1`, with the comment updated to describe the failure mode. - `test_overallocation` moves LEFT/LEFT SEMI/LEFT ANTI/LEFT MARK from the succeed-via-fallback group into the must-OOM group alongside FULL, and adds single-partition-probe regression tests for LEFT SEMI, LEFT ANTI and LEFT MARK (spill still allowed there). Backport notes: applied by hand because `git cherry-pick -x` is not usable here (the worktree's git directory is not writable from this sandbox). The only structural difference from the upstream diff is that this fork computes `spill_state` inline in `execute` instead of through a `can_spill` local (the fork's fallback is initiated lazily from the stream, so it has no eager `left_spill_manager`); the gate condition and the comments are carried over verbatim. All other hunks (tests) applied byte-identically. (cherry picked from commit c4910e0) (backport of apache#24675, c4910e0) Signed-off-by: discord9 <discord9@163.com> (cherry picked from commit 3d17c338d4aee595d8b8d074f9c93a2df7693784)
apache#24891) Closes apache#24883 Under a memory limit, a grouped aggregation on two or more partitions can stop and never complete: when `RepartitionExec` cannot reserve memory for a batch it writes the batch to its spill pool and sends a marker, and the reader then blocks on the pool until it gets a batch. When two input tasks spill at the same time the pool holds two open files. The reader read only the oldest file and, when that file had no unread batch, waited for it, while the batch it needed was in the newer file. Once every channel had data the gate closed and the writers waited too, so nothing could wake the reader. `SpillPoolReader` now keeps every file it received (oldest first) and returns the first available batch. A file that has no unread batch is skipped; the reader still waits on the oldest file that has unread batches whose read is not complete, so a single writer keeps FIFO order. The writer side is unchanged. Also backported: - `test: reproduce the RepartitionExec spill pool deadlock with a SQL query` (`datafusion/core/tests/memory_limit/repartition_mem_limit.rs`), which sums the `spill_count` metric and fails if no attempt spilled. - `test: add a scenario fuzzer for the spill pool contract`, including `test_read_error_is_reported_to_the_reader`, a test that a second writer's batch is readable while the first writer is held, and `spill_pool_scenario_fuzz` (default 50 seeds). - the extended-CI job sets `DATAFUSION_SPILL_POOL_FUZZ_ITERATIONS: 1000`. Backport notes: applied by hand because `git cherry-pick -x` is not usable here (the worktree's git directory is not writable from this sandbox). All three files applied byte-identically; the only fork-specific deltas in the touched regions are pre-existing cosmetic differences (`#[deprecated]` without `since`, `sort_unstable` vs `sort`), which this commit does not touch. (cherry picked from commit 09a2aff) (backport of apache#24891, 09a2aff) Signed-off-by: discord9 <discord9@163.com> (cherry picked from commit ac60580950ee42b7358a74fdf1203a0dc5e84992)
The null-aware probe phase decides whether the probe key saw a NULL with `Array::null_count()`, which is 0 for a dictionary key that points at a NULL dictionary value (a logical NULL). Upstream changed this check to `logical_null_count()` in apache#23957 ("feat: null aware RightAnti hash join execution + planning support", 0a4e930); this fork's base predates it, so a dictionary-encoded `NOT IN` probe side is currently treated as NULL-free. Carry just that one line. The null-aware fixes that follow rely on it: their dictionary-NULL coverage on the probe side is meaningless otherwise. >>> patch: .backport-artifacts/p0-adapt-logical-null.patch Adaptation, not a backport: upstream commit 0a4e930 also brings the RightAnti executor, which is out of scope here. Signed-off-by: discord9 <discord9@163.com> (cherry picked from commit b44ec5964eff95be09722bb3b894c8e86618efb9)
…tion saw NULL RE-IMPLEMENTATION, not a faithful cherry-pick. apache#25076 (5747e87) does not apply here: this fork's base predates the null-aware branching refactor (apache#24957, `NullAwareMode`) and the chunked final emission (apache#25028, `EmitUnmatchedBuildRowsState`), and `git show 5747e87 | git apply --check` fails on `exec.rs`, `stream.rs` and the three manifests. The semantics below are the upstream ones; the shape is this fork's. The bug: the final stage read the shared "probe side saw NULL" flag and only then decremented the probe-partition counter, so a partition could read `false`, let a sibling record a NULL and finish, then become the last partition itself and emit build rows that `NOT IN` must suppress. The counter also decremented with `Relaxed`, so nothing ordered a sibling's store before the last partition's load. Changes: - New `hash_join/probe_completion.rs`, ported nearly verbatim: `ProbeCompletion` owns the probe-partition counter and the two shared flags, `report_completed()` decrements with `AcqRel` and returns a `ProbeSideSummary` only to the last partition, so the final stage cannot read the flags before its own decrement. Its sequential unit tests and the `--cfg datafusion_loom` model-checked tests come along with the workspace `loom` dependency and the `cfg(datafusion_loom)` check-cfg registration. - `JoinLeftData` holds one `ProbeCompletion` instead of `probe_threads_counter`/`probe_side_non_empty`/`probe_side_has_null`. The probe phase records through `record_probe_batch()` and keeps a best-effort `probe_side_has_null_hint()` early exit. - `process_unmatched_build_batch` decrements first and hands the resulting summary to the new `null_aware_left_anti_final_indices`, which holds the `LeftAnti` rules: a probe NULL seen by any partition suppresses every build row; otherwise a NULL build key is dropped unless the probe side was empty. - This fork has no null-aware `LeftMark`, so the mark half of the upstream PR has no counterpart here. Tests (`hash_join/exec.rs`): - `test_null_aware_anti_join_probe_null_in_other_partition`: two probe partitions, only partition 0 sees the NULL, drained in both finishing orders; no rows may be emitted. - `test_null_aware_anti_join_probe_logical_null_in_other_partition`: the same with a dictionary key whose NULL is logical only. - `test_null_aware_anti_join_empty_probe_keeps_null_build_key`: pins `NULL NOT IN (empty) = TRUE` (added here; upstream covers it via slt only). (backport of apache#25076, 5747e87) Signed-off-by: discord9 <discord9@163.com> (cherry picked from commit 3fb9a3084e39781b90d5dbffa01f2664f64dcc2d)
…results RE-IMPLEMENTATION, not a faithful cherry-pick. apache#25339 (95bb0a0) builds on the correlated null-aware machinery of apache#21585/apache#24957 (correlation scope maps, `NullAwareMode`, `null_indices_bitmap`, shared null-mark buffers), none of which exists here; `git show 95bb0a0 | git apply --check` fails on `exec.rs`, `stream.rs` and `join_selection.rs`, and it also edits benchmark files this fork does not have. The bug: a correlated `NOT IN` whose correlation cannot become an equi-join key leaves a residual join filter, and the executor ignored that filter when deciding whether a NULL on the subquery side makes `NOT IN` UNKNOWN, so a NULL the filter excludes still poisoned every outer row. Same semantics for the shapes this fork can plan (`on[0]` is the `NOT IN` value key and the residual correlation lives in the join filter; the null-aware validation still requires a single join key, where upstream also accepted `on[1..]` scope keys): - `JoinLeftData` gains the shared `null_indices_bitmap` of build rows whose `NOT IN` is UNKNOWN, allocated with the build side for correlated joins. - A correlated null-aware join (one with a join filter) no longer lets a probe NULL poison the whole join: `mark_null_candidates_for_probe_batch` records, per build row, whether a NULL that the residual filter keeps makes `NOT IN` UNKNOWN - case 1, a NULL build key with a probe row that passes the filter; case 2, a probe row with a NULL key that passes the filter. Without scope keys both cases are cross products, already-marked build rows are skipped, and the filter is not re-evaluated for them. - The final stage drops the marked build rows instead of consulting the probe-side flags, so outer rows whose subquery is empty keep `NOT IN (empty) = TRUE`. - The uncorrelated probe-side NULL check now uses logical nulls too, so dictionary keys are covered on both paths. Tests: - `hash_join/exec.rs`: `test_null_aware_left_anti_filter_only` (a `LeftAnti` with a second-column `>` filter and no scope keys; only the rows with an empty subquery result are emitted), from the upstream PR. - `sqllogictest/test_files/null_aware_anti_join.slt`: new correlated-residual section - the query from the upstream issue, an `EXPLAIN` assertion that the shape stays `LeftAnti ... filter=..., null_aware` on a single equi key, and the case where the residual leaves the subquery empty for every outer row. (backport of apache#25339, 95bb0a0) Signed-off-by: discord9 <discord9@163.com> (cherry picked from commit 163cc1d5fb8a8565825a13ebfc82c010bc8750fa)
RE-IMPLEMENTATION, not a faithful cherry-pick. apache#26578 (a79a694) reuses `null_aware_build_key_nulls`, which apache#25693 introduced for null-aware `LeftMark` - a join type this fork does not have - and `git show a79a694 | git apply --check` fails on both touched files. The bug: the final stage kept a build row whose key is a dictionary entry pointing at a NULL value, although `NULL NOT IN (5, 6)` is UNKNOWN and the row should be dropped exactly as when the key is a physical NULL. Changes: - `JoinLeftData` stores the logical nulls of the scalar build key for an uncorrelated null-aware join, computed once with the build side (`logical_nulls()`), charged to the memory reservation only when it materializes a new buffer instead of reusing the input's. - `null_aware_left_anti_final_indices` filters the final build rows by those logical nulls instead of `Array::is_null`. Test: `test_null_aware_left_anti_build_logical_null` (`NULL NOT IN (5, 6)` with a dictionary key that points at a NULL dictionary value), from the upstream PR. (backport of apache#26578, a79a694) Signed-off-by: discord9 <discord9@163.com> (cherry picked from commit 44a08c06981fd370993073cd9c4f75a9fd91e5eb)
…ty result Follow-up to the apache#25076 backport. When any probe partition saw a NULL join key, `build.key NOT IN (probe)` is UNKNOWN for every build row, so a null-aware LeftAnti emits nothing. This fork still materialises the whole final index set in `get_final_indices_from_shared_bitmap` before `null_aware_left_anti_final_indices` discards it, which costs an `O(build rows)` scan plus `UInt64Array(N)` and `UInt32Array(N)` (~12 bytes per build row) that are dropped immediately. The base code returned before building those indices. The apache#25076 backport moved the decision behind them, because the completion protocol has to run first: only the last probe partition gets the final `ProbeSideSummary`. Restore the early return after the summary is obtained and before the indices are built. The `AcqRel` decrement and the ordering guarantee are untouched; correlated joins keep going because their UNKNOWN decision is per build row and already recorded by the probe phase. Signed-off-by: discord9 <discord9@163.com>
…on state #39 introduced `PreparedHashJoinBuild::probe_data`, which builds a fresh `JoinLeftData` per consuming join. It still created the `probe_threads_counter` and the two probe-side atomics that the null-aware backports replace with `ProbeCompletion`, and the `collect_left_input` call it makes predates the two null-aware flags. A prepared build is only reachable for ordinary joins: `prepared_key_indices` requires `INNER`, `CollectLeft` and `!null_aware`, and both `prepare_build` and `execute` re-check it. The per-join null bitmaps therefore start empty and `null_aware_build_key_nulls` stays `None`. The correlation bitmap of a correlated null-aware join is mutable per-execution state, so it is charged to the same `_visited_reservation` lease as the visited bitmap rather than to the build reservation. #39 hands that build data out through `build_lease`, and a dynamic filter can retain it after this join's own state is dropped; charging the bitmap there would keep paying for memory that is already released. `correlated_bitmap_reservation_is_execution_local` pins that down: with the correlated flag on, dropping `JoinLeftData` returns the pool to the producer's charge alone, and dropping the retained lease returns it to zero. Signed-off-by: discord9 <discord9@163.com>
42cd9be to
1da34f9
Compare
* chore(deps): bump datafusion to ae04160b Move all 16 `[patch.crates-io]` DataFusion pins from `cc5534492` to `ae04160b`, the merge commit of GreptimeTeam/datafusion#42. That fork commit backports six upstream apache/datafusion fixes: - #26452 timestamp-to-time casts no longer drive statistics pruning - #24675 memory-limited NLJ fallback refuses left-emission joins with a multi-partition probe side instead of emitting wrong unmatched rows - #24891 RepartitionExec spill-pool deadlock with two open files - #25076 null-aware anti join no longer emits rows after a sibling probe partition saw NULL - #25339 correlated NOT IN with a non-equality correlation - #26578 null-aware anti join drops build rows with a dictionary NULL key The first three are faithful backports. The last three are re-implementations: upstream restructured `hash_join/stream.rs` before writing them, so the patches do not apply to this fork's older copy of that file. `#25339` covers the single-equi-key plus residual-filter shape; multi-key correlated `NOT IN` still fails at planning as before. `Cargo.lock` is regenerated (35 `datafusion*` packages). No runtime acceptance has been run against this pin; see the PR description for the three checks worth doing before shipping. Signed-off-by: discord9 <discord9@163.com> * fix(common-query): pass the pruning bitmap to HashTableLookupExpr::new GreptimeTeam#39 added a fifth parameter to `HashTableLookupExpr::new`; the dynamic-filter payload round-trip test still constructed it with four arguments, which breaks `--all-targets` builds even though the library itself compiles. Signed-off-by: discord9 <discord9@163.com> * chore(deps): bump datafusion to 70f84c5b Move the pins from `ae04160b` to `70f84c5b`, the head of `greptimedb-55.1-thin-fork` after GreptimeTeam/datafusion#37 was merged on top of GreptimeTeam#39/GreptimeTeam#41/GreptimeTeam#42. `ae04160b` did not contain GreptimeTeam#37, so the reverse-cast constraint propagation guard was lost and `tests/cases/distributed/optimizer/partition_pruning_cast` failed: the filter `CAST(k AS BIGINT) = 1` made the planner treat the float column `k` as a singleton and drop the requested `ORDER BY k DESC`. The new pin carries both the reverse-cast guard and the six release backports, so the regression is covered again. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com>
Backports six upstream
apache/datafusionfixes ontogreptimedb-55.1-thin-fork(basee3221b8f7). Every upstream PR gets its own commit; where the fork needed a change of its own there is a separate commit.Closes nothing; these are release-driven backports.
Commits
2f0170b5f6110f74f5avoid pruning timestamp-to-time castsgit apply, byte-identical hunks)3d6f97775c4910e076refuse memory-limited NLJ fallback for left-emission joins with multi-partition probe9fea8985909a2aff72RepartitionExec deadlock with two open spill filesdf74addb4logical_null_count(); the line is upstream apache#23957 (0a4e93052), which this base predates06ae5f1c65747e87e9null-aware anti join emitted rows after a sibling probe partition saw NULL1eb128b3495bb0a0dfcorrelated NOT IN with a non-equality correlation1dc486fdfa79a69488drop build rows with a dictionary NULL keyWhy three of these are re-implementations, not cherry-picks
This fork's
hash_join/stream.rsate3221b8f7is byte-identical to upstream0d5f9b106(apache#23106) — it is upstream code, not a GreptimeDB rewrite. Upstream then restructured the same file before writing these fixes:#23957null-aware RightAnti execution + planning#21585null-aware hash mark-joins#24957refactor: simplify null-aware and join-type branching in hash join#25028perf: emit unmatched build rows in batch_size chunks in HashJoinExec#24587hash join dynamic filter with dictionary-encoded keysAll three upstream patches fail
git apply --checkagainst this base (Cargo.lock:4084,exec.rs:357, and benchmark files that do not exist here). A faithful backport would first require porting that feature/refactor chain — including new null-aware Mark support — which is not a release fix.We therefore re-implemented the fixes' semantics on the older structure:
#25076: newhash_join/probe_completion.rsis a near-verbatim port of the upstream module (ProbeCompletion,AcqReldecrement,ProbeSideSummaryonly to the last partition, sequential tests plus--cfg datafusion_loommodel tests). The call order instream.rsis fixed in place because the upstream context (NullAwareMode, chunked emission) does not exist here.#25339: partial. Per-build-row UNKNOWN is applied for the shapes this fork can plan: single equi key plus residual filter. Multi-key (scope-key) correlatedNOT INstill fails at planning (hash_join/exec.rs, "only supports single column join key"); that was already the behaviour before this PR and is unchanged.#26578: semantics identical, build-key NULLs now come fromlogical_nulls()so dictionary-encoded NULL values are seen.The
adaptcommit is separate because thelogical_null_count()line is upstream's, just from an earlier PR than this base contains.Overlap with open PRs
No shared upstream PRs with #39 or #41.
File-level overlap with #39, which targets the same base
e3221b8f7:hash_join/exec.rs,hash_join/stream.rs,hash_join/mod.rs— touched by fix: null-aware anti join could emit rows after a sibling probe partition saw NULL apache/datafusion#25076 / fix: correlated NOT IN with a non-equality correlation returns wrong results apache/datafusion#25339 / fix: drop build rows with a dictionary NULL key in null-aware anti joins apache/datafusion#26578pruning/src/pruning_predicate.rs— touched by fix: avoid pruning timestamp-to-time casts apache/datafusion#26452Whichever of the two lands second needs a rebase. #41 (
aggregates/aggregate_stream.rs,aggregates/mod.rs) has no overlap.Verification
Run per lane and again on the integrated branch.
datafusion-pruning: 90/90 tests; newrow_group_predicate_cast_timestamp_to_time; newpush_down_filter_parquet.sltsection (162 lines). Negative control: removing the guard makes both the unit test and the slt section fail.datafusion-physical-plan: 1720/1720 before, 1734/1734 after;null_awarefiltered run 45/45; loom model tests 4/4 (RUSTFLAGS="--cfg datafusion_loom").datafusion-sqllogictest -- null_aware_anti_join: 1/1 file, includes a correlated-residual section with anEXPLAINshape assertion (HashJoinExec: mode=CollectLeft, join_type=LeftAnti, ..., null_aware).core_integrationmemory_limit::repartition_mem_limit: 2/2, including a 12-attempt spill deadlock reproducer that assertsspill_count > 0so it cannot pass vacuously.Left3 → 12 rows,LeftAnti2 → 11,LeftMark3 → 12,spill_count = 2;test_overallocationfails under the old gate. The wrong rows this PR prevents are reproducible on this fork.cargo fmt --all -- --check,cargo clippy -D warnings(physical-plan, pruning, physical-optimizer),cargo check --offline --lockedon the integrated branch.Not verified
extendedsuite, doctests, the CI 1000-seed fuzz scale, and benchmarks were not run.null_aware+PartitionMode::Partitionedis still not rejected at planning; join selection keeps such plans onCollectLeft. Hand-builtPartitionedplans would give each partition its own probe summary. Pre-existing gap, not widened here.#24891also adds three lines to.github/workflows/extended.yml(fuzz seed env vars); kept for fidelity.