faucet-source-delta 1.1.0

Apache Delta Lake source for the faucet-stream ecosystem — local FS + S3/Azure/GCS, time travel
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
//! Delta Lake source stream executor.
//!
//! Reads a Delta table's active data files at the latest version (or a pinned
//! `version` / `timestamp`) and yields each row as a `serde_json::Value`
//! object. No datafusion: the active file set comes from the Delta log
//! (`get_files_by_partitions`) and each parquet file is streamed through the async
//! Arrow reader faucet's Parquet source uses. Partition-column values (which
//! live in the Hive-style path, not the file) are reconstructed and merged
//! back into every row, typed against the table schema.

use std::collections::HashMap;
use std::pin::Pin;

use arrow::datatypes::{DataType, SchemaRef};
use async_trait::async_trait;
use faucet_common_delta::convert::record_batch_to_json;
use faucet_core::{FaucetError, Stream, StreamPage};
use futures::StreamExt;
use object_store::path::Path as ObjPath;
use parquet::arrow::ProjectionMask;
use parquet::arrow::async_reader::{ParquetObjectReader, ParquetRecordBatchStreamBuilder};
use serde_json::Value;

use crate::config::DeltaSourceConfig;

/// A source that reads an Apache Delta Lake table into JSON records.
pub struct DeltaSource {
    config: DeltaSourceConfig,
}

/// One active data file plus the partition values encoded in its path.
struct DataFile {
    path: ObjPath,
    /// `col -> JSON value` for every partition column, typed against the table
    /// schema. `null` for the Hive default-partition sentinel.
    partitions: HashMap<String, Value>,
}

impl DeltaSource {
    /// Build a new Delta source. Validates config eagerly; the table is opened
    /// on each read so time-travel/version pins re-resolve.
    pub async fn new(config: DeltaSourceConfig) -> Result<Self, FaucetError> {
        config
            .validate()
            .map_err(|e| FaucetError::Config(format!("invalid delta source config: {e}")))?;
        config.connection.register_handlers();
        Ok(Self { config })
    }

    /// Open the table at the configured version / timestamp / latest.
    async fn open(&self) -> Result<deltalake::DeltaTable, FaucetError> {
        match (self.config.version, &self.config.timestamp) {
            (Some(v), _) => self.config.connection.open_at_version(v).await,
            (None, Some(ts)) => self.config.connection.open_at_timestamp(ts).await,
            (None, None) => self.config.connection.open().await,
        }
    }

    /// Resolve the active files + their partition values, and the table's Arrow
    /// schema (used to type partition values and validate projection).
    async fn resolve(
        &self,
        table: &deltalake::DeltaTable,
    ) -> Result<(Vec<DataFile>, SchemaRef, Vec<String>), FaucetError> {
        let state = table
            .snapshot()
            .map_err(|e| FaucetError::Source(format!("delta: table has no snapshot: {e}")))?;
        let arrow_schema = state.snapshot().arrow_schema();
        let partition_cols = state.metadata().partition_columns().to_vec();

        let paths = table
            .get_files_by_partitions(&[])
            .await
            .map_err(|e| FaucetError::Source(format!("delta: could not list table files: {e}")))?;

        let files = paths
            .into_iter()
            .map(|path| {
                let partitions =
                    parse_partition_values(path.as_ref(), &partition_cols, &arrow_schema);
                DataFile { path, partitions }
            })
            .collect();
        Ok((files, arrow_schema, partition_cols))
    }

    /// The projection over the *data* file columns: the requested columns minus
    /// any partition columns (which are not stored in the file). `None` (read
    /// all file columns) when no projection is configured.
    fn data_projection(&self, partition_cols: &[String]) -> Option<Vec<String>> {
        if self.config.columns.is_empty() {
            return None;
        }
        Some(
            self.config
                .columns
                .iter()
                .filter(|c| !partition_cols.contains(c))
                .cloned()
                .collect(),
        )
    }
}

#[async_trait]
impl faucet_core::Source for DeltaSource {
    fn config_schema(&self) -> Value {
        serde_json::to_value(faucet_core::schema_for!(DeltaSourceConfig))
            .expect("schema serialization")
    }

