-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathvalidate.py
More file actions
357 lines (296 loc) · 13.3 KB
/
validate.py
File metadata and controls
357 lines (296 loc) · 13.3 KB
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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from collections.abc import Iterator
from pyiceberg.exceptions import ValidationException
from pyiceberg.expressions import BooleanExpression
from pyiceberg.expressions.visitors import ROWS_CANNOT_MATCH, _InclusiveMetricsEvaluator
from pyiceberg.manifest import (
INITIAL_SEQUENCE_NUMBER,
DataFile,
ManifestContent,
ManifestEntry,
ManifestEntryStatus,
ManifestFile,
)
from pyiceberg.schema import Schema
from pyiceberg.table import Table
from pyiceberg.table.delete_file_index import DeleteFileIndex
from pyiceberg.table.snapshots import Operation, Snapshot, ancestors_between
from pyiceberg.typedef import Record
VALIDATE_DATA_FILES_EXIST_OPERATIONS: set[Operation] = {Operation.OVERWRITE, Operation.REPLACE, Operation.DELETE}
VALIDATE_ADDED_DATA_FILES_OPERATIONS: set[Operation] = {Operation.APPEND, Operation.OVERWRITE}
VALIDATE_ADDED_DELETE_FILES_OPERATIONS: set[Operation] = {Operation.DELETE, Operation.OVERWRITE}
def _validation_history(
table: Table,
from_snapshot: Snapshot,
to_snapshot: Snapshot,
matching_operations: set[Operation],
manifest_content_filter: ManifestContent,
) -> tuple[list[ManifestFile], set[int]]:
"""Return newly added manifests and snapshot IDs between the starting snapshot and parent snapshot.
Args:
table: Table to get the history from
from_snapshot: Parent snapshot to get the history from
to_snapshot: Starting snapshot
matching_operations: Operations to match on
manifest_content_filter: Manifest content type to filter
Raises:
ValidationException: If no matching snapshot is found or only one snapshot is found
Returns:
List of manifest files and set of snapshots ID's matching conditions
"""
manifests_files: list[ManifestFile] = []
snapshots: set[int] = set()
last_snapshot = None
for snapshot in ancestors_between(from_snapshot, to_snapshot, table.metadata):
last_snapshot = snapshot
summary = snapshot.summary
if summary is None:
raise ValidationException(f"No summary found for snapshot {snapshot}!")
if summary.operation not in matching_operations:
continue
snapshots.add(snapshot.snapshot_id)
# TODO: Maybe do the IO in a separate thread at some point, and collect at the bottom (we can easily merge the sets
manifests_files.extend(
[
manifest
for manifest in snapshot.manifests(table.io)
if manifest.added_snapshot_id == snapshot.snapshot_id and manifest.content == manifest_content_filter
]
)
if last_snapshot is not None and last_snapshot.snapshot_id != from_snapshot.snapshot_id:
raise ValidationException("No matching snapshot found.")
return manifests_files, snapshots
def _filter_manifest_entries(
entry: ManifestEntry,
snapshot_ids: set[int],
data_filter: BooleanExpression | None,
partition_set: dict[int, set[Record]] | None,
entry_status: ManifestEntryStatus | None,
schema: Schema,
) -> bool:
"""Filter manifest entries based on data filter and partition set.
Args:
entry: Manifest entry to filter
snapshot_ids: set of snapshot ids to match data files
data_filter: Optional filter to match data files
partition_set: Optional set of partitions to match data files
entry_status: Optional status to match data files
schema: schema for filtering
Returns:
True if the entry should be included, False otherwise
"""
if entry.snapshot_id not in snapshot_ids:
return False
if entry_status is not None and entry.status != entry_status:
return False
if data_filter is not None:
evaluator = _InclusiveMetricsEvaluator(schema, data_filter)
if evaluator.eval(entry.data_file) is ROWS_CANNOT_MATCH:
return False
if partition_set is not None:
partition = entry.data_file.partition
spec_id = entry.data_file.spec_id
if spec_id not in partition_set or partition not in partition_set[spec_id]:
return False
return True
def _deleted_data_files(
table: Table,
starting_snapshot: Snapshot,
data_filter: BooleanExpression | None,
partition_set: dict[int, set[Record]] | None,
parent_snapshot: Snapshot | None,
) -> Iterator[ManifestEntry]:
"""Find deleted data files matching a filter since a starting snapshot.
Args:
table: Table to validate
starting_snapshot: Snapshot current at the start of the operation
data_filter: Expression used to find deleted data files
partition_set: dict of {spec_id: set[partition]} to filter on
parent_snapshot: Ending snapshot on the branch being validated
Returns:
List of conflicting manifest-entries
"""
# if there is no current table state, no files have been deleted
if parent_snapshot is None:
return
manifests, snapshot_ids = _validation_history(
table,
parent_snapshot,
starting_snapshot,
VALIDATE_DATA_FILES_EXIST_OPERATIONS,
ManifestContent.DATA,
)
for manifest in manifests:
for entry in manifest.fetch_manifest_entry(table.io, discard_deleted=False):
if _filter_manifest_entries(
entry, snapshot_ids, data_filter, partition_set, ManifestEntryStatus.DELETED, table.schema()
):
yield entry
def _validate_deleted_data_files(
table: Table,
starting_snapshot: Snapshot,
data_filter: BooleanExpression | None,
parent_snapshot: Snapshot,
) -> None:
"""Validate that no files matching a filter have been deleted from the table since a starting snapshot.
Args:
table: Table to validate
starting_snapshot: Snapshot current at the start of the operation
data_filter: Expression used to find deleted data files
parent_snapshot: Ending snapshot on the branch being validated
"""
conflicting_entries = _deleted_data_files(table, starting_snapshot, data_filter, None, parent_snapshot)
if any(conflicting_entries):
conflicting_snapshots = {entry.snapshot_id for entry in conflicting_entries}
raise ValidationException(f"Deleted data files were found matching the filter for snapshots {conflicting_snapshots}!")
def _added_data_files(
table: Table,
starting_snapshot: Snapshot,
data_filter: BooleanExpression | None,
partition_set: dict[int, set[Record]] | None,
parent_snapshot: Snapshot | None,
) -> Iterator[ManifestEntry]:
"""Return manifest entries for data files added between the starting snapshot and parent snapshot.
Args:
table: Table to get the history from
starting_snapshot: Starting snapshot to get the history from
data_filter: Optional filter to match data files
partition_set: Optional set of partitions to match data files
parent_snapshot: Parent snapshot to get the history from
Returns:
Iterator of manifest entries for added data files matching the conditions
"""
if parent_snapshot is None:
return
manifests, snapshot_ids = _validation_history(
table,
parent_snapshot,
starting_snapshot,
VALIDATE_ADDED_DATA_FILES_OPERATIONS,
ManifestContent.DATA,
)
for manifest in manifests:
for entry in manifest.fetch_manifest_entry(table.io):
if _filter_manifest_entries(entry, snapshot_ids, data_filter, partition_set, None, table.schema()):
yield entry
def _added_delete_files(
table: Table,
starting_snapshot: Snapshot,
data_filter: BooleanExpression | None,
partition_set: dict[int, set[Record]] | None,
parent_snapshot: Snapshot | None,
) -> DeleteFileIndex:
"""Return matching delete files that have been added to the table since a starting snapshot.
Args:
table: Table to get the history from
starting_snapshot: Starting snapshot to get the history from
data_filter: Optional filter to match data files
partition_set: Optional set of partitions to match data files
parent_snapshot: Parent snapshot to get the history from
Returns:
DeleteFileIndex
"""
if parent_snapshot is None or table.format_version < 2:
return DeleteFileIndex(table.schema())
manifests, snapshot_ids = _validation_history(
table, parent_snapshot, starting_snapshot, VALIDATE_ADDED_DELETE_FILES_OPERATIONS, ManifestContent.DELETES
)
dfi = DeleteFileIndex(table.schema())
for manifest in manifests:
for entry in manifest.fetch_manifest_entry(table.io, discard_deleted=True):
if _filter_manifest_entries(
entry, snapshot_ids, data_filter, partition_set, ManifestEntryStatus.ADDED, table.schema()
):
dfi.add_delete_file(entry, entry.data_file.partition)
return dfi
def _starting_sequence_number(table: Table, starting_snapshot: Snapshot) -> int:
"""Find the starting sequence number from a snapshot.
Args:
table: Table to find snapshot from
starting_snapshot: Snapshot from where to start looking
Returns
Sequence number as int
"""
if starting_snapshot is not None:
if seq := starting_snapshot.sequence_number:
return seq
return INITIAL_SEQUENCE_NUMBER
def _validate_added_data_files(
table: Table,
starting_snapshot: Snapshot,
data_filter: BooleanExpression | None,
parent_snapshot: Snapshot | None,
) -> None:
"""Validate that no files matching a filter have been added to the table since a starting snapshot.
Args:
table: Table to validate
starting_snapshot: Snapshot current at the start of the operation
data_filter: Expression used to find added data files
parent_snapshot: Ending snapshot on the branch being validated
"""
conflicting_entries = _added_data_files(table, starting_snapshot, data_filter, None, parent_snapshot)
if any(conflicting_entries):
conflicting_snapshots = {entry.snapshot_id for entry in conflicting_entries if entry.snapshot_id is not None}
raise ValidationException(f"Added data files were found matching the filter for snapshots {conflicting_snapshots}!")
def _validate_no_new_delete_files(
table: Table,
starting_snapshot: Snapshot,
data_filter: BooleanExpression | None,
partition_set: dict[int, set[Record]] | None,
parent_snapshot: Snapshot | None,
) -> None:
"""Validate no new delete files matching a filter have been added to the table since starting a snapshot.
Args:
table: Table to validate
starting_snapshot: Snapshot current at the start of the operation
data_filter: Expression used to find added data files
partition_set: Dictionary of partition spec to set of partition records
parent_snapshot: Ending snapshot on the branch being validated
"""
deletes = _added_delete_files(table, starting_snapshot, data_filter, partition_set, parent_snapshot)
if deletes.is_empty():
return
conflicting_delete_paths = [file.file_path for file in deletes.referenced_delete_files()]
raise ValidationException(
f"Found new conflicting delete files that can apply to records matching {data_filter}: {conflicting_delete_paths}"
)
def _validate_no_new_deletes_for_data_files(
table: Table,
starting_snapshot: Snapshot,
data_filter: BooleanExpression | None,
data_files: set[DataFile],
parent_snapshot: Snapshot | None,
) -> None:
"""Validate no new delete files must be applied for data files that have been added to the table since a starting snapshot.
Args:
table: Table to validate
starting_snapshot: Snapshot current at the start of the operation
data_filter: Expression used to find added data files
data_files: data files to validate have no new deletes
parent_snapshot: Ending snapshot on the branch being validated
"""
# If there is no current state, or no files has been added
if parent_snapshot is None or table.format_version < 2:
return
deletes = _added_delete_files(table, starting_snapshot, data_filter, None, parent_snapshot)
seq_num = _starting_sequence_number(table, starting_snapshot)
# Fail to any delete file found that applies to files written in or before the starting snapshot
for data_file in data_files:
delete_files = deletes.for_data_file(seq_num, data_file, data_file.partition)
if len(delete_files) > 0:
raise ValidationException(f"Cannot commit, found new delete for replace data file {data_file}")