delta_kernel 0.6.0

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
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
use std::collections::HashMap;
use std::iter;
use std::sync::{Arc, LazyLock};
use std::time::{SystemTime, UNIX_EPOCH};

use crate::actions::schemas::{GetNullableContainerStructField, GetStructField};
use crate::actions::COMMIT_INFO_NAME;
use crate::actions::{get_log_add_schema, get_log_commit_info_schema};
use crate::error::Error;
use crate::expressions::{column_expr, Scalar, StructData};
use crate::path::ParsedLogPath;
use crate::schema::{SchemaRef, StructField, StructType};
use crate::snapshot::Snapshot;
use crate::{DataType, DeltaResult, Engine, EngineData, Expression, Version};

use itertools::chain;
use url::Url;

const KERNEL_VERSION: &str = env!("CARGO_PKG_VERSION");
const UNKNOWN_OPERATION: &str = "UNKNOWN";

pub(crate) static WRITE_METADATA_SCHEMA: LazyLock<SchemaRef> = LazyLock::new(|| {
    Arc::new(StructType::new(vec![
        <String>::get_struct_field("path"),
        <HashMap<String, String>>::get_nullable_container_struct_field("partitionValues"),
        <i64>::get_struct_field("size"),
        <i64>::get_struct_field("modificationTime"),
        <bool>::get_struct_field("dataChange"),
    ]))
});

/// Get the expected schema for engine data passed to [`add_write_metadata`].
///
/// [`add_write_metadata`]: crate::transaction::Transaction::add_write_metadata
pub fn get_write_metadata_schema() -> &'static SchemaRef {
    &WRITE_METADATA_SCHEMA
}

/// A transaction represents an in-progress write to a table. After creating a transaction, changes
/// to the table may be staged via the transaction methods before calling `commit` to commit the
/// changes to the table.
///
/// # Examples
///
/// ```rust,ignore
/// // create a transaction
/// let mut txn = table.new_transaction(&engine)?;
/// // stage table changes (right now only commit info)
/// txn.commit_info(Box::new(ArrowEngineData::new(engine_commit_info)));
/// // commit! (consume the transaction)
/// txn.commit(&engine)?;
/// ```
pub struct Transaction {
    read_snapshot: Arc<Snapshot>,
    operation: Option<String>,
    commit_info: Option<Arc<dyn EngineData>>,
    write_metadata: Vec<Box<dyn EngineData>>,
}

impl std::fmt::Debug for Transaction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!(
            "Transaction {{ read_snapshot version: {}, commit_info: {} }}",
            self.read_snapshot.version(),
            self.commit_info.is_some()
        ))
    }
}

impl Transaction {
    /// Create a new transaction from a snapshot. The snapshot will be used to read the current
    /// state of the table (e.g. to read the current version).
    ///
    /// Instead of using this API, the more typical (user-facing) API is
    /// [Table::new_transaction](crate::table::Table::new_transaction) to create a transaction from
    /// a table automatically backed by the latest snapshot.
    pub(crate) fn try_new(snapshot: impl Into<Arc<Snapshot>>) -> DeltaResult<Self> {
        let read_snapshot = snapshot.into();

        // important! before a read/write to the table we must check it is supported
        read_snapshot.protocol().ensure_write_supported()?;

        Ok(Transaction {
            read_snapshot,
            operation: None,
            commit_info: None,
            write_metadata: vec![],
        })
    }

