Skip to content
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
22 changes: 22 additions & 0 deletions docs/docs/multimodal-table/data-evolution.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,28 @@ when the commit contains existing-row BLOB or VECTOR staged files. It does
not suppress logical concurrent-update conflicts. If recovery is unavailable,
re-plan the operation against the latest snapshot.

## Batch Incremental DIFF Reads

With `incremental-between-scan-mode=diff`, Data Evolution compares the live file
identities in the two snapshots, including their RowID metadata. It returns the
visible end-snapshot rows in the RowID ranges of file entries absent from the
starting snapshot. Files shared with the starting snapshot
still supply unchanged columns and the end-snapshot deletion-vector anchor.
Projecting only unchanged columns does not remove the corresponding updates.

This is a file incremental read, not a row-level changelog. A range can contain
rows whose values did not change, and compaction can cause rows to be returned
again. A deletion without newly added data files produces no rows; deleted rows
are excluded when a new column file covers their range. Tag-to-tag DIFF reads
use the same semantics. DIFF planning still materializes snapshot metadata.
Requested RowID ranges further restrict the selected ranges. Automatic global-index
pruning is disabled for incremental scans until the index and data plan can be bound
to the same historical endpoint. DIFF rejects unbound external global-index
results. Use `withRowRanges` to select explicit RowIDs in DIFF. DELTA, including
AUTO when it selects DELTA, accepts caller-supplied RowIDs and scores from
an external global-index result. Ordinary snapshot reads retain their index
optimizations.

## Compaction and Row-ID Lifetime

Repeated updates add column files. Ordinary compaction consolidates them while
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,16 @@ public DataEvolutionBatchScan withGlobalIndexResult(GlobalIndexResult globalInde
return this;
}

/** Applies a row-ID selection against the snapshot on which it was evaluated. */
/** Applies a single-snapshot search result, pinning its source snapshot when available. */
public DataEvolutionBatchScan withGlobalIndexResult(
GlobalIndexResult globalIndexResult, Snapshot snapshot) {
GlobalIndexResult globalIndexResult, @Nullable Snapshot snapshot) {
Preconditions.checkArgument(
table.coreOptions().startupMode() != CoreOptions.StartupMode.INCREMENTAL,
"A single-snapshot global-index result cannot replace an incremental scan.");
withGlobalIndexResult(globalIndexResult);
batchScan.withStartingSnapshot(snapshot);
if (snapshot != null) {
batchScan.withStartingSnapshot(snapshot);
}
return this;
}

Expand Down Expand Up @@ -511,6 +516,14 @@ private boolean queryAuthEnabled() {
}

