datafusion-execution 56.0.0

Execution configuration support 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// 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

//! Shares reserved workspace across merge reservations and temporary loans.

use std::fmt::{self, Display, Formatter};
use std::sync::Arc;

use datafusion_common::{Result, resources_err};
use parking_lot::Mutex;

use super::{MemoryConsumer, MemoryLimit, MemoryPool, MemoryReservation};

/// Shares reserved merge workspace across child reservations and temporary loans.
///
/// This pool charges a single reservation to its parent [`MemoryPool`]. After
/// acquiring workspace through a child [`MemoryReservation`], call [`Self::retain`]
/// to keep that capacity available even when children release their reservations.
/// Children and [`WorkspaceLoan`]s share this capacity; only usage above the
/// existing parent reservation requires additional parent memory. Call
/// [`Self::release_unused`] when no further merge needs the idle workspace.
/// Live children and loans keep the pool and their reserved memory alive.
///
/// All parent accounting and allocation policy uses the [`MemoryConsumer`]
/// supplied to [`Self::new`]. Child consumers are not registered with the parent,
/// so their names and [`MemoryConsumer::can_spill`] flags do not affect the
/// parent's accounting or fair shares. Consumers that need a separate parent
/// allocation policy should register directly with the parent. They can borrow
/// already-acquired workspace with [`Self::borrow`] and request any additional
/// capacity through their own reservation.
///
/// # Example
///
/// ```
/// # use std::sync::Arc;
/// # use datafusion_common::Result;
/// # use datafusion_execution::memory_pool::{
/// #     GreedyMemoryPool, MemoryConsumer, MemoryPool, MergeMemoryPool,
/// # };
/// # fn main() -> Result<()> {
/// let parent: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(100));
/// let workspace = Arc::new(MergeMemoryPool::new(
///     Arc::clone(&parent),
///     MemoryConsumer::new("merge workspace"),
/// ));
/// let pool: Arc<dyn MemoryPool> = Arc::clone(&workspace) as _;
/// let reservation = MemoryConsumer::new("merge children").register(&pool);
/// reservation.try_grow(60)?;
/// workspace.retain(60);
/// reservation.free();
///
/// // Another operator can use only the space outside the retained workspace.
/// let contender = MemoryConsumer::new("contender").register(&parent);
/// contender.try_grow(40)?;
/// assert_eq!(parent.reserved(), 100);
///
/// // Children and loans reuse that workspace without another parent grant.
/// let cursor = reservation.new_empty();
/// cursor.try_grow(20)?;
/// let loan = workspace.borrow(60);
/// assert_eq!(loan.size(), 40);
/// workspace.release_unused();
/// assert_eq!(parent.reserved(), 100); // Both the cursor and loan remain charged.
/// drop(loan);
/// assert_eq!(parent.reserved(), 60); // Only the cursor and contender remain.
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct MergeMemoryPool {
    parent: Arc<dyn MemoryPool>,
    state: Mutex<MergeMemoryState>,
}

#[derive(Debug)]
struct MergeMemoryState {
    /// The only reservation charged to the execution pool.
    reservation: MemoryReservation,
    /// Total usage of all reservations and workspace loans in this pool,
    /// including siblings created with `new_empty` or `split`.
    used: usize,
    /// Workspace already acquired for the next merge. It is reusable by any
    /// child reservation, but unavailable to other execution-pool consumers.
    retained: usize,
}

/// A temporary claim on workspace already reserved by a [`MergeMemoryPool`].
///
/// Created by [`MergeMemoryPool::borrow`]. The loan shares capacity with child
/// reservations and keeps the pool alive until it is dropped. Shrinking or
/// dropping the loan returns its bytes to the pool; the parent reservation is
/// reduced only when those bytes are no longer retained as workspace.
#[derive(Debug)]
pub struct WorkspaceLoan {
    pool: Arc<MergeMemoryPool>,
    size: usize,
}

impl WorkspaceLoan {
    /// Returns the number of bytes currently held by this loan.
    pub fn size(&self) -> usize {
        self.size
    }

    /// Returns `size` bytes to the pool.
    ///
    /// # Panics
    ///
    /// Panics if `size` exceeds [`Self::size`].
    pub fn shrink(&mut self, size: usize) {
        self.size = self
            .size
            .checked_sub(size)
            .expect("workspace loan underflow");
        if size != 0 {
            self.pool.release(size);
        }
    }
}

impl Drop for WorkspaceLoan {
    fn drop(&mut self) {
        self.shrink(self.size);
    }
}

