datafusion-ducklake 0.6.0

DuckLake query engine for rust, built with datafusion.
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
//! User-Defined Table Functions (UDTFs) for DuckLake catalog metadata

use datafusion::catalog::TableFunctionImpl;
use datafusion::common::{Result as DataFusionResult, ScalarValue, plan_err};
use datafusion::datasource::TableProvider;
use datafusion::logical_expr::Expr;
use std::sync::Arc;

use crate::information_schema::{FilesTable, SnapshotsTable, TableInfoTable};
use crate::metadata_provider::MetadataProvider;
use crate::path_resolver::{parse_object_store_url, resolve_path};
use crate::table_changes::{TableChangesTable, TableInsertionsTable};
use crate::table_deletions::TableDeletionsTable;
use crate::types::build_arrow_schema;

#[derive(Debug)]
pub struct DucklakeSnapshotsFunction {
    provider: Arc<dyn MetadataProvider>,
}

impl DucklakeSnapshotsFunction {
    pub fn new(provider: Arc<dyn MetadataProvider>) -> Self {
        Self {
            provider,
        }
    }
}

impl TableFunctionImpl for DucklakeSnapshotsFunction {
    fn call(&self, exprs: &[Expr]) -> DataFusionResult<Arc<dyn TableProvider>> {
        if !exprs.is_empty() {
            return plan_err!("ducklake_snapshots() takes no arguments");
        }

        Ok(Arc::new(SnapshotsTable::new(self.provider.clone())))
    }
}

#[derive(Debug)]
pub struct DucklakeTableInfoFunction {
    provider: Arc<dyn MetadataProvider>,
}

impl DucklakeTableInfoFunction {
    pub fn new(provider: Arc<dyn MetadataProvider>) -> Self {
        Self {
            provider,
        }
    }
}

impl TableFunctionImpl for DucklakeTableInfoFunction {
    fn call(&self, exprs: &[Expr]) -> DataFusionResult<Arc<dyn TableProvider>> {
        if !exprs.is_empty() {
            return plan_err!("ducklake_table_info() takes no arguments");
        }

        Ok(Arc::new(TableInfoTable::new(self.provider.clone())))
    }
}

#[derive(Debug)]
pub struct DucklakeListFilesFunction {
    provider: Arc<dyn MetadataProvider>,
}

impl DucklakeListFilesFunction {
    pub fn new(provider: Arc<dyn MetadataProvider>) -> Self {
        Self {
            provider,
        }
    }
}

impl TableFunctionImpl for DucklakeListFilesFunction {
    fn call(&self, exprs: &[Expr]) -> DataFusionResult<Arc<dyn TableProvider>> {
        if !exprs.is_empty() {
            return plan_err!("ducklake_list_files() takes no arguments");
        }

        Ok(Arc::new(FilesTable::new(self.provider.clone())))
    }
}

#[derive(Debug)]
pub struct DucklakeTableChangesFunction {
    provider: Arc<dyn MetadataProvider>,
}

impl DucklakeTableChangesFunction {
    pub fn new(provider: Arc<dyn MetadataProvider>) -> Self {
        Self {
            provider,
        }
    }
}

/// Everything a CDC table function needs about its target table.
struct CdcTableContext {
    table_id: i64,
    object_store_url: datafusion::execution::object_store::ObjectStoreUrl,
    table_path: String,
    table_schema: Arc<arrow::datatypes::Schema>,
}

/// Split `'schema.table'` (defaulting to schema `main`).
fn parse_table_name(table_name: &str) -> (&str, &str) {
    if let Some(dot_pos) = table_name.find('.') {
        (&table_name[..dot_pos], &table_name[dot_pos + 1..])
    } else {
        ("main", table_name)
    }
}