    fn connector_name(&self) -> &'static str {
        "delta"
    }

    fn dataset_uri(&self) -> String {
        self.config.connection.redacted_uri()
    }

    async fn check(
        &self,
        ctx: &faucet_core::check::CheckContext,
    ) -> Result<faucet_core::check::CheckReport, FaucetError> {
        use faucet_core::check::{CheckReport, Probe};
        let started = std::time::Instant::now();
        // Metadata-only open (no data scan). The source needs the table to
        // exist, so an absent table fails the probe.
        let probe =
            match tokio::time::timeout(ctx.timeout, self.config.connection.open_optional()).await {
                Ok(Ok(Some(_))) => Probe::pass("table", started.elapsed()),
                Ok(Ok(None)) => Probe::fail_hint(
                    "table",
                    started.elapsed(),
                    format!(
                        "delta source: no Delta table at '{}'",
                        self.config.connection.redacted_uri()
                    ),
                    "Verify table_uri points at an existing Delta table.",
                ),
                Ok(Err(e)) => Probe::fail_hint(
                    "table",
                    started.elapsed(),
                    format!("delta source probe failed: {e}"),
                    "Verify table_uri, credentials, and object-store reachability.",
                ),
                Err(_) => Probe::fail_hint(
                    "table",
                    started.elapsed(),
                    format!("delta source probe timed out after {:?}", ctx.timeout),
                    "Check object-store network reachability.",
                ),
            };
        Ok(CheckReport::single(probe))
    }

    async fn fetch_with_context(
        &self,
        _context: &HashMap<String, Value>,
    ) -> Result<Vec<Value>, FaucetError> {
        let mut out = Vec::new();
        let mut stream = self.stream_pages(_context, self.config.batch_size);
        while let Some(page) = stream.next().await {
            out.extend(page?.records);
        }
        Ok(out)
    }

    fn stream_pages<'a>(
        &'a self,
        _context: &'a HashMap<String, Value>,
        _batch_size: usize,
    ) -> Pin<Box<dyn Stream<Item = Result<StreamPage, FaucetError>> + Send + 'a>> {
        Box::pin(async_stream::try_stream! {
            let table = self.open().await?;
            let (files, _schema, partition_cols) = self.resolve(&table).await?;
            let store = table.object_store();
            let data_projection = self.data_projection(&partition_cols);
            let requested: Option<&[String]> =
                if self.config.columns.is_empty() { None } else { Some(&self.config.columns) };

            tracing::info!(
                files = files.len(),
                uri = %self.config.connection.redacted_uri(),
                "delta source resolved active files",
            );

            for file in &files {
                let reader = ParquetObjectReader::new(store.clone(), file.path.clone());
                let mut builder = ParquetRecordBatchStreamBuilder::new(reader).await.map_err(|e| {
                    FaucetError::Source(format!(
                        "delta: could not open data file '{}': {e}",
                        file.path
                    ))
                })?;

                if self.config.batch_size > 0 {
                    builder = builder.with_batch_size(self.config.batch_size);
                }
                if let Some(cols) = &data_projection {
                    // Only project columns actually present in this file. A
                    // requested column that is neither a data column here nor a
                    // partition column is genuinely absent → surface it.
                    let pq = builder.parquet_schema();
                    let present: Vec<&str> = cols
                        .iter()
                        .filter(|c| pq.columns().iter().any(|col| col.name() == c.as_str()))
                        .map(String::as_str)
                        .collect();
                    let mask = ProjectionMask::columns(pq, present.iter().copied());
                    builder = builder.with_projection(mask);
                }

                let mut batches = builder.build().map_err(|e| {
                    FaucetError::Source(format!(
                        "delta: could not build reader for '{}': {e}",
                        file.path
                    ))
                })?;

                while let Some(batch) = batches.next().await {
                    let batch = batch.map_err(|e| {
                        FaucetError::Source(format!("delta: read error in '{}': {e}", file.path))
                    })?;
                    let mut rows = record_batch_to_json(&batch)?;
                    if !rows.is_empty() {
                        for row in &mut rows {
                            merge_partitions(row, &file.partitions, requested);
                        }
                        yield StreamPage { records: rows, bookmark: None };
                    }
                }
            }
        })
    }

    /// Delta reads are natively Arrow (each data file is a parquet stream), so
    /// the source participates in the opt-in columnar fast path (#375): a
    /// `delta → parquet` / `delta → delta` chain never materializes `Value`.
    #[cfg(feature = "arrow")]
    fn supports_columnar(&self) -> bool {
        true
    }

    /// Stream the table as Arrow [`ColumnarPage`](faucet_core::ColumnarPage)s.
    /// Mirrors `stream_pages` but yields each parquet
    /// `RecordBatch` directly; Hive partition-column values (which live in the
    /// file path, not the parquet data) are appended as constant Arrow columns
    /// so the columnar output matches the row-wise output field-for-field.
    #[cfg(feature = "arrow")]
    fn stream_batches<'a>(
        &'a self,
        _context: &'a HashMap<String, Value>,
        _batch_size: usize,
    ) -> Pin<Box<dyn Stream<Item = Result<faucet_core::ColumnarPage, FaucetError>> + Send + 'a>>
    {
        Box::pin(async_stream::try_stream! {
            let table = self.open().await?;
            let (files, schema, partition_cols) = self.resolve(&table).await?;
            let store = table.object_store();
            let data_projection = self.data_projection(&partition_cols);
            let requested: Option<&[String]> =
                if self.config.columns.is_empty() { None } else { Some(&self.config.columns) };

            for file in &files {
                let reader = ParquetObjectReader::new(store.clone(), file.path.clone());
                let mut builder = ParquetRecordBatchStreamBuilder::new(reader).await.map_err(|e| {
                    FaucetError::Source(format!("delta: could not open data file '{}': {e}", file.path))
                })?;
                if self.config.batch_size > 0 {
                    builder = builder.with_batch_size(self.config.batch_size);
                }
                if let Some(cols) = &data_projection {
                    let pq = builder.parquet_schema();
                    let present: Vec<&str> = cols
                        .iter()
                        .filter(|c| pq.columns().iter().any(|col| col.name() == c.as_str()))
                        .map(String::as_str)
                        .collect();
                    let mask = ProjectionMask::columns(pq, present.iter().copied());
                    builder = builder.with_projection(mask);
                }
                let mut batches = builder.build().map_err(|e| {
                    FaucetError::Source(format!("delta: could not build reader for '{}': {e}", file.path))
                })?;
                while let Some(batch) = batches.next().await {
                    let batch = batch.map_err(|e| {
                        FaucetError::Source(format!("delta: read error in '{}': {e}", file.path))
                    })?;
                    if batch.num_rows() == 0 {
                        continue;
                    }
                    let batch = append_partition_columns(batch, &file.partitions, &schema, requested)?;
                    yield faucet_core::ColumnarPage { batch, bookmark: None };
                }
            }
        })
    }
}

