delta_kernel 0.30.1

Core crate providing a Delta/Deltalake implementation focused on interoperability with a wide range of query engines.
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
//! IcebergCompat invariant checks shared across versions.
//!
//! Each `delta.enableIcebergCompatV{N}` version owns a submodule (currently
//! [`v2`] and [`v3`]), and exposes a single
//! `pub(crate) const V{N}_VALIDATOR: IcebergCompatValidator`. Callers feed that
//! constant and the validation context to [`validate_iceberg_compat_if_needed`].

pub(crate) mod v2;
pub(crate) mod v3;

use crate::schema::PrimitiveType::{Byte, Decimal, Double, Float, Integer, Long, Short};
use crate::schema::{ColumnMetadataKey, DataType, MetadataValue, StructField};
use crate::table_configuration::TableConfiguration;
use crate::table_features::TableFeature;
use crate::transforms::{transform_output_type, SchemaTransform};
use crate::{KernelError, KernelResult};

pub(crate) enum IcebergCompatVersion {
    // TODO: Add V1 when kernel supports it
    V2,
    V3,
}

impl IcebergCompatVersion {
    pub(super) fn as_table_feature(&self) -> TableFeature {
        match self {
            Self::V2 => TableFeature::IcebergCompatV2,
            Self::V3 => TableFeature::IcebergCompatV3,
        }
    }
}