private Optional<GlobalIndexResult> evalGlobalIndex() {
CoreOptions options = table.coreOptions();
boolean incremental = options.startupMode() == CoreOptions.StartupMode.INCREMENTAL;
if (incremental && globalIndexResult != null) {
Preconditions.checkArgument(
!batchScan.usesSnapshotDiff(),
"Unbound global-index results are not supported for incremental DIFF reads; "
+ "use withRowRanges for explicit row-id selections.");
}
// the index ranks raw values, which a mask may invalidate; fall back to a full scan.
// Checked before the supplied result too: withGlobalIndexResult is public, so a caller
// can hand in one that was computed off the raw values.
Expand All @@ -523,7 +536,6 @@ private Optional<GlobalIndexResult> evalGlobalIndex() {
if (filter == null) {
return Optional.empty();
}
CoreOptions options = table.coreOptions();
if (!options.globalIndexEnabled()) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public long rowCount() {

@Override
public OptionalLong mergedRowCount() {
if (split.deletionFiles().isPresent()
&& split.deletionFiles().get().stream().anyMatch(Objects::nonNull)) {
return OptionalLong.empty();
}
return OptionalLong.of(rowCount());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
package org.apache.paimon.globalindex;

import org.apache.paimon.data.InternalRow;
import org.apache.paimon.fs.Path;
import org.apache.paimon.reader.FileRecordIterator;
import org.apache.paimon.reader.RecordReader;
import org.apache.paimon.reader.ScoreRecordIterator;
import org.apache.paimon.reader.ScoreRecordReader;
import org.apache.paimon.table.SpecialFields;
import org.apache.paimon.types.RowType;
import org.apache.paimon.utils.Filter;
import org.apache.paimon.utils.ProjectedRow;
import org.apache.paimon.utils.Range;

Expand All @@ -32,6 +35,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import static org.apache.paimon.utils.Preconditions.checkArgument;

Expand All @@ -57,42 +61,99 @@ public ScoreRecordIterator<InternalRow> readBatch() throws IOException {
if (iterator == null) {
return null;
}
return new ScoreRecordIterator<InternalRow>() {
ScoreRecordIterator<InternalRow> scored =
new ScoreRecordIterator<InternalRow>() {

private float score = Float.NaN;
private long rowId;
private float score = Float.NaN;
private long rowId;

@Override
public float returnedScore() {
return score;
}
@Override
public float returnedScore() {
return score;
}

@Override
public long returnedRowId() {
return rowId;
}
@Override
public long returnedRowId() {
return rowId;
}

@Override
public InternalRow next() throws IOException {
InternalRow row = iterator.next();
if (row != null && rowIdIndex >= 0) {
this.rowId = row.getLong(rowIdIndex);
if (rowIdToScore != null) {
this.score = rowIdToScore.get(rowId);
@Override
public InternalRow next() throws IOException {
InternalRow row = iterator.next();
if (row != null && rowIdIndex >= 0) {
this.rowId = row.getLong(rowIdIndex);
if (rowIdToScore != null) {
this.score = rowIdToScore.get(rowId);
}
if (projectedRow != null) {
projectedRow.replaceRow(row);
return projectedRow;
}
}
return row;
}
if (projectedRow != null) {
projectedRow.replaceRow(row);
return projectedRow;

@Override
public void releaseBatch() {
iterator.releaseBatch();
}
}
return row;
}
};
return iterator instanceof FileRecordIterator
? new FileScoreRecordIterator<>(scored, (FileRecordIterator<?>) iterator)
: scored;
}

@Override
public void releaseBatch() {
iterator.releaseBatch();
}
};
private static class FileScoreRecordIterator<T>
implements FileRecordIterator<T>, ScoreRecordIterator<T> {

private final ScoreRecordIterator<T> scored;
private final FileRecordIterator<?> file;

private FileScoreRecordIterator(ScoreRecordIterator<T> scored, FileRecordIterator<?> file) {
this.scored = scored;
this.file = file;
}

@Override
public long returnedPosition() {
return file.returnedPosition();
}

@Override
public Path filePath() {
return file.filePath();
}

@Override
public float returnedScore() {
return scored.returnedScore();
}

@Override
public long returnedRowId() {
return scored.returnedRowId();
}

@Nullable
@Override
public T next() throws IOException {
return scored.next();
}

@Override
public void releaseBatch() {
scored.releaseBatch();
}

@Override
public <R> FileScoreRecordIterator<R> transform(Function<T, R> function) {
return new FileScoreRecordIterator<>(scored.transform(function), file);
}

@Override
public FileScoreRecordIterator<T> filter(Filter<T> filter) {
return new FileScoreRecordIterator<>(scored.filter(filter), file);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ public FileStoreScan withReadType(RowType readType) {
return this;
}

/** Reads complete file identities and dependencies before computing a snapshot difference. */
public Plan planForIncrementalDiff() {
Predicate savedFilter = inputFilter;
RowType savedReadType = readType;
Long savedLimit = limit;
try {
withFilter(null);
readType = null;
limit = null;
return plan();
} finally {
withFilter(savedFilter);
readType = savedReadType;
limit = savedLimit;
}
}

@Override
public Iterator<ManifestEntry> readManifestEntries(
List<ManifestFileMeta> manifestFiles, boolean useSequential) {
Expand Down Expand Up @@ -217,6 +234,15 @@ private boolean filterByStats(List<ManifestEntry> entries) {
stats.rowCount(), stats.minValues(), stats.maxValues(), stats.nullCounts());
}

/** Prunes a complete dependency group after identifying the changed RowIDs. */
public List<ManifestEntry> pruneIncrementalDiffDependencies(List<ManifestEntry> group) {
if (readType != null && readType.containsField(SpecialFields.SEQUENCE_NUMBER.name())) {
// The newest file can supply the sequence number without any projected data columns.
return group;
}
return pruneByReadType(group);
}

/**
* Per-file column pruning within a row-id-range group: drop files whose physical columns have
* no overlap with the query's {@code readType} or filter. Necessary for columnar-split DE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.paimon.schema.TableSchema;
import org.apache.paimon.table.BucketMode;
import org.apache.paimon.table.source.snapshot.FullStartingScanner;
import org.apache.paimon.table.source.snapshot.IncrementalDiffStartingScanner;
import org.apache.paimon.table.source.snapshot.ReadPlanStartingScanner;
import org.apache.paimon.table.source.snapshot.SnapshotReader;
import org.apache.paimon.table.source.snapshot.StartingScanner;
Expand Down Expand Up @@ -115,15 +116,12 @@ public InnerTableScan withTopN(TopN topN) {
/** Selects the snapshot for a single-snapshot scan before planning its data splits. */
@Nullable
public Snapshot prepareSnapshot() {
if (startingScanner == null) {
startingScanner = createStartingScanner(false);
}
if (!(startingScanner instanceof ReadPlanStartingScanner)) {
StartingScanner scanner = startingScanner();
if (!(scanner instanceof ReadPlanStartingScanner)) {
return null;
}

SnapshotReader configured =
((ReadPlanStartingScanner) startingScanner).prepareRead(snapshotReader);
SnapshotReader configured = ((ReadPlanStartingScanner) scanner).prepareRead(snapshotReader);
return configured == null ? null : configured.specifiedSnapshot();
}

Expand All @@ -144,17 +142,15 @@ protected final TableScan.Plan planWithoutAuth() {
return preProcessedPlan;
}

if (startingScanner == null) {
startingScanner = createStartingScanner(false);
}
StartingScanner scanner = startingScanner();

StartingScanner.Result result;
Optional<StartingScanner.Result> pushed = applyPushDownLimit();
if (pushed.isPresent()) {
result = pushed.get();
} else {
pushed = applyPushDownTopN();
result = pushed.orElseGet(() -> startingScanner.scan(snapshotReader));
result = pushed.orElseGet(() -> scanner.scan(snapshotReader));
}

if (result instanceof ScannedResult) {
Expand All @@ -179,10 +175,7 @@ public List<PartitionEntry> listPartitionEntries() {
// it would report partitions the caller cannot read, and pushing a filter on a masked
// column against raw partition values would drop partitions the query matches
applyAuthRules();
if (startingScanner == null) {
startingScanner = createStartingScanner(false);
}
return startingScanner.scanPartitions(snapshotReader);
return startingScanner().scanPartitions(snapshotReader);
}

@Override
Expand All @@ -191,6 +184,18 @@ public List<BinaryRow> topNPartitions(int num, int partitionFieldCount) {
listPartitionEntries(), schema.logicalPartitionType(), num, partitionFieldCount);
}

/** Whether the resolved starting scanner compares two complete snapshots. */
public boolean usesSnapshotDiff() {
return startingScanner() instanceof IncrementalDiffStartingScanner;
}

private StartingScanner startingScanner() {
if (startingScanner == null) {
startingScanner = createStartingScanner(false);
}
return startingScanner;
}

private Optional<StartingScanner.Result> applyPushDownLimit() {
// A read-time filter (WHERE or auth) drops rows after scanning, so only push the limit down
// when neither is present.
Expand Down
Loading
Loading