datafusion-physical-plan 56.0.0

Physical (ExecutionPlan) implementations for DataFusion query engine
Documentation
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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements

//! Completion protocol shared by the probe partitions of a join.
//!
//! The probe partitions of one `HashJoinExec` or `NestedLoopJoinExec` run
//! concurrently over a shared build side. Some facts have to cross partition
//! boundaries:
//!
//! - which partition emits the build-side rows in the final stage, namely the
//!   last one to finish,
//! - whether any partition stopped before it finished, in which case its
//!   matches are missing and nobody may emit, and
//! - what the partitions collectively saw, which the null-aware `NOT IN` rules
//!   of the hash join need: did any partition see a row, and did any see a
//!   NULL join key.
//!
//! [`ProbeCompletion`] owns all of them, because reading the facts without
//! ordering them against the choice of the last partition produced wrong
//! results. A partition that read the NULL
//! flag *before* decrementing the counter could observe `false`, have a
//! sibling record a NULL and finish, then become the last partition itself and
//! emit build rows that `NOT IN` must suppress.
//!
//! The invariant is therefore: **whichever caller observes itself to be last
//! sees every fact recorded by every other partition.** It rests on two
//! properties, and the type is shaped to keep both.
//!
//! 1. *Order.* The facts are reachable only through the value returned by
//!    [`ProbeCompletion::report_completed`], so no caller can read them before
//!    its own decrement.
//! 2. *Visibility.* That decrement is `AcqRel`, so the last partition
//!    synchronizes with the release of every partition that finished before
//!    it. Were the counter `Relaxed`, nothing would order a sibling's store
//!    before the final load even when the decrement happened first.
//!
//! Neither property can be pinned down by an ordinary concurrent test: the
//! window is a few instructions wide, and on x86 a `Relaxed` decrement lowers
//! to the same instruction as an `AcqRel` one. Both are model-checked in
//! `loom_tests` instead.

use std::sync::atomic::Ordering;

/// What every probe partition together saw, as observed by the last one to
/// finish.
///
/// Obtainable only from [`ProbeCompletion::report_completed`], which is what
/// stops the final stage from reading the shared state too early.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct ProbeSideSummary {
    /// Some probe partition saw a row. An entirely empty probe side is not the
    /// same as one that matched nothing: `NULL NOT IN (empty)` is TRUE.
    pub(super) non_empty: bool,
    /// Some probe partition saw a NULL join key.
    pub(super) has_null: bool,
}

/// Describes the state of a probe partition after it has finished its work.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Completion {
    /// Not the last partition to finish.
    NotLast,
    /// Last partition to finish and emit a summary.
    Emit(ProbeSideSummary),
    /// The last partition with some previous partition abandoned.
    Abandoned,
}

/// Defines [`ProbeCompletion`] over the given atomic types.
///
/// [loom] can only explore its own atomics, so the protocol is written once
/// here and instantiated twice: over [`std::sync::atomic`] for the join, and
/// over `loom::sync::atomic` for the model in `loom_tests`. Both share this
/// single copy of the orderings and the call sequence, which is what lets the
/// model speak for the real thing. Keep it that way: a second copy of these
/// orderings would not be covered.
///
/// [loom]: https://docs.rs/loom
macro_rules! define_probe_completion {
    (counter: $counter:ty, flag: $flag:ty) => {
        /// Counts the probe partitions that are still running.
        type Counter = $counter;
        /// One fact, set by any partition and read by the last one.
        type Flag = $flag;

        /// Tracks how many probe partitions are still running, together with
        /// the facts they contribute.
        ///
        /// See the [module docs](self) for the invariant this upholds.
        #[derive(Debug)]
        pub(super) struct ProbeCompletion {
            /// Probe partitions that have not finished yet.
            running: Counter,
            /// Set once any probe partition has seen a row.
            saw_row: Flag,
            /// Set once any probe partition has seen a NULL join key.
            saw_null_key: Flag,
            /// Set once any probe partition has stopped before it finished.
            abandoned: Flag,
        }

        impl ProbeCompletion {
            /// Creates the protocol state for `probe_threads` partitions.
            pub(super) fn new(probe_threads: usize) -> Self {
                Self {
                    running: Counter::new(probe_threads),
                    saw_row: Flag::new(false),
                    saw_null_key: Flag::new(false),
                    abandoned: Flag::new(false),
                }
            }

            /// Records what one probe partition saw in one batch.
            ///
            /// `Relaxed` suffices: the `AcqRel` decrement in
            /// [`Self::report_completed`] publishes these stores to the last
            /// partition.
            pub(super) fn record_batch(&self, non_empty: bool, has_null: bool) {
                if non_empty {
                    self.saw_row.store(true, Ordering::Relaxed);
                }
                if has_null {
                    self.saw_null_key.store(true, Ordering::Relaxed);
                }
            }

            /// Whether some partition has already recorded a NULL join key.
            ///
            /// A hint for skipping work during the probe phase, which may lag
            /// behind sibling partitions. It must never decide what the final
            /// stage emits; use the [`ProbeSideSummary`] from
            /// [`Self::report_completed`] for that.
            pub(super) fn saw_null_key_hint(&self) -> bool {
                self.saw_null_key.load(Ordering::Relaxed)
            }

            /// Marks the calling partition finished. Only the last one gets
            /// more than [`Completion::NotLast`]: [`Completion::Emit`] with
            /// what all partitions saw, or [`Completion::Abandoned`] if some
            /// partition stopped early (see [`Self::abandon`]).
            ///
            /// The `AcqRel` decrement publishes this partition's own stores
            /// and acquires those of the partitions that finished earlier, so
            /// the summary handed to the last caller is complete.
            pub(super) fn report_completed(&self) -> Completion {
                // `fetch_sub` returns the value from *before* the decrement,
                // so `1` means this call is the one that took it to zero.
                let was_last = self.running.fetch_sub(1, Ordering::AcqRel) == 1;
                match (was_last, self.abandoned.load(Ordering::Relaxed)) {
                    (false, _) => Completion::NotLast,
                    (true, false) => {
                        let summary = ProbeSideSummary {
                            non_empty: self.saw_row.load(Ordering::Relaxed),
                            has_null: self.saw_null_key.load(Ordering::Relaxed),
                        };
                        Completion::Emit(summary)
                    }
                    (true, true) => Completion::Abandoned,
                }
            }

            /// Marks `count` probe partitions as having stopped before they finished.
            ///
            /// They still count down, so the partitions that remain are not left
            /// waiting. But their matches are missing, so the build-side rows must not
            /// be emitted: the last partition is not handed a summary.
            ///
            /// `Relaxed` suffices for the flag, as in [`Self::record_batch`]: the
            /// `AcqRel` decrement publishes it.
            pub(super) fn abandon(&self, count: usize) -> bool {
                if count == 0 {
                    return false;
                }
                self.abandoned.store(true, Ordering::Relaxed);
                self.running.fetch_sub(count, Ordering::AcqRel) == count
            }
        }
    };
}

