delta-arrow-reader 0.4.1

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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
//! Optional DataFusion table-provider and registration surface.

mod dynamic_filters;
mod dynamic_partition_pruning;
mod execution;
mod planning;

pub use execution::{
    IntraFileRepartitioning, ScanMetrics, ScanMetricsSnapshot, collect_scan_metrics,
};

use std::{collections::HashSet, fmt, sync::Arc};

use arrow::datatypes::{DataType, Schema, SchemaRef};
use async_trait::async_trait;
use datafusion::{
    catalog::Session,
    common::{DataFusionError, Result as DataFusionResult},
    datasource::{TableProvider, TableType, physical_plan::wrap_partition_type_in_dict},
    execution::context::SessionContext,
    logical_expr::{Expr, TableProviderFilterPushDown},
    physical_plan::ExecutionPlan,
};

use self::{
    execution::create_datafusion_execution_plan,
    planning::{FilterCapabilities, plan_datafusion_filters, plan_datafusion_scan},
};

use crate::{
    DeltaReaderError, DeltaScanExecutionOptions, DeltaTable, ParquetReaderBackend,
    delta::kernel::kernel_pruning_predicate,
    reader::planning::{DeltaScanPartitionTargetOptions, build_physical_row_predicate, plan_scan},
    reader::transform::schema_with_view_types,
};

const TRACING_TARGET: &str = "delta_arrow_reader::datafusion";

/// DataFusion-specific scan settings for one provider.
#[must_use = "scan options do nothing unless passed to a provider"]
#[derive(Debug, Clone)]
pub struct ScanOptions {
    /// Reader execution settings used by each provider scan.
    pub execution_options: DeltaScanExecutionOptions,
    /// Optional explicit scan partition target.
    pub target_partitions: Option<usize>,
    /// Controls when DataFusion may split direct Parquet reads into ranged scan tasks.
    pub intra_file_repartitioning: IntraFileRepartitioning,
    /// Decode string and binary data-file columns into Arrow view arrays.
    pub use_arrow_view_types: bool,
}

impl Default for ScanOptions {
    fn default() -> Self {
        Self {
            execution_options: DeltaScanExecutionOptions::default(),
            target_partitions: None,
            intra_file_repartitioning: IntraFileRepartitioning::default(),
            use_arrow_view_types: true,
        }
    }
}

/// Immutable DataFusion provider for one loaded Delta table snapshot.
///
/// ```no_run
/// use std::sync::Arc;
/// use datafusion::prelude::SessionContext;
/// use delta_arrow_reader::{
///     DeltaTableBuilder,
///     datafusion::{DeltaTableProvider, ScanOptions},
/// };
///
/// # async fn build_provider() -> Result<(), Box<dyn std::error::Error>> {
/// let table = DeltaTableBuilder::new("/tmp/example-delta-table")
///     .load_table()
///     .await?;
/// let provider = DeltaTableProvider::try_new(
///     table,
///     ScanOptions::default(),
/// )?;
/// SessionContext::new().register_table("orders", Arc::new(provider))?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct DeltaTableProvider {
    table: DeltaTable,
    schema: SchemaRef,
    options: ScanOptions,
    registration_name: Option<String>,
}

impl DeltaTableProvider {
    /// Creates a provider after validating its options and table protocol.
    pub fn try_new(table: DeltaTable, options: ScanOptions) -> Result<Self, DeltaReaderError> {
        Self::try_new_with_registration_name(table, options, None)
    }

    fn try_new_with_registration_name(
        table: DeltaTable,
        options: ScanOptions,
        registration_name: Option<String>,
    ) -> Result<Self, DeltaReaderError> {
        if options.target_partitions == Some(0) {
            return Err(DeltaReaderError::InvalidConfiguration {
                reason: "scan_partition_target_must_be_positive",
            });
        }
        table.validate_protocol()?;
        let partition_columns = table.partition_columns().iter().cloned().collect();
        let schema = build_provider_schema(
            &table.schema(),
            &partition_columns,
            options.use_arrow_view_types,
        );
        Ok(Self {
            table,
            schema,
            options,
            registration_name,
        })
    }

