delta-arrow-reader 0.5.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Delta Kernel data-file reader.

use std::sync::Arc;

use arrow::record_batch::RecordBatch;
use delta_kernel::{FileMeta, engine::arrow_data::EngineDataArrowExt};
use futures_util::stream;
use snafu::{IntoError, ResultExt};
use tokio::{sync::mpsc, task::JoinHandle};

use crate::{
    DeltaReaderError,
    error::{CancelledSnafu, DataFileReadSnafu, PhysicalToLogicalTransformSnafu},
    reader::{
        deletion_vector::load_deletion_vector_masker_blocking,
        planning::{DeltaScanFileTask, DeltaScanPlan},
        scheduling::{FileBatchStream, FileExecutor, FileReadPermit, ScanCancellation},
        transform::align_batch_to_logical_schema,
    },
};

struct DeltaKernelFileStreamState {
    receiver: mpsc::Receiver<RecordBatch>,
    task: Option<JoinHandle<Result<(), DeltaReaderError>>>,
}

impl Drop for DeltaKernelFileStreamState {
    fn drop(&mut self) {
        self.receiver.close();
        if let Some(task) = self.task.as_ref() {
            task.abort();
        }
    }
}

pub(crate) fn delta_kernel_file_executor(
    plan: &Arc<DeltaScanPlan>,
) -> FileExecutor<DeltaScanFileTask, FileBatchStream> {
    let plan = Arc::clone(plan);
    let output_buffer_batches = plan.execution_options.output_buffer_batches_per_partition();

    Arc::new(move |task, permit, cancellation| {
        let plan = Arc::clone(&plan);
        Box::pin(async move {
            if cancellation.is_cancelled() {
                return CancelledSnafu {
                    reason: "scan_execution_cancelled",
                }
                .fail();
            }
            Ok(spawn_blocking_file_stream(
                output_buffer_batches,
                permit,
                cancellation,
                move |output, cancellation| read_file(plan.as_ref(), task, output, &cancellation),
            ))
        })
    })
}

fn read_file(
    plan: &DeltaScanPlan,
    task: DeltaScanFileTask,
    output: mpsc::Sender<RecordBatch>,
    cancellation: &ScanCancellation,
) -> Result<(), DeltaReaderError> {
    if cancellation.is_cancelled() {
        return Ok(());
    }
    let DeltaScanFileTask {
        path,
        file_size,
        modification_time_ms,
        deletion_vector,
        transform,
        ..
    } = task;
    let physical_predicate = if deletion_vector.is_present() {
        None
    } else {
        plan.physical_predicate
            .as_ref()
            .map(|predicate| predicate.as_ref().clone())
    };
    let mut deletion_vector = load_deletion_vector_masker_blocking(
        plan.engine_context.as_ref(),
        deletion_vector,
        &plan.metrics,
    )?;

    if cancellation.is_cancelled() {
        return Ok(());
    }
    let size = file_size.ok_or_else(|| {
        data_file_error(
            "data_file_size_missing",
            delta_kernel::Error::generic("file size is required for Delta Kernel reads"),
        )
    })?;
    let modification_time_ms = modification_time_ms.ok_or_else(|| {
        data_file_error(
            "data_file_modification_time_missing",
            delta_kernel::Error::generic(
                "file modification time is required for Delta Kernel reads",
            ),
        )
    })?;
    let location = plan
        .engine_context
        .table_url()
        .join(&path)
        .boxed()
        .context(DataFileReadSnafu {
            reason: "data_file_path_resolution_failed",
        })?;
    let metadata = FileMeta::new(location, modification_time_ms, size);
    let batches = plan
        .engine_context
        .engine()
        .parquet_handler()
        .read_parquet_files(
            std::slice::from_ref(&metadata),
            plan.kernel_schemas.physical(),
            physical_predicate,
        )
        .boxed()
        .context(DataFileReadSnafu {
            reason: "parquet_read_setup_failed",
        })?;

    for batch in batches {
        if cancellation.is_cancelled() {
            return Ok(());
        }
        let batch = batch.boxed().context(DataFileReadSnafu {
            reason: "parquet_batch_read_failed",
        })?;
        let batch = EngineDataArrowExt::try_into_record_batch(batch)
            .boxed()
            .context(DataFileReadSnafu {
                reason: "parquet_arrow_conversion_failed",
            })?;
        let batch = transform
            .apply(plan.engine_context.as_ref(), &plan.kernel_schemas, batch)
            .boxed()
            .context(PhysicalToLogicalTransformSnafu {
                reason: "physical_to_logical_transform_failed",
            })?;
        let batch = align_batch_to_logical_schema(
            batch,
            &plan.logical_schema,
            "Delta Kernel output does not match the planned logical schema",
        )?;
        let batch = match deletion_vector.as_mut() {
            Some(deletion_vector) => deletion_vector.mask_ordered_batch(batch)?,
            None => batch,
        };
        if cancellation.is_cancelled() || output.blocking_send(batch).is_err() {
            return Ok(());
        }
    }

    if let Some(deletion_vector) = deletion_vector.as_mut() {
        deletion_vector.finish()?;
    }
    Ok(())
}