/// Append Hive partition columns to a data-file `RecordBatch` as constant
/// columns, honoring the same `requested`-projection semantics as
/// [`merge_partitions`] (add a partition column only when unprojected-away, and
/// never shadow a real data column of the same name). Each constant column is
/// built through the core `Value → RecordBatch` shim with the table's declared
/// Arrow type, so a partition value round-trips identically to the row path.
#[cfg(feature = "arrow")]
fn append_partition_columns(
    batch: arrow::array::RecordBatch,
    partitions: &HashMap<String, Value>,
    table_schema: &SchemaRef,
    requested: Option<&[String]>,
) -> Result<arrow::array::RecordBatch, FaucetError> {
    use arrow::datatypes::{Field, Schema};
    use std::sync::Arc;

    if partitions.is_empty() {
        return Ok(batch);
    }
    let in_schema = batch.schema();
    let n = batch.num_rows();
    let mut fields: Vec<Arc<Field>> = in_schema.fields().iter().cloned().collect();
    let mut columns = batch.columns().to_vec();

    // Deterministic order so the output schema is stable run-to-run.
    let mut keys: Vec<&String> = partitions.keys().collect();
    keys.sort();
    for k in keys {
        if let Some(cols) = requested
            && !cols.iter().any(|c| c == k)
        {
            continue;
        }
        if in_schema.field_with_name(k).is_ok() {
            continue; // a real data column of this name wins (merge_partitions)
        }
        let field = table_schema
            .field_with_name(k)
            .cloned()
            .unwrap_or_else(|_| Field::new(k, arrow::datatypes::DataType::Utf8, true));
        let one_schema = Arc::new(Schema::new(vec![field.clone().with_nullable(true)]));
        let mut obj = serde_json::Map::new();
        obj.insert(k.clone(), partitions[k].clone());
        let rows = vec![Value::Object(obj); n];
        let col_batch = faucet_core::values_to_record_batch(&rows, one_schema)?;
        fields.push(Arc::new(field));
        columns.push(col_batch.column(0).clone());
    }

    arrow::array::RecordBatch::try_new(Arc::new(Schema::new(fields)), columns)
        .map_err(|e| FaucetError::Source(format!("delta: assembling columnar batch failed: {e}")))
}

