delta_kernel 0.25.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
//! File statistics and deltas for CRC tracking.
//!
//! [`FileStats`] represents absolute file-level statistics (count, size, histogram) for a table
//! version. [`FileStatsDelta`] captures the net changes from a single commit as a single delta
//! [`FileSizeHistogram`] (adds minus removes).
//!
//! [`FileStatsDelta`] captures how many files were added/removed and their total sizes. It can be
//! produced from either:
//! 1. In-memory transaction data via [`FileStatsDelta::try_compute_for_txn`]
//! 2. A parsed .json commit file

use std::sync::LazyLock;

use super::FileSizeHistogram;
use crate::engine_data::{FilteredEngineData, GetData, TypedGetData as _};
use crate::schema::{ColumnName, ColumnNamesAndTypes, DataType};
use crate::utils::require;
use crate::{DeltaResult, EngineData, Error, RowVisitor};

/// File-level statistics for a table version: total file count, size, and histogram.
///
/// Obtained via [`Snapshot::get_file_stats_if_present`] or [`Crc::file_stats()`]. Returns
/// `None` when the source CRC's `file_stats_state` is not `Complete`.
///
/// [`Snapshot::get_file_stats_if_present`]: crate::snapshot::Snapshot::get_file_stats_if_present
/// [`Crc::file_stats()`]: super::Crc::file_stats
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FileStats {
    /// Number of active [`Add`](crate::actions::Add) file actions in this table version.
    pub(crate) num_files: i64,
    /// Total size of the table in bytes (sum of all active
    /// [`Add`](crate::actions::Add) file sizes).
    pub(crate) table_size_bytes: i64,
    /// Size distribution of active files, if available.
    pub(crate) file_size_histogram: Option<FileSizeHistogram>,
}

impl FileStats {
    /// Returns the number of active [`Add`](crate::actions::Add) file actions in this table
    /// version.
    pub fn num_files(&self) -> i64 {
        self.num_files
    }

    /// Returns the total size of the table in bytes (sum of all active
    /// [`Add`](crate::actions::Add) file sizes).
    pub fn table_size_bytes(&self) -> i64 {
        self.table_size_bytes
    }

    /// Returns the size distribution of active files, if available.
    pub fn file_size_histogram(&self) -> Option<&FileSizeHistogram> {
        self.file_size_histogram.as_ref()
    }
}

/// Gross file-change totals from a single commit, plus an optional net file-size histogram.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct FileStatsDelta {
    pub(crate) gross_add_files: u64,
    pub(crate) gross_remove_files: u64,
    pub(crate) gross_add_bytes: u64,
    pub(crate) gross_remove_bytes: u64,
    /// Net change in file size histogram (adds minus removes per bin). May contain negative
    /// values in bins where more files were removed than added. `None` when the delta source
    /// does not provide histogram data.
    pub(crate) net_histogram: Option<FileSizeHistogram>,
}

const INCREMENTAL_SAFE_OPS: &[&str] = &[
    "WRITE",
    "MERGE",
    "UPDATE",
    "DELETE",
    "OPTIMIZE",
    "CREATE TABLE",
    "REPLACE TABLE",
    "CREATE TABLE AS SELECT",
    "REPLACE TABLE AS SELECT",
    "CREATE OR REPLACE TABLE AS SELECT",
];

/// Returns `true` if the given operation can be safely tracked by incremental file stats.
///
/// Incremental-safe operations produce add/remove actions whose net counts give correct file
/// stats. Unknown or missing operations are treated as unsafe. For example, ANALYZE STATS
/// re-adds existing files with updated statistics; naively counting those adds would
/// double-count file stats.
pub(crate) fn is_incremental_safe_operation(operation: &str) -> bool {
    INCREMENTAL_SAFE_OPS.contains(&operation)
}

impl FileStatsDelta {
    /// Net change in file count (added minus removed).
    pub(crate) fn net_files(&self) -> i64 {
        self.gross_add_files as i64 - self.gross_remove_files as i64
    }

    /// Net change in total bytes (added minus removed).
    pub(crate) fn net_bytes(&self) -> i64 {
        self.gross_add_bytes as i64 - self.gross_remove_bytes as i64
    }