fn spawn_blocking_file_stream(
    output_buffer_batches: usize,
    permit: FileReadPermit,
    cancellation: ScanCancellation,
    producer: impl FnOnce(mpsc::Sender<RecordBatch>, ScanCancellation) -> Result<(), DeltaReaderError>
    + Send
    + 'static,
) -> FileBatchStream {
    let (output, receiver) = mpsc::channel(output_buffer_batches);
    let task = tokio::task::spawn_blocking(move || {
        let _permit = permit;
        producer(output, cancellation)
    });
    let state = DeltaKernelFileStreamState {
        receiver,
        task: Some(task),
    };

    Box::pin(stream::unfold(state, |mut state| async move {
        if let Some(batch) = state.receiver.recv().await {
            return Some((Ok(batch), state));
        }
        let result = state.task.as_mut()?.await;
        state.task.take();
        let error = match result {
            Ok(Ok(())) => return None,
            Ok(Err(error)) => error,
            Err(source) => data_file_error("delta_kernel_task_failed", source),
        };
        Some((Err(error), state))
    }))
}

fn data_file_error(
    reason: &'static str,
    source: impl std::error::Error + Send + Sync + 'static,
) -> DeltaReaderError {
    DataFileReadSnafu { reason }.into_error(Box::new(source))
}

#[cfg(test)]
mod tests {
    use std::{
        error::Error as _,
        sync::{
            Arc,
            atomic::{AtomicBool, AtomicUsize, Ordering},
        },
        time::Duration,
    };

    use arrow::{
        array::{Array, Int32Array},
        datatypes::{DataType, Field, Schema},
        record_batch::RecordBatch,
    };
    use futures_util::StreamExt;
    use tokio::time::timeout;

    use super::spawn_blocking_file_stream;
    use crate::{
        DeltaScanExecutionOptions, ParquetReaderBackend,
        reader::scheduling::{ScanCancellation, ScanReadLimiter},
    };

    fn options() -> Result<DeltaScanExecutionOptions, crate::DeltaReaderError> {
        Ok(DeltaScanExecutionOptions::new()
            .with_prefetch_files_per_partition(0)
            .with_max_concurrent_file_reads_per_partition(1)?
            .with_max_concurrent_file_reads_per_scan(Some(1))?
            .with_parquet_backend(ParquetReaderBackend::DeltaKernel))
    }

    fn batch(id: i32) -> Result<RecordBatch, arrow::error::ArrowError> {
        RecordBatch::try_new(
            Arc::new(Schema::new(vec![Field::new("id", DataType::Int32, false)])),
            vec![Arc::new(Int32Array::from(vec![id]))],
        )
    }

