Skip to content
Closed
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 @@ -212,21 +212,32 @@ public <T> List<T> read(
}

/**
* Scans projected manifest entries without materializing {@link PojoManifestEntry}s.
*
* <p>Every returned {@link ProjectedManifestEntry} has independent backing data and can be
* retained after the iterator advances or closes. The caller must close the iterator.
*
* <p>This method intentionally bypasses the manifest cache because cached entries are
* materialized with the complete manifest schema.
* Scans projected manifest entries without materializing {@link PojoManifestEntry}s. Returned
* entries have independent backing data. The caller must close the iterator.
*/
public CloseableIterator<ProjectedManifestEntry> scan(String fileName, Projection projection) {
return scan(fileName, projection, null, null, null);
}

/**
* Scans projected entries with manifest pruning while keeping only the current reader block in
* memory.
*/
public CloseableIterator<ProjectedManifestEntry> scan(
String fileName,
Projection projection,
@Nullable PartitionPredicate partitionFilter,
@Nullable BucketFilter bucketFilter,
@Nullable ManifestSidecar.Selection selected) {
try {
CloseableIterator<InternalRow> rows =
createManifestIterator(
fileIO,
pathFactory.toPath(fileName),
projection.projectedType(),
partitionFilter,
bucketFilter,
selected,
null,
null);
return new CloseableIterator<ProjectedManifestEntry>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.paimon.manifest.ManifestFileMeta;
import org.apache.paimon.manifest.ManifestSidecar;
import org.apache.paimon.manifest.PartitionEntry;
import org.apache.paimon.manifest.ProjectedManifestEntry;
import org.apache.paimon.manifest.SimpleFileEntry;
import org.apache.paimon.operation.metrics.ScanMetrics;
import org.apache.paimon.operation.metrics.ScanStats;
Expand All @@ -41,6 +42,7 @@
import org.apache.paimon.table.source.ScanMode;
import org.apache.paimon.types.RowType;
import org.apache.paimon.utils.BiFilter;
import org.apache.paimon.utils.CloseableIterator;
import org.apache.paimon.utils.Filter;
import org.apache.paimon.utils.ListUtils;
import org.apache.paimon.utils.Pair;
Expand All @@ -60,6 +62,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -435,6 +438,236 @@ public Iterator<ManifestEntry> readFileIterator(List<ManifestFileMeta> manifestF
return readManifestEntries(manifestFileMetas, true);
}

@Override
public boolean supportsStreamingPlan() {
return limit == null && !postFilterManifestEntriesEnabled();
}

@Override
public StreamingPlan streamingPlan() {
long started = System.nanoTime();
if (!supportsStreamingPlan()) {
throw new UnsupportedOperationException(
"This scan requires post-processing the complete manifest entry list");
}
ManifestsReader.Result manifestsResult = readManifests();
if (manifestsResult.filteredManifests.stream()
.anyMatch(manifest -> manifest.numDeletedFiles() > 0)) {
throw new StreamingPlanFallbackException(
"Streaming file planning requires add-only manifest entries");
}
Snapshot snapshot = manifestsResult.snapshot;
long allDataFiles =
manifestsResult.allManifests.stream()
.mapToLong(f -> f.numAddedFiles() - f.numDeletedFiles())
.sum();
CloseableIterator<ManifestEntry> files =
streamAddedManifestEntries(manifestsResult.filteredManifests);
CloseableIterator<ManifestEntry> measuredFiles =
reportStreamingScanOnCompletion(
files,
System.nanoTime() - started,
snapshot,
manifestsResult.filteredManifests.size(),
allDataFiles);
return new StreamingPlan() {
@Nullable
@Override
public Long watermark() {
return snapshot == null ? null : snapshot.watermark();
}

@Nullable
@Override
public Snapshot snapshot() {
return snapshot;
}

@Override
public CloseableIterator<ManifestEntry> files() {
return measuredFiles;
}
};
}

private CloseableIterator<ManifestEntry> reportStreamingScanOnCompletion(
CloseableIterator<ManifestEntry> files,
long preparationNanos,
@Nullable Snapshot snapshot,
int scannedManifests,
long allDataFiles) {
return new CloseableIterator<ManifestEntry>() {
private long resultFiles;
private long scanNanos = preparationNanos;
private boolean reported;

@Override
public boolean hasNext() {
long started = System.nanoTime();
boolean hasNext;
try {
hasNext = files.hasNext();
} finally {
scanNanos = Math.addExact(scanNanos, System.nanoTime() - started);
}
if (!hasNext) {
report();
}
return hasNext;
}

@Override
public ManifestEntry next() {
long started = System.nanoTime();
ManifestEntry entry;
try {
entry = files.next();
} finally {
scanNanos = Math.addExact(scanNanos, System.nanoTime() - started);
}
resultFiles = Math.addExact(resultFiles, 1);
return entry;
}

private void report() {
if (reported) {
return;
}
reported = true;
long scanDuration = scanNanos / 1_000_000;
LOG.info(
"Streaming file store scan plan completed in {} ms. Files size : {}",
scanDuration,
resultFiles);
if (scanMetrics != null) {
scanMetrics.reportScan(
new ScanStats(
scanDuration,
snapshot == null ? 0 : snapshot.id(),
scannedManifests,
allDataFiles - resultFiles,
resultFiles));
}
}

@Override
public void close() throws Exception {
files.close();
}
};
}

private CloseableIterator<ManifestEntry> streamAddedManifestEntries(
List<ManifestFileMeta> manifests) {
List<ManifestFileMeta> addedManifests =
manifests.stream()
.filter(manifest -> manifest.numAddedFiles() > 0)
.collect(Collectors.toList());
Iterator<ManifestFileMeta> manifestIterator = addedManifests.iterator();
Filter<InternalRow> rowFilter = createEntryRowFilter();
BucketFilter bucketFilter = createBucketFilter();
PartitionPredicate partitionFilter = manifestsReader.partitionFilter();
ManifestEntrySerializer serializer = new ManifestEntrySerializer();

return new CloseableIterator<ManifestEntry>() {
private CloseableIterator<ProjectedManifestEntry> entries = CloseableIterator.empty();
private ManifestEntry next;
private boolean closed;

@Override
public boolean hasNext() {
if (next == null && !closed) {
advance();
}
return next != null;
}

@Override
public ManifestEntry next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
ManifestEntry result = next;
next = null;
return result;
}

private void advance() {
try {
while (next == null) {
while (entries.hasNext()) {
ProjectedManifestEntry projected = entries.next();
try {
if (rowFilter.test(projected.fullRow())
&& (manifestEntryFilter == null
|| manifestEntryFilter.test(projected))
&& filterByStats(projected)) {
ManifestEntry materialized =
serializer.fromRow(projected.fullRow());
next =
dropStats
? materialized.copyWithoutStats()
: materialized;
return;
}
} finally {
projected.clear();
}
}

closeEntries();
if (!manifestIterator.hasNext()) {
closed = true;
return;
}

ManifestFileMeta manifest = manifestIterator.next();
ManifestFile manifestFile = manifestFileFactory.create();
ManifestSidecar.Selection selected =
manifestFile.selectBlocks(
manifest, rowRangeIndex, partitionFilter, bucketFilter);
if (selected != null && selected.blocks().isEmpty()) {
continue;
}
entries =
manifestFile.scan(
manifest.fileName(),
ProjectedManifestEntry.fullProjection(),
partitionFilter,
bucketFilter,
selected);
}
} catch (RuntimeException | Error error) {
try {
close();
} catch (Exception closeError) {
error.addSuppressed(closeError);
}
throw error;
}
}

private void closeEntries() {
try {
entries.close();
entries = CloseableIterator.empty();
} catch (Exception error) {
throw new RuntimeException("Failed to close streaming manifest reader", error);
}
}

@Override
public void close() throws Exception {
if (!closed) {
closed = true;
next = null;
entries.close();
entries = CloseableIterator.empty();
}
}
};
}

public Iterator<ManifestEntry> readManifestEntries(
List<ManifestFileMeta> manifests, boolean useSequential) {
return scanMode == ScanMode.ALL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.paimon.table.source.ScanMode;
import org.apache.paimon.types.RowType;
import org.apache.paimon.utils.BiFilter;
import org.apache.paimon.utils.CloseableIterator;
import org.apache.paimon.utils.Filter;
import org.apache.paimon.utils.Range;
import org.apache.paimon.utils.RowRangeIndex;
Expand Down Expand Up @@ -125,6 +126,25 @@ public interface FileStoreScan {

Iterator<ManifestEntry> readFileIterator(List<ManifestFileMeta> manifestFileMetas);

/** Whether this scan can expose its effective files without first materializing them. */
default boolean supportsStreamingPlan() {
return false;
}

/** Produce snapshot metadata and an iterator over the effective files of the plan. */
default StreamingPlan streamingPlan() {
throw new UnsupportedOperationException(
"Streaming file planning is not supported by " + getClass().getName());
}

/** Signals that a scan discovered a shape which requires the eager planning implementation. */
final class StreamingPlanFallbackException extends UnsupportedOperationException {

public StreamingPlanFallbackException(String message) {
super(message);
}
}

default List<BinaryRow> listPartitions() {
return readPartitionEntries().stream()
.map(PartitionEntry::partition)
Expand Down Expand Up @@ -164,4 +184,16 @@ static Map<BinaryRow, Map<Integer, List<ManifestEntry>>> groupByPartFiles(
return groupBy;
}
}

/** Snapshot metadata plus a single-pass iterator over effective manifest entries. */
interface StreamingPlan {

@Nullable
Long watermark();

@Nullable
Snapshot snapshot();

CloseableIterator<ManifestEntry> files();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.paimon.schema.SchemaManager;
import org.apache.paimon.schema.TableSchema;
import org.apache.paimon.table.BucketMode;
import org.apache.paimon.table.source.snapshot.ReadPlanStartingScanner;
import org.apache.paimon.table.source.snapshot.SnapshotReader;
import org.apache.paimon.table.source.snapshot.StartingScanner;
import org.apache.paimon.table.source.snapshot.StartingScanner.ScannedResult;
Expand Down Expand Up @@ -141,6 +142,36 @@ protected final TableScan.Plan planWithoutAuth() {
return postProcessPlan(DataFilePlan.fromResult(result));
}

@Override
protected SnapshotReader.SplitPlan openSplitPlanWithoutAuth() {
if (!incrementalSplitPlanningEnabled()
|| !snapshotReader.supportsFineGrainedSplitPlanning()
|| pushDownLimit != null
|| topN != null) {
return super.openSplitPlanWithoutAuth();
}
if (startingScanner == null) {
startingScanner = createStartingScanner(false);
}
if (!(startingScanner instanceof ReadPlanStartingScanner)) {
return super.openSplitPlanWithoutAuth();
}
if (!hasNext) {
throw new EndOfScanException();
}
hasNext = false;
SnapshotReader.SplitPlan plan =
((ReadPlanStartingScanner) startingScanner).openSplitPlan(snapshotReader);
if (plan.snapshotId() != null) {
maybeCreateReadProtectionTag(plan.snapshotId());
}
return plan;
}

protected boolean incrementalSplitPlanningEnabled() {
return false;
}

@Nullable
protected Plan preProcessPlan() {
return null;
Expand Down
Loading
Loading