    /// Compute file stats and a delta histogram from a transaction's staged add and remove
    /// metadata.
    ///
    /// A commit writes three kinds of file actions:
    ///   (1) Add actions (from `add_files_metadata`)
    ///   (2) Remove actions (from `remove_files_metadata`)
    ///   (3) DV update actions (which contain both a Remove and an Add for the same file at
    ///       the same size).
    ///
    /// Only the first two need visiting -- DV updates have a net-zero effect on file counts,
    /// sizes, and histograms.
    ///
    /// `bin_boundaries` specifies the histogram bin boundaries to use. When `Some`, the
    /// delta histogram is built with those boundaries (matching the previous CRC's histogram).
    /// When `None`, the standard default boundaries are used. Callers should pass the previous
    /// CRC's boundaries when available so that `try_apply_delta` in [`Crc::apply`] succeeds.
    pub(crate) fn try_compute_for_txn(
        add_files_metadata: &[Box<dyn EngineData>],
        remove_files_metadata: &[FilteredEngineData],
        bin_boundaries: Option<&[i64]>,
    ) -> DeltaResult<Self> {
        let mut histogram = match bin_boundaries {
            Some(b) => FileSizeHistogram::create_empty_with_boundaries(b.to_vec())?,
            None => FileSizeHistogram::create_default(),
        };
        let mut gross_add_files = 0u64;
        let mut gross_remove_files = 0u64;
        let mut gross_add_bytes = 0u64;
        let mut gross_remove_bytes = 0u64;

        // Visit add files (insert into histogram). Every row is a file being added.
        for batch in add_files_metadata {
            let mut visitor = FileStatsVisitor::new(None, false, &mut histogram);
            visitor.visit_rows_of(batch.as_ref())?;
            gross_add_files += visitor.count;
            gross_add_bytes += visitor.total_size;
        }

        // Visit remove files (remove from histogram). Each FilteredEngineData has its own
        // selection vector, so we create a visitor per batch.
        for filtered_batch in remove_files_metadata {
            let sv = filtered_batch.selection_vector();
            let sv_opt = if sv.is_empty() { None } else { Some(sv) };
            let mut visitor = FileStatsVisitor::new(sv_opt, true, &mut histogram);
            visitor.visit_rows_of(filtered_batch.data())?;
            gross_remove_files += visitor.count;
            gross_remove_bytes += visitor.total_size;
        }

        Ok(FileStatsDelta {
            gross_add_files,
            gross_remove_files,
            gross_add_bytes,
            gross_remove_bytes,
            net_histogram: Some(histogram),
        })
    }
}

/// Read a file `size` (a non-negative byte count stored as `i64`) as `u64`, erroring on a
/// negative size (corrupt input).
pub(crate) fn size_to_u64(size: i64) -> DeltaResult<u64> {
    u64::try_from(size)
        .map_err(|_| Error::internal_error(format!("File size must be non-negative, got {size}")))
}

/// Visitor that extracts the `size` column from file metadata and updates a shared histogram.
///
/// `is_remove` selects whether each visited row is inserted into (add) or removed from (remove)
/// the shared delta histogram. `count`/`total_size` always accumulate gross magnitude; the
/// add/remove direction is the caller's to track.
///
/// Accepts an optional selection vector to filter which rows are visited. AddFiles pass `None`
/// (count every row); RemoveFiles may pass `Some(sv)` from [`FilteredEngineData`] to skip rows
/// that are not actually being removed.
struct FileStatsVisitor<'sv, 'h> {
    /// Optional selection vector. When `Some`, only rows marked `true` are counted. Rows beyond
    /// the SV length are implicitly selected.
    selection_vector: Option<&'sv [bool]>,
    /// Offset into the selection vector, tracking position across multiple visit calls.
    offset: usize,
    /// Whether visited rows are removed from (vs added to) the histogram.
    is_remove: bool,
    /// Count of files visited.
    count: u64,
    /// Total bytes of files visited.
    total_size: u64,
    /// Shared histogram that all visitors (add and remove) write to.
    histogram: &'h mut FileSizeHistogram,
}

impl<'sv, 'h> FileStatsVisitor<'sv, 'h> {
    fn new(
        selection_vector: Option<&'sv [bool]>,
        is_remove: bool,
        histogram: &'h mut FileSizeHistogram,
    ) -> Self {
        Self {
            selection_vector,
            offset: 0,
            is_remove,
            count: 0,
            total_size: 0,
            histogram,
        }
    }
}

