forked from delta-io/delta-kernel-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_replay.rs
532 lines (493 loc) · 21.5 KB
/
log_replay.rs
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
use std::clone::Clone;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, LazyLock};
use itertools::Itertools;
use tracing::debug;
use super::data_skipping::DataSkippingFilter;
use super::{ScanData, Transform};
use crate::actions::get_log_add_schema;
use crate::engine_data::{GetData, RowVisitor, TypedGetData as _};
use crate::expressions::{column_expr, column_name, ColumnName, Expression, ExpressionRef};
use crate::predicates::{DefaultPredicateEvaluator, PredicateEvaluator as _};
use crate::scan::{DeletionVectorDescriptor, Scalar, TransformExpr};
use crate::schema::{ColumnNamesAndTypes, DataType, MapType, SchemaRef, StructField, StructType};
use crate::utils::require;
use crate::{DeltaResult, Engine, EngineData, Error, ExpressionEvaluator};
/// The subset of file action fields that uniquely identifies it in the log, used for deduplication
/// of adds and removes during log replay.
#[derive(Debug, Hash, Eq, PartialEq)]
struct FileActionKey {
path: String,
dv_unique_id: Option<String>,
}
impl FileActionKey {
fn new(path: impl Into<String>, dv_unique_id: Option<String>) -> Self {
let path = path.into();
Self { path, dv_unique_id }
}
}
struct LogReplayScanner {
partition_filter: Option<ExpressionRef>,
data_skipping_filter: Option<DataSkippingFilter>,
/// A set of (data file path, dv_unique_id) pairs that have been seen thus
/// far in the log. This is used to filter out files with Remove actions as
/// well as duplicate entries in the log.
seen: HashSet<FileActionKey>,
}
/// A visitor that deduplicates a stream of add and remove actions into a stream of valid adds. Log
/// replay visits actions newest-first, so once we've seen a file action for a given (path, dvId)
/// pair, we should ignore all subsequent (older) actions for that same (path, dvId) pair. If the
/// first action for a given file is a remove, then that file does not show up in the result at all.
struct AddRemoveDedupVisitor<'seen> {
seen: &'seen mut HashSet<FileActionKey>,
selection_vector: Vec<bool>,
logical_schema: SchemaRef,
transform: Option<Arc<Transform>>,
partition_filter: Option<ExpressionRef>,
row_transform_exprs: Vec<Option<ExpressionRef>>,
is_log_batch: bool,
}
impl AddRemoveDedupVisitor<'_> {
/// Checks if log replay already processed this logical file (in which case the current action
/// should be ignored). If not already seen, register it so we can recognize future duplicates.
/// Returns `true` if we have seen the file and should ignore it, `false` if we have not seen it
/// and should process it.
fn check_and_record_seen(&mut self, key: FileActionKey) -> bool {
// Note: each (add.path + add.dv_unique_id()) pair has a
// unique Add + Remove pair in the log. For example:
// https://github.com/delta-io/delta/blob/master/spark/src/test/resources/delta/table-with-dv-large/_delta_log/00000000000000000001.json
if self.seen.contains(&key) {
debug!(
"Ignoring duplicate ({}, {:?}) in scan, is log {}",
key.path, key.dv_unique_id, self.is_log_batch
);
true
} else {
debug!(
"Including ({}, {:?}) in scan, is log {}",
key.path, key.dv_unique_id, self.is_log_batch
);
if self.is_log_batch {
// Remember file actions from this batch so we can ignore duplicates as we process
// batches from older commit and/or checkpoint files. We don't track checkpoint
// batches because they are already the oldest actions and never replace anything.
self.seen.insert(key);
}
false
}
}
fn parse_partition_value(
&self,
field_idx: usize,
partition_values: &HashMap<String, String>,
) -> DeltaResult<(usize, (String, Scalar))> {
let field = self.logical_schema.fields.get_index(field_idx);
let Some((_, field)) = field else {
return Err(Error::InternalError(format!(
"out of bounds partition column field index {field_idx}"
)));
};
let name = field.physical_name();
let partition_value =
super::parse_partition_value(partition_values.get(name), field.data_type())?;
Ok((field_idx, (name.to_string(), partition_value)))
}
fn parse_partition_values(
&self,
transform: &Transform,
partition_values: &HashMap<String, String>,
) -> DeltaResult<HashMap<usize, (String, Scalar)>> {
transform
.iter()
.filter_map(|transform_expr| match transform_expr {
TransformExpr::Partition(field_idx) => {
Some(self.parse_partition_value(*field_idx, partition_values))
}
TransformExpr::Static(_) => None,
})
.try_collect()
}
/// Compute an expression that will transform from physical to logical for a given Add file action
fn get_transform_expr(
&self,
transform: &Transform,
mut partition_values: HashMap<usize, (String, Scalar)>,
) -> DeltaResult<ExpressionRef> {
let transforms = transform
.iter()
.map(|transform_expr| match transform_expr {
TransformExpr::Partition(field_idx) => {
let Some((_, partition_value)) = partition_values.remove(field_idx) else {
return Err(Error::InternalError(format!(
"missing partition value for field index {field_idx}"
)));
};
Ok(partition_value.into())
}
TransformExpr::Static(field_expr) => Ok(field_expr.clone()),
})
.try_collect()?;
Ok(Arc::new(Expression::Struct(transforms)))
}
fn is_file_partition_pruned(
&self,
partition_values: &HashMap<usize, (String, Scalar)>,
) -> bool {
if partition_values.is_empty() {
return false;
}
let Some(partition_filter) = &self.partition_filter else {
return false;
};
let partition_values: HashMap<_, _> = partition_values
.values()
.map(|(k, v)| (ColumnName::new([k]), v.clone()))
.collect();
let evaluator = DefaultPredicateEvaluator::from(partition_values);
evaluator.eval_sql_where(partition_filter) == Some(false)
}
/// True if this row contains an Add action that should survive log replay. Skip it if the row
/// is not an Add action, or the file has already been seen previously.
fn is_valid_add<'a>(&mut self, i: usize, getters: &[&'a dyn GetData<'a>]) -> DeltaResult<bool> {
// Add will have a path at index 0 if it is valid; otherwise, if it is a log batch, we may
// have a remove with a path at index 4. In either case, extract the three dv getters at
// indexes that immediately follow a valid path index.
let (path, dv_getters, is_add) = if let Some(path) = getters[0].get_str(i, "add.path")? {
(path, &getters[2..5], true)
} else if !self.is_log_batch {
return Ok(false);
} else if let Some(path) = getters[5].get_opt(i, "remove.path")? {
(path, &getters[6..9], false)
} else {
return Ok(false);
};
let dv_unique_id = match dv_getters[0].get_opt(i, "deletionVector.storageType")? {
Some(storage_type) => Some(DeletionVectorDescriptor::unique_id_from_parts(
storage_type,
dv_getters[1].get(i, "deletionVector.pathOrInlineDv")?,
dv_getters[2].get_opt(i, "deletionVector.offset")?,
)),
None => None,
};
// Apply partition pruning (to adds only) before deduplication, so that we don't waste memory
// tracking pruned files. Removes don't get pruned and we'll still have to track them.
//
// WARNING: It's not safe to partition-prune removes (just like it's not safe to data skip
// removes), because they are needed to suppress earlier incompatible adds we might
// encounter if the table's schema was replaced after the most recent checkpoint.
let partition_values = match &self.transform {
Some(transform) if is_add => {
let partition_values = getters[1].get(i, "add.partitionValues")?;
let partition_values = self.parse_partition_values(transform, &partition_values)?;
if self.is_file_partition_pruned(&partition_values) {
return Ok(false);
}
partition_values
}
_ => Default::default(),
};
// Check both adds and removes (skipping already-seen), but only transform and return adds
let file_key = FileActionKey::new(path, dv_unique_id);
if self.check_and_record_seen(file_key) || !is_add {
return Ok(false);
}
let transform = self
.transform
.as_ref()
.map(|transform| self.get_transform_expr(transform, partition_values))
.transpose()?;
if transform.is_some() {
// fill in any needed `None`s for previous rows
self.row_transform_exprs.resize_with(i, Default::default);
self.row_transform_exprs.push(transform);
}
Ok(true)
}
}
impl RowVisitor for AddRemoveDedupVisitor<'_> {
fn selected_column_names_and_types(&self) -> (&'static [ColumnName], &'static [DataType]) {
// NOTE: The visitor assumes a schema with adds first and removes optionally afterward.
static NAMES_AND_TYPES: LazyLock<ColumnNamesAndTypes> = LazyLock::new(|| {
const STRING: DataType = DataType::STRING;
const INTEGER: DataType = DataType::INTEGER;
let ss_map: DataType = MapType::new(STRING, STRING, true).into();
let types_and_names = vec![
(STRING, column_name!("add.path")),
(ss_map, column_name!("add.partitionValues")),
(STRING, column_name!("add.deletionVector.storageType")),
(STRING, column_name!("add.deletionVector.pathOrInlineDv")),
(INTEGER, column_name!("add.deletionVector.offset")),
(STRING, column_name!("remove.path")),
(STRING, column_name!("remove.deletionVector.storageType")),
(STRING, column_name!("remove.deletionVector.pathOrInlineDv")),
(INTEGER, column_name!("remove.deletionVector.offset")),
];
let (types, names) = types_and_names.into_iter().unzip();
(names, types).into()
});
let (names, types) = NAMES_AND_TYPES.as_ref();
if self.is_log_batch {
(names, types)
} else {
// All checkpoint actions are already reconciled and Remove actions in checkpoint files
// only serve as tombstones for vacuum jobs. So we only need to examine the adds here.
(&names[..5], &types[..5])
}
}
fn visit<'a>(&mut self, row_count: usize, getters: &[&'a dyn GetData<'a>]) -> DeltaResult<()> {
let expected_getters = if self.is_log_batch { 9 } else { 5 };
require!(
getters.len() == expected_getters,
Error::InternalError(format!(
"Wrong number of AddRemoveDedupVisitor getters: {}",
getters.len()
))
);
for i in 0..row_count {
if self.selection_vector[i] {
self.selection_vector[i] = self.is_valid_add(i, getters)?;
}
}
Ok(())
}
}
// NB: If you update this schema, ensure you update the comment describing it in the doc comment
// for `scan_row_schema` in scan/mod.rs! You'll also need to update ScanFileVisitor as the
// indexes will be off, and [`get_add_transform_expr`] below to match it.
pub(crate) static SCAN_ROW_SCHEMA: LazyLock<Arc<StructType>> = LazyLock::new(|| {
// Note that fields projected out of a nullable struct must be nullable
let partition_values = MapType::new(DataType::STRING, DataType::STRING, true);
let file_constant_values =
StructType::new([StructField::nullable("partitionValues", partition_values)]);
let deletion_vector = StructType::new([
StructField::nullable("storageType", DataType::STRING),
StructField::nullable("pathOrInlineDv", DataType::STRING),
StructField::nullable("offset", DataType::INTEGER),
StructField::nullable("sizeInBytes", DataType::INTEGER),
StructField::nullable("cardinality", DataType::LONG),
]);
Arc::new(StructType::new([
StructField::nullable("path", DataType::STRING),
StructField::nullable("size", DataType::LONG),
StructField::nullable("modificationTime", DataType::LONG),
StructField::nullable("stats", DataType::STRING),
StructField::nullable("deletionVector", deletion_vector),
StructField::nullable("fileConstantValues", file_constant_values),
]))
});
pub(crate) static SCAN_ROW_DATATYPE: LazyLock<DataType> =
LazyLock::new(|| SCAN_ROW_SCHEMA.clone().into());
fn get_add_transform_expr() -> Expression {
Expression::Struct(vec![
column_expr!("add.path"),
column_expr!("add.size"),
column_expr!("add.modificationTime"),
column_expr!("add.stats"),
column_expr!("add.deletionVector"),
Expression::Struct(vec![column_expr!("add.partitionValues")]),
])
}
impl LogReplayScanner {
/// Create a new [`LogReplayScanner`] instance
fn new(engine: &dyn Engine, physical_predicate: Option<(ExpressionRef, SchemaRef)>) -> Self {
Self {
partition_filter: physical_predicate.as_ref().map(|(e, _)| e.clone()),
data_skipping_filter: DataSkippingFilter::new(engine, physical_predicate),
seen: Default::default(),
}
}
fn process_scan_batch(
&mut self,
add_transform: &dyn ExpressionEvaluator,
actions: &dyn EngineData,
logical_schema: SchemaRef,
transform: Option<Arc<Transform>>,
is_log_batch: bool,
) -> DeltaResult<ScanData> {
// Apply data skipping to get back a selection vector for actions that passed skipping. We
// will update the vector below as log replay identifies duplicates that should be ignored.
let selection_vector = match &self.data_skipping_filter {
Some(filter) => filter.apply(actions)?,
None => vec![true; actions.len()],
};
assert_eq!(selection_vector.len(), actions.len());
let mut visitor = AddRemoveDedupVisitor {
seen: &mut self.seen,
selection_vector,
logical_schema,
transform,
partition_filter: self.partition_filter.clone(),
row_transform_exprs: Vec::new(),
is_log_batch,
};
visitor.visit_rows_of(actions)?;
// TODO: Teach expression eval to respect the selection vector we just computed so carefully!
let selection_vector = visitor.selection_vector;
let result = add_transform.evaluate(actions)?;
Ok(ScanData {
filtered_data: (result, selection_vector),
transforms: visitor.row_transform_exprs,
})
}
}
/// Given an iterator of (engine_data, bool) tuples and a predicate, returns an iterator of
/// `(engine_data, selection_vec)`. Each row that is selected in the returned `engine_data` _must_
/// be processed to complete the scan. Non-selected rows _must_ be ignored. The boolean flag
/// indicates whether the record batch is a log or checkpoint batch.
pub(crate) fn scan_action_iter(
engine: &dyn Engine,
action_iter: impl Iterator<Item = DeltaResult<(Box<dyn EngineData>, bool)>>,
logical_schema: SchemaRef,
transform: Option<Arc<Transform>>,
physical_predicate: Option<(ExpressionRef, SchemaRef)>,
) -> impl Iterator<Item = DeltaResult<ScanData>> {
let mut log_scanner = LogReplayScanner::new(engine, physical_predicate);
let add_transform = engine.get_expression_handler().get_evaluator(
get_log_add_schema().clone(),
get_add_transform_expr(),
SCAN_ROW_DATATYPE.clone(),
);
action_iter
.map(move |action_res| {
let (batch, is_log_batch) = action_res?;
log_scanner.process_scan_batch(
add_transform.as_ref(),
batch.as_ref(),
logical_schema.clone(),
transform.clone(),
is_log_batch,
)
})
.filter(|res| {
res.as_ref().map_or(true, |scan_data| {
let (_, sel_vec) = &scan_data.filtered_data;
sel_vec.contains(&true)
})
})
}
#[cfg(test)]
mod tests {
use std::{collections::HashMap, sync::Arc};
use crate::actions::get_log_schema;
use crate::expressions::{column_name, Scalar};
use crate::scan::state::{DvInfo, Stats};
use crate::scan::test_utils::{
add_batch_simple, add_batch_with_partition_col, add_batch_with_remove,
run_with_validate_callback,
};
use crate::scan::{get_state_info, Scan};
use crate::Expression;
use crate::{
engine::sync::SyncEngine,
schema::{DataType, SchemaRef, StructField, StructType},
ExpressionRef,
};
use super::scan_action_iter;
// dv-info is more complex to validate, we validate that works in the test for visit_scan_files
// in state.rs
fn validate_simple(
_: &mut (),
path: &str,
size: i64,
stats: Option<Stats>,
_: DvInfo,
_: Option<ExpressionRef>,
part_vals: HashMap<String, String>,
) {
assert_eq!(
path,
"part-00000-fae5310a-a37d-4e51-827b-c3d5516560ca-c000.snappy.parquet"
);
assert_eq!(size, 635);
assert!(stats.is_some());
assert_eq!(stats.as_ref().unwrap().num_records, 10);
assert_eq!(part_vals.get("date"), Some(&"2017-12-10".to_string()));
assert_eq!(part_vals.get("non-existent"), None);
}
#[test]
fn test_scan_action_iter() {
run_with_validate_callback(
vec![add_batch_simple(get_log_schema().clone())],
None, // not testing schema
None, // not testing transform
&[true, false],
(),
validate_simple,
);
}
#[test]
fn test_scan_action_iter_with_remove() {
run_with_validate_callback(
vec![add_batch_with_remove(get_log_schema().clone())],
None, // not testing schema
None, // not testing transform
&[false, false, true, false],
(),
validate_simple,
);
}
#[test]
fn test_no_transforms() {
let batch = vec![add_batch_simple(get_log_schema().clone())];
let logical_schema = Arc::new(crate::schema::StructType::new(vec![]));
let iter = scan_action_iter(
&SyncEngine::new(),
batch.into_iter().map(|batch| Ok((batch as _, true))),
logical_schema,
None,
None,
);
for res in iter {
let scan_data = res.unwrap();
assert!(scan_data.transforms.is_empty(), "Should have no transforms");
}
}
#[test]
fn test_simple_transform() {
let schema: SchemaRef = Arc::new(StructType::new([
StructField::new("value", DataType::INTEGER, true),
StructField::new("date", DataType::DATE, true),
]));
let partition_cols = ["date".to_string()];
let state_info = get_state_info(schema.as_ref(), &partition_cols).unwrap();
let static_transform = Some(Arc::new(Scan::get_static_transform(&state_info.all_fields)));
let batch = vec![add_batch_with_partition_col()];
let iter = scan_action_iter(
&SyncEngine::new(),
batch.into_iter().map(|batch| Ok((batch as _, true))),
schema,
static_transform,
None,
);
fn validate_transform(transform: Option<&ExpressionRef>, expected_date_offset: i32) {
assert!(transform.is_some());
let Expression::Struct(inner) = transform.unwrap().as_ref() else {
panic!("Transform should always be a struct expr");
};
assert_eq!(inner.len(), 2, "expected two items in transform struct");
let Expression::Column(ref name) = inner[0] else {
panic!("Expected first expression to be a column");
};
assert_eq!(name, &column_name!("value"), "First col should be 'value'");
let Expression::Literal(ref scalar) = inner[1] else {
panic!("Expected second expression to be a literal");
};
assert_eq!(
scalar,
&Scalar::Date(expected_date_offset),
"Didn't get expected date offset"
);
}
for res in iter {
let scan_data = res.unwrap();
let transforms = scan_data.transforms;
// in this case we have a metadata action first and protocol 3rd, so we expect 4 items,
// the first and 3rd being a `None`
assert_eq!(transforms.len(), 4, "Should have 4 transforms");
assert!(transforms[0].is_none(), "transform at [0] should be None");
assert!(transforms[2].is_none(), "transform at [2] should be None");
validate_transform(transforms[1].as_ref(), 17511);
validate_transform(transforms[3].as_ref(), 17510);
}
}
}