/// Parse a snapshot-time string as stored by the catalog backends. Accepts
/// `YYYY-MM-DD HH:MM:SS[.fff]` / `YYYY-MM-DDTHH:MM:SS[.fff]` / `YYYY-MM-DD`,
/// with an optional trailing `Z`, ` UTC`, `+00` or `+00:00` (times are UTC).
fn parse_snapshot_timestamp(raw: &str) -> Option<chrono::NaiveDateTime> {
    let mut t = raw.trim();
    for suffix in ["Z", " UTC", "+00:00", "+00"] {
        if let Some(stripped) = t.strip_suffix(suffix) {
            t = stripped.trim();
            break;
        }
    }
    for fmt in ["%Y-%m-%d %H:%M:%S%.f", "%Y-%m-%dT%H:%M:%S%.f"] {
        if let Ok(ts) = chrono::NaiveDateTime::parse_from_str(t, fmt) {
            return Some(ts);
        }
    }
    chrono::NaiveDate::parse_from_str(t, "%Y-%m-%d")
        .ok()
        .and_then(|d| d.and_hms_opt(0, 0, 0))
}

/// Resolve a snapshot bound argument: an integer snapshot id, or a timestamp
/// (a string literal or a `TIMESTAMP '...'` cast) resolved against the
/// catalog's snapshot times — a START bound resolves to the FIRST snapshot
/// at-or-after the timestamp, an END bound to the LAST snapshot at-or-before
/// it, matching official DuckLake's `AT (TIMESTAMP => ...)` semantics.
fn resolve_snapshot_bound(
    provider: &Arc<dyn MetadataProvider>,
    expr: &Expr,
    fn_name: &str,
    arg_name: &str,
    is_start: bool,
) -> DataFusionResult<i64> {
    // `TIMESTAMP '...'` parses as a cast around a literal; unwrap it.
    let mut expr = expr;
    while let Expr::Cast(cast) = expr {
        expr = cast.expr.as_ref();
    }
    let ts: chrono::NaiveDateTime = match expr {
        Expr::Literal(ScalarValue::Int64(Some(v)), _) => return Ok(*v),
        Expr::Literal(ScalarValue::Int32(Some(v)), _) => return Ok(*v as i64),
        Expr::Literal(ScalarValue::Utf8(Some(s)), _) => match parse_snapshot_timestamp(s) {
            Some(ts) => ts,
            None => {
                return plan_err!(
                    "{arg_name} of {fn_name}() is not a valid timestamp: '{s}' \
                     (expected 'YYYY-MM-DD[ HH:MM:SS[.ffffff]]', UTC)"
                );
            },
        },
        Expr::Literal(other, _) => {
            let unit_and_value = match other {
                ScalarValue::TimestampSecond(Some(v), _) => Some((*v, 1_000_000_000i64)),
                ScalarValue::TimestampMillisecond(Some(v), _) => Some((*v, 1_000_000)),
                ScalarValue::TimestampMicrosecond(Some(v), _) => Some((*v, 1_000)),
                ScalarValue::TimestampNanosecond(Some(v), _) => Some((*v, 1)),
                _ => None,
            };
            match unit_and_value {
                Some((v, factor)) => {
                    let nanos = v.saturating_mul(factor);
                    match chrono::DateTime::from_timestamp(
                        nanos.div_euclid(1_000_000_000),
                        (nanos.rem_euclid(1_000_000_000)) as u32,
                    ) {
                        Some(dt) => dt.naive_utc(),
                        None => {
                            return plan_err!(
                                "{arg_name} of {fn_name}() is out of timestamp range"
                            );
                        },
                    }
                },
                None => {
                    return plan_err!(
                        "{arg_name} of {fn_name}() must be an integer snapshot id or a \
                         timestamp literal"
                    );
                },
            }
        },
        _ => {
            return plan_err!(
                "{arg_name} of {fn_name}() must be an integer snapshot id or a timestamp \
                 literal"
            );
        },
    };

    let snapshots = provider
        .list_snapshots()
        .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?;
    let mut best: Option<i64> = None;
    for s in &snapshots {
        let Some(t) = s.timestamp.as_deref().and_then(parse_snapshot_timestamp) else {
            continue;
        };
        let candidate = if is_start {
            t >= ts
        } else {
            t <= ts
        };
        if candidate {
            best = Some(match best {
                None => s.snapshot_id,
                Some(b) if is_start => b.min(s.snapshot_id),
                Some(b) => b.max(s.snapshot_id),
            });
        }
    }
    best.ok_or_else(|| {
        datafusion::error::DataFusionError::Plan(format!(
            "{fn_name}(): no snapshot {} timestamp {ts}",
            if is_start {
                "at or after"
            } else {
                "at or before"
            },
        ))
    })
}