define_probe_completion!(
    counter: std::sync::atomic::AtomicUsize,
    flag: std::sync::atomic::AtomicBool
);

#[cfg(test)]
mod tests {
    use super::*;

    /// The sequential contract: only the last caller is handed the summary,
    /// and it carries what the earlier partitions recorded.
    ///
    /// The concurrent invariant is covered by `loom_tests` instead.
    #[test]
    fn only_the_last_partition_receives_the_summary() {
        let completion = ProbeCompletion::new(3);

        completion.record_batch(true, false);
        assert_eq!(completion.report_completed(), Completion::NotLast);

        completion.record_batch(true, true);
        assert_eq!(completion.report_completed(), Completion::NotLast);

        assert_eq!(
            completion.report_completed(),
            Completion::Emit(ProbeSideSummary {
                non_empty: true,
                has_null: true,
            })
        );
    }

    /// A probe side that never saw a row leaves both facts clear, which is how
    /// `NULL NOT IN (empty)` stays TRUE.
    #[test]
    fn an_untouched_probe_side_reports_nothing_seen() {
        let completion = ProbeCompletion::new(1);

        assert_eq!(
            completion.report_completed(),
            Completion::Emit(ProbeSideSummary {
                non_empty: false,
                has_null: false,
            })
        );
    }

    /// The hint may lag, but it must never report a NULL that was not
    /// recorded.
    #[test]
    fn the_hint_reflects_recorded_nulls() {
        let completion = ProbeCompletion::new(1);
        assert!(!completion.saw_null_key_hint());

        completion.record_batch(true, false);
        assert!(!completion.saw_null_key_hint());

        completion.record_batch(true, true);
        assert!(completion.saw_null_key_hint());
    }

    /// Abandoning no partitions changes nothing. A caller may report a count of
    /// zero, for example when no partition went away before the left side
    /// spilled, and that must not suppress the build-side rows.
    #[test]
    fn abandoning_no_partitions_still_emits() {
        let completion = ProbeCompletion::new(1);
        assert!(!completion.abandon(0));
        assert_eq!(
            completion.report_completed(),
            Completion::Emit(ProbeSideSummary {
                non_empty: false,
                has_null: false,
            })
        );
    }
}

/// Model-checked concurrency tests for the protocol.
///
/// These instantiate the protocol over loom's atomics (see the
/// [module docs](self)) and let loom enumerate the interleavings and store
/// visibility the memory model allows. They fail if the decrement in
/// `report_completed` or `abandon` is weakened to `Relaxed`.
///
/// Compiled only under `--cfg datafusion_loom`, which is also what pulls the `loom`
/// dev-dependency into the build (see `datafusion-physical-plan`'s
/// `Cargo.toml` for why it is kept out of the ordinary one). Run them with:
///
/// ```text
/// RUSTFLAGS="--cfg datafusion_loom" cargo test -p datafusion-physical-plan --lib loom_tests
/// ```
#[cfg(all(test, datafusion_loom))]
mod loom_tests {
    use super::Completion;
    use super::ProbeSideSummary;
    use loom::sync::Arc;
    use std::sync::atomic::Ordering;