type IcebergCompatCheckFn = fn(&TableConfiguration) -> KernelResult<()>;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum IcebergCompatValidationContext {
    TableConfiguration,
    Write,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum IcebergCompatCheckMode {
    Always,
    WriteOnly,
}

/// A single invariant that must hold when an icebergCompat version is enabled.
pub(crate) struct IcebergCompatCheck {
    mode: IcebergCompatCheckMode,
    check: IcebergCompatCheckFn,
}

impl IcebergCompatCheck {
    pub(super) const fn always(check: IcebergCompatCheckFn) -> Self {
        Self {
            mode: IcebergCompatCheckMode::Always,
            check,
        }
    }

    pub(super) const fn write_only(check: IcebergCompatCheckFn) -> Self {
        Self {
            mode: IcebergCompatCheckMode::WriteOnly,
            check,
        }
    }

    fn runs_in(&self, context: IcebergCompatValidationContext) -> bool {
        self.mode == IcebergCompatCheckMode::Always
            || context == IcebergCompatValidationContext::Write
    }
}

/// Pairs an [`IcebergCompatVersion`] with the ordered list of invariants that
/// run when that version is enabled.
pub(crate) struct IcebergCompatValidator {
    pub(super) version: IcebergCompatVersion,
    pub(super) checks: &'static [IcebergCompatCheck],
}

pub(crate) fn validate_iceberg_compat_if_needed(
    tc: &TableConfiguration,
    validator: &IcebergCompatValidator,
    context: IcebergCompatValidationContext,
) -> KernelResult<()> {
    if !tc.is_feature_enabled(&validator.version.as_table_feature()) {
        return Ok(());
    }
    for check in validator.checks {
        if check.runs_in(context) {
            (check.check)(tc)?;
        }
    }
    Ok(())
}

/// Walks `tc.logical_schema()` and errors on the first field whose data type
/// fails `is_supported`. Reports the offending column path + type in the error.
pub(super) fn check_only_supported_types(
    tc: &TableConfiguration,
    is_supported: fn(&DataType) -> bool,
    feature_label: &str,
) -> KernelResult<()> {
    let mut v = TypeAllowListVisitor {
        is_supported,
        offender: None,
        path: vec![],
    };
    v.transform_struct(&tc.logical_schema());
    let Some(offender) = v.offender else {
        return Ok(());
    };
    Err(KernelError::generic(format!(
        "{feature_label} does not support type at column: {offender}",
    )))
}

struct TypeAllowListVisitor {
    is_supported: fn(&DataType) -> bool,
    /// The first unsupported field encountered, rendered as `<dotted.path> (<type>)`, or `None`
    /// if every field is supported.
    ///
    /// For example, given schema `struct<events: array<struct<ts: timestamp>>>` (and assuming
    /// `timestamp` is unsupported), the offender is `events.element.ts (timestamp)`.
    offender: Option<String>,
    path: Vec<String>,
}

impl TypeAllowListVisitor {
    /// If `dt` isn't allowed, record it as the offender. No-op if an offender is already set.
    fn check(&mut self, dt: &DataType) {
        if self.offender.is_none() && !(self.is_supported)(dt) {
            self.offender = Some(format!("{} ({})", self.path.join("."), dt));
        }
    }

    /// Descend into a nested element type (array element, map key, or map value).
    fn visit_nested(&mut self, seg: &str, ele_type: &DataType) {
        if self.offender.is_some() {
            return;
        }
        self.path.push(seg.to_string());
        self.check(ele_type);
        self.transform(ele_type);
        self.path.pop();
    }
}

impl<'a> SchemaTransform<'a> for TypeAllowListVisitor {
    transform_output_type!(|'a, T| ());

    fn transform_struct_field(&mut self, f: &'a StructField) {
        if self.offender.is_some() {
            return;
        }
        self.path.push(f.name().to_string());
        self.check(f.data_type());
        self.recurse_into_struct_field(f);
        self.path.pop();
    }

    fn transform_array_element(&mut self, etype: &'a DataType) {
        self.visit_nested("element", etype);
    }

    fn transform_map_key(&mut self, etype: &'a DataType) {
        self.visit_nested("key", etype);
    }

    fn transform_map_value(&mut self, etype: &'a DataType) {
        self.visit_nested("value", etype);
    }
}

/// Rejects fields carrying the legacy `parquet.field.nested.ids` metadata.
///
/// See <https://github.com/delta-io/delta/issues/6688>.
pub(super) fn check_no_legacy_nested_ids(tc: &TableConfiguration) -> KernelResult<()> {
    let mut v = LegacyNestedIdsVisitor {
        path: vec![],
        offender: None,
    };
    v.transform_struct(&tc.logical_schema());
    let Some(offender) = v.offender else {
        return Ok(());
    };
    Err(KernelError::generic(format!(
        "field `{offender}` carries deprecated `{}` metadata; use `{}` instead. \
         See https://github.com/delta-io/delta/issues/6688",
        ColumnMetadataKey::ParquetFieldNestedIds.as_ref(),
        ColumnMetadataKey::ColumnMappingNestedIds.as_ref(),
    )))
}

struct LegacyNestedIdsVisitor {
    path: Vec<String>,
    offender: Option<String>,
}

impl<'a> SchemaTransform<'a> for LegacyNestedIdsVisitor {
    transform_output_type!(|'a, T| ());

    fn transform_struct_field(&mut self, f: &'a StructField) {
        if self.offender.is_some() {
            return;
        }
        self.path.push(f.name().to_string());
        if f.metadata()
            .contains_key(ColumnMetadataKey::ParquetFieldNestedIds.as_ref())
        {
            self.offender = Some(self.path.join("."));
            return;
        }
        self.recurse_into_struct_field(f);
        self.path.pop();
    }
}

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct TypeChange {
    from_type: DataType,
    to_type: DataType,
}

/// The widenings legal under Iceberg V2, and therefore under `icebergCompatV1`/`V2`.
/// `icebergCompatV3` currently also permits exactly this set; if V3 ever allows additional
/// widenings those must be added separately, not by expanding this list.
fn is_iceberg_allowed_type_change(from: &DataType, to: &DataType) -> bool {
    match (from, to) {
        (DataType::Primitive(Byte), DataType::Primitive(Short | Integer | Long)) => true,
        (DataType::Primitive(Short), DataType::Primitive(Integer | Long)) => true,
        (DataType::Primitive(Integer), DataType::Primitive(Long)) => true,
        (DataType::Primitive(Float), DataType::Primitive(Double)) => true,
        (DataType::Primitive(Decimal(from_decimal)), DataType::Primitive(Decimal(to_decimal))) => {
            from_decimal.scale() == to_decimal.scale()
                && to_decimal.precision() > from_decimal.precision()
        }
        _ => false,
    }
}

struct TypeChangesValidator {
    path: Vec<String>,
    version: IcebergCompatVersion,
}

impl TypeChangesValidator {
    fn validate_field_type_changes(&self, field: &StructField) -> KernelResult<()> {
        let type_changes_key = ColumnMetadataKey::TypeChanges.as_ref();
        let Some(metadata) = field.metadata().get(type_changes_key) else {
            return Ok(());
        };
        let path = self.path.join(".");
        let MetadataValue::Other(value) = metadata else {
            return Err(KernelError::schema(format!(
                "Field '{path}' has a non-array `{type_changes_key}` annotation: \
                 {metadata}"
            )));
        };
        let type_changes: Vec<TypeChange> = serde_json::from_value(value.clone()).map_err(|e| {
            KernelError::schema(format!(
                "Field '{path}' has an invalid `{type_changes_key}` annotation: {e}"
            ))
        })?;
        for type_change in type_changes {
            if !is_iceberg_allowed_type_change(&type_change.from_type, &type_change.to_type) {
                return Err(KernelError::schema(format!(
                    "{} does not support type change on field '{path}': {} -> {}",
                    self.version.as_table_feature(),
                    type_change.from_type,
                    type_change.to_type
                )));
            }
        }
        Ok(())
    }
}

impl<'a> SchemaTransform<'a> for TypeChangesValidator {
    transform_output_type!(|'a, T| KernelResult<()>);

    fn transform_struct_field(&mut self, field: &'a StructField) -> KernelResult<()> {
        self.path.push(field.name().clone());
        let result = self
            .validate_field_type_changes(field)
            .and_then(|_| self.recurse_into_struct_field(field));
        self.path.pop();
        result
    }

    fn transform_array_element(&mut self, etype: &'a DataType) -> KernelResult<()> {
        self.path.push("element".to_string());
        let result = self.transform(etype);
        self.path.pop();
        result
    }

    fn transform_map_key(&mut self, ktype: &'a DataType) -> KernelResult<()> {
        self.path.push("key".to_string());
        let result = self.transform(ktype);
        self.path.pop();
        result
    }

    fn transform_map_value(&mut self, vtype: &'a DataType) -> KernelResult<()> {
        self.path.push("value".to_string());
        let result = self.transform(vtype);
        self.path.pop();
        result
    }

    fn transform_variant(&mut self, _stype: &'a crate::schema::StructType) -> KernelResult<()> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use rstest::rstest;

    use super::*;
    use crate::schema::{schema, ArrayType, MetadataValue, PrimitiveType, StructType};

    // === LegacyNestedIdsVisitor: parquet.field.nested.ids detection ===

    fn field_with_metadata(name: &str, dtype: impl Into<DataType>, key: &str) -> StructField {
        let mut f = StructField::nullable(name, dtype);
        f.metadata.insert(
            key.to_string(),
            MetadataValue::Other(serde_json::json!({ "x.element": 1 })),
        );
        f
    }

    // `array<integer>` fields carrying nested-id metadata under the new vs. legacy key.
    fn nested_ids_field(name: &str) -> StructField {
        let ty = ArrayType::new(DataType::INTEGER, true);
        field_with_metadata(name, ty, ColumnMetadataKey::ColumnMappingNestedIds.as_ref())
    }
    fn legacy_ids_field(name: &str) -> StructField {
        let ty = ArrayType::new(DataType::INTEGER, true);
        field_with_metadata(name, ty, ColumnMetadataKey::ParquetFieldNestedIds.as_ref())
    }

    fn simple_schema() -> StructType {
        schema! {
            not_null "id": INTEGER,
            nullable "name": STRING,
        }
    }

    fn schema_with_good_nested_ids() -> StructType {
        schema! { (nested_ids_field("x")) }
    }

    fn schema_with_legacy_at(name: &str) -> StructType {
        schema! { (legacy_ids_field(name)) }
    }

    fn schema_struct_with_legacy_at_inner() -> StructType {
        schema! { nullable "parent": { (legacy_ids_field("inner")) } }
    }

    fn schema_array_struct_with_legacy_at_inner() -> StructType {
        schema! { nullable "arr": [ nullable { (legacy_ids_field("inner")) } ] }
    }

    fn schema_map_value_struct_with_legacy_at_inner() -> StructType {
        schema! { nullable "m": { STRING => nullable { (legacy_ids_field("inner")) } } }
    }

    fn schema_two_legacy_fields() -> StructType {
        schema! { (legacy_ids_field("a")), (legacy_ids_field("b")) }
    }

    #[rstest]
    #[case::clean_schema(simple_schema(), None)]
    #[case::column_mapping_nested_id_key_only(schema_with_good_nested_ids(), None)]
    #[case::top_level_legacy(schema_with_legacy_at("top"), Some("top".to_string()))]
    #[case::nested_struct_legacy(schema_struct_with_legacy_at_inner(), Some("parent.inner".to_string()))]
    #[case::array_struct_legacy(schema_array_struct_with_legacy_at_inner(), Some("arr.inner".to_string()))]
    #[case::map_value_struct_legacy(schema_map_value_struct_with_legacy_at_inner(), Some("m.inner".to_string()))]
    #[case::first_offender_wins(schema_two_legacy_fields(), Some("a".to_string()))]
    fn legacy_nested_ids_visitor_finds_first_offender(
        #[case] schema: StructType,
        #[case] expected: Option<String>,
    ) {
        let mut v = LegacyNestedIdsVisitor {
            path: vec![],
            offender: None,
        };
        v.transform_struct(&schema);
        assert_eq!(v.offender, expected);
    }

    // === TypeAllowlistVisitor tests ===

    /// Narrow allowlist: Integer, String, and Struct.
    fn narrow_allowlist(dt: &DataType) -> bool {
        matches!(
            dt,
            DataType::Primitive(PrimitiveType::Integer | PrimitiveType::String)
                | DataType::Struct(_)
        )
    }

    /// Wide allowlist: Integer, String, Struct, Array, Map.
    fn wide_allowlist(dt: &DataType) -> bool {
        matches!(
            dt,
            DataType::Primitive(PrimitiveType::Integer | PrimitiveType::String)
                | DataType::Struct(_)
                | DataType::Array(_)
                | DataType::Map(_)
        )
    }

    fn schema_struct_with_float_inner() -> StructType {
        schema! { nullable "s": { nullable "f": FLOAT } }
    }

    fn schema_array_of_float() -> StructType {
        schema! { nullable "arr": [ nullable FLOAT ] }
    }

    fn schema_map_key_float() -> StructType {
        schema! { nullable "m": { FLOAT => nullable STRING } }
    }

    fn schema_map_value_float() -> StructType {
        schema! { nullable "m": { STRING => nullable FLOAT } }
    }

    fn schema_float_long() -> StructType {
        schema! {
            nullable "a": LONG,
            nullable "b": FLOAT,
        }
    }

    /// `a: array<map<string, array<float>>>` followed by a sibling `b: array<float>`.
    fn schema_deep_nested_float() -> StructType {
        schema! {
            nullable "a": [ nullable { STRING => nullable [ nullable FLOAT ] } ],
            nullable "b": [ nullable FLOAT ],
        }
    }

    #[rstest]
    #[case::accept_int_string(narrow_allowlist, simple_schema(), None)]
    #[case::reject_top_level_array(
        narrow_allowlist,
        schema_with_legacy_at("top"),
        Some("top (array<integer>)".to_string()),
    )]
    #[case::reject_nested_primitive_in_struct(
        narrow_allowlist,
        schema_struct_with_float_inner(),
        Some("s.f (float)".to_string()),
    )]
    #[case::reject_primitive_inside_array(
        wide_allowlist,
        schema_array_of_float(),
        Some("arr.element (float)".to_string()),
    )]
    #[case::reject_primitive_inside_map_key(
        wide_allowlist,
        schema_map_key_float(),
        Some("m.key (float)".to_string()),
    )]
    #[case::reject_primitive_inside_map_value(
        wide_allowlist,
        schema_map_value_float(),
        Some("m.value (float)".to_string()),
    )]
    #[case::first_offender_wins(
        wide_allowlist,
        schema_float_long(),
        Some("a (long)".to_string()),
    )]
    #[case::reject_deep_then_short_circuits_sibling(
        wide_allowlist,
        schema_deep_nested_float(),
        Some("a.element.value.element (float)".to_string()),
    )]
    fn type_allowlist_visitor(
        #[case] is_supported: fn(&DataType) -> bool,
        #[case] schema: StructType,
        #[case] expected: Option<String>,
    ) {
        let mut v = TypeAllowListVisitor {
            is_supported,
            offender: None,
            path: vec![],
        };
        v.transform_struct(&schema);
        assert_eq!(v.offender, expected);
    }

    fn decimal(precision: u8, scale: u8) -> DataType {
        DataType::decimal(precision, scale).unwrap()
    }

    #[rstest]
    // Allowed: integer promotions, float->double, decimal precision increase at equal scale.
    #[case::byte_short(DataType::BYTE, DataType::SHORT, true)]
    #[case::byte_integer(DataType::BYTE, DataType::INTEGER, true)]
    #[case::byte_long(DataType::BYTE, DataType::LONG, true)]
    #[case::short_integer(DataType::SHORT, DataType::INTEGER, true)]
    #[case::short_long(DataType::SHORT, DataType::LONG, true)]
    #[case::integer_long(DataType::INTEGER, DataType::LONG, true)]
    #[case::float_double(DataType::FLOAT, DataType::DOUBLE, true)]
    #[case::decimal_precision_increase(decimal(10, 2), decimal(20, 2), true)]
    // Rejected: promotions Iceberg V2 forbids even though Type Widening permits them, plus
    // non-widenings.
    #[case::integer_double(DataType::INTEGER, DataType::DOUBLE, false)]
    #[case::long_double(DataType::LONG, DataType::DOUBLE, false)]
    #[case::date_timestamp_ntz(DataType::DATE, DataType::TIMESTAMP_NTZ, false)]
    #[case::integer_decimal(DataType::INTEGER, decimal(11, 1), false)]
    #[case::decimal_scale_increase(decimal(10, 2), decimal(20, 5), false)]
    #[case::decimal_precision_decrease(decimal(20, 2), decimal(10, 2), false)]
    #[case::no_op_string(DataType::STRING, DataType::STRING, false)]
    fn iceberg_allowed_type_change_matches_iceberg_v2_rules(
        #[case] from: DataType,
        #[case] to: DataType,
        #[case] expected: bool,
    ) {
        assert_eq!(is_iceberg_allowed_type_change(&from, &to), expected);
    }
}