/// Parse the common `('schema.table', start, end)` argument list of the CDC
/// table functions and resolve the target table.
fn parse_cdc_args(
    provider: &Arc<dyn MetadataProvider>,
    exprs: &[Expr],
    fn_name: &str,
) -> DataFusionResult<(i64, i64, CdcTableContext)> {
    if exprs.len() != 3 {
        return plan_err!(
            "{fn_name}() requires 3 arguments: \
             {fn_name}('schema.table', start_snapshot, end_snapshot)"
        );
    }
    let table_name = match &exprs[0] {
        Expr::Literal(ScalarValue::Utf8(Some(name)), _) => name.clone(),
        _ => {
            return plan_err!(
                "First argument to {fn_name}() must be a string literal \
                 (e.g., 'main.users' or 'users')"
            );
        },
    };
    let start_snapshot =
        resolve_snapshot_bound(provider, &exprs[1], fn_name, "start_snapshot", true)?;
    let end_snapshot = resolve_snapshot_bound(provider, &exprs[2], fn_name, "end_snapshot", false)?;
    if start_snapshot > end_snapshot {
        return plan_err!(
            "start_snapshot ({}) must be less than or equal to end_snapshot ({})",
            start_snapshot,
            end_snapshot
        );
    }

    let (schema_name, table_name_only) = parse_table_name(&table_name);
    let snapshot_id = provider
        .get_current_snapshot()
        .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?;
    let schema = provider
        .get_schema_by_name(schema_name, snapshot_id)
        .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?
        .ok_or_else(|| {
            datafusion::error::DataFusionError::Plan(format!(
                "Schema '{}' not found in catalog",
                schema_name
            ))
        })?;
    let table = provider
        .get_table_by_name(schema.schema_id, table_name_only, snapshot_id)
        .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?
        .ok_or_else(|| {
            datafusion::error::DataFusionError::Plan(format!(
                "Table '{}.{}' not found in catalog",
                schema_name, table_name_only
            ))
        })?;
    let data_path = provider
        .get_data_path()
        .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?;
    let (object_store_url, catalog_path) = parse_object_store_url(&data_path)
        .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?;
    let schema_path = resolve_path(&catalog_path, &schema.path, schema.path_is_relative)
        .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?;
    let table_path = resolve_path(&schema_path, &table.path, table.path_is_relative)
        .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?;
    let columns = provider
        .get_table_structure(table.table_id, end_snapshot)
        .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?;
    let table_schema = Arc::new(
        build_arrow_schema(&columns)
            .map_err(|e| datafusion::error::DataFusionError::External(Box::new(e)))?,
    );

    Ok((
        start_snapshot,
        end_snapshot,
        CdcTableContext {
            table_id: table.table_id,
            object_store_url,
            table_path,
            table_schema,
        },
    ))
}

impl TableFunctionImpl for DucklakeTableChangesFunction {
    fn call(&self, exprs: &[Expr]) -> DataFusionResult<Arc<dyn TableProvider>> {
        let (start_snapshot, end_snapshot, ctx) =
            parse_cdc_args(&self.provider, exprs, "ducklake_table_changes")?;
        Ok(Arc::new(TableChangesTable::new(
            self.provider.clone(),
            ctx.table_id,
            start_snapshot,
            end_snapshot,
            Arc::new(ctx.object_store_url),
            ctx.table_path,
            ctx.table_schema,
        )))
    }
}

#[derive(Debug)]
pub struct DucklakeTableDeletionsFunction {
    provider: Arc<dyn MetadataProvider>,
}

impl DucklakeTableDeletionsFunction {
    pub fn new(provider: Arc<dyn MetadataProvider>) -> Self {
        Self {
            provider,
        }
    }
}

