-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathscan_file.rs
433 lines (403 loc) · 16.8 KB
/
scan_file.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
//! This module handles [`CdfScanFile`]s for [`TableChangesScan`]. A [`CdfScanFile`] consists of all the
//! metadata required to generate a change data feed. [`CdfScanFile`] can be constructed using
//! [`CdfScanFileVisitor`]. The visitor reads from engine data with the schema [`cdf_scan_row_schema`].
//! You can convert engine data to this schema using the [`cdf_scan_row_expression`].
use itertools::Itertools;
use std::collections::HashMap;
use std::sync::{Arc, LazyLock};
use super::log_replay::TableChangesScanData;
use crate::actions::visitors::visit_deletion_vector_at;
use crate::engine_data::{GetData, TypedGetData};
use crate::expressions::{column_expr, Expression};
use crate::scan::state::DvInfo;
use crate::schema::{
ColumnName, ColumnNamesAndTypes, DataType, MapType, SchemaRef, StructField, StructType,
};
use crate::utils::require;
use crate::{DeltaResult, Error, RowVisitor};
// The type of action associated with a [`CdfScanFile`].
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum CdfScanFileType {
Add,
Remove,
Cdc,
}
/// Represents all the metadata needed to read a Change Data Feed.
#[derive(Debug, PartialEq, Clone)]
pub(crate) struct CdfScanFile {
/// The type of action this file belongs to. This may be one of add, remove, or cdc
pub scan_type: CdfScanFileType,
/// A `&str` which is the path to the file
pub path: String,
/// A [`DvInfo`] struct with the path to the action's deletion vector
pub dv_info: DvInfo,
/// An optional [`DvInfo`] struct. If present, this is deletion vector of a remove action with
/// the same path as this [`CdfScanFile`]
pub remove_dv: Option<DvInfo>,
/// A `HashMap<String, String>` which are partition values
pub partition_values: HashMap<String, String>,
/// The commit version that this action was performed in
pub commit_version: i64,
/// The timestamp of the commit that this action was performed in
pub commit_timestamp: i64,
}
pub(crate) type CdfScanCallback<T> = fn(context: &mut T, scan_file: CdfScanFile);
/// Transforms an iterator of [`TableChangesScanData`] into an iterator of
/// [`CdfScanFile`] by visiting the engine data.
pub(crate) fn scan_data_to_scan_file(
scan_data: impl Iterator<Item = DeltaResult<TableChangesScanData>>,
) -> impl Iterator<Item = DeltaResult<CdfScanFile>> {
scan_data
.map(|scan_data| -> DeltaResult<_> {
let scan_data = scan_data?;
let callback: CdfScanCallback<Vec<CdfScanFile>> =
|context, scan_file| context.push(scan_file);
Ok(visit_cdf_scan_files(&scan_data, vec![], callback)?.into_iter())
}) // Iterator-Result-Iterator
.flatten_ok() // Iterator-Result
}
/// Request that the kernel call a callback on each valid file that needs to be read for the
/// scan.
///
/// The arguments to the callback are:
/// * `context`: an `&mut context` argument. this can be anything that engine needs to pass through to each call
/// * `CdfScanFile`: a [`CdfScanFile`] struct that holds all the metadata required to perform Change Data
/// Feed
///
/// ## Context
/// A note on the `context`. This can be any value the engine wants. This function takes ownership
/// of the passed arg, but then returns it, so the engine can repeatedly call `visit_cdf_scan_files`
/// with the same context.
///
/// ## Example
/// ```ignore
/// let mut context = [my context];
/// for res in scan_data { // scan data table_changes_scan.scan_data()
/// let (data, vector, remove_dv) = res?;
/// context = delta_kernel::table_changes::scan_file::visit_cdf_scan_files(
/// data.as_ref(),
/// selection_vector,
/// context,
/// my_callback,
/// )?;
/// }
/// ```
pub(crate) fn visit_cdf_scan_files<T>(
scan_data: &TableChangesScanData,
context: T,
callback: CdfScanCallback<T>,
) -> DeltaResult<T> {
let mut visitor = CdfScanFileVisitor {
callback,
context,
selection_vector: &scan_data.selection_vector,
remove_dvs: scan_data.remove_dvs.as_ref(),
};
visitor.visit_rows_of(scan_data.scan_data.as_ref())?;
Ok(visitor.context)
}
/// A visitor that extracts [`CdfScanFile`]s from engine data. Expects data to have the schema
/// [`cdf_scan_row_schema`].
struct CdfScanFileVisitor<'a, T> {
callback: CdfScanCallback<T>,
selection_vector: &'a [bool],
remove_dvs: &'a HashMap<String, DvInfo>,
context: T,
}
impl<T> RowVisitor for CdfScanFileVisitor<'_, T> {
fn visit<'a>(&mut self, row_count: usize, getters: &[&'a dyn GetData<'a>]) -> DeltaResult<()> {
require!(
getters.len() == 18,
Error::InternalError(format!(
"Wrong number of CdfScanFileVisitor getters: {}",
getters.len()
))
);
for row_index in 0..row_count {
if !self.selection_vector[row_index] {
continue;
}
let (scan_type, path, deletion_vector, partition_values) =
if let Some(path) = getters[0].get_opt(row_index, "scanFile.add.path")? {
let scan_type = CdfScanFileType::Add;
let deletion_vector = visit_deletion_vector_at(row_index, &getters[1..=5])?;
let partition_values = getters[6]
.get_opt(row_index, "scanFile.add.fileConstantValues.partitionValues")?;
(scan_type, path, deletion_vector, partition_values)
} else if let Some(path) = getters[7].get_opt(row_index, "scanFile.remove.path")? {
let scan_type = CdfScanFileType::Remove;
let deletion_vector = visit_deletion_vector_at(row_index, &getters[8..=12])?;
let partition_values = getters[13].get_opt(
row_index,
"scanFile.remove.fileConstantValues.partitionValues",
)?;
(scan_type, path, deletion_vector, partition_values)
} else if let Some(path) = getters[14].get_opt(row_index, "scanFile.cdc.path")? {
let scan_type = CdfScanFileType::Cdc;
let partition_values = getters[15]
.get_opt(row_index, "scanFile.cdc.fileConstantValues.partitionValues")?;
(scan_type, path, None, partition_values)
} else {
continue;
};
let partition_values = partition_values.unwrap_or_else(Default::default);
let scan_file = CdfScanFile {
remove_dv: self.remove_dvs.get(&path).cloned(),
scan_type,
path,
dv_info: DvInfo { deletion_vector },
partition_values,
commit_timestamp: getters[16].get(row_index, "scanFile.timestamp")?,
commit_version: getters[17].get(row_index, "scanFile.commit_version")?,
};
(self.callback)(&mut self.context, scan_file)
}
Ok(())
}
fn selected_column_names_and_types(&self) -> (&'static [ColumnName], &'static [DataType]) {
static NAMES_AND_TYPES: LazyLock<ColumnNamesAndTypes> =
LazyLock::new(|| cdf_scan_row_schema().leaves(None));
NAMES_AND_TYPES.as_ref()
}
}
/// Get the schema that scan rows (from [`TableChanges::scan_data`]) will be returned with.
pub(crate) fn cdf_scan_row_schema() -> SchemaRef {
static CDF_SCAN_ROW_SCHEMA: LazyLock<Arc<StructType>> = LazyLock::new(|| {
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),
]);
let partition_values = MapType::new(DataType::STRING, DataType::STRING, true);
let file_constant_values =
StructType::new([StructField::nullable("partitionValues", partition_values)]);
let add = StructType::new([
StructField::nullable("path", DataType::STRING),
StructField::nullable("deletionVector", deletion_vector.clone()),
StructField::nullable("fileConstantValues", file_constant_values.clone()),
]);
let remove = StructType::new([
StructField::nullable("path", DataType::STRING),
StructField::nullable("deletionVector", deletion_vector),
StructField::nullable("fileConstantValues", file_constant_values.clone()),
]);
let cdc = StructType::new([
StructField::nullable("path", DataType::STRING),
StructField::nullable("fileConstantValues", file_constant_values),
]);
Arc::new(StructType::new([
StructField::nullable("add", add),
StructField::nullable("remove", remove),
StructField::nullable("cdc", cdc),
StructField::not_null("timestamp", DataType::LONG),
StructField::not_null("commit_version", DataType::LONG),
]))
});
CDF_SCAN_ROW_SCHEMA.clone()
}
/// Expression to convert an action with `log_schema` into one with
/// [`cdf_scan_row_schema`]. This is the expression used to create [`TableChangesScanData`].
pub(crate) fn cdf_scan_row_expression(commit_timestamp: i64, commit_number: i64) -> Expression {
Expression::struct_from([
Expression::struct_from([
column_expr!("add.path"),
column_expr!("add.deletionVector"),
Expression::struct_from([column_expr!("add.partitionValues")]),
]),
Expression::struct_from([
column_expr!("remove.path"),
column_expr!("remove.deletionVector"),
Expression::struct_from([column_expr!("remove.partitionValues")]),
]),
Expression::struct_from([
column_expr!("cdc.path"),
Expression::struct_from([column_expr!("cdc.partitionValues")]),
]),
commit_timestamp.into(),
commit_number.into(),
])
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::sync::Arc;
use itertools::Itertools;
use super::{scan_data_to_scan_file, CdfScanFile, CdfScanFileType};
use crate::actions::deletion_vector::DeletionVectorDescriptor;
use crate::actions::{Add, Cdc, Metadata, Protocol, Remove};
use crate::engine::sync::SyncEngine;
use crate::log_segment::LogSegment;
use crate::scan::state::DvInfo;
use crate::schema::{DataType, StructField, StructType};
use crate::table_changes::log_replay::table_changes_action_iter;
use crate::table_configuration::TableConfiguration;
use crate::utils::test_utils::{Action, LocalMockTable};
use crate::Engine;
#[tokio::test]
async fn test_scan_file_visiting() {
let engine = SyncEngine::new();
let mut mock_table = LocalMockTable::new();
let dv_info = DeletionVectorDescriptor {
storage_type: "u".to_string(),
path_or_inline_dv: "vBn[lx{q8@P<9BNH/isA".to_string(),
offset: Some(1),
size_in_bytes: 36,
cardinality: 2,
};
let add_partition_values = HashMap::from([("a".to_string(), "b".to_string())]);
let add_paired = Add {
path: "fake_path_1".into(),
deletion_vector: Some(dv_info.clone()),
partition_values: add_partition_values,
data_change: true,
..Default::default()
};
let remove_paired = Remove {
path: "fake_path_1".into(),
deletion_vector: None,
partition_values: None,
data_change: true,
..Default::default()
};
let rm_dv = DeletionVectorDescriptor {
storage_type: "u".to_string(),
path_or_inline_dv: "U5OWRz5k%CFT.Td}yCPW".to_string(),
offset: Some(1),
size_in_bytes: 38,
cardinality: 3,
};
let rm_partition_values = Some(HashMap::from([("c".to_string(), "d".to_string())]));
let remove = Remove {
path: "fake_path_2".into(),
deletion_vector: Some(rm_dv),
partition_values: rm_partition_values,
data_change: true,
..Default::default()
};
let cdc_partition_values = HashMap::from([("x".to_string(), "y".to_string())]);
let cdc = Cdc {
path: "fake_path_3".into(),
partition_values: cdc_partition_values,
..Default::default()
};
let remove_no_partition = Remove {
path: "fake_path_2".into(),
deletion_vector: None,
partition_values: None,
data_change: true,
..Default::default()
};
mock_table
.commit([
Action::Remove(remove_paired.clone()),
Action::Add(add_paired.clone()),
Action::Remove(remove.clone()),
])
.await;
mock_table.commit([Action::Cdc(cdc.clone())]).await;
mock_table
.commit([Action::Remove(remove_no_partition.clone())])
.await;
let table_root = url::Url::from_directory_path(mock_table.table_root()).unwrap();
let log_root = table_root.join("_delta_log/").unwrap();
let log_segment = LogSegment::for_table_changes(
engine.get_file_system_client().as_ref(),
log_root,
0,
None,
)
.unwrap();
let table_schema = StructType::new([
StructField::nullable("id", DataType::INTEGER),
StructField::nullable("value", DataType::STRING),
]);
let schema_string = serde_json::to_string(&table_schema).unwrap();
let metadata = Metadata {
schema_string,
configuration: HashMap::from([
("delta.enableChangeDataFeed".to_string(), "true".to_string()),
(
"delta.enableDeletionVectors".to_string(),
"true".to_string(),
),
("delta.columnMapping.mode".to_string(), "none".to_string()),
]),
..Default::default()
};
let protocol = Protocol::try_new(
3,
7,
Some::<Vec<String>>(vec![]),
Some::<Vec<String>>(vec![]),
)
.unwrap();
let table_config = TableConfiguration::try_new(metadata, protocol, table_root, 0).unwrap();
let scan_data = table_changes_action_iter(
Arc::new(engine),
log_segment.ascending_commit_files.clone(),
table_schema.into(),
None,
table_config,
)
.unwrap();
let scan_files: Vec<_> = scan_data_to_scan_file(scan_data).try_collect().unwrap();
// Generate the expected [`CdfScanFile`]
let timestamps = log_segment
.ascending_commit_files
.iter()
.map(|commit| commit.location.last_modified)
.collect_vec();
let expected_remove_dv = DvInfo {
deletion_vector: None,
};
let expected_scan_files = vec![
CdfScanFile {
scan_type: CdfScanFileType::Add,
path: add_paired.path,
dv_info: DvInfo {
deletion_vector: add_paired.deletion_vector,
},
partition_values: add_paired.partition_values,
commit_version: 0,
commit_timestamp: timestamps[0],
remove_dv: Some(expected_remove_dv),
},
CdfScanFile {
scan_type: CdfScanFileType::Remove,
path: remove.path,
dv_info: DvInfo {
deletion_vector: remove.deletion_vector,
},
partition_values: remove.partition_values.unwrap(),
commit_version: 0,
commit_timestamp: timestamps[0],
remove_dv: None,
},
CdfScanFile {
scan_type: CdfScanFileType::Cdc,
path: cdc.path,
dv_info: DvInfo {
deletion_vector: None,
},
partition_values: cdc.partition_values,
commit_version: 1,
commit_timestamp: timestamps[1],
remove_dv: None,
},
CdfScanFile {
scan_type: CdfScanFileType::Remove,
path: remove_no_partition.path,
dv_info: DvInfo {
deletion_vector: None,
},
partition_values: HashMap::new(),
commit_version: 2,
commit_timestamp: timestamps[2],
remove_dv: None,
},
];
assert_eq!(scan_files, expected_scan_files);
}
}