    /// Consume the transaction and commit it to the table. The result is a [CommitResult] which
    /// will include the failed transaction in case of a conflict so the user can retry.
    pub fn commit(self, engine: &dyn Engine) -> DeltaResult<CommitResult> {
        // step one: construct the iterator of actions we want to commit
        let engine_commit_info = self
            .commit_info
            .as_ref()
            .ok_or_else(|| Error::MissingCommitInfo)?;
        let commit_info = generate_commit_info(
            engine,
            self.operation.as_deref(),
            engine_commit_info.as_ref(),
        );
        let adds = generate_adds(engine, self.write_metadata.iter().map(|a| a.as_ref()));
        let actions = chain(iter::once(commit_info), adds);

        // step two: set new commit version (current_version + 1) and path to write
        let commit_version = self.read_snapshot.version() + 1;
        let commit_path =
            ParsedLogPath::new_commit(self.read_snapshot.table_root(), commit_version)?;

        // step three: commit the actions as a json file in the log
        let json_handler = engine.get_json_handler();
        match json_handler.write_json_file(&commit_path.location, Box::new(actions), false) {
            Ok(()) => Ok(CommitResult::Committed(commit_version)),
            Err(Error::FileAlreadyExists(_)) => Ok(CommitResult::Conflict(self, commit_version)),
            Err(e) => Err(e),
        }
    }

    /// Set the operation that this transaction is performing. This string will be persisted in the
    /// commit and visible to anyone who describes the table history.
    pub fn with_operation(mut self, operation: String) -> Self {
        self.operation = Some(operation);
        self
    }

    /// WARNING: This is an unstable API and will likely change in the future.
    ///
    /// Add commit info to the transaction. This is commit-wide metadata that is written as the
    /// first action in the commit. The engine data passed here must have exactly one row, and we
    /// only read one column: `engineCommitInfo` which must be a map<string, string> encoding the
    /// metadata.
    ///
    /// The engine is required to provide commit info before committing the transaction. If the
    /// engine would like to omit engine-specific commit info, it can do so by passing pass a
    /// commit_info engine data chunk with one row and one column of type `Map<string, string>`
    /// that can either be `null` or contain an empty map.
    ///
    /// Any other columns in the data chunk are ignored.
    pub fn with_commit_info(mut self, commit_info: Box<dyn EngineData>) -> Self {
        self.commit_info = Some(commit_info.into());
        self
    }

    // Generate the logical-to-physical transform expression which must be evaluated on every data
    // chunk before writing. At the moment, this is a transaction-wide expression.
    fn generate_logical_to_physical(&self) -> Expression {
        // for now, we just pass through all the columns except partition columns.
        // note this is _incorrect_ if table config deems we need partition columns.
        let partition_columns = &self.read_snapshot.metadata().partition_columns;
        let fields = self.read_snapshot.schema().fields();
        let fields = fields
            .filter(|f| !partition_columns.contains(f.name()))
            .map(|f| Expression::column([f.name()]));
        Expression::struct_from(fields)
    }

    /// Get the write context for this transaction. At the moment, this is constant for the whole
    /// transaction.
    // Note: after we introduce metadata updates (modify table schema, etc.), we need to make sure
    // that engines cannot call this method after a metadata change, since the write context could
    // have invalid metadata.
    pub fn get_write_context(&self) -> WriteContext {
        let target_dir = self.read_snapshot.table_root();
        let snapshot_schema = self.read_snapshot.schema();
        let logical_to_physical = self.generate_logical_to_physical();
        WriteContext::new(
            target_dir.clone(),
            Arc::new(snapshot_schema.clone()),
            logical_to_physical,
        )
    }

    /// Add write metadata about files to include in the transaction. This API can be called
    /// multiple times to add multiple batches.
    ///
    /// The expected schema for `write_metadata` is given by [`get_write_metadata_schema`].
    pub fn add_write_metadata(&mut self, write_metadata: Box<dyn EngineData>) {
        self.write_metadata.push(write_metadata);
    }
}

// convert write_metadata into add actions using an expression to transform the data in a single
// pass
fn generate_adds<'a>(
    engine: &dyn Engine,
    write_metadata: impl Iterator<Item = &'a dyn EngineData> + Send + 'a,
) -> impl Iterator<Item = DeltaResult<Box<dyn EngineData>>> + Send + 'a {
    let expression_handler = engine.get_expression_handler();
    let write_metadata_schema = get_write_metadata_schema();
    let log_schema = get_log_add_schema();

    write_metadata.map(move |write_metadata_batch| {
        let adds_expr = Expression::struct_from([Expression::struct_from(
            write_metadata_schema
                .fields()
                .map(|f| Expression::column([f.name()])),
        )]);
        let adds_evaluator = expression_handler.get_evaluator(
            write_metadata_schema.clone(),
            adds_expr,
            log_schema.clone().into(),
        );
        adds_evaluator.evaluate(write_metadata_batch)
    })
}