impl TableFunctionImpl for DucklakeTableDeletionsFunction {
    fn call(&self, exprs: &[Expr]) -> DataFusionResult<Arc<dyn TableProvider>> {
        let (start_snapshot, end_snapshot, ctx) =
            parse_cdc_args(&self.provider, exprs, "ducklake_table_deletions")?;
        Ok(Arc::new(TableDeletionsTable::new(
            self.provider.clone(),
            ctx.table_id,
            start_snapshot,
            end_snapshot,
            Arc::new(ctx.object_store_url),
            ctx.table_path,
            ctx.table_schema,
        )))
    }
}

/// `ducklake_table_insertions('schema.table', start, end)`: every row added
/// in the inclusive snapshot window, with `(snapshot_id, rowid)` leading and
/// no `change_type` column — official DuckLake's insertions feed.
#[derive(Debug)]
pub struct DucklakeTableInsertionsFunction {
    provider: Arc<dyn MetadataProvider>,
}

impl DucklakeTableInsertionsFunction {
    pub fn new(provider: Arc<dyn MetadataProvider>) -> Self {
        Self {
            provider,
        }
    }
}

impl TableFunctionImpl for DucklakeTableInsertionsFunction {
    fn call(&self, exprs: &[Expr]) -> DataFusionResult<Arc<dyn TableProvider>> {
        let (start_snapshot, end_snapshot, ctx) =
            parse_cdc_args(&self.provider, exprs, "ducklake_table_insertions")?;
        Ok(Arc::new(TableInsertionsTable::new(
            self.provider.clone(),
            ctx.table_id,
            start_snapshot,
            end_snapshot,
            Arc::new(ctx.object_store_url),
            ctx.table_path,
            ctx.table_schema,
        )))
    }
}

/// Registers all ducklake_*() table functions with a SessionContext.
pub fn register_ducklake_functions(
    ctx: &datafusion::execution::context::SessionContext,
    provider: Arc<dyn MetadataProvider>,
) {
    ctx.register_udtf(
        "ducklake_snapshots",
        Arc::new(DucklakeSnapshotsFunction::new(provider.clone())),
    );
    ctx.register_udtf(
        "ducklake_table_info",
        Arc::new(DucklakeTableInfoFunction::new(provider.clone())),
    );
    ctx.register_udtf(
        "ducklake_list_files",
        Arc::new(DucklakeListFilesFunction::new(provider.clone())),
    );
    ctx.register_udtf(
        "ducklake_table_changes",
        Arc::new(DucklakeTableChangesFunction::new(provider.clone())),
    );
    ctx.register_udtf(
        "ducklake_table_insertions",
        Arc::new(DucklakeTableInsertionsFunction::new(provider.clone())),
    );
    ctx.register_udtf(
        "ducklake_table_deletions",
        Arc::new(DucklakeTableDeletionsFunction::new(provider.clone())),
    );
}

#[cfg(test)]
mod snapshot_bound_tests {
    use super::parse_snapshot_timestamp;

    #[test]
    fn parses_backend_snapshot_time_formats() {
        for raw in [
            "2026-07-16 12:34:56.123456",
            "2026-07-16 12:34:56",
            "2026-07-16T12:34:56.123456",
            "2026-07-16 12:34:56+00",
            "2026-07-16 12:34:56+00:00",
            "2026-07-16 12:34:56 UTC",
            "2026-07-16T12:34:56Z",
        ] {
            assert!(
                parse_snapshot_timestamp(raw).is_some(),
                "failed to parse {raw:?}"
            );
        }
        // A bare date is midnight UTC.
        assert_eq!(
            parse_snapshot_timestamp("2026-07-16").unwrap(),
            parse_snapshot_timestamp("2026-07-16 00:00:00").unwrap()
        );
    }

    #[test]
    fn rejects_garbage() {
        assert!(parse_snapshot_timestamp("not a time").is_none());
        assert!(parse_snapshot_timestamp("").is_none());
    }
}