    define_probe_completion!(
        counter: loom::sync::atomic::AtomicUsize,
        flag: loom::sync::atomic::AtomicBool
    );

    /// The invariant behind the fix: whichever partition observes itself to be
    /// last sees the NULL its sibling recorded.
    ///
    /// This is the regression test for the wrong-results bug. One partition
    /// records a NULL and finishes while the other finishes having recorded
    /// nothing, and the last one must not conclude that no NULL was seen.
    #[test]
    fn the_last_partition_observes_a_sibling_null() {
        loom::model(|| {
            let completion = Arc::new(ProbeCompletion::new(2));

            let recorder = {
                let completion = Arc::clone(&completion);
                loom::thread::spawn(move || {
                    completion.record_batch(true, true);
                    completion.report_completed()
                })
            };

            let observer = completion.report_completed();
            let recorded = recorder.join().unwrap();

            let summary = match (recorded, observer) {
                (Completion::Emit(summary), Completion::NotLast)
                | (Completion::NotLast, Completion::Emit(summary)) => summary,
                (Completion::Emit(_), Completion::Emit(_)) => {
                    panic!("two partitions both finished last")
                }
                (Completion::NotLast, Completion::NotLast) => {
                    panic!("no partition finished last")
                }
                other => panic!("unexpected outcome: {other:?}"),
            };

            assert!(
                summary.has_null,
                "the last partition missed the NULL its sibling recorded"
            );
            assert!(
                summary.non_empty,
                "the last partition missed the row its sibling recorded"
            );
        });
    }

    /// With both partitions recording, the summary must be the union of what
    /// they saw, whichever finishes last.
    #[test]
    fn the_summary_unions_what_every_partition_recorded() {
        loom::model(|| {
            let completion = Arc::new(ProbeCompletion::new(2));

            let other = {
                let completion = Arc::clone(&completion);
                loom::thread::spawn(move || {
                    // Rows, but no NULL key.
                    completion.record_batch(true, false);
                    completion.report_completed()
                })
            };

            // A NULL key in an otherwise empty batch.
            completion.record_batch(false, true);
            let here = completion.report_completed();
            let there = other.join().unwrap();

            let summary = match (here, there) {
                (Completion::Emit(summary), Completion::NotLast)
                | (Completion::NotLast, Completion::Emit(summary)) => summary,
                other => panic!("some partition must finish last: {other:?}"),
            };
            assert_eq!(
                summary,
                ProbeSideSummary {
                    non_empty: true,
                    has_null: true,
                },
                "the summary lost a fact one of the partitions recorded"
            );
        });
    }

    /// Exactly one partition may take the final stage. Two would emit the
    /// build side twice; none would drop it.
    #[test]
    fn exactly_one_partition_finishes_last() {
        loom::model(|| {
            let completion = Arc::new(ProbeCompletion::new(2));

            let other = {
                let completion = Arc::clone(&completion);
                loom::thread::spawn(move || {
                    completion.report_completed() != Completion::NotLast
                })
            };

            let here = completion.report_completed() != Completion::NotLast;
            let there = other.join().unwrap();

            assert!(here ^ there, "exactly one partition must finish last");
        });
    }

    /// The probe-phase hint never invents a NULL: if no partition recorded
    /// one, no observation of the hint may report one.
    #[test]
    fn the_hint_never_reports_an_unrecorded_null() {
        loom::model(|| {
            let completion = Arc::new(ProbeCompletion::new(2));

            let other = {
                let completion = Arc::clone(&completion);
                loom::thread::spawn(move || {
                    completion.record_batch(true, false);
                    completion.saw_null_key_hint()
                })
            };

            let here = completion.saw_null_key_hint();
            let there = other.join().unwrap();

            assert!(!here && !there, "the hint reported an unrecorded NULL");
        });
    }

    /// A partition that stops early still counts down, so exactly one
    /// partition finishes last. But its matches are missing, so no partition
    /// may emit, whichever of the two finishes last.
    #[test]
    fn an_abandoned_partition_counts_down_but_suppresses_the_emit() {
        loom::model(|| {
            let completion = Arc::new(ProbeCompletion::new(2));

            let quitter = {
                let completion = Arc::clone(&completion);
                loom::thread::spawn(move || completion.abandon(1))
            };

            let here = completion.report_completed();
            let quitter_was_last = quitter.join().unwrap();

            match (here, quitter_was_last) {
                // This partition finished last and saw the abandon
                (Completion::Abandoned, false) => {}
                // The quitter finished last and releases the shared state
                (Completion::NotLast, true) => {}
                other => panic!("unexpected outcome: {other:?}"),
            }
        });
    }
}