/// Parse Hive-style `col=value` segments out of a data file path, typing each
/// value against the table's Arrow schema. Only the declared partition columns
/// are extracted; unknown segments are ignored.
fn parse_partition_values(
    path: &str,
    partition_cols: &[String],
    schema: &SchemaRef,
) -> HashMap<String, Value> {
    let mut out = HashMap::new();
    if partition_cols.is_empty() {
        return out;
    }
    for segment in path.split('/') {
        if let Some((k, v)) = segment.split_once('=')
            && partition_cols.iter().any(|c| c == k)
        {
            let decoded = percent_decode(v);
            let dt = schema
                .field_with_name(k)
                .ok()
                .map(|f| f.data_type().clone())
                .unwrap_or(DataType::Utf8);
            out.insert(k.to_string(), coerce_partition_value(&decoded, &dt));
        }
    }
    out
}

/// The Delta Hive-default-partition sentinel — represents a NULL partition
/// value.
const HIVE_NULL: &str = "__HIVE_DEFAULT_PARTITION__";

/// Coerce a string partition value to JSON, typed by the column's Arrow type.
fn coerce_partition_value(raw: &str, dt: &DataType) -> Value {
    if raw == HIVE_NULL || raw.is_empty() {
        return Value::Null;
    }
    match dt {
        DataType::Boolean => match raw {
            "true" => Value::Bool(true),
            "false" => Value::Bool(false),
            _ => Value::String(raw.to_string()),
        },
        DataType::Int8
        | DataType::Int16
        | DataType::Int32
        | DataType::Int64
        | DataType::UInt8
        | DataType::UInt16
        | DataType::UInt32
        | DataType::UInt64 => raw
            .parse::<i64>()
            .map(|n| Value::Number(n.into()))
            .unwrap_or_else(|_| Value::String(raw.to_string())),
        DataType::Float32 | DataType::Float64 => {
            serde_json::Number::from_f64(raw.parse::<f64>().unwrap_or(f64::NAN))
                .map(Value::Number)
                .unwrap_or_else(|| Value::String(raw.to_string()))
        }
        // Dates/timestamps/strings/decimals: keep the logical string form.
        _ => Value::String(raw.to_string()),
    }
}

/// Merge partition values into a data row, then narrow to `requested` columns
/// (when a projection is configured). Partition values fill keys not present in
/// the data (the file never stores them).
fn merge_partitions(
    row: &mut Value,
    partitions: &HashMap<String, Value>,
    requested: Option<&[String]>,
) {
    if let Value::Object(map) = row {
        for (k, v) in partitions {
            match requested {
                Some(cols) if !cols.iter().any(|c| c == k) => continue,
                _ => {
                    map.entry(k.clone()).or_insert_with(|| v.clone());
                }
            }
        }
        if let Some(cols) = requested {
            map.retain(|k, _| cols.iter().any(|c| c == k));
        }
    }
}

/// Minimal `%XX` percent-decoder for Hive-encoded partition path segments.
/// Leaves malformed escapes untouched.
fn percent_decode(s: &str) -> String {
    if !s.contains('%') {
        return s.to_string();
    }
    let bytes = s.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%' && i + 2 < bytes.len() {
            let hi = (bytes[i + 1] as char).to_digit(16);
            let lo = (bytes[i + 2] as char).to_digit(16);
            if let (Some(h), Some(l)) = (hi, lo) {
                out.push((h * 16 + l) as u8);
                i += 3;
                continue;
            }
        }
        out.push(bytes[i]);
        i += 1;
    }
    String::from_utf8_lossy(&out).into_owned()
}

#[cfg(test)]
mod tests {
    use super::*;
    use arrow::datatypes::{Field, Schema};
    use serde_json::json;
    use std::sync::Arc;

