delta-arrow-reader 0.7.0

Read-only Delta Lake to Apache Arrow reader
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
//! Reuses numeric fallback estimates and bounds their CPU work across queries.

use std::{
    collections::VecDeque,
    sync::{Arc, LazyLock, Mutex},
};

use tokio::{sync::Semaphore, time::Instant};

use super::fallback_cost::{FallbackCost, FallbackPlan, TRIALS, sample_costs};
use crate::reader::backend::direct_parquet::network_profile::RangeReadCalibration;

const CACHE_ENTRIES: usize = 128;
static WORK_SLOT: LazyLock<Arc<Semaphore>> = LazyLock::new(|| Arc::new(Semaphore::new(1)));

pub(in super::super) struct FallbackEstimator {
    calibration: Arc<RangeReadCalibration>,
    workers: usize,
    work_slot: Arc<Semaphore>,
    // A calibration owns its cache; file offsets also belong in the key because they seed draws.
    cache: Mutex<VecDeque<(Arc<FallbackPlan>, FallbackCost)>>,
}

impl FallbackEstimator {
    pub(in super::super) fn new(calibration: Arc<RangeReadCalibration>) -> Arc<Self> {
        tracing::trace!(target: "delta_arrow_reader::diagnostics::intra_page",
            bytes_per_second = calibration.bytes_per_second,
            ordinary_latencies = ?calibration.ordinary_request_latencies,
            partial_latencies = ?calibration.partial_request_latencies,
            partial_durations = ?calibration.partial_request_durations,
            small_reads = ?calibration.small_reads,
            "Variable-length cost calibration");
        Arc::new(Self {
            calibration,
            workers: std::thread::available_parallelism().map_or(1, |count| count.get().min(4)),
            work_slot: Arc::clone(&WORK_SLOT),
            cache: Mutex::new(VecDeque::new()),
        })
    }

    pub(in super::super) fn calibration(&self) -> &Arc<RangeReadCalibration> {
        &self.calibration
    }

    fn cached(&self, plan: &FallbackPlan) -> Option<FallbackCost> {
        self.cache
            .lock()
            .ok()?
            .iter()
            .find_map(|(key, cost)| (key.as_ref() == plan).then_some(*cost))
    }

    /// Estimates numeric costs, reusing cached results for the same calibration and plan.
    /// The caller bounds waiting; cancelled jobs retain capacity until they finish.
    pub(in super::super) async fn estimate(
        self: &Arc<Self>,
        plan: FallbackPlan,
        deadline: Instant,
    ) -> Option<FallbackCost> {
        if let Some(cost) = self.cached(&plan) {
            return Some(cost);
        }
        let permit = Arc::new(self.work_slot.clone().acquire_owned().await.ok()?);
        if Instant::now() >= deadline {
            return None;
        }
        if let Some(cost) = self.cached(&plan) {
            return Some(cost);
        }
        let plan = Arc::new(plan);
        let mut jobs = Vec::with_capacity(self.workers);
        for worker in 0..self.workers {
            let calibration = Arc::clone(&self.calibration);
            let plan = Arc::clone(&plan);
            let permit = Arc::clone(&permit);
            let trials = worker * TRIALS / self.workers..(worker + 1) * TRIALS / self.workers;
            jobs.push(tokio::task::spawn_blocking(move || {
                // The jobs, not the awaiting query, own capacity during cancellation or failure.
                let _permit = permit;
                if Instant::now() >= deadline {
                    return None;
                }
                sample_costs(&calibration, &plan, trials)
            }));
        }
        let mut trials = Vec::with_capacity(TRIALS);
        for job in jobs {
            trials.extend(job.await.ok()??);
        }
        let cost = FallbackCost::from_trials(trials)?;
        if let Ok(mut cache) = self.cache.lock() {
            if cache.len() == CACHE_ENTRIES {
                cache.pop_front();
            }
            cache.push_back((plan, cost));
        }
        Some(cost)
    }
}

#[cfg(test)]
#[expect(
    clippy::single_range_in_vec_init,
    reason = "These fixtures are lists of byte ranges, not lists of row numbers."
)]
mod tests {
    use std::{sync::mpsc, time::Duration};

    use super::*;
    use crate::reader::backend::direct_parquet::intra_page::{
        discovery_allowance, remaining_discovery_time,
    };

