Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include deletion vector when filtering active add entries in Delta #24524

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public record DeletionVectorEntry(String storageType, String pathOrInlineDv, Opt
requireNonNull(offset, "offset is null");
}

// https://github.com/delta-io/delta/blob/34f02d8/kernel/kernel-api/src/main/java/io/delta/kernel/internal/actions/DeletionVectorDescriptor.java#L167-L174
public String uniqueId()
{
String uniqueFileId = storageType + pathOrInlineDv;
if (offset.isPresent()) {
return uniqueFileId + "@" + offset;
}
return uniqueFileId;
}

public long getRetainedSizeInBytes()
{
return INSTANCE_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,21 +429,23 @@ public static ImmutableList<DeltaLakeColumnMetadata> columnsWithStats(List<Delta

private Stream<AddFileEntry> activeAddEntries(Stream<DeltaLakeTransactionLogEntry> checkpointEntries, List<Transaction> transactions)
{
Map<String, AddFileEntry> activeJsonEntries = new LinkedHashMap<>();
HashSet<String> removedFiles = new HashSet<>();
Map<FileEntryKey, AddFileEntry> activeJsonEntries = new LinkedHashMap<>();
HashSet<FileEntryKey> removedFiles = new HashSet<>();

// The json entries containing the last few entries in the log need to be applied on top of the parquet snapshot:
// - Any files which have been removed need to be excluded
// - Any files with newer add actions need to be updated with the most recent metadata
transactions.forEach(transaction -> {
Map<String, AddFileEntry> addFilesInTransaction = new LinkedHashMap<>();
Set<String> removedFilesInTransaction = new HashSet<>();
Map<FileEntryKey, AddFileEntry> addFilesInTransaction = new LinkedHashMap<>();
Set<FileEntryKey> removedFilesInTransaction = new HashSet<>();
transaction.transactionEntries().forEach(deltaLakeTransactionLogEntry -> {
if (deltaLakeTransactionLogEntry.getAdd() != null) {
addFilesInTransaction.put(deltaLakeTransactionLogEntry.getAdd().getPath(), deltaLakeTransactionLogEntry.getAdd());
AddFileEntry add = deltaLakeTransactionLogEntry.getAdd();
addFilesInTransaction.put(new FileEntryKey(add.getPath(), add.getDeletionVector().map(DeletionVectorEntry::uniqueId)), add);
}
else if (deltaLakeTransactionLogEntry.getRemove() != null) {
removedFilesInTransaction.add(deltaLakeTransactionLogEntry.getRemove().path());
RemoveFileEntry remove = deltaLakeTransactionLogEntry.getRemove();
removedFilesInTransaction.add(new FileEntryKey(remove.path(), remove.deletionVector().map(DeletionVectorEntry::uniqueId)));
}
});

Expand All @@ -456,11 +458,16 @@ else if (deltaLakeTransactionLogEntry.getRemove() != null) {
Stream<AddFileEntry> filteredCheckpointEntries = checkpointEntries
.map(DeltaLakeTransactionLogEntry::getAdd)
.filter(Objects::nonNull)
.filter(addEntry -> !removedFiles.contains(addEntry.getPath()) && !activeJsonEntries.containsKey(addEntry.getPath()));
.filter(addEntry -> {
FileEntryKey key = new FileEntryKey(addEntry.getPath(), addEntry.getDeletionVector().map(DeletionVectorEntry::uniqueId));
return !removedFiles.contains(key) && !activeJsonEntries.containsKey(key);
});

return Stream.concat(filteredCheckpointEntries, activeJsonEntries.values().stream());
}

private record FileEntryKey(String path, Optional<String> deletionVectorId) {}

public Stream<RemoveFileEntry> getRemoveEntries(ConnectorSession session, TableSnapshot tableSnapshot)
{
return getEntries(
Expand Down
Loading