    fn schema() -> SchemaRef {
        Arc::new(Schema::new(vec![
            Field::new("id", DataType::Int64, true),
            Field::new("dt", DataType::Utf8, true),
            Field::new("region", DataType::Utf8, true),
            Field::new("part", DataType::Int64, true),
        ]))
    }

    #[test]
    fn parses_typed_partition_values() {
        let s = schema();
        let cols = vec!["dt".to_string(), "part".to_string()];
        let m = parse_partition_values("t/dt=2026-01-01/part=7/file.parquet", &cols, &s);
        assert_eq!(m["dt"], json!("2026-01-01"));
        assert_eq!(m["part"], json!(7));
    }

    #[test]
    fn hive_null_becomes_json_null() {
        let s = schema();
        let cols = vec!["region".to_string()];
        let m = parse_partition_values("t/region=__HIVE_DEFAULT_PARTITION__/f.parquet", &cols, &s);
        assert_eq!(m["region"], Value::Null);
    }

    #[test]
    fn percent_decoding_of_partition_values() {
        let s = schema();
        let cols = vec!["region".to_string()];
        let m = parse_partition_values("t/region=a%2Fb/f.parquet", &cols, &s);
        assert_eq!(m["region"], json!("a/b"));
    }

    #[test]
    fn no_partition_columns_is_empty() {
        let s = schema();
        assert!(parse_partition_values("t/f.parquet", &[], &s).is_empty());
    }

    #[test]
    fn merge_injects_and_projects() {
        let mut row = json!({"id": 1});
        let mut parts = HashMap::new();
        parts.insert("dt".to_string(), json!("2026-01-01"));
        merge_partitions(&mut row, &parts, None);
        assert_eq!(row["dt"], json!("2026-01-01"));
        assert_eq!(row["id"], json!(1));

        // With projection, only requested keys survive.
        let mut row2 = json!({"id": 1, "name": "x"});
        let cols = vec!["id".to_string(), "dt".to_string()];
        merge_partitions(&mut row2, &parts, Some(&cols));
        assert_eq!(row2["id"], json!(1));
        assert_eq!(row2["dt"], json!("2026-01-01"));
        assert!(row2.get("name").is_none());
    }

    #[test]
    fn coerce_bool_and_float() {
        assert_eq!(
            coerce_partition_value("true", &DataType::Boolean),
            json!(true)
        );
        assert_eq!(
            coerce_partition_value("1.5", &DataType::Float64),
            json!(1.5)
        );
        assert_eq!(coerce_partition_value("x", &DataType::Int64), json!("x"));
        // Non-parseable values for bool/float columns fall back to a string.
        assert_eq!(
            coerce_partition_value("maybe", &DataType::Boolean),
            json!("maybe")
        );
        assert_eq!(
            coerce_partition_value("nan-ish", &DataType::Float32),
            json!("nan-ish")
        );
        // Empty and the Hive sentinel both become JSON null.
        assert_eq!(coerce_partition_value("", &DataType::Utf8), Value::Null);
        assert_eq!(
            coerce_partition_value(HIVE_NULL, &DataType::Int64),
            Value::Null
        );
        // A date column keeps the logical string form.
        assert_eq!(
            coerce_partition_value("2026-01-01", &DataType::Date32),
            json!("2026-01-01")
        );
    }

    #[tokio::test]
    async fn source_trait_metadata_methods() {
        use faucet_core::Source;
        let src = DeltaSource::new(DeltaSourceConfig::new("file:///tmp/delta_src_meta"))
            .await
            .unwrap();
        assert_eq!(src.connector_name(), "delta");
        assert_eq!(src.dataset_uri(), "file:///tmp/delta_src_meta");
        assert!(src.config_schema().is_object());
    }

    #[tokio::test]
    async fn fetch_missing_table_errors() {
        use faucet_core::Source;
        let dir = tempfile::tempdir().unwrap();
        let uri = dir
            .path()
            .join("no_such_table")
            .to_string_lossy()
            .into_owned();
        let src = DeltaSource::new(DeltaSourceConfig::new(&uri))
            .await
            .unwrap();
        // `open()` fails (not a Delta table) → mapped to FaucetError::Source.
        let err = src.fetch_with_context(&HashMap::new()).await.unwrap_err();
        assert!(matches!(err, FaucetError::Source(_)), "{err}");
    }
}