-
Notifications
You must be signed in to change notification settings - Fork 3k
Core, Spark 3.4: Add filter to Rewrite position deletes #7582
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -24,8 +24,10 @@ | |
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import org.apache.iceberg.expressions.Expression; | ||
| import org.apache.iceberg.expressions.Expressions; | ||
| import org.apache.iceberg.expressions.ManifestEvaluator; | ||
| import org.apache.iceberg.expressions.Projections; | ||
| import org.apache.iceberg.expressions.ResidualEvaluator; | ||
| import org.apache.iceberg.io.CloseableIterable; | ||
| import org.apache.iceberg.io.CloseableIterator; | ||
|
|
@@ -130,18 +132,28 @@ private Schema calculateSchema() { | |
| public static class PositionDeletesBatchScan | ||
| extends SnapshotScan<BatchScan, ScanTask, ScanTaskGroup<ScanTask>> implements BatchScan { | ||
|
|
||
| private Expression baseTableFilter = Expressions.alwaysTrue(); | ||
|
|
||
| protected PositionDeletesBatchScan(Table table, Schema schema) { | ||
| super(table, schema, TableScanContext.empty()); | ||
| } | ||
|
|
||
| /** @deprecated the API will be removed in v1.5.0 */ | ||
| @Deprecated | ||
| protected PositionDeletesBatchScan(Table table, Schema schema, TableScanContext context) { | ||
| super(table, schema, context); | ||
| } | ||
|
|
||
| protected PositionDeletesBatchScan( | ||
| Table table, Schema schema, TableScanContext context, Expression baseTableFilter) { | ||
| super(table, schema, context); | ||
| this.baseTableFilter = baseTableFilter; | ||
| } | ||
|
|
||
| @Override | ||
| protected PositionDeletesBatchScan newRefinedScan( | ||
| Table newTable, Schema newSchema, TableScanContext newContext) { | ||
| return new PositionDeletesBatchScan(newTable, newSchema, newContext); | ||
| return new PositionDeletesBatchScan(newTable, newSchema, newContext, baseTableFilter); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -155,39 +167,70 @@ protected List<String> scanColumns() { | |
| return context().returnColumnStats() ? DELETE_SCAN_WITH_STATS_COLUMNS : DELETE_SCAN_COLUMNS; | ||
| } | ||
|
|
||
| /** | ||
| * Sets a filter that applies on base table of this position deletes table, to use for this | ||
| * scan. | ||
| * | ||
| * <p>Only the partition expressions part of the filter will be applied to the position deletes | ||
| * table, as the schema of the base table does not otherwise match the schema of position | ||
| * deletes table. | ||
| * | ||
| * <ul> | ||
| * <li>Only the partition expressions of the filter that can be projected on the base table | ||
| * partition specs, via {@link | ||
| * org.apache.iceberg.expressions.Projections.ProjectionEvaluator#project(Expression)} | ||
| * will be evaluated. Note, not all partition expressions can be projected. | ||
| * <li>Because it cannot apply beyond the partition expression, this filter will not | ||
| * contribute to the residuals of tasks returned by this scan. (See {@link | ||
| * PositionDeletesScanTask#residual()}) | ||
| * </ul> | ||
| * | ||
| * @param expr expression filter that applies on the base table of this posiiton deletes table | ||
| * @return this for method chaining | ||
| */ | ||
| public BatchScan baseTableFilter(Expression expr) { | ||
| return new PositionDeletesBatchScan( | ||
| table(), schema(), context(), Expressions.and(baseTableFilter, expr)); | ||
| } | ||
|
|
||
| @Override | ||
| protected CloseableIterable<ScanTask> doPlanFiles() { | ||
| String schemaString = SchemaParser.toJson(tableSchema()); | ||
|
|
||
| // prepare transformed partition specs and caches | ||
| Map<Integer, PartitionSpec> transformedSpecs = transformSpecs(tableSchema(), table().specs()); | ||
|
|
||
| LoadingCache<Integer, String> specStringCache = | ||
| partitionCacheOf(transformedSpecs, PartitionSpecParser::toJson); | ||
| LoadingCache<Integer, ManifestEvaluator> deletesTableEvalCache = | ||
| partitionCacheOf( | ||
| transformedSpecs, | ||
| spec -> ManifestEvaluator.forRowFilter(filter(), spec, isCaseSensitive())); | ||
| LoadingCache<Integer, ManifestEvaluator> baseTableEvalCache = | ||
| partitionCacheOf( | ||
| table().specs(), // evaluate base table filters on base table specs | ||
| spec -> ManifestEvaluator.forRowFilter(baseTableFilter, spec, isCaseSensitive())); | ||
| LoadingCache<Integer, ResidualEvaluator> residualCache = | ||
| partitionCacheOf( | ||
| transformedSpecs, | ||
| spec -> | ||
| ResidualEvaluator.of( | ||
| spec, | ||
| // there are no applicable filters in the base table's filter | ||
| // that we can use to evaluate on the position deletes table | ||
| shouldIgnoreResiduals() ? Expressions.alwaysTrue() : filter(), | ||
| isCaseSensitive())); | ||
|
|
||
| LoadingCache<Integer, String> specStringCache = | ||
| partitionCacheOf(transformedSpecs, PartitionSpecParser::toJson); | ||
|
|
||
| LoadingCache<Integer, ManifestEvaluator> evalCache = | ||
| partitionCacheOf( | ||
| transformedSpecs, | ||
| spec -> ManifestEvaluator.forRowFilter(filter(), spec, isCaseSensitive())); | ||
|
|
||
| // iterate through delete manifests | ||
| List<ManifestFile> manifests = snapshot().deleteManifests(table().io()); | ||
|
|
||
| CloseableIterable<ManifestFile> matchingManifests = | ||
| CloseableIterable.filter( | ||
| scanMetrics().skippedDeleteManifests(), | ||
| CloseableIterable.withNoopClose(manifests), | ||
| manifest -> evalCache.get(manifest.partitionSpecId()).eval(manifest)); | ||
|
|
||
| manifest -> | ||
| baseTableEvalCache.get(manifest.partitionSpecId()).eval(manifest) | ||
| && deletesTableEvalCache.get(manifest.partitionSpecId()).eval(manifest)); | ||
| matchingManifests = | ||
| CloseableIterable.count(scanMetrics().scannedDeleteManifests(), matchingManifests); | ||
|
|
||
|
|
@@ -196,7 +239,12 @@ protected CloseableIterable<ScanTask> doPlanFiles() { | |
| matchingManifests, | ||
| manifest -> | ||
| posDeletesScanTasks( | ||
| manifest, schemaString, transformedSpecs, residualCache, specStringCache)); | ||
| manifest, | ||
| table().specs().get(manifest.partitionSpecId()), | ||
| schemaString, | ||
| transformedSpecs, | ||
| residualCache, | ||
| specStringCache)); | ||
|
|
||
| if (planExecutor() != null) { | ||
| return new ParallelIterable<>(tasks, planExecutor()); | ||
|
|
@@ -207,6 +255,7 @@ protected CloseableIterable<ScanTask> doPlanFiles() { | |
|
|
||
| private CloseableIterable<ScanTask> posDeletesScanTasks( | ||
| ManifestFile manifest, | ||
| PartitionSpec spec, | ||
| String schemaString, | ||
| Map<Integer, PartitionSpec> transformedSpecs, | ||
| LoadingCache<Integer, ResidualEvaluator> residualCache, | ||
|
|
@@ -223,12 +272,16 @@ public void close() throws IOException { | |
|
|
||
| @Override | ||
| public CloseableIterator<ScanTask> iterator() { | ||
| Expression partitionFilter = | ||
| Projections.inclusive(spec, isCaseSensitive()).project(baseTableFilter); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could have cached this and used |
||
|
|
||
| // Filter partitions | ||
| CloseableIterable<ManifestEntry<DeleteFile>> deleteFileEntries = | ||
| ManifestFiles.readDeleteManifest(manifest, table().io(), transformedSpecs) | ||
| .caseSensitive(isCaseSensitive()) | ||
| .select(scanColumns()) | ||
| .filterRows(filter()) | ||
| .filterPartitions(partitionFilter) | ||
| .scanMetrics(scanMetrics()) | ||
| .liveEntries(); | ||
|
|
||
|
|
||
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Shall we do this filter only if either of the filter expression is non-trivial? Otherwise, what's the point of doing this work?
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.
Well, this is for manifests, so shouldn't matter that much. Never mind.