-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Spark: Read DVs when reading from .position_deletes table #11657
Open
nastra
wants to merge
2
commits into
apache:main
Choose a base branch
from
nastra:dv-iterable-for-position-deletes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+419
−0
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/source/DVIterable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package org.apache.iceberg.spark.source; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import org.apache.iceberg.DeleteFile; | ||
import org.apache.iceberg.MetadataColumns; | ||
import org.apache.iceberg.PartitionSpec; | ||
import org.apache.iceberg.Schema; | ||
import org.apache.iceberg.deletes.PositionDeleteIndex; | ||
import org.apache.iceberg.io.CloseableGroup; | ||
import org.apache.iceberg.io.CloseableIterable; | ||
import org.apache.iceberg.io.CloseableIterator; | ||
import org.apache.iceberg.io.InputFile; | ||
import org.apache.iceberg.puffin.BlobMetadata; | ||
import org.apache.iceberg.puffin.Puffin; | ||
import org.apache.iceberg.puffin.PuffinReader; | ||
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
import org.apache.iceberg.relocated.com.google.common.collect.Iterables; | ||
import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
import org.apache.iceberg.util.Pair; | ||
import org.apache.spark.sql.catalyst.InternalRow; | ||
import org.apache.spark.sql.catalyst.expressions.GenericInternalRow; | ||
import org.apache.spark.unsafe.types.UTF8String; | ||
|
||
class DVIterable extends CloseableGroup implements CloseableIterable<InternalRow> { | ||
private final Puffin.ReadBuilder builder; | ||
private final PartitionSpec spec; | ||
private final DeleteFile deleteFile; | ||
private final Schema projection; | ||
|
||
DVIterable(InputFile inputFile, DeleteFile deleteFile, PartitionSpec spec, Schema projection) { | ||
this.deleteFile = deleteFile; | ||
this.builder = Puffin.read(inputFile); | ||
this.spec = spec; | ||
this.projection = projection; | ||
} | ||
|
||
@Override | ||
public CloseableIterator<InternalRow> iterator() { | ||
PuffinReader reader = builder.build(); | ||
addCloseable(reader); | ||
return new DVIterator(reader); | ||
} | ||
|
||
private class DVIterator implements CloseableIterator<InternalRow> { | ||
private final PuffinReader reader; | ||
private Iterator<Long> positions = Collections.emptyIterator(); | ||
private List<Object> rowValues; | ||
private Integer deletedPositionIndex; | ||
|
||
DVIterator(PuffinReader reader) { | ||
this.reader = reader; | ||
try { | ||
reader.fileMetadata().blobs().stream() | ||
.filter( | ||
blob -> | ||
// read the correct blob for the referenced data file | ||
Objects.equals( | ||
deleteFile.referencedDataFile(), | ||
blob.properties().get("referenced-data-file"))) | ||
.findFirst() | ||
.ifPresent( | ||
blob -> { | ||
// there should only be a single element | ||
Pair<BlobMetadata, ByteBuffer> current = | ||
Iterables.getOnlyElement(reader.readAll(ImmutableList.of(blob))); | ||
List<Long> pos = Lists.newArrayList(); | ||
PositionDeleteIndex.deserialize(current.second().array(), deleteFile) | ||
.forEach(pos::add); | ||
this.positions = pos.iterator(); | ||
}); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return positions.hasNext(); | ||
} | ||
|
||
@Override | ||
public InternalRow next() { | ||
long position = positions.next(); | ||
|
||
if (null == rowValues) { | ||
this.rowValues = Lists.newArrayList(); | ||
if (null != projection.findField(MetadataColumns.DELETE_FILE_PATH.fieldId())) { | ||
rowValues.add(UTF8String.fromString(deleteFile.referencedDataFile())); | ||
} | ||
|
||
if (null != projection.findField(MetadataColumns.DELETE_FILE_POS.fieldId())) { | ||
rowValues.add(position); | ||
// remember the index where the deleted position needs to be set | ||
deletedPositionIndex = rowValues.size() - 1; | ||
} | ||
|
||
if (null != projection.findField(MetadataColumns.DELETE_FILE_ROW_FIELD_ID)) { | ||
// there's no info about deleted rows with DVs, so always return null | ||
rowValues.add(null); | ||
} | ||
|
||
if (null != projection.findField(MetadataColumns.PARTITION_COLUMN_ID)) { | ||
StructInternalRow partition = new StructInternalRow(spec.partitionType()); | ||
partition.setStruct(deleteFile.partition()); | ||
rowValues.add(partition); | ||
} | ||
|
||
if (null != projection.findField(MetadataColumns.SPEC_ID_COLUMN_ID)) { | ||
rowValues.add(deleteFile.specId()); | ||
} | ||
|
||
if (null != projection.findField(MetadataColumns.FILE_PATH_COLUMN_ID)) { | ||
rowValues.add(UTF8String.fromString(deleteFile.location())); | ||
} | ||
} else if (null != deletedPositionIndex) { | ||
// only update the deleted position if necessary, everything else stays the same | ||
rowValues.set(deletedPositionIndex, position); | ||
} | ||
nastra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return new GenericInternalRow(rowValues.toArray()); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException("Remove is not supported"); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (null != reader) { | ||
reader.close(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[optional] might be too much, but can we have one reader per DV file ? considering specifically for this use case we will have to read all the blobs in the DV file eventually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you have a particular use case in mind as this isn't something that is being needed currently when reading the
PositionDeletesTable
?