/// WriteContext is data derived from a [`Transaction`] that can be provided to writers in order to
/// write table data.
///
/// [`Transaction`]: struct.Transaction.html
pub struct WriteContext {
    target_dir: Url,
    schema: SchemaRef,
    logical_to_physical: Expression,
}

impl WriteContext {
    fn new(target_dir: Url, schema: SchemaRef, logical_to_physical: Expression) -> Self {
        WriteContext {
            target_dir,
            schema,
            logical_to_physical,
        }
    }

    pub fn target_dir(&self) -> &Url {
        &self.target_dir
    }

    pub fn schema(&self) -> &SchemaRef {
        &self.schema
    }

    pub fn logical_to_physical(&self) -> &Expression {
        &self.logical_to_physical
    }
}

/// Result after committing a transaction. If 'committed', the version is the new version written
/// to the log. If 'conflict', the transaction is returned so the caller can resolve the conflict
/// (along with the version which conflicted).
// TODO(zach): in order to make the returning of a transcation useful, we need to add APIs to
// update the transaction to a new version etc.
#[derive(Debug)]
pub enum CommitResult {
    /// The transaction was successfully committed at the version.
    Committed(Version),
    /// The transaction conflicted with an existing version (at the version given).
    Conflict(Transaction, Version),
}

// given the engine's commit info we want to create commitInfo action to commit (and append more actions to)
fn generate_commit_info(
    engine: &dyn Engine,
    operation: Option<&str>,
    engine_commit_info: &dyn EngineData,
) -> DeltaResult<Box<dyn EngineData>> {
    if engine_commit_info.len() != 1 {
        return Err(Error::InvalidCommitInfo(format!(
            "Engine commit info should have exactly one row, found {}",
            engine_commit_info.len()
        )));
    }

    let timestamp: i64 = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|_| Error::generic("time went backwards"))?
        .as_millis()
        .try_into()
        .map_err(|_| Error::generic("milliseconds since unix_epoch exceeded i64 size"))?;
    let commit_info_exprs = [
        // TODO(zach): we should probably take a timestamp closer to actual commit time?
        Expression::literal(timestamp),
        Expression::literal(operation.unwrap_or(UNKNOWN_OPERATION)),
        // HACK (part 1/2): since we don't have proper map support, we create a literal struct with
        // one null field to create data that serializes as "operationParameters": {}
        Expression::literal(Scalar::Struct(StructData::try_new(
            vec![StructField::new(
                "operation_parameter_int",
                DataType::INTEGER,
                true,
            )],
            vec![Scalar::Null(DataType::INTEGER)],
        )?)),
        Expression::literal(format!("v{}", KERNEL_VERSION)),
        column_expr!("engineCommitInfo"),
    ];
    let commit_info_expr = Expression::struct_from([Expression::struct_from(commit_info_exprs)]);
    let commit_info_schema = get_log_commit_info_schema().as_ref();

    // HACK (part 2/2): we need to modify the commit info schema to match the expression above (a
    // struct with a single null int field).
    let mut commit_info_empty_struct_schema = commit_info_schema.clone();
    let commit_info_field = commit_info_empty_struct_schema
        .fields
        .get_mut(COMMIT_INFO_NAME)
        .ok_or_else(|| Error::missing_column(COMMIT_INFO_NAME))?;
    let DataType::Struct(mut commit_info_data_type) = commit_info_field.data_type().clone() else {
        return Err(Error::internal_error(
            "commit_info_field should be a struct",
        ));
    };
    let engine_commit_info_schema =
        commit_info_data_type.project_as_struct(&["engineCommitInfo"])?;
    let hack_data_type = DataType::Struct(Box::new(StructType::new(vec![StructField::new(
        "hack_operation_parameter_int",
        DataType::INTEGER,
        true,
    )])));

    commit_info_data_type
        .fields
        .get_mut("operationParameters")
        .ok_or_else(|| Error::missing_column("operationParameters"))?
        .data_type = hack_data_type;
    commit_info_field.data_type = DataType::Struct(commit_info_data_type);

    let commit_info_evaluator = engine.get_expression_handler().get_evaluator(
        engine_commit_info_schema.into(),
        commit_info_expr,
        commit_info_empty_struct_schema.into(),
    );

    commit_info_evaluator.evaluate(engine_commit_info)
}

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

    use crate::engine::arrow_data::ArrowEngineData;
    use crate::engine::arrow_expression::ArrowExpressionHandler;
    use crate::schema::MapType;
    use crate::{ExpressionHandler, FileSystemClient, JsonHandler, ParquetHandler};

    use arrow::json::writer::LineDelimitedWriter;
    use arrow::record_batch::RecordBatch;
    use arrow_array::builder::StringBuilder;
    use arrow_schema::Schema as ArrowSchema;
    use arrow_schema::{DataType as ArrowDataType, Field};

    struct ExprEngine(Arc<dyn ExpressionHandler>);

    impl ExprEngine {
        fn new() -> Self {
            ExprEngine(Arc::new(ArrowExpressionHandler))
        }
    }

    impl Engine for ExprEngine {
        fn get_expression_handler(&self) -> Arc<dyn ExpressionHandler> {
            self.0.clone()
        }

        fn get_json_handler(&self) -> Arc<dyn JsonHandler> {
            unimplemented!()
        }

        fn get_parquet_handler(&self) -> Arc<dyn ParquetHandler> {
            unimplemented!()
        }

        fn get_file_system_client(&self) -> Arc<dyn FileSystemClient> {
            unimplemented!()
        }
    }

    fn build_map(entries: Vec<(&str, &str)>) -> arrow_array::MapArray {
        let key_builder = StringBuilder::new();
        let val_builder = StringBuilder::new();
        let names = arrow_array::builder::MapFieldNames {
            entry: "entries".to_string(),
            key: "key".to_string(),
            value: "value".to_string(),
        };
        let mut builder =
            arrow_array::builder::MapBuilder::new(Some(names), key_builder, val_builder);
        for (key, val) in entries {
            builder.keys().append_value(key);
            builder.values().append_value(val);
            builder.append(true).unwrap();
        }
        builder.finish()
    }

    // convert it to JSON just for ease of comparison (and since we ultimately persist as JSON)
    fn as_json_and_scrub_timestamp(data: Box<dyn EngineData>) -> serde_json::Value {
        let record_batch: RecordBatch = data
            .into_any()
            .downcast::<ArrowEngineData>()
            .unwrap()
            .into();

        let buf = Vec::new();
        let mut writer = LineDelimitedWriter::new(buf);
        writer.write_batches(&[&record_batch]).unwrap();
        writer.finish().unwrap();
        let buf = writer.into_inner();

        let mut result: serde_json::Value = serde_json::from_slice(&buf).unwrap();
        *result
            .get_mut("commitInfo")
            .unwrap()
            .get_mut("timestamp")
            .unwrap() = serde_json::Value::Number(0.into());
        result
    }

    #[test]
    fn test_generate_commit_info() -> DeltaResult<()> {
        let engine = ExprEngine::new();
        let engine_commit_info_schema = Arc::new(ArrowSchema::new(vec![Field::new(
            "engineCommitInfo",
            ArrowDataType::Map(
                Arc::new(Field::new(
                    "entries",
                    ArrowDataType::Struct(
                        vec![
                            Field::new("key", ArrowDataType::Utf8, false),
                            Field::new("value", ArrowDataType::Utf8, true),
                        ]
                        .into(),
                    ),
                    false,
                )),
                false,
            ),
            false,
        )]));

        let map_array = build_map(vec![("engineInfo", "default engine")]);
        let commit_info_batch =
            RecordBatch::try_new(engine_commit_info_schema, vec![Arc::new(map_array)])?;

        let actions = generate_commit_info(
            &engine,
            Some("test operation"),
            &ArrowEngineData::new(commit_info_batch),
        )?;

        let expected = serde_json::json!({
            "commitInfo": {
                "timestamp": 0,
                "operation": "test operation",
                "kernelVersion": format!("v{}", env!("CARGO_PKG_VERSION")),
                "operationParameters": {},
                "engineCommitInfo": {
                    "engineInfo": "default engine"
                }
            }
        });

        assert_eq!(actions.len(), 1);
        let result = as_json_and_scrub_timestamp(actions);
        assert_eq!(result, expected);

        Ok(())
    }

    #[test]
    fn test_commit_info_with_multiple_columns() -> DeltaResult<()> {
        let engine = ExprEngine::new();
        let engine_commit_info_schema = Arc::new(ArrowSchema::new(vec![
            Field::new(
                "engineCommitInfo",
                ArrowDataType::Map(
                    Arc::new(Field::new(
                        "entries",
                        ArrowDataType::Struct(
                            vec![
                                Field::new("key", ArrowDataType::Utf8, false),
                                Field::new("value", ArrowDataType::Utf8, true),
                            ]
                            .into(),
                        ),
                        false,
                    )),
                    false,
                ),
                false,
            ),
            Field::new("operation", ArrowDataType::Utf8, true),
        ]));

        let map_array = build_map(vec![("engineInfo", "default engine")]);

        let commit_info_batch = RecordBatch::try_new(
            engine_commit_info_schema,
            vec![
                Arc::new(map_array),
                Arc::new(arrow_array::StringArray::from(vec!["some_string"])),
            ],
        )?;

        let actions = generate_commit_info(
            &engine,
            Some("test operation"),
            &ArrowEngineData::new(commit_info_batch),
        )?;

        let expected = serde_json::json!({
            "commitInfo": {
                "timestamp": 0,
                "operation": "test operation",
                "kernelVersion": format!("v{}", env!("CARGO_PKG_VERSION")),
                "operationParameters": {},
                "engineCommitInfo": {
                    "engineInfo": "default engine"
                }
            }
        });

        assert_eq!(actions.len(), 1);
        let result = as_json_and_scrub_timestamp(actions);
        assert_eq!(result, expected);

        Ok(())
    }

    #[test]
    fn test_invalid_commit_info_missing_column() -> DeltaResult<()> {
        let engine = ExprEngine::new();
        let engine_commit_info_schema = Arc::new(ArrowSchema::new(vec![Field::new(
            "some_column_name",
            ArrowDataType::Utf8,
            true,
        )]));
        let commit_info_batch = RecordBatch::try_new(
            engine_commit_info_schema,
            vec![Arc::new(arrow_array::StringArray::new_null(1))],
        )?;

        let _ = generate_commit_info(
            &engine,
            Some("test operation"),
            &ArrowEngineData::new(commit_info_batch),
        )
        .map_err(|e| match e {
            Error::Arrow(arrow_schema::ArrowError::SchemaError(_)) => (),
            Error::Backtraced { source, .. }
                if matches!(
                    &*source,
                    Error::Arrow(arrow_schema::ArrowError::SchemaError(_))
                ) => {}
            _ => panic!("expected arrow schema error error, got {:?}", e),
        });

        Ok(())
    }

    #[test]
    fn test_invalid_commit_info_invalid_column_type() -> DeltaResult<()> {
        let engine = ExprEngine::new();
        let engine_commit_info_schema = Arc::new(ArrowSchema::new(vec![Field::new(
            "engineCommitInfo",
            ArrowDataType::Utf8,
            true,
        )]));
        let commit_info_batch = RecordBatch::try_new(
            engine_commit_info_schema,
            vec![Arc::new(arrow_array::StringArray::new_null(1))],
        )?;

        let _ = generate_commit_info(
            &engine,
            Some("test operation"),
            &ArrowEngineData::new(commit_info_batch),
        )
        .map_err(|e| match e {
            Error::Arrow(arrow_schema::ArrowError::InvalidArgumentError(_)) => (),
            Error::Backtraced { source, .. }
                if matches!(
                    &*source,
                    Error::Arrow(arrow_schema::ArrowError::InvalidArgumentError(_))
                ) => {}
            _ => panic!("expected arrow invalid arg error, got {:?}", e),
        });

        Ok(())
    }

    fn assert_empty_commit_info(
        data: Box<dyn EngineData>,
        write_engine_commit_info: bool,
    ) -> DeltaResult<()> {
        assert_eq!(data.len(), 1);
        let expected = if write_engine_commit_info {
            serde_json::json!({
                "commitInfo": {
                    "timestamp": 0,
                    "operation": "test operation",
                    "kernelVersion": format!("v{}", env!("CARGO_PKG_VERSION")),
                    "operationParameters": {},
                    "engineCommitInfo": {}
                }
            })
        } else {
            serde_json::json!({
                "commitInfo": {
                    "timestamp": 0,
                    "operation": "test operation",
                    "kernelVersion": format!("v{}", env!("CARGO_PKG_VERSION")),
                    "operationParameters": {},
                }
            })
        };
        let result = as_json_and_scrub_timestamp(data);
        assert_eq!(result, expected);
        Ok(())
    }

    // Three cases for empty commit info:
    // 1. `engineCommitInfo` column with an empty Map<string, string>
    // 2. `engineCommitInfo` null column of type Map<string, string>
    // 3. a column that has a name other than `engineCommitInfo`; Delta can detect that the column
    //    is missing and substitute a null literal in its place. The type of that column doesn't
    //    matter, Delta will ignore it.
    #[test]
    fn test_empty_commit_info() -> DeltaResult<()> {
        // test with null map and empty map
        for is_null in [true, false] {
            let engine = ExprEngine::new();
            let engine_commit_info_schema = Arc::new(ArrowSchema::new(vec![Field::new(
                "engineCommitInfo",
                ArrowDataType::Map(
                    Arc::new(Field::new(
                        "entries",
                        ArrowDataType::Struct(
                            vec![
                                Field::new("key", ArrowDataType::Utf8, false),
                                Field::new("value", ArrowDataType::Utf8, true),
                            ]
                            .into(),
                        ),
                        false,
                    )),
                    false,
                ),
                true,
            )]));
            use arrow_array::builder::StringBuilder;
            let key_builder = StringBuilder::new();
            let val_builder = StringBuilder::new();
            let names = arrow_array::builder::MapFieldNames {
                entry: "entries".to_string(),
                key: "key".to_string(),
                value: "value".to_string(),
            };
            let mut builder =
                arrow_array::builder::MapBuilder::new(Some(names), key_builder, val_builder);
            builder.append(is_null).unwrap();
            let array = builder.finish();

            let commit_info_batch =
                RecordBatch::try_new(engine_commit_info_schema, vec![Arc::new(array)])?;

            let actions = generate_commit_info(
                &engine,
                Some("test operation"),
                &ArrowEngineData::new(commit_info_batch),
            )?;

            assert_empty_commit_info(actions, is_null)?;
        }
        Ok(())
    }

    #[test]
    fn test_write_metadata_schema() {
        let schema = get_write_metadata_schema();
        let expected = StructType::new(vec![
            StructField::new("path", DataType::STRING, false),
            StructField::new(
                "partitionValues",
                MapType::new(DataType::STRING, DataType::STRING, true),
                false,
            ),
            StructField::new("size", DataType::LONG, false),
            StructField::new("modificationTime", DataType::LONG, false),
            StructField::new("dataChange", DataType::BOOLEAN, false),
        ]);
        assert_eq!(*schema, expected.into());
    }
}