    fn batch_id(batch: &RecordBatch) -> Result<i32, &'static str> {
        batch
            .column(0)
            .as_any()
            .downcast_ref::<Int32Array>()
            .map(|ids| ids.value(0))
            .ok_or("expected Int32 id")
    }

    #[tokio::test]
    async fn blocking_handoff_is_bounded_ordered_and_releases_its_permit()
    -> Result<(), Box<dyn std::error::Error>> {
        let limiter = ScanReadLimiter::new(options()?, 1, 1);
        let permit = limiter.partition(0)?.acquire().await?;
        let sent = Arc::new(AtomicUsize::new(0));
        let producer_sent = Arc::clone(&sent);
        let mut stream =
            spawn_blocking_file_stream(1, permit, ScanCancellation::new(), move |output, _| {
                for id in 1..=3 {
                    output
                        .blocking_send(
                            batch(id).map_err(|error| {
                                super::data_file_error("test_batch_failed", error)
                            })?,
                        )
                        .map_err(|_| {
                            super::data_file_error(
                                "test_receiver_closed",
                                std::io::Error::other("receiver closed"),
                            )
                        })?;
                    producer_sent.fetch_add(1, Ordering::SeqCst);
                }
                Ok(())
            });

        timeout(Duration::from_secs(5), async {
            while sent.load(Ordering::SeqCst) != 1 {
                tokio::task::yield_now().await;
            }
        })
        .await?;
        assert_eq!(limiter.active_file_reads(), 1);

        let mut ids = Vec::new();
        while let Some(batch) = stream.next().await {
            ids.push(batch_id(&batch?)?);
        }
        assert_eq!(ids, [1, 2, 3]);
        assert_eq!(sent.load(Ordering::SeqCst), 3);
        assert_eq!(limiter.active_file_reads(), 0);
        Ok(())
    }

    #[tokio::test]
    async fn dropping_handoff_stops_at_the_next_send_boundary()
    -> Result<(), Box<dyn std::error::Error>> {
        let limiter = ScanReadLimiter::new(options()?, 1, 1);
        let permit = limiter.partition(0)?.acquire().await?;
        let sent = Arc::new(AtomicUsize::new(0));
        let finished = Arc::new(AtomicBool::new(false));
        let producer_sent = Arc::clone(&sent);
        let producer_finished = Arc::clone(&finished);
        let stream =
            spawn_blocking_file_stream(1, permit, ScanCancellation::new(), move |output, _| {
                for id in 1..=3 {
                    if output
                        .blocking_send(
                            batch(id).map_err(|error| {
                                super::data_file_error("test_batch_failed", error)
                            })?,
                        )
                        .is_err()
                    {
                        break;
                    }
                    producer_sent.fetch_add(1, Ordering::SeqCst);
                }
                producer_finished.store(true, Ordering::SeqCst);
                Ok(())
            });

        timeout(Duration::from_secs(5), async {
            while sent.load(Ordering::SeqCst) != 1 {
                tokio::task::yield_now().await;
            }
        })
        .await?;
        drop(stream);
        timeout(Duration::from_secs(5), async {
            while !finished.load(Ordering::SeqCst) || limiter.active_file_reads() != 0 {
                tokio::task::yield_now().await;
            }
        })
        .await?;

        assert_eq!(sent.load(Ordering::SeqCst), 1);
        assert_eq!(limiter.active_file_reads(), 0);
        Ok(())
    }

    #[tokio::test]
    async fn producer_error_and_panic_reach_the_stream_once()
    -> Result<(), Box<dyn std::error::Error>> {
        let limiter = ScanReadLimiter::new(options()?, 1, 1);
        let permit = limiter.partition(0)?.acquire().await?;
        let mut failed =
            spawn_blocking_file_stream(1, permit, ScanCancellation::new(), |output, _| {
                output
                    .blocking_send(
                        batch(1)
                            .map_err(|error| super::data_file_error("test_batch_failed", error))?,
                    )
                    .map_err(|_| {
                        super::data_file_error(
                            "test_receiver_closed",
                            std::io::Error::other("receiver closed"),
                        )
                    })?;
                Err(super::data_file_error(
                    "injected_reader_failure",
                    std::io::Error::other("sensitive injected detail"),
                ))
            });
        let first = failed.next().await.ok_or("expected buffered batch")??;
        assert_eq!(batch_id(&first)?, 1);
        let error = failed
            .next()
            .await
            .ok_or("expected producer error")?
            .expect_err("producer must fail");
        assert_eq!(error.code(), "data_file_read");
        assert!(!error.to_string().contains("sensitive"));
        assert!(failed.next().await.is_none());

        let permit = limiter.partition(0)?.acquire().await?;
        let mut panicked = spawn_blocking_file_stream(
            1,
            permit,
            ScanCancellation::new(),
            |_, _| -> Result<(), crate::DeltaReaderError> { panic!("injected panic") },
        );
        let error = panicked
            .next()
            .await
            .ok_or("expected panic error")?
            .expect_err("panic must reach the stream");
        assert_eq!(error.code(), "data_file_read");
        assert!(
            error
                .source()
                .is_some_and(|source| source.downcast_ref::<tokio::task::JoinError>().is_some())
        );
        assert!(panicked.next().await.is_none());
        assert_eq!(limiter.active_file_reads(), 0);
        Ok(())
    }

    #[test]
    fn delta_kernel_boundary_adds_no_runtime_or_backend_infrastructure()
    -> Result<(), Box<dyn std::error::Error>> {
        let source = include_str!("kernel_reader.rs");
        let manifest = include_str!("../../../Cargo.toml");
        for forbidden in [
            concat!("Runtime", "::new"),
            concat!("new_", "current_thread"),
            concat!("new_", "multi_thread"),
            concat!("unbounded_", "channel"),
            concat!("std", "::thread::spawn"),
            concat!("tracing_", "subscriber"),
            concat!("data", "fusion"),
            concat!("delta", "_funnel"),
        ] {
            assert!(!source.contains(forbidden), "unexpected {forbidden}");
        }
        let blocking_boundary = concat!("tokio::task::", "spawn_blocking(");
        assert_eq!(source.matches(blocking_boundary).count(), 1);
        assert!(!manifest.contains("deltalake"));
        assert!(!manifest.contains("buoyant_kernel"));
        Ok(())
    }
}