impl MergeMemoryPool {
    /// Creates a pool whose memory is charged to `consumer` in `parent`.
    ///
    /// Registers the consumer without reserving any memory. The consumer's
    /// allocation policy applies to the combined usage of all children and loans.
    pub fn new(parent: Arc<dyn MemoryPool>, consumer: MemoryConsumer) -> Self {
        let reservation = consumer.register(&parent);
        Self {
            parent,
            state: Mutex::new(MergeMemoryState {
                reservation,
                used: 0,
                retained: 0,
            }),
        }
    }

    /// Sets the amount of acquired workspace to retain when children release it.
    ///
    /// The workspace must first be acquired through a child reservation. This
    /// method sets the retained floor without changing the parent reservation.
    /// Use [`Self::release_unused`] to stop retaining idle workspace immediately.
    ///
    /// # Panics
    ///
    /// Panics if `size` exceeds this pool's current reservation in the parent,
    /// as reported by its [`MemoryPool::reserved`] method.
    pub fn retain(&self, size: usize) {
        let mut state = self.state.lock();
        assert!(size <= state.reservation.size());
        state.retained = size;
    }

    /// Lends up to `size` unused bytes without acquiring more parent memory.
    ///
    /// Returns a smaller loan if less workspace is available, including an empty
    /// loan if all capacity is in use. The loan is counted alongside child
    /// reservations so they cannot spend the same credit. Dropping it returns
    /// any outstanding bytes.
    pub fn borrow(self: &Arc<Self>, size: usize) -> WorkspaceLoan {
        let mut state = self.state.lock();
        let size = size.min(state.reservation.size() - state.used);
        state.used += size;
        WorkspaceLoan {
            pool: Arc::clone(self),
            size,
        }
    }

    /// Stops retaining idle workspace and returns unused capacity to the parent.
    ///
    /// Live child reservations and loans remain charged. Their future releases
    /// also return memory to the parent, unless [`Self::retain`] is called again.
    pub fn release_unused(&self) {
        let mut state = self.state.lock();
        state.retained = 0;
        state.reservation.resize(state.used);
    }

    fn release(&self, size: usize) {
        let mut state = self.state.lock();
        state.used = state.used.checked_sub(size).expect("memory underflow");
        state.reservation.resize(state.used.max(state.retained));
    }
}

impl Display for MergeMemoryPool {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "MergeMemoryPool")
    }
}

impl MemoryPool for MergeMemoryPool {
    fn name(&self) -> &str {
        "MergeMemoryPool"
    }

    fn grow(&self, _reservation: &MemoryReservation, additional: usize) {
        let mut state = self.state.lock();
        let used = state.used.checked_add(additional).expect("memory overflow");
        if used > state.reservation.size() {
            state.reservation.resize(used);
        }
        state.used = used;
    }

    fn try_grow(
        &self,
        _reservation: &MemoryReservation,
        additional: usize,
    ) -> Result<()> {
        let mut state = self.state.lock();
        let Some(used) = state.used.checked_add(additional) else {
            return resources_err!("Sort merge memory reservation overflow");
        };
        if used > state.reservation.size() {
            state.reservation.try_resize(used)?;
        }
        state.used = used;
        Ok(())
    }

    fn shrink(&self, _reservation: &MemoryReservation, subtractive: usize) {
        self.release(subtractive);
    }

    fn reserved(&self) -> usize {
        self.state.lock().reservation.size()
    }