    fn estimator() -> Arc<FallbackEstimator> {
        let calibration = Arc::new(RangeReadCalibration {
            bytes_per_second: 1,
            ordinary_request_latencies: vec![Duration::from_secs(1)].into(),
            small_reads: None,
            partial_request_latencies: vec![Duration::from_millis(10)].into(),
            partial_request_durations: vec![Duration::from_millis(20)].into(),
        });
        let mut estimator = FallbackEstimator::new(calibration);
        let unique = Arc::get_mut(&mut estimator).unwrap();
        unique.workers = 4;
        unique.work_slot = Arc::new(Semaphore::new(1));
        estimator
    }

    fn plan(offset: u64) -> FallbackPlan {
        FallbackPlan::new(&[offset..offset + 1000], &[offset..offset + 100]).unwrap()
    }

    async fn allowance(estimator: &Arc<FallbackEstimator>, offset: u64) -> Option<Duration> {
        let started = Instant::now();
        remaining_discovery_time(
            estimator,
            plan(offset),
            started,
            started + Duration::from_secs(5),
        )
        .await
    }

    #[tokio::test]
    async fn completed_probe_timings_cannot_reuse_an_unobserved_budget() {
        let estimator = estimator();
        assert!(allowance(&estimator, 0).await.is_some());
        let mut observed = plan(0);
        observed.record_probe_time(Some(Duration::from_secs(200)));
        let started = Instant::now();
        assert!(
            remaining_discovery_time(
                &estimator,
                observed.clone(),
                started,
                started + Duration::from_secs(5)
            )
            .await
            .is_none()
        );
        assert!(estimator.cached(&observed).is_some());
        assert!(allowance(&estimator, 0).await.is_some());
    }

    async fn occupy_blocking_thread() -> mpsc::Sender<()> {
        let (release, wait) = mpsc::channel();
        let (ready, started) = tokio::sync::oneshot::channel();
        tokio::task::spawn_blocking(move || {
            let _ = ready.send(());
            let _ = wait.recv();
        });
        started.await.unwrap();
        release
    }