impl RowVisitor for FileStatsVisitor<'_, '_> {
    fn selected_column_names_and_types(&self) -> (&'static [ColumnName], &'static [DataType]) {
        static NAMES_AND_TYPES: LazyLock<ColumnNamesAndTypes> =
            LazyLock::new(|| (vec![ColumnName::new(["size"])], vec![DataType::LONG]).into());
        NAMES_AND_TYPES.as_ref()
    }

    fn visit<'a>(&mut self, row_count: usize, getters: &[&'a dyn GetData<'a>]) -> DeltaResult<()> {
        require!(
            getters.len() == 1,
            Error::InternalError(format!(
                "Wrong number of FileStatsVisitor getters: {}",
                getters.len()
            ))
        );
        for i in 0..row_count {
            let selected = match self.selection_vector {
                Some(sv) => sv.get(self.offset + i).copied().unwrap_or(true),
                None => true,
            };
            if selected {
                let size: i64 = getters[0].get(i, "size")?;
                self.count += 1;
                self.total_size += size_to_u64(size)?;
                if self.is_remove {
                    self.histogram.remove(size)?;
                } else {
                    self.histogram.insert(size)?;
                }
            }
        }
        self.offset += row_count;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use rstest::rstest;
    use test_utils::{generate_batch, IntoArray};

    use super::*;
    use crate::engine::arrow_data::ArrowEngineData;

    fn size_batch(sizes: Vec<i64>) -> Box<dyn EngineData> {
        let batch = generate_batch(vec![("size", sizes.into_array())]).unwrap();
        Box::new(ArrowEngineData::new(batch))
    }

    struct TryComputeCase {
        add_batches: Vec<Vec<i64>>,
        remove_batches: Vec<Vec<i64>>,
        expected_net_files: i64,
        expected_net_bytes: i64,
    }