    fn memory_limit(&self) -> MemoryLimit {
        self.parent.memory_limit()
    }
}

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

    fn reservation(
        parent: &Arc<dyn MemoryPool>,
    ) -> (Arc<MergeMemoryPool>, MemoryReservation) {
        let pool = Arc::new(MergeMemoryPool::new(
            Arc::clone(parent),
            MemoryConsumer::new("merge workspace"),
        ));
        let reservation = MemoryConsumer::new("merge children")
            .register(&(Arc::clone(&pool) as Arc<dyn MemoryPool>));
        (pool, reservation)
    }

    #[test]
    fn reuses_workspace_across_child_reservations() -> Result<()> {
        let parent: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(100));
        let (pool, reservation) = reservation(&parent);
        reservation.try_grow(60)?;
        pool.retain(60);
        reservation.free();

        let contender = MemoryConsumer::new("contender").register(&parent);
        contender.try_grow(40)?;
        assert_eq!(parent.reserved(), 100);

        let cursor = reservation.new_empty();
        let rows = reservation.new_empty();
        cursor.try_grow(20)?;
        rows.try_grow(40)?;
        assert!(rows.try_grow(1).is_err());
        assert_eq!(rows.size(), 40);
        assert_eq!(parent.reserved(), 100);

        drop(cursor);
        drop(rows);
        assert_eq!(pool.reserved(), 60);
        reservation.try_grow(60)?; // Workspace for the next spill needs no new grant.
        assert_eq!(parent.reserved(), 100);

        drop(reservation);
        drop(pool);
        assert_eq!(parent.reserved(), 40);
        drop(contender);
        assert_eq!(parent.reserved(), 0);
        Ok(())
    }

    #[test]
    fn child_keeps_workspace_alive_after_sorter_drops() -> Result<()> {
        let parent: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(100));
        let (pool, mut reservation) = reservation(&parent);
        reservation.try_grow(60)?;
        pool.retain(60);
        let child = reservation.take();
        let sibling = child.split(20);
        drop(reservation);
        drop(pool);
        assert_eq!(parent.reserved(), 60);

        child.free();
        assert_eq!(parent.reserved(), 60);
        child.try_grow(40)?;
        drop(child);
        assert_eq!(parent.reserved(), 60);
        drop(sibling);
        assert_eq!(parent.reserved(), 0);
        Ok(())
    }

    #[test]
    fn releases_excess_capacity_but_retains_workspace() -> Result<()> {
        let parent: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(100));
        let (pool, reservation) = reservation(&parent);
        reservation.try_grow(40)?;
        pool.retain(40);
        reservation.try_grow(50)?;
        assert_eq!(parent.reserved(), 90);
        reservation.shrink(60);
        assert_eq!(parent.reserved(), 40);
        let loan = pool.borrow(10);
        assert_eq!(loan.size(), 10);
        pool.release_unused();
        assert_eq!(parent.reserved(), 40); // Live loans remain charged.
        drop(loan);
        assert_eq!(parent.reserved(), 30);
        drop(reservation);
        assert_eq!(parent.reserved(), 0);
        Ok(())
    }

    #[test]
    fn does_not_reserve_workspace_implicitly() -> Result<()> {
        let parent: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(8));
        let (pool, reservation) = reservation(&parent);
        reservation.grow(0);
        reservation.try_grow(0)?;
        assert_eq!(parent.reserved(), 0);
        reservation.try_grow(8)?;
        assert!(reservation.try_grow(1).is_err());
        assert!(reservation.try_grow(usize::MAX).is_err());
        assert_eq!(reservation.size(), 8);
        assert_eq!(pool.reserved(), 8);
        reservation.free();
        assert_eq!(parent.reserved(), 0);
        Ok(())
    }

    #[test]
    fn delegates_infallible_growth_to_parent() {
        let parent: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(8));
        let (pool, reservation) = reservation(&parent);
        reservation.grow(16);
        assert_eq!(parent.reserved(), 16);
        assert_eq!(pool.reserved(), 16);
        drop(reservation);
        assert_eq!(parent.reserved(), 0);
    }

    #[test]
    fn workspace_loans_share_credit_with_cursors() -> Result<()> {
        let parent: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(100));
        let (pool, reservation) = reservation(&parent);
        reservation.try_grow(60)?;
        pool.retain(60);
        reservation.free();
        let contender = MemoryConsumer::new("contender").register(&parent);
        contender.try_grow(40)?;
        let cursor = reservation.new_empty();
        cursor.try_grow(20)?;

        // Both loans stay alive until after both requests complete. Together they
        // may claim only the 40 bytes not already assigned to the cursor.
        let gate = std::sync::Barrier::new(2);
        let (mut first, second) = std::thread::scope(|scope| {
            let first = scope.spawn(|| {
                gate.wait();
                pool.borrow(30)
            });
            let second = scope.spawn(|| {
                gate.wait();
                pool.borrow(30)
            });
            (first.join().unwrap(), second.join().unwrap())
        });
        assert_eq!(first.size() + second.size(), 40);
        assert!(matches!((first.size(), second.size()), (30, 10) | (10, 30)));
        assert_eq!(pool.borrow(1).size(), 0);
        assert_eq!(parent.reserved(), 100);

        first.shrink(5);
        cursor.try_grow(5)?;
        assert_eq!(first.size() + second.size() + cursor.size(), 60);
        assert_eq!(pool.borrow(1).size(), 0);
        assert!(cursor.try_grow(1).is_err());
        assert_eq!(cursor.size(), 25);
        assert_eq!(parent.reserved(), 100);

        let returned = first.size();
        drop(first);
        let replacement = pool.borrow(usize::MAX);
        assert_eq!(replacement.size(), returned);
        assert_eq!(parent.reserved(), 100);

        // The final loan owns the workspace even after the sorter and its child
        // reservations are gone; dropping it releases the parent allocation once.
        drop(reservation);
        drop(pool);
        drop(cursor);
        drop(second);
        assert_eq!(parent.reserved(), 100);
        drop(replacement);
        assert_eq!(parent.reserved(), 40);
        drop(contender);
        assert_eq!(parent.reserved(), 0);
        Ok(())
    }

    #[test]
    fn workspace_loan_preserves_ordinary_consumer_fair_share() -> Result<()> {
        use crate::memory_pool::{FairSpillPool, TrackConsumersPool};
        use std::num::NonZeroUsize;

        let tracked = Arc::new(TrackConsumersPool::new(
            FairSpillPool::new(100),
            NonZeroUsize::new(3).unwrap(),
        ));
        let parent: Arc<dyn MemoryPool> = Arc::clone(&tracked) as Arc<dyn MemoryPool>;
        let (pool, reservation) = reservation(&parent);
        reservation.try_grow(20)?;
        pool.retain(20);
        reservation.free();
        let ordinary = MemoryConsumer::new("ordinary sort")
            .with_can_spill(true)
            .register(&parent);
        let contender = MemoryConsumer::new("other spillable sort")
            .with_can_spill(true)
            .register(&parent);
        ordinary.try_grow(40)?;
        assert_eq!(parent.reserved(), 60);

        let snapshot = || {
            let mut metrics = tracked
                .metrics()
                .into_iter()
                .map(|m| (m.name, m.can_spill, m.reserved, m.peak))
                .collect::<Vec<_>>();
            metrics.sort_unstable();
            metrics
        };
        let before = snapshot();
        assert_eq!(before.len(), 3);

        // Although 40 bytes remain globally free, the ordinary consumer's share
        // is (100 - 20) / 2 = 40. Only the acquired 20 bytes may be borrowed;
        // the remaining 5 must be denied under the original consumer's policy.
        let result: Result<()> = (|| {
            let sorted_size = 65;
            let loan = pool.borrow(sorted_size - ordinary.size());
            assert_eq!(loan.size(), 20);
            ordinary.try_resize(sorted_size - loan.size())?;
            Ok(())
        })();
        assert!(result.is_err());
        assert_eq!(ordinary.size(), 40);
        assert_eq!(parent.reserved(), 60);
        assert_eq!(snapshot(), before);

        // Failed resize returned its loan, so a smaller expansion can now use
        // existing credit without a new grant or a different parent consumer.
        let loan = pool.borrow(55 - ordinary.size());
        assert_eq!(loan.size(), 15);
        ordinary.try_resize(55 - loan.size())?;
        assert_eq!(ordinary.size(), 40);
        assert_eq!(parent.reserved(), 60);
        assert_eq!(snapshot(), before);

        drop(loan);
        drop(ordinary);
        drop(contender);
        drop(reservation);
        drop(pool);
        assert_eq!(parent.reserved(), 0);
        assert!(tracked.metrics().is_empty());
        Ok(())
    }

    #[test]
    fn failed_sorted_resize_returns_workspace_loan() -> Result<()> {
        let parent: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(100));
        let (pool, reservation) = reservation(&parent);
        assert_eq!(pool.borrow(usize::MAX).size(), 0);
        assert_eq!(parent.reserved(), 0);
        reservation.try_grow(60)?;
        pool.retain(60);
        reservation.free();
        let ordinary = MemoryConsumer::new("ordinary sort")
            .with_can_spill(true)
            .register(&parent);
        ordinary.try_grow(40)?;

        let result: Result<()> = (|| {
            let sorted_size = 120;
            let loan = pool.borrow(sorted_size - ordinary.size());
            assert_eq!(loan.size(), 60);
            ordinary.try_resize(sorted_size - loan.size())?;
            Ok(())
        })();
        assert!(result.is_err());
        assert_eq!(ordinary.size(), 40);
        assert_eq!(parent.reserved(), 100);
        let restored = pool.borrow(usize::MAX);
        assert_eq!(restored.size(), 60);
        assert_eq!(parent.reserved(), 100);

        drop(restored);
        drop(ordinary);
        drop(reservation);
        drop(pool);
        assert_eq!(parent.reserved(), 0);
        Ok(())
    }
}