Repository navigation
Only apply position deletes to the data files they are attached to - #4411
rahulsmahadev wants to merge 3 commits into
Conversation
_read_all_delete_files merged the positions of every position delete file in a scan by data file path, so a delete file that also names another data file removed that file's rows, even when the planner had not attached the delete to it (for example an older delete for a file that add_files registered again). Keep, per delete file, only the positions of the data files whose task has it attached, reading each delete file once. Co-authored-by: Isaac <no-reply@databricks.com>
|
@geruh, this keeps each position delete to the data files that DeleteFileIndex (#2918) attached it to. @KaiqiJinWow, you last changed |
Co-authored-by: Isaac <no-reply@databricks.com>
ptimizeroracle
left a comment
There was a problem hiding this comment.
ran the trigger from the description on main with _read_all_delete_files swapped for the one in this diff (transcribed, not the branch): to_arrow() goes from 18 to 19 rows and agrees with count().
one shape it still misses: the same path live twice in a snapshot. append P, commit a position delete of (P, 0), then add_files([P], check_duplicate_files=False). the planner attaches the delete to the seq 1 entry only, but the lookup is still deletes_per_file.get(task.file.file_path), so the seq 3 entry loses row 0 too: to_arrow() 18 rows, count() 19 (it scans one task at a time, so it is right). keeping positions per delete file and gathering them per task from task.delete_files in batches_for_task fixed all three of my shapes, made attached_paths unnecessary, and left main's delete/scan/count tests unchanged (192 passed). rarer state, same root cause.
(ai-assisted, checked by me)
The same path can be live twice in a snapshot (add_files with check_duplicate_files=False), and the planner attaches a position delete only to the entries it applies to. Looking the positions up by path applied it to every entry of the path. Keep the positions per delete file and gather them for each task from task.delete_files, as ptimizeroracle suggested. Co-authored-by: Isaac <no-reply@databricks.com>
|
Thanks for checking this, and for the duplicate-path case. I reproduced it ( |
Rationale for this change
A position delete file can list rows of several data files.
ArrowScanloads the position deletes of every task andmerges them into one map keyed by data file path. A data file then gets the positions of any delete file that names
its path, even when the planner did not attach that delete file to its task. So a delete file that also names another
data file can remove rows from a file it was not planned for, undoing the sequence number and partition checks that
DeleteFileIndexmade for it.The trigger, on a v2 table with a position delete file written by another engine. P has ids 0..9, Q has ids 100..109,
and the delete file, committed before the first line, lists row 0 of P and row 0 of Q:
The lookup by path also mixes up two live entries of the same path. After
add_files([p_path], check_duplicate_files=False)registers P a second time, newer than a delete of P's row 0, the planner attaches the delete to the older entry only, but
both entries lose row 0:
to_arrow()returns 18 rows andcount()19. ptimizeroracle found this case in review.The delete predates P, so P must keep its row 0. Scan Planning in
format/spec.mdsays that a position delete appliesonly when "The data file's data sequence number is less than or equal to the delete file's data sequence number", and
that the data file and position delete file must have equal partitions (both spec and partition values). Java gives each task only its
own deletes:
GenericDeleteFilteris built fromtask.deletes(), andBaseDeleteLoaderreads the delete files it isgiven and keeps the positions for the data file path it is asked for.
This change does the same, as ptimizeroracle suggested:
_read_all_delete_filesstill reads each delete file once,but keeps its positions per delete file, and
_positional_deletes_for_taskgathers the positions of a task's data filefrom the delete files attached to that task (
task.delete_files). The key is unchanged: the path plus the contentoffset and size, so deletion vector ranges in one Puffin file stay separate. Planning is unchanged.
_DeleteFileKeycomes from #3478. PR #3285 also edits_read_all_delete_files: it skips non-position delete files inthe loop over
task.delete_files. The two changes now conflict in that loop, and after both merge,_positional_deletes_for_taskneeds the same check, since it looks up every delete file of the task.Are these changes tested?
Yes. The two scan tests fail on main and pass with this change.
test_position_delete_respects_sequence_number_of_readded_file(tests/table/test_init.py,catalogfixture) runs thetrigger above, and a second shape where the delete is committed after P is registered again, so it removes row 0 of
both files. It asserts the exact rows, that
count()agrees withto_arrow(), and that the row-filtered scan agreeswith the unfiltered scan. On main, the first shape fails because id 0 is missing.
test_position_delete_applies_only_to_the_entry_of_a_path_it_is_attached_to(tests/table/test_init.py) registers thesame path twice, with a position delete of row 0 in between, and asserts that only the older entry loses row 0 and
that
count()agrees withto_arrow(). On main,to_arrow()returns 18 rows instead of 19.test_positional_deletes_for_task_use_only_the_delete_files_attached_to_it(tests/io/test_pyarrow.py) checks thepositions each task gets for four attachment shapes. The third gives the first data file a newer delete file as well, so
a fix that merges the positions of all tasks fails it, and the fourth has the same path in two tasks. The two existing
_read_all_delete_filestests for deletion vectors now check the positions per task.commit_partition_position_delete(tests/table/test_commit_retry.py) now takes a list of(file_path, pos)rows, so onedelete file can name two data files. Its existing callers pass one row.
Are there any user-facing changes?
Yes, a read bug fix: scans no longer drop rows that a newer data file or a newer entry of the same path keeps, and an upsert over such a file no longer
inserts a duplicate of a row it should update. There is no API change.
Please add the
changeloglabel.This pull request and its description were written by Isaac.