-
Notifications
You must be signed in to change notification settings - Fork 3k
Core: TableMetadata Projection #14502
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
Open
singhpk234
wants to merge
2
commits into
apache:main
Choose a base branch
from
singhpk234:feature/snapshot-transformer
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.
+745
−3
Open
Changes from all commits
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
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
204 changes: 204 additions & 0 deletions
204
core/src/main/java/org/apache/iceberg/TableMetadataProjection.java
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 |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
|
|
||
| /** | ||
| * A projection wrapper for {@link TableMetadata} that transforms snapshots on-demand. | ||
| * | ||
| * <p>This projection follows the same pattern as StructProjection, applying transformations lazily | ||
| * when snapshot-related methods are accessed. Unlike embedding transformation logic in | ||
| * TableMetadata itself, this keeps the transformation concerns separate. | ||
| * | ||
| * <p>The projection is lightweight - it extends TableMetadata and overrides only snapshot-related | ||
| * methods, allowing all other metadata (schemas, specs, properties, etc.) to be inherited | ||
| * naturally. | ||
| * | ||
| * <p>Example use cases: | ||
| * | ||
| * <ul> | ||
| * <li>Filtering sensitive data from snapshot summaries before exposing metadata | ||
| * <li>Enriching snapshots with additional computed properties | ||
| * <li>Testing scenarios requiring modified snapshot metadata without persisting changes | ||
| * </ul> | ||
| * | ||
| * <p>Example usage: | ||
| * | ||
| * <pre> | ||
| * // Remove sensitive data from snapshot summaries | ||
| * TableMetadata cleaned = TableMetadataProjection.create( | ||
| * originalMetadata, | ||
| * snapshot -> { | ||
| * if (snapshot instanceof BaseSnapshot) { | ||
| * BaseSnapshot base = (BaseSnapshot) snapshot; | ||
| * Map<String, String> filteredSummary = filterSensitiveKeys(base.summary()); | ||
| * return new BaseSnapshot( | ||
| * base.sequenceNumber(), base.snapshotId(), base.parentId(), | ||
| * base.timestampMillis(), base.operation(), filteredSummary, | ||
| * base.schemaId(), base.manifestListLocation(), | ||
| * base.firstRowId(), base.addedRows(), base.keyId()); | ||
| * } | ||
| * return snapshot; | ||
| * }); | ||
| * </pre> | ||
| */ | ||
| public class TableMetadataProjection extends TableMetadata { | ||
|
|
||
| /** | ||
| * Creates a projecting wrapper for {@link TableMetadata} that transforms snapshots. | ||
| * | ||
| * @param metadata the base table metadata to wrap | ||
| * @param snapshotTransformer function to transform each snapshot | ||
| * @return a projection that applies the transformer to all snapshots | ||
| */ | ||
| public static TableMetadata create( | ||
| TableMetadata metadata, Function<Snapshot, Snapshot> snapshotTransformer) { | ||
| Preconditions.checkArgument(metadata != null, "Metadata cannot be null"); | ||
| Preconditions.checkArgument(snapshotTransformer != null, "Snapshot transformer cannot be null"); | ||
| return new TableMetadataProjection(metadata, snapshotTransformer); | ||
| } | ||
|
|
||
| private final Function<Snapshot, Snapshot> snapshotTransformer; | ||
| private volatile List<Snapshot> transformedSnapshots; | ||
| private volatile Map<Long, Snapshot> transformedSnapshotsById; | ||
| private volatile Map<String, SnapshotRef> transformedRefs; | ||
|
|
||
| private TableMetadataProjection( | ||
| TableMetadata base, Function<Snapshot, Snapshot> snapshotTransformer) { | ||
| // Call super constructor with all fields from base metadata | ||
| // Pass null for snapshots since we'll provide them via override | ||
| super( | ||
| base.metadataFileLocation(), | ||
| base.formatVersion(), | ||
| base.uuid(), | ||
| base.location(), | ||
| base.lastSequenceNumber(), | ||
| base.lastUpdatedMillis(), | ||
| base.lastColumnId(), | ||
| base.currentSchemaId(), | ||
| base.schemas(), | ||
| base.defaultSpecId(), | ||
| base.specs(), | ||
| base.lastAssignedPartitionId(), | ||
| base.defaultSortOrderId(), | ||
| base.sortOrders(), | ||
| base.properties(), | ||
| base.currentSnapshotId(), // Get raw field value without triggering loading | ||
| null, // snapshots will be provided via override | ||
| base::snapshots, // lazy supplier to trigger transformation | ||
| base.snapshotLog(), | ||
| base.previousFiles(), | ||
| base.refs(), | ||
| base.statisticsFiles(), | ||
| base.partitionStatisticsFiles(), | ||
| base.nextRowId(), | ||
| base.encryptionKeys(), | ||
| base.changes()); | ||
|
|
||
| this.snapshotTransformer = snapshotTransformer; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Snapshot> snapshots() { | ||
| if (transformedSnapshots == null) { | ||
| synchronized (this) { | ||
| if (transformedSnapshots == null) { | ||
| // Trigger lazy loading via super, which will call our supplier | ||
| List<Snapshot> baseSnapshots = super.snapshots(); | ||
| transformedSnapshots = | ||
| baseSnapshots.stream() | ||
| .map(snapshotTransformer) | ||
| .collect(ImmutableList.toImmutableList()); | ||
| } | ||
| } | ||
| } | ||
| return transformedSnapshots; | ||
| } | ||
|
|
||
| @Override | ||
| public Snapshot snapshot(long snapshotId) { | ||
| // Build index lazily and look up transformed snapshot | ||
| if (transformedSnapshotsById == null) { | ||
| synchronized (this) { | ||
| if (transformedSnapshotsById == null) { | ||
| transformedSnapshotsById = | ||
| snapshots().stream() | ||
| .collect(ImmutableMap.toImmutableMap(Snapshot::snapshotId, s -> s)); | ||
| } | ||
| } | ||
| } | ||
| return transformedSnapshotsById.get(snapshotId); | ||
| } | ||
|
|
||
| @Override | ||
| public Snapshot currentSnapshot() { | ||
| long currentId = currentSnapshotId(); | ||
| if (currentId == -1) { | ||
| return null; | ||
| } | ||
|
|
||
| // Trigger loading if needed and return transformed snapshot | ||
| return snapshot(currentId); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, SnapshotRef> refs() { | ||
| if (transformedRefs == null) { | ||
| synchronized (this) { | ||
| if (transformedRefs == null) { | ||
| // Rebuild refs to point to transformed snapshots | ||
| // Filter out any refs that point to snapshots that no longer exist after transformation | ||
| Map<Long, Snapshot> transformedById = transformedSnapshotsById(); | ||
| transformedRefs = | ||
| super.refs().entrySet().stream() | ||
| .filter(entry -> transformedById.containsKey(entry.getValue().snapshotId())) | ||
| .collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
| } | ||
| } | ||
| } | ||
| return transformedRefs; | ||
| } | ||
|
|
||
| @Override | ||
| public SnapshotRef ref(String name) { | ||
| return refs().get(name); | ||
| } | ||
|
|
||
| private Map<Long, Snapshot> transformedSnapshotsById() { | ||
| if (transformedSnapshotsById == null) { | ||
| synchronized (this) { | ||
| if (transformedSnapshotsById == null) { | ||
| transformedSnapshotsById = | ||
| snapshots().stream() | ||
| .collect(ImmutableMap.toImmutableMap(Snapshot::snapshotId, s -> s)); | ||
| } | ||
| } | ||
| } | ||
| return transformedSnapshotsById; | ||
| } | ||
|
|
||
| // All other methods (schemas, specs, properties, statistics, etc.) are | ||
| // automatically inherited from TableMetadata without any delegation needed | ||
| } |
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.
Why does this need to change
TableMetadata? I think it can be dangerous if now there are cases wheresnapshotsis null and that doesn't represent the actual table state. If I understand the intent of this PR, it is to have an alternative toTableMetadatathat suppresses some information.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.
Ah, I see that this is because
TableMetadataProjectioninherits fromTableMetadata. I think that pattern would cause problems as I mentioned above becauseTableMetadataProjectionis a perfectly validTableMetadata.Is this really needed? If the idea is to transform snapshots when they are returned by accessors, you may not need to change this class at all.
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.
Agree, this is not needed for this pr at all this is mostly for case when snapshot is null and we call indexAndValidateSnapshots which fails with an NPE here
Happy to remove it or create a seperate pr for fixing this, caught this while testing projection
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.
I think that there should never be a case where
snapshotsis null. I'd probably keep that assumption in the code and, if anything, just add a check in the constructor that the list is valid.