    async fn wait_until_dispatched(estimator: &FallbackEstimator) {
        tokio::time::timeout(Duration::from_secs(5), async {
            while estimator.work_slot.available_permits() != 0 {
                tokio::task::yield_now().await;
            }
        })
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn cached_estimate_charges_each_calls_own_decision_time() {
        let estimator = estimator();
        let expected = FallbackCost::from_trials(
            sample_costs(&estimator.calibration, &plan(0), 0..TRIALS).unwrap(),
        )
        .unwrap();
        assert!(allowance(&estimator, 0).await.is_some());
        assert_eq!(estimator.cached(&plan(0)), Some(expected));

        let _held = estimator.work_slot.acquire().await.unwrap();
        let before = Instant::now();
        let started = before - Duration::from_secs(10);
        let actual = remaining_discovery_time(
            &estimator,
            plan(0),
            started,
            before + Duration::from_secs(5),
        )
        .await
        .unwrap();
        assert!(actual <= discovery_allowance(expected, before - started).unwrap());
        assert!(actual >= discovery_allowance(expected, Instant::now() - started).unwrap());
        assert!(
            remaining_discovery_time(
                &estimator,
                plan(0),
                before - Duration::from_secs(60),
                before + Duration::from_secs(5),
            )
            .await
            .is_none()
        );
        assert!(
            remaining_discovery_time(&estimator, plan(0), before, before)
                .await
                .is_none()
        );
        assert!(
            remaining_discovery_time(
                &estimator,
                plan(0),
                before + Duration::from_secs(60),
                before + Duration::from_secs(120),
            )
            .await
            .is_none()
        );
    }

    #[tokio::test]
    async fn parallel_estimate_matches_serial_trials_with_latency_variation() {
        let mut estimator = estimator();
        Arc::get_mut(&mut estimator).unwrap().calibration = Arc::new(RangeReadCalibration {
            bytes_per_second: 18_750_000,
            ordinary_request_latencies: [5, 20, 400].map(Duration::from_millis).into(),
            small_reads: None,
            partial_request_latencies: [8, 30, 900].map(Duration::from_millis).into(),
            partial_request_durations: [4008, 4030, 4900].map(Duration::from_millis).into(),
        });
        let plan = || {
            FallbackPlan::new(
                &[0..120_000, 200_000..300_000, 500_000..900_000],
                &[0..8192, 550_000..558_192],
            )
            .unwrap()
        };
        let expected = FallbackCost::from_trials(
            sample_costs(&estimator.calibration, &plan(), 0..TRIALS).unwrap(),
        )
        .unwrap();
        let started = Instant::now();
        assert!(
            remaining_discovery_time(
                &estimator,
                plan(),
                started,
                started + Duration::from_secs(5)
            )
            .await
            .is_none(),
            "the complete fallback exceeds the allowance"
        );
        assert_eq!(estimator.cached(&plan()), Some(expected));
    }

    #[tokio::test]
    async fn separate_estimators_share_the_process_work_limit() {
        let profile = estimator().calibration.clone();
        let first = FallbackEstimator::new(profile.clone());
        let second = FallbackEstimator::new(profile);
        let held = first.work_slot.acquire().await.unwrap();
        let started = Instant::now();
        assert!(
            remaining_discovery_time(
                &second,
                plan(0),
                started,
                started + Duration::from_millis(5)
            )
            .await
            .is_none()
        );
        assert!(second.cache.lock().unwrap().is_empty());
        drop(held);
        assert!(allowance(&second, 0).await.is_some());
    }

    #[tokio::test]
    async fn cache_keys_keep_offsets_probe_boundaries_and_calibrations_distinct() {
        let first = estimator();
        let second = estimator();
        assert!(allowance(&first, 0).await.is_some());
        let _first_held = first.work_slot.acquire().await.unwrap();
        let _second_held = second.work_slot.acquire().await.unwrap();
        for (estimator, plan) in [
            (&first, plan(1)),
            (&first, FallbackPlan::new(&[0..1000], &[1..101]).unwrap()),
            (
                &first,
                FallbackPlan::new(&[0..100, 100..1000], &[0..100]).unwrap(),
            ),
            (&second, plan(0)),
        ] {
            let started = Instant::now();
            assert!(
                remaining_discovery_time(
                    estimator,
                    plan,
                    started,
                    started + Duration::from_millis(5)
                )
                .await
                .is_none()
            );
        }
        assert_eq!(first.cache.lock().unwrap().len(), 1);
        assert!(second.cache.lock().unwrap().is_empty());
    }

    #[tokio::test]
    async fn cache_evicts_old_geometry_without_losing_recent_results() {
        let estimator = estimator();
        for offset in 0..=CACHE_ENTRIES as u64 {
            assert!(allowance(&estimator, offset).await.is_some());
        }
        assert_eq!(estimator.cache.lock().unwrap().len(), CACHE_ENTRIES);
        assert!(estimator.cached(&plan(0)).is_none());
        assert!(estimator.cached(&plan(CACHE_ENTRIES as u64)).is_some());
    }

    #[test]
    fn cancelled_query_keeps_capacity_while_jobs_are_queued() {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .max_blocking_threads(1)
            .build()
            .unwrap();
        runtime.block_on(async {
            let estimator = estimator();
            let release = occupy_blocking_thread().await;
            let caller = Arc::clone(&estimator);
            let waiting = tokio::spawn(async move { allowance(&caller, 0).await });
            wait_until_dispatched(&estimator).await;
            waiting.abort();
            assert!(waiting.await.unwrap_err().is_cancelled());
            assert_eq!(estimator.work_slot.available_permits(), 0);
            assert!(estimator.cache.lock().unwrap().is_empty());

            drop(release);
            let available =
                tokio::time::timeout(Duration::from_secs(5), estimator.work_slot.acquire())
                    .await
                    .unwrap()
                    .unwrap();
            drop(available);
            assert!(allowance(&estimator, 0).await.is_some());
        });
    }

    #[test]
    fn timed_out_query_keeps_capacity_until_queued_jobs_can_exit() {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .max_blocking_threads(1)
            .build()
            .unwrap();
        runtime.block_on(async {
            let estimator = estimator();
            let release = occupy_blocking_thread().await;
            let started = Instant::now();
            assert!(
                remaining_discovery_time(
                    &estimator,
                    plan(0),
                    started,
                    started + Duration::from_millis(50)
                )
                .await
                .is_none()
            );
            assert_eq!(estimator.work_slot.available_permits(), 0);
            assert!(estimator.cache.lock().unwrap().is_empty());

            drop(release);
            let available =
                tokio::time::timeout(Duration::from_secs(5), estimator.work_slot.acquire())
                    .await
                    .unwrap()
                    .unwrap();
            drop(available);
            assert!(allowance(&estimator, 0).await.is_some());
        });
    }

    #[tokio::test]
    async fn failed_estimates_release_capacity_without_caching_partial_results() {
        let mut estimator = estimator();
        let state = Arc::get_mut(&mut estimator).unwrap();
        Arc::get_mut(&mut state.calibration)
            .unwrap()
            .bytes_per_second = 0;
        assert!(allowance(&estimator, 0).await.is_none());
        let available = tokio::time::timeout(Duration::from_secs(5), estimator.work_slot.acquire())
            .await
            .unwrap()
            .unwrap();
        assert!(estimator.cache.lock().unwrap().is_empty());
        drop(available);
    }
}