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

Iceberg iterators refactor #5549

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 @@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.NoSuchElementException;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
import org.apache.iceberg.FileScanTask;
Expand Down Expand Up @@ -85,105 +86,98 @@ protected InputFile getInputFile(String location) {
*/
public CloseableIterable<HiveBatchContext> filterBatch(CloseableIterable<HiveBatchContext> batches) {

CloseableIterator<HiveBatchContext> iterator = new DeleteFilterBatchIterator(batches);

return new CloseableIterable<HiveBatchContext>() {

@Override
public CloseableIterator<HiveBatchContext> iterator() {
return iterator;
}

@Override
public void close() throws IOException {
iterator.close();
}
};
}

// VRB iterator with the delete filter
private class DeleteFilterBatchIterator implements CloseableIterator<HiveBatchContext> {

// Delete filter pipeline setup logic:
// A HiveRow iterable (deleteInputIterable) is provided as input iterable for the DeleteFilter.
// The content in deleteInputIterable is provided by row iterators from the incoming VRBs i.e. on the arrival of
// a new batch the underlying iterator gets swapped.
SwappableHiveRowIterable deleteInputIterable = new SwappableHiveRowIterable();
private final SwappableHiveRowIterable deleteInputIterable;

// Output iterable of DeleteFilter, and its iterator
CloseableIterable<HiveRow> deleteOutputIterable = filter(deleteInputIterable);
CloseableIterator<HiveRow> deleteOutputIterator = deleteOutputIterable.iterator();
private final CloseableIterable<HiveRow> deleteOutputIterable;

return new CloseableIterable<HiveBatchContext>() {
private final CloseableIterator<HiveBatchContext> srcIterator;

@Override
public CloseableIterator<HiveBatchContext> iterator() {
DeleteFilterBatchIterator(CloseableIterable<HiveBatchContext> batches) {
deleteInputIterable = new SwappableHiveRowIterable();
deleteOutputIterable = filter(deleteInputIterable);
srcIterator = batches.iterator();
}

CloseableIterator<HiveBatchContext> srcIterator = batches.iterator();
@Override
public boolean hasNext() {
return srcIterator.hasNext();
}

return new CloseableIterator<HiveBatchContext>() {
@Override
public HiveBatchContext next() {
try {
if (!hasNext()) {
throw new NoSuchElementException();
}
HiveBatchContext batchContext = srcIterator.next();
VectorizedRowBatch batch = batchContext.getBatch();

@Override
public boolean hasNext() {
return srcIterator.hasNext();
}
int oldSize = batch.size;
int newSize = 0;

@Override
public HiveBatchContext next() {
try {
if (!hasNext()) {
throw new NoSuchElementException();
}
HiveBatchContext currentBatchContext = srcIterator.next();
deleteInputIterable.currentRowIterator = currentBatchContext.rowIterator();
VectorizedRowBatch batch = currentBatchContext.getBatch();

int oldSize = batch.size;
int newSize = 0;

// Apply delete filtering and adjust the selected array so that undeleted row indices are filled with it.
while (deleteOutputIterator.hasNext()) {
HiveRow row = deleteOutputIterator.next();
if (!row.isDeleted()) {
batch.selected[newSize++] = row.physicalBatchIndex();
}
}

if (newSize < oldSize) {
batch.size = newSize;
batch.selectedInUse = true;
}
return currentBatchContext;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
try (CloseableIterator<HiveRow> rowIterator = batchContext.rowIterator()) {
deleteInputIterable.currentRowIterator = rowIterator;

@Override
public void close() throws IOException {
srcIterator.close();
// Apply delete filtering and adjust the selected array so that undeleted row indices are filled with it.
for (HiveRow row : deleteOutputIterable) {
if (!row.isDeleted()) {
batch.selected[newSize++] = row.physicalBatchIndex();
}
}
};
}
if (newSize < oldSize) {
batch.size = newSize;
batch.selectedInUse = true;
}
return batchContext;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public void close() throws IOException {
batches.close();
}
};
@Override
public void close() throws IOException {
IOUtils.close(srcIterator, deleteOutputIterable);
}
}

// HiveRow iterable that wraps an interchangeable source HiveRow iterable
static class SwappableHiveRowIterable implements CloseableIterable<HiveRow> {
static final class SwappableHiveRowIterable implements CloseableIterable<HiveRow> {

private CloseableIterator<HiveRow> currentRowIterator;

@Override
public CloseableIterator<HiveRow> iterator() {

return new CloseableIterator<HiveRow>() {

@Override
public boolean hasNext() {
return currentRowIterator.hasNext();
}

@Override
public HiveRow next() {
return currentRowIterator.next();
}

@Override
public void close() throws IOException {
currentRowIterator.close();
}
};
return currentRowIterator;
}

@Override
public void close() throws IOException {
currentRowIterator.close();
IOUtils.close(currentRowIterator);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -63,7 +64,6 @@
import org.apache.iceberg.orc.ORC;
import org.apache.iceberg.parquet.Parquet;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
import org.apache.iceberg.types.Type;
import org.apache.iceberg.types.TypeUtil;
import org.apache.iceberg.types.Types;
Expand Down Expand Up @@ -91,22 +91,21 @@ public final class IcebergRecordReader<T> extends AbstractIcebergRecordReader<T>
}
}

private Iterable<FileScanTask> tasks;
private Iterator<FileScanTask> tasks;
private CloseableIterator<T> currentIterator;
private T current;

@Override
public void initialize(InputSplit split, TaskAttemptContext newContext) {
// For now IcebergInputFormat does its own split planning and does not accept FileSplit instances
super.initialize(split, newContext);
ScanTaskGroup<FileScanTask> task = ((IcebergSplit) split).taskGroup();
this.tasks = task.tasks();
ScanTaskGroup<FileScanTask> taskGroup = ((IcebergSplit) split).taskGroup();
this.tasks = taskGroup.tasks().iterator();
this.currentIterator = nextTask();
}

private CloseableIterator<T> nextTask() {
CloseableIterator<T> closeableIterator = CloseableIterable.concat(
Iterables.transform(tasks, task -> open(task, expectedSchema))).iterator();
CloseableIterator<T> closeableIterator = open(tasks.next(), expectedSchema).iterator();
if (!isFetchVirtualColumns() || Utilities.getIsVectorized(conf)) {
return closeableIterator;
}
Expand All @@ -116,13 +115,15 @@ private CloseableIterator<T> nextTask() {

@Override
public boolean nextKeyValue() throws IOException {
if (currentIterator.hasNext()) {
current = currentIterator.next();
return true;
} else {
while (!currentIterator.hasNext()) {
currentIterator.close();
return false;
if (!tasks.hasNext()) {
return false;
}
currentIterator = nextTask();
}
current = currentIterator.next();
return true;
}

@Override
Expand Down
Loading