    #[rstest]
    #[case::empty(TryComputeCase {
        add_batches: vec![],
        remove_batches: vec![],
        expected_net_files: 0,
        expected_net_bytes: 0,
    })]
    #[case::adds_only(TryComputeCase {
        add_batches: vec![vec![100, 200, 300]],
        remove_batches: vec![],
        expected_net_files: 3,
        expected_net_bytes: 600, // 600 = 100 + 200 + 300
    })]
    #[case::multiple_add_batches(TryComputeCase {
        add_batches: vec![vec![100, 200], vec![300, 400, 500]],
        remove_batches: vec![],
        expected_net_files: 5,
        expected_net_bytes: 1500, // 1500 = 100 + 200 + 300 + 400 + 500
    })]
    #[case::removes_only(TryComputeCase {
        add_batches: vec![],
        remove_batches: vec![vec![500, 700]],
        expected_net_files: -2,
        expected_net_bytes: -1200, // -1200 = -(500 + 700)
    })]
    #[case::adds_and_removes(TryComputeCase {
        add_batches: vec![vec![100, 200], vec![300, 400]],
        remove_batches: vec![vec![500], vec![600, 700]],
        expected_net_files: 1,
        expected_net_bytes: -800, // -800 = (100 + 200 + 300 + 400) -(500 + 600 + 700)
    })]
    fn test_try_compute(#[case] case: TryComputeCase) {
        let adds: Vec<_> = case.add_batches.into_iter().map(size_batch).collect();
        let removes: Vec<_> = case
            .remove_batches
            .into_iter()
            .map(|sizes| FilteredEngineData::with_all_rows_selected(size_batch(sizes)))
            .collect();
        let stats = FileStatsDelta::try_compute_for_txn(&adds, &removes, None).unwrap();
        assert_eq!(stats.net_files(), case.expected_net_files);
        assert_eq!(stats.net_bytes(), case.expected_net_bytes);
    }

    #[test]
    fn test_with_selection_vectors() {
        // Multiple add batches + multiple remove batches with mixed SV scenarios
        let adds = vec![size_batch(vec![100, 200]), size_batch(vec![300])];
        let removes = vec![
            // First remove batch: all rows selected (no SV)
            FilteredEngineData::with_all_rows_selected(size_batch(vec![400, 500])),
            // Second remove batch: partial selection (600 skipped)
            FilteredEngineData::try_new(size_batch(vec![600, 700, 800]), vec![false, true, true])
                .unwrap(),
        ];
        let stats = FileStatsDelta::try_compute_for_txn(&adds, &removes, None).unwrap();
        // adds: 3 files, 600 bytes (100 + 200 + 300)
        // removes: 4 files, 2400 bytes (400 + 500 + 700 + 800)
        assert_eq!(stats.net_files(), -1); // 3 - 4
        assert_eq!(stats.net_bytes(), -1800); // 600 - 2400
        assert_eq!(stats.gross_add_files, 3);
        assert_eq!(stats.gross_remove_files, 4);
        assert_eq!(stats.gross_add_bytes, 600);
        assert_eq!(stats.gross_remove_bytes, 2400);
    }

    #[test]
    fn try_compute_builds_delta_histogram_from_add_and_remove_sizes() {
        let adds = vec![size_batch(vec![100, 200, 300])];
        let removes = vec![FilteredEngineData::with_all_rows_selected(size_batch(
            vec![500, 700],
        ))];
        let stats = FileStatsDelta::try_compute_for_txn(&adds, &removes, None).unwrap();

        // All sizes < 8KB so they all land in bin 0. Net: 3 adds - 2 removes = 1 file,
        // 600 - 1200 = -600 bytes.
        let delta = stats.net_histogram.unwrap();
        assert_eq!(delta.file_counts[0], 1);
        assert_eq!(delta.total_bytes[0], -600);
    }

    #[test]
    fn try_compute_empty_batches_produce_zero_histogram() {
        let stats = FileStatsDelta::try_compute_for_txn(&[], &[], None).unwrap();
        let delta = stats.net_histogram.unwrap();
        assert!(delta.file_counts.iter().all(|&c| c == 0));
        assert!(delta.total_bytes.iter().all(|&b| b == 0));
    }

    #[test]
    fn try_compute_histogram_with_selection_vectors() {
        let adds = vec![size_batch(vec![100, 200])];
        let removes = vec![FilteredEngineData::try_new(
            size_batch(vec![300, 400, 500]),
            vec![true, false, true], // 300 selected, 400 skipped, 500 selected
        )
        .unwrap()];
        let stats = FileStatsDelta::try_compute_for_txn(&adds, &removes, None).unwrap();

        // Net bin 0: 2 adds - 2 removes = 0 files, 300 - 800 = -500 bytes
        let delta = stats.net_histogram.unwrap();
        assert_eq!(delta.file_counts[0], 0);
        assert_eq!(delta.total_bytes[0], -500);
    }

    #[test]
    fn try_compute_with_custom_boundaries_uses_them() {
        // Custom 3-bin histogram: [0, 200) [200, 1000) [1000, inf)
        let boundaries: &[i64] = &[0, 200, 1000];
        let adds = vec![size_batch(vec![50, 300, 1500])];
        let removes = vec![FilteredEngineData::with_all_rows_selected(size_batch(
            vec![100, 500],
        ))];
        let stats = FileStatsDelta::try_compute_for_txn(&adds, &removes, Some(boundaries)).unwrap();

        let delta = stats.net_histogram.unwrap();
        assert_eq!(delta.sorted_bin_boundaries, vec![0, 200, 1000]);
        // Net per bin: (1-1, 1-1, 1-0) = (0, 0, 1)
        assert_eq!(delta.file_counts, vec![0, 0, 1]);
        // Net per bin: (50-100, 300-500, 1500-0) = (-50, -200, 1500)
        assert_eq!(delta.total_bytes, vec![-50, -200, 1500]);
    }

    #[test]
    fn try_compute_with_custom_boundaries_produces_mergeable_histogram() {
        // Build a base histogram with custom boundaries, then verify delta merges correctly.
        let boundaries = vec![0, 200, 1000];
        let mut base = FileSizeHistogram::create_empty_with_boundaries(boundaries.clone()).unwrap();
        base.insert(150).unwrap(); // bin 0
        base.insert(500).unwrap(); // bin 1

        let adds = vec![size_batch(vec![100, 300])];
        let removes = vec![FilteredEngineData::with_all_rows_selected(size_batch(
            vec![150],
        ))];
        let stats =
            FileStatsDelta::try_compute_for_txn(&adds, &removes, Some(&boundaries)).unwrap();

        let delta = stats.net_histogram.unwrap();
        let merged = base.try_apply_delta(&delta).unwrap();
        assert_eq!(merged.file_counts, vec![1, 2, 0]); // (1+1-1), (1+1-0), (0+0-0)
        assert_eq!(merged.total_bytes, vec![100, 800, 0]); // (150+100-150), (500+300-0)
    }
}