-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtests.rs
553 lines (487 loc) · 20.5 KB
/
tests.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
use std::{path::PathBuf, sync::Arc};
use itertools::Itertools;
use object_store::{memory::InMemory, path::Path, ObjectStore};
use url::Url;
use crate::engine::default::executor::tokio::TokioBackgroundExecutor;
use crate::engine::default::filesystem::ObjectStoreFileSystemClient;
use crate::engine::sync::SyncEngine;
use crate::log_segment::LogSegment;
use crate::snapshot::CheckpointMetadata;
use crate::{FileSystemClient, Table};
use test_utils::delta_path_for_version;
// NOTE: In addition to testing the meta-predicate for metadata replay, this test also verifies
// that the parquet reader properly infers nullcount = rowcount for missing columns. The two
// checkpoint part files that contain transaction app ids have truncated schemas that would
// otherwise fail skipping due to their missing nullcount stat:
//
// Row group 0: count: 1 total(compressed): 111 B total(uncompressed):107 B
// --------------------------------------------------------------------------------
// type nulls min / max
// txn.appId BINARY 0 "3ae45b72-24e1-865a-a211-3..." / "3ae45b72-24e1-865a-a211-3..."
// txn.version INT64 0 "4390" / "4390"
#[test]
fn test_replay_for_metadata() {
let path = std::fs::canonicalize(PathBuf::from("./tests/data/parquet_row_group_skipping/"));
let url = url::Url::from_directory_path(path.unwrap()).unwrap();
let engine = SyncEngine::new();
let table = Table::new(url);
let snapshot = table.snapshot(&engine, None).unwrap();
let data: Vec<_> = snapshot
.log_segment
.replay_for_metadata(&engine)
.unwrap()
.try_collect()
.unwrap();
// The checkpoint has five parts, each containing one action:
// 1. txn (physically missing P&M columns)
// 2. metaData
// 3. protocol
// 4. add
// 5. txn (physically missing P&M columns)
//
// The parquet reader should skip parts 1, 3, and 5. Note that the actual `read_metadata`
// always skips parts 4 and 5 because it terminates the iteration after finding both P&M.
//
// NOTE: Each checkpoint part is a single-row file -- guaranteed to produce one row group.
//
// WARNING: https://github.com/delta-io/delta-kernel-rs/issues/434 -- We currently
// read parts 1 and 5 (4 in all instead of 2) because row group skipping is disabled for
// missing columns, but can still skip part 3 because has valid nullcount stats for P&M.
assert_eq!(data.len(), 4);
}
// get an ObjectStore path for a checkpoint file, based on version, part number, and total number of parts
fn delta_path_for_multipart_checkpoint(version: u64, part_num: u32, num_parts: u32) -> Path {
let path =
format!("_delta_log/{version:020}.checkpoint.{part_num:010}.{num_parts:010}.parquet");
Path::from(path.as_str())
}
// Utility method to build a log using a list of log paths and an optional checkpoint hint. The
// CheckpointMetadata is written to `_delta_log/_last_checkpoint`.
fn build_log_with_paths_and_checkpoint(
paths: &[Path],
checkpoint_metadata: Option<&CheckpointMetadata>,
) -> (Box<dyn FileSystemClient>, Url) {
let store = Arc::new(InMemory::new());
let data = bytes::Bytes::from("kernel-data");
// add log files to store
tokio::runtime::Runtime::new()
.expect("create tokio runtime")
.block_on(async {
for path in paths {
store
.put(path, data.clone().into())
.await
.expect("put log file in store");
}
if let Some(checkpoint_metadata) = checkpoint_metadata {
let checkpoint_str =
serde_json::to_string(checkpoint_metadata).expect("Serialize checkpoint");
store
.put(
&Path::from("_delta_log/_last_checkpoint"),
checkpoint_str.into(),
)
.await
.expect("Write _last_checkpoint");
}
});
let client = ObjectStoreFileSystemClient::new(
store,
false, // don't have ordered listing
Path::from("/"),
Arc::new(TokioBackgroundExecutor::new()),
);
let table_root = Url::parse("memory:///").expect("valid url");
let log_root = table_root.join("_delta_log/").unwrap();
(Box::new(client), log_root)
}
#[test]
fn build_snapshot_with_out_of_date_last_checkpoint() {
let checkpoint_metadata = CheckpointMetadata {
version: 3,
size: 10,
parts: None,
size_in_bytes: None,
num_of_add_files: None,
checkpoint_schema: None,
checksum: None,
};
let (client, log_root) = build_log_with_paths_and_checkpoint(
&[
delta_path_for_version(0, "json"),
delta_path_for_version(1, "checkpoint.parquet"),
delta_path_for_version(2, "json"),
delta_path_for_version(3, "checkpoint.parquet"),
delta_path_for_version(4, "json"),
delta_path_for_version(5, "checkpoint.parquet"),
delta_path_for_version(6, "json"),
delta_path_for_version(7, "json"),
],
Some(&checkpoint_metadata),
);
let log_segment =
LogSegment::for_snapshot(client.as_ref(), log_root, checkpoint_metadata, None).unwrap();
let commit_files = log_segment.ascending_commit_files;
let checkpoint_parts = log_segment.checkpoint_parts;
assert_eq!(checkpoint_parts.len(), 1);
assert_eq!(commit_files.len(), 2);
assert_eq!(checkpoint_parts[0].version, 5);
assert_eq!(commit_files[0].version, 6);
assert_eq!(commit_files[1].version, 7);
}
#[test]
fn build_snapshot_with_correct_last_multipart_checkpoint() {
let checkpoint_metadata = CheckpointMetadata {
version: 5,
size: 10,
parts: Some(3),
size_in_bytes: None,
num_of_add_files: None,
checkpoint_schema: None,
checksum: None,
};
let (client, log_root) = build_log_with_paths_and_checkpoint(
&[
delta_path_for_version(0, "json"),
delta_path_for_version(1, "checkpoint.parquet"),
delta_path_for_version(1, "json"),
delta_path_for_version(2, "json"),
delta_path_for_version(3, "checkpoint.parquet"),
delta_path_for_version(3, "json"),
delta_path_for_version(4, "json"),
delta_path_for_multipart_checkpoint(5, 1, 3),
delta_path_for_multipart_checkpoint(5, 2, 3),
delta_path_for_multipart_checkpoint(5, 3, 3),
delta_path_for_version(5, "json"),
delta_path_for_version(6, "json"),
delta_path_for_version(7, "json"),
],
Some(&checkpoint_metadata),
);
let log_segment =
LogSegment::for_snapshot(client.as_ref(), log_root, checkpoint_metadata, None).unwrap();
let commit_files = log_segment.ascending_commit_files;
let checkpoint_parts = log_segment.checkpoint_parts;
assert_eq!(checkpoint_parts.len(), 3);
assert_eq!(commit_files.len(), 2);
assert_eq!(checkpoint_parts[0].version, 5);
assert_eq!(commit_files[0].version, 6);
assert_eq!(commit_files[1].version, 7);
}
#[test]
fn build_snapshot_with_missing_checkpoint_part_from_hint_fails() {
let checkpoint_metadata = CheckpointMetadata {
version: 5,
size: 10,
parts: Some(3),
size_in_bytes: None,
num_of_add_files: None,
checkpoint_schema: None,
checksum: None,
};
let (client, log_root) = build_log_with_paths_and_checkpoint(
&[
delta_path_for_version(0, "json"),
delta_path_for_version(1, "checkpoint.parquet"),
delta_path_for_version(1, "json"),
delta_path_for_version(2, "json"),
delta_path_for_version(3, "checkpoint.parquet"),
delta_path_for_version(3, "json"),
delta_path_for_version(4, "json"),
delta_path_for_multipart_checkpoint(5, 1, 3),
// Part 2 is missing!
delta_path_for_multipart_checkpoint(5, 3, 3),
delta_path_for_version(5, "json"),
delta_path_for_version(6, "json"),
delta_path_for_version(7, "json"),
],
Some(&checkpoint_metadata),
);
let log_segment =
LogSegment::for_snapshot(client.as_ref(), log_root, checkpoint_metadata, None);
assert!(log_segment.is_err())
}
#[test]
fn build_snapshot_with_bad_checkpoint_hint_fails() {
let checkpoint_metadata = CheckpointMetadata {
version: 5,
size: 10,
parts: Some(1),
size_in_bytes: None,
num_of_add_files: None,
checkpoint_schema: None,
checksum: None,
};
let (client, log_root) = build_log_with_paths_and_checkpoint(
&[
delta_path_for_version(0, "json"),
delta_path_for_version(1, "checkpoint.parquet"),
delta_path_for_version(1, "json"),
delta_path_for_version(2, "json"),
delta_path_for_version(3, "checkpoint.parquet"),
delta_path_for_version(3, "json"),
delta_path_for_version(4, "json"),
delta_path_for_multipart_checkpoint(5, 1, 2),
delta_path_for_multipart_checkpoint(5, 2, 2),
delta_path_for_version(5, "json"),
delta_path_for_version(6, "json"),
delta_path_for_version(7, "json"),
],
Some(&checkpoint_metadata),
);
let log_segment =
LogSegment::for_snapshot(client.as_ref(), log_root, checkpoint_metadata, None);
assert!(log_segment.is_err())
}
#[ignore]
#[test]
fn build_snapshot_with_missing_checkpoint_part_no_hint() {
// TODO: Handle checkpoints correctly so that this test passes: https://github.com/delta-io/delta-kernel-rs/issues/497
// Part 2 of 3 is missing from checkpoint 5. The Snapshot should be made of checkpoint
// number 3 and commit files 4 to 7.
let (client, log_root) = build_log_with_paths_and_checkpoint(
&[
delta_path_for_version(0, "json"),
delta_path_for_version(1, "checkpoint.parquet"),
delta_path_for_version(1, "json"),
delta_path_for_version(2, "json"),
delta_path_for_version(3, "checkpoint.parquet"),
delta_path_for_version(3, "json"),
delta_path_for_version(4, "json"),
delta_path_for_multipart_checkpoint(5, 1, 3),
// Part 2 is missing!
delta_path_for_multipart_checkpoint(5, 3, 3),
delta_path_for_version(5, "json"),
delta_path_for_version(6, "json"),
delta_path_for_version(7, "json"),
],
None,
);
let log_segment = LogSegment::for_snapshot(client.as_ref(), log_root, None, None).unwrap();
let commit_files = log_segment.ascending_commit_files;
let checkpoint_parts = log_segment.checkpoint_parts;
assert_eq!(checkpoint_parts.len(), 1);
assert_eq!(checkpoint_parts[0].version, 3);
let versions = commit_files.into_iter().map(|x| x.version).collect_vec();
let expected_versions = vec![4, 5, 6, 7];
assert_eq!(versions, expected_versions);
}
#[test]
fn build_snapshot_without_checkpoints() {
let (client, log_root) = build_log_with_paths_and_checkpoint(
&[
delta_path_for_version(0, "json"),
delta_path_for_version(1, "json"),
delta_path_for_version(1, "checkpoint.parquet"),
delta_path_for_version(2, "json"),
delta_path_for_version(3, "json"),
delta_path_for_version(3, "checkpoint.parquet"),
delta_path_for_version(4, "json"),
delta_path_for_version(5, "json"),
delta_path_for_version(5, "checkpoint.parquet"),
delta_path_for_version(6, "json"),
delta_path_for_version(7, "json"),
],
None,
);
///////// Specify no checkpoint or end version /////////
let log_segment =
LogSegment::for_snapshot(client.as_ref(), log_root.clone(), None, None).unwrap();
let commit_files = log_segment.ascending_commit_files;
let checkpoint_parts = log_segment.checkpoint_parts;
assert_eq!(checkpoint_parts.len(), 1);
assert_eq!(checkpoint_parts[0].version, 5);
// All commit files should still be there
let versions = commit_files.into_iter().map(|x| x.version).collect_vec();
let expected_versions = vec![6, 7];
assert_eq!(versions, expected_versions);
///////// Specify only end version /////////
let log_segment = LogSegment::for_snapshot(client.as_ref(), log_root, None, Some(2)).unwrap();
let commit_files = log_segment.ascending_commit_files;
let checkpoint_parts = log_segment.checkpoint_parts;
assert_eq!(checkpoint_parts.len(), 1);
assert_eq!(checkpoint_parts[0].version, 1);
// All commit files should still be there
let versions = commit_files.into_iter().map(|x| x.version).collect_vec();
let expected_versions = vec![2];
assert_eq!(versions, expected_versions);
}
#[test]
fn build_snapshot_with_checkpoint_greater_than_time_travel_version() {
let checkpoint_metadata = CheckpointMetadata {
version: 5,
size: 10,
parts: None,
size_in_bytes: None,
num_of_add_files: None,
checkpoint_schema: None,
checksum: None,
};
let (client, log_root) = build_log_with_paths_and_checkpoint(
&[
delta_path_for_version(0, "json"),
delta_path_for_version(1, "json"),
delta_path_for_version(1, "checkpoint.parquet"),
delta_path_for_version(2, "json"),
delta_path_for_version(3, "json"),
delta_path_for_version(3, "checkpoint.parquet"),
delta_path_for_version(4, "json"),
delta_path_for_version(5, "json"),
delta_path_for_version(5, "checkpoint.parquet"),
delta_path_for_version(6, "json"),
delta_path_for_version(7, "json"),
],
None,
);
let log_segment =
LogSegment::for_snapshot(client.as_ref(), log_root, checkpoint_metadata, Some(4)).unwrap();
let commit_files = log_segment.ascending_commit_files;
let checkpoint_parts = log_segment.checkpoint_parts;
assert_eq!(checkpoint_parts.len(), 1);
assert_eq!(checkpoint_parts[0].version, 3);
assert_eq!(commit_files.len(), 1);
assert_eq!(commit_files[0].version, 4);
}
#[test]
fn build_snapshot_with_start_checkpoint_and_time_travel_version() {
let checkpoint_metadata = CheckpointMetadata {
version: 3,
size: 10,
parts: None,
size_in_bytes: None,
num_of_add_files: None,
checkpoint_schema: None,
checksum: None,
};
let (client, log_root) = build_log_with_paths_and_checkpoint(
&[
delta_path_for_version(0, "json"),
delta_path_for_version(1, "checkpoint.parquet"),
delta_path_for_version(2, "json"),
delta_path_for_version(3, "checkpoint.parquet"),
delta_path_for_version(4, "json"),
delta_path_for_version(5, "checkpoint.parquet"),
delta_path_for_version(6, "json"),
delta_path_for_version(7, "json"),
],
Some(&checkpoint_metadata),
);
let log_segment =
LogSegment::for_snapshot(client.as_ref(), log_root, checkpoint_metadata, Some(4)).unwrap();
assert_eq!(log_segment.checkpoint_parts[0].version, 3);
assert_eq!(log_segment.ascending_commit_files.len(), 1);
assert_eq!(log_segment.ascending_commit_files[0].version, 4);
}
#[test]
fn build_table_changes_with_commit_versions() {
let (client, log_root) = build_log_with_paths_and_checkpoint(
&[
delta_path_for_version(0, "json"),
delta_path_for_version(1, "json"),
delta_path_for_version(1, "checkpoint.parquet"),
delta_path_for_version(2, "json"),
delta_path_for_version(3, "json"),
delta_path_for_version(3, "checkpoint.parquet"),
delta_path_for_version(4, "json"),
delta_path_for_version(5, "json"),
delta_path_for_version(5, "checkpoint.parquet"),
delta_path_for_version(6, "json"),
delta_path_for_version(7, "json"),
],
None,
);
///////// Specify start version and end version /////////
let log_segment =
LogSegment::for_table_changes(client.as_ref(), log_root.clone(), 2, 5).unwrap();
let commit_files = log_segment.ascending_commit_files;
let checkpoint_parts = log_segment.checkpoint_parts;
// Checkpoints should be omitted
assert_eq!(checkpoint_parts.len(), 0);
// Commits between 2 and 5 (inclusive) should be returned
let versions = commit_files.into_iter().map(|x| x.version).collect_vec();
let expected_versions = (2..=5).collect_vec();
assert_eq!(versions, expected_versions);
///////// Start version and end version are the same /////////
let log_segment =
LogSegment::for_table_changes(client.as_ref(), log_root.clone(), 0, Some(0)).unwrap();
let commit_files = log_segment.ascending_commit_files;
let checkpoint_parts = log_segment.checkpoint_parts;
// Checkpoints should be omitted
assert_eq!(checkpoint_parts.len(), 0);
// There should only be commit version 0
assert_eq!(commit_files.len(), 1);
assert_eq!(commit_files[0].version, 0);
///////// Specify no start or end version /////////
let log_segment = LogSegment::for_table_changes(client.as_ref(), log_root, 0, None).unwrap();
let commit_files = log_segment.ascending_commit_files;
let checkpoint_parts = log_segment.checkpoint_parts;
// Checkpoints should be omitted
assert_eq!(checkpoint_parts.len(), 0);
// Commits between 2 and 7 (inclusive) should be returned
let versions = commit_files.into_iter().map(|x| x.version).collect_vec();
let expected_versions = (0..=7).collect_vec();
assert_eq!(versions, expected_versions);
}
#[test]
fn test_non_contiguous_log() {
// Commit with version 1 is missing
let (client, log_root) = build_log_with_paths_and_checkpoint(
&[
delta_path_for_version(0, "json"),
delta_path_for_version(2, "json"),
],
None,
);
let log_segment_res = LogSegment::for_table_changes(client.as_ref(), log_root.clone(), 0, None);
assert!(log_segment_res.is_err());
let log_segment_res = LogSegment::for_table_changes(client.as_ref(), log_root.clone(), 1, None);
assert!(log_segment_res.is_err());
let log_segment_res = LogSegment::for_table_changes(client.as_ref(), log_root, 0, Some(1));
assert!(log_segment_res.is_err());
}
#[test]
fn table_changes_fails_with_larger_start_version_than_end() {
// Commit with version 1 is missing
let (client, log_root) = build_log_with_paths_and_checkpoint(
&[
delta_path_for_version(0, "json"),
delta_path_for_version(1, "json"),
],
None,
);
let log_segment_res = LogSegment::for_table_changes(client.as_ref(), log_root, 1, Some(0));
assert!(log_segment_res.is_err());
}
#[test]
fn test_list_files_with_unusual_patterns() {
// Create paths for all the unusual patterns
let paths = vec![
// hex instead of decimal
Path::from("_delta_log/00000000deadbeef.commit.json"),
// bogus part numbering
Path::from("_delta_log/00000000000000000000.checkpoint.0000000010.0000000000.parquet"),
// v2 checkpoint
Path::from(
"_delta_log/00000000000000000010.checkpoint.80a083e8-7026-4e79-81be-64bd76c43a11.json",
),
// compacted log file
Path::from("_delta_log/00000000000000000004.00000000000000000006.compacted.json"),
// CRC file
Path::from("_delta_log/00000000000000000001.crc"),
// Valid commit file for comparison
Path::from("_delta_log/00000000000000000002.json"),
];
let (client, log_root) = build_log_with_paths_and_checkpoint(&paths, None);
// Test that list_log_files doesn't fail
let result = super::list_log_files(&*client, &log_root, None, None);
assert!(result.is_ok(), "list_log_files should not fail");
// Collect the results and verify
let paths: Vec<_> = result.unwrap().try_collect().unwrap();
// We should find exactly one file (the valid commit)
assert_eq!(paths.len(), 1, "Should find exactly one valid file");
// Verify that the valid commit file is included
let has_valid_commit = paths.iter().any(|p| p.version == 2 && p.is_commit());
assert!(has_valid_commit, "Should find the valid commit file");
// All other files should have been filtered out by ParsedLogPath::try_from
// and the filter_map_ok(identity) call
}