    fn plan(
        &self,
        state: &dyn Session,
        projection: Option<&[usize]>,
        filters: &[Expr],
    ) -> Result<(Arc<dyn ExecutionPlan>, usize), DeltaReaderError> {
        let _planning = tracing::debug_span!(
            target: "delta_arrow_reader::profile",
            "Delta scan planning"
        )
        .entered();
        let partition_columns = self
            .table
            .partition_columns()
            .iter()
            .cloned()
            .collect::<HashSet<_>>();
        let filter_refs = filters.iter().collect::<Vec<_>>();
        let mut datafusion_plan = plan_datafusion_scan(
            &self.table.schema(),
            &partition_columns,
            projection,
            &filter_refs,
            FilterCapabilities {
                supports_exact_row_filtering: self.options.execution_options.parquet_backend()
                    == ParquetReaderBackend::Direct,
            },
        )?;
        if datafusion_plan
            .filters
            .decisions
            .iter()
            .any(|decision| decision.pushdown == TableProviderFilterPushDown::Unsupported)
        {
            return Err(DeltaReaderError::UnsupportedPredicate {
                reason: "datafusion_scan_contains_unsupported_filter",
            });
        }
        let scan_projection = datafusion_plan.projection.scan_projection.clone();
        let hidden_columns = datafusion_plan.projection.hidden_columns.clone();
        let pruning_predicate = datafusion_plan
            .filters
            .pruning_predicate
            .as_ref()
            .map(|predicate| {
                kernel_pruning_predicate(predicate).ok_or(DeltaReaderError::UnsupportedPredicate {
                    reason: "datafusion_predicate_not_kernel_safe",
                })
            })
            .transpose()?;
        let exact_row_predicate = datafusion_plan
            .filters
            .exact_row_predicate
            .as_ref()
            .map(|predicate| {
                kernel_pruning_predicate(predicate).ok_or(DeltaReaderError::UnsupportedPredicate {
                    reason: "exact_row_predicate_not_kernel_safe",
                })
            })
            .transpose()?;
        let exact_row_predicate = build_physical_row_predicate(
            self.table.snapshot(),
            scan_projection.as_deref(),
            &hidden_columns,
            exact_row_predicate,
        )?;
        let mut reader_plan = plan_scan(
            self.table.snapshot(),
            scan_projection.as_deref(),
            &hidden_columns,
            pruning_predicate,
            datafusion_plan.filters.requires_statistics,
            self.options.execution_options,
            DeltaScanPartitionTargetOptions {
                explicit_target_partitions: self.options.target_partitions,
                datafusion_target_partitions: Some(state.config().target_partitions()),
            },
        )?;
        reader_plan.logical_schema = build_provider_schema(
            &reader_plan.logical_schema,
            &partition_columns,
            self.options.use_arrow_view_types,
        );
        reader_plan.physical_schema = build_provider_schema(
            &reader_plan.physical_schema,
            &partition_columns,
            self.options.use_arrow_view_types,
        );
        reader_plan.projected_schema = build_provider_schema(
            &reader_plan.projected_schema,
            &partition_columns,
            self.options.use_arrow_view_types,
        );
        datafusion_plan.projection.output_schema = build_provider_schema(
            &datafusion_plan.projection.output_schema,
            &partition_columns,
            self.options.use_arrow_view_types,
        );
        let partition_count = reader_plan.partitions.len();
        let plan = {
            let _setup = tracing::debug_span!(
                target: "delta_arrow_reader::profile",
                "Delta scan execution setup"
            )
            .entered();
            create_datafusion_execution_plan(
                reader_plan,
                datafusion_plan,
                exact_row_predicate,
                self.registration_name.clone(),
                self.options.use_arrow_view_types,
                self.options.intra_file_repartitioning,
            )
        };
        Ok((plan, partition_count))
    }
}

fn build_provider_schema(
    schema: &Schema,
    partition_columns: &HashSet<String>,
    use_arrow_view_types: bool,
) -> SchemaRef {
    let view_schema = schema_with_view_types(schema);
    Arc::new(Schema::new_with_metadata(
        schema
            .fields()
            .iter()
            .zip(view_schema.fields())
            .map(|(logical, view)| {
                if partition_columns.contains(logical.name())
                    && matches!(
                        logical.data_type(),
                        DataType::Utf8
                            | DataType::LargeUtf8
                            | DataType::Binary
                            | DataType::LargeBinary
                    )
                {
                    Arc::new(
                        logical
                            .as_ref()
                            .clone()
                            .with_data_type(wrap_partition_type_in_dict(
                                logical.data_type().clone(),
                            )),
                    )
                } else if use_arrow_view_types {
                    Arc::clone(view)
                } else {
                    Arc::clone(logical)
                }
            })
            .collect::<Vec<_>>(),
        schema.metadata().clone(),
    ))
}

impl fmt::Debug for DeltaTableProvider {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("DeltaTableProvider")
            .field("snapshot_version", &self.table.version())
            .finish_non_exhaustive()
    }
}

#[async_trait]
impl TableProvider for DeltaTableProvider {
    fn schema(&self) -> SchemaRef {
        Arc::clone(&self.schema)
    }

    fn table_type(&self) -> TableType {
        TableType::Base
    }

    async fn scan(
        &self,
        state: &dyn Session,
        projection: Option<&Vec<usize>>,
        filters: &[Expr],
        _limit: Option<usize>,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        match self.plan(state, projection.map(Vec::as_slice), filters) {
            Ok((plan, partition_count)) => {
                tracing::debug!(
                    target: TRACING_TARGET,
                    event = "provider_scan.planned",
                    snapshot_version = self.table.version(),
                    partition_count,
                    backend = ?self.options.execution_options.parquet_backend(),
                    outcome = "planned"
                );
                Ok(plan)
            }
            Err(error) => {
                trace_failure(
                    "provider_scan.failed",
                    self.table.version(),
                    self.options.execution_options.parquet_backend(),
                    &error,
                );
                Err(DataFusionError::External(Box::new(error)))
            }
        }
    }

    fn supports_filters_pushdown(
        &self,
        filters: &[&Expr],
    ) -> DataFusionResult<Vec<TableProviderFilterPushDown>> {
        let partition_columns = self
            .table
            .partition_columns()
            .iter()
            .cloned()
            .collect::<HashSet<_>>();
        let datafusion_plan = plan_datafusion_filters(
            &self.table.schema(),
            &partition_columns,
            filters,
            FilterCapabilities {
                supports_exact_row_filtering: self.options.execution_options.parquet_backend()
                    == ParquetReaderBackend::Direct,
            },
        );
        Ok(datafusion_plan
            .decisions
            .iter()
            .map(|decision| decision.pushdown.clone())
            .collect())
    }
}

/// Result of registering one loaded Delta table in a DataFusion context.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TableRegistration {
    /// Caller-supplied DataFusion table name.
    pub name: String,
    /// Loaded Delta snapshot version.
    pub snapshot_version: u64,
}

/// Registers one loaded Delta table in a DataFusion session.
///
/// Registration performs no scan. Existing registrations are preserved and
/// reported through [`DeltaReaderError`].
///
/// ```no_run
/// use datafusion::prelude::SessionContext;
/// use delta_arrow_reader::{
///     DeltaTableBuilder,
///     datafusion::{ScanOptions, register_table},
/// };
///
/// # async fn register() -> Result<(), Box<dyn std::error::Error>> {
/// let context = SessionContext::new();
/// let table = DeltaTableBuilder::new("/tmp/example-delta-table")
///     .load_table()
///     .await?;
/// let registration = register_table(
///     &context,
///     "orders",
///     table,
///     ScanOptions::default(),
/// )?;
/// assert_eq!(registration.name, "orders");
/// # Ok(())
/// # }
/// ```
pub fn register_table(
    context: &SessionContext,
    name: impl Into<String>,
    table: DeltaTable,
    options: ScanOptions,
) -> Result<TableRegistration, DeltaReaderError> {
    let name = name.into();
    let snapshot_version = table.version();
    let backend = options.execution_options.parquet_backend();
    let result = (|| {
        validate_registration_name(&name)?;
        let provider =
            DeltaTableProvider::try_new_with_registration_name(table, options, Some(name.clone()))?;
        context
            .register_table(name.as_str(), Arc::new(provider))
            .map_err(|source| DeltaReaderError::DataFusionAdapter {
                reason: "table_registration_failed",
                source: Box::new(source),
            })?;
        Ok(TableRegistration {
            name,
            snapshot_version,
        })
    })();
    match result {
        Ok(registration) => {
            tracing::debug!(
                target: TRACING_TARGET,
                event = "provider_registration.registered",
                snapshot_version,
                partition_count = tracing::field::Empty,
                backend = ?backend,
                outcome = "registered"
            );
            Ok(registration)
        }
        Err(error) => {
            trace_failure(
                "provider_registration.failed",
                snapshot_version,
                backend,
                &error,
            );
            Err(error)
        }
    }
}

fn validate_registration_name(name: &str) -> Result<(), DeltaReaderError> {
    let mut chars = name.chars();
    let valid = chars
        .next()
        .is_some_and(|first| first == '_' || first.is_ascii_alphabetic())
        && chars.all(|value| value == '_' || value.is_ascii_alphanumeric());
    if !valid || is_reserved_sql_keyword(name) {
        let reason = if name.is_empty() {
            "table_registration_name_empty"
        } else {
            "table_registration_name_invalid"
        };
        return Err(DeltaReaderError::DataFusionAdapter {
            reason,
            source: Box::new(DataFusionError::Plan(reason.to_owned())),
        });
    }
    Ok(())
}

fn is_reserved_sql_keyword(name: &str) -> bool {
    const KEYWORDS: &[&str] = &[
        "all",
        "alter",
        "analyze",
        "and",
        "anti",
        "as",
        "asof",
        "by",
        "case",
        "connect",
        "cross",
        "delete",
        "distinct",
        "distribute",
        "drop",
        "else",
        "end",
        "except",
        "exists",
        "explain",
        "false",
        "fetch",
        "for",
        "format",
        "from",
        "full",
        "global",
        "group",
        "having",
        "in",
        "inner",
        "insert",
        "intersect",
        "into",
        "is",
        "join",
        "lateral",
        "left",
        "like",
        "limit",
        "minus",
        "natural",
        "not",
        "null",
        "offset",
        "on",
        "open",
        "or",
        "order",
        "outer",
        "partition",
        "pivot",
        "prewhere",
        "qualify",
        "returning",
        "right",
        "sample",
        "select",
        "semi",
        "set",
        "settings",
        "sort",
        "start",
        "table",
        "tablesample",
        "then",
        "top",
        "true",
        "union",
        "unpivot",
        "update",
        "using",
        "values",
        "view",
        "when",
        "where",
        "window",
        "with",
    ];
    KEYWORDS
        .iter()
        .any(|keyword| name.eq_ignore_ascii_case(keyword))
}

fn trace_failure(
    event: &'static str,
    snapshot_version: u64,
    backend: ParquetReaderBackend,
    error: &DeltaReaderError,
) {
    tracing::debug!(
        target: TRACING_TARGET,
        event,
        snapshot_version,
        partition_count = tracing::field::Empty,
        backend = ?backend,
        outcome = "failed",
        error_code = error.code(),
        error_phase = error.phase().as_str()
    );
}

#[cfg(test)]
mod tests {
    use std::collections::{HashMap, HashSet};

    use arrow::datatypes::{DataType, Field, Schema};

    use super::{build_provider_schema, validate_registration_name};

    #[test]
    fn registration_names_preserve_the_frozen_unquoted_identifier_boundary() {
        for name in ["orders", "_customers", "Regions_2026", "line_items"] {
            assert!(validate_registration_name(name).is_ok(), "{name}");
        }

        for name in [
            "",
            "2026_orders",
            "orders.latest",
            "line-items",
            "line items",
            "\"orders\"",
            "orders$",
            "ordérs",
            "select",
            "FROM",
            "Join",
            "where",
            "table",
        ] {
            assert!(validate_registration_name(name).is_err(), "{name}");
        }
    }

    #[test]
    fn datafusion_schema_uses_views_except_for_dictionary_partitions() {
        let field_metadata = HashMap::from([("field-key".to_owned(), "field-value".to_owned())]);
        let schema_metadata = HashMap::from([("schema-key".to_owned(), "schema-value".to_owned())]);
        let schema = Schema::new_with_metadata(
            vec![
                Field::new("text", DataType::Utf8, true).with_metadata(field_metadata.clone()),
                Field::new("payload", DataType::Binary, true),
                Field::new("region", DataType::Utf8, true),
                Field::new("partition_payload", DataType::LargeBinary, true),
                Field::new("id", DataType::Int32, false),
            ],
            schema_metadata.clone(),
        );
        let partitions = HashSet::from(["region".to_owned(), "partition_payload".to_owned()]);

        let mapped = build_provider_schema(&schema, &partitions, true);

        assert_eq!(
            mapped.as_ref(),
            &Schema::new_with_metadata(
                vec![
                    Field::new("text", DataType::Utf8View, true).with_metadata(field_metadata),
                    Field::new("payload", DataType::BinaryView, true),
                    Field::new(
                        "region",
                        DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8)),
                        true,
                    ),
                    Field::new(
                        "partition_payload",
                        DataType::Dictionary(
                            Box::new(DataType::UInt16),
                            Box::new(DataType::LargeBinary),
                        ),
                        true,
                    ),
                    Field::new("id", DataType::Int32, false),
                ],
                schema_metadata.clone(),
            )
        );

        let standard = build_provider_schema(&schema, &partitions, false);
        assert_eq!(standard.field(0).data_type(), &DataType::Utf8);
        assert_eq!(standard.field(1).data_type(), &DataType::Binary);
        assert_eq!(
            standard.field(2).data_type(),
            &DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8))
        );
        assert_eq!(
            standard.field(3).data_type(),
            &DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::LargeBinary),)
        );
        assert_eq!(standard.field(4).data_type(), &DataType::Int32);
        assert_eq!(standard.metadata(), &schema_metadata);
    }
}