-
Notifications
You must be signed in to change notification settings - Fork 2.9k
API, Core, Spark: Scan API for partition stats #14508
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| public interface PartitionStatistics extends StructLike { | ||
|
|
||
| /** Returns the partition of these partition statistics */ | ||
| StructLike partition(); | ||
|
|
||
| /** Returns the spec ID of the partition of these partition statistics */ | ||
| int specId(); | ||
|
|
||
| /** Returns the number of data records in the partition */ | ||
| long dataRecordCount(); | ||
|
|
||
| /** Returns the number of data files in the partition */ | ||
| int dataFileCount(); | ||
|
|
||
| /** Returns the total size of data files in bytes in the partition */ | ||
| long totalDataFileSizeInBytes(); | ||
|
|
||
| /** Returns the number of positional delete records in the partition */ | ||
| long positionDeleteRecordCount(); | ||
|
|
||
| /** Returns the number of positional delete files in the partition */ | ||
| int positionDeleteFileCount(); | ||
|
|
||
| /** Returns the number of equality delete records in the partition */ | ||
| long equalityDeleteRecordCount(); | ||
|
|
||
| /** Returns the number of equality delete files in the partition */ | ||
| int equalityDeleteFileCount(); | ||
|
|
||
| /** Returns the total number of records in the partition */ | ||
| Long totalRecords(); | ||
|
|
||
| /** Returns the timestamp in milliseconds when the partition was last updated */ | ||
| Long lastUpdatedAt(); | ||
|
|
||
| /** Returns the ID of the snapshot that last updated this partition */ | ||
| Long lastUpdatedSnapshotId(); | ||
|
|
||
| /** Returns the number of delete vectors in the partition */ | ||
| int dvCount(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * 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 org.apache.iceberg.expressions.Expression; | ||
| import org.apache.iceberg.io.CloseableIterable; | ||
|
|
||
| public interface PartitionStatisticsScan { | ||
|
|
||
| /** | ||
| * Create a new scan from this scan's configuration that will use the given snapshot by ID. | ||
| * | ||
| * @param snapshotId a snapshot ID | ||
| * @return a new scan based on this with the given snapshot ID | ||
| * @throws IllegalArgumentException if the snapshot cannot be found | ||
| */ | ||
| PartitionStatisticsScan useSnapshot(long snapshotId); | ||
|
|
||
| /** | ||
| * Create a new scan from the results of this, where partitions are filtered by the {@link | ||
| * Expression}. | ||
| * | ||
| * @param filter a filter expression | ||
| * @return a new scan based on this with results filtered by the expression | ||
| */ | ||
| PartitionStatisticsScan filter(Expression filter); | ||
|
|
||
| /** | ||
| * Create a new scan from this with the schema as its projection. | ||
| * | ||
| * @param schema a projection schema | ||
|
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. How does the user create the I would prefer something like the
Collaborator
Author
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're right. Let me add this to the possible follow-up steps in my comment at the top |
||
| * @return a new scan based on this with the given projection | ||
| */ | ||
| PartitionStatisticsScan project(Schema schema); | ||
|
|
||
| /** | ||
| * Scans a partition statistics file belonging to a particular snapshot | ||
| * | ||
| * @return an Iterable of partition statistics | ||
| */ | ||
| CloseableIterable<PartitionStatistics> scan(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * 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.Optional; | ||
| import org.apache.iceberg.expressions.Expression; | ||
| import org.apache.iceberg.io.CloseableIterable; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.types.Types; | ||
|
|
||
| public class BasePartitionStatisticsScan implements PartitionStatisticsScan { | ||
|
|
||
| private final Table table; | ||
| private Long snapshotId; | ||
|
|
||
| public BasePartitionStatisticsScan(Table table) { | ||
| this.table = table; | ||
| } | ||
|
|
||
| @Override | ||
| public PartitionStatisticsScan useSnapshot(long newSnapshotId) { | ||
| Preconditions.checkArgument( | ||
| table.snapshot(newSnapshotId) != null, "Cannot find snapshot with ID %s", newSnapshotId); | ||
|
|
||
| this.snapshotId = newSnapshotId; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public PartitionStatisticsScan filter(Expression newFilter) { | ||
| throw new UnsupportedOperationException("Filtering is not supported"); | ||
| } | ||
|
|
||
| @Override | ||
| public PartitionStatisticsScan project(Schema newSchema) { | ||
| throw new UnsupportedOperationException("Projection is not supported"); | ||
| } | ||
|
|
||
| @Override | ||
| public CloseableIterable<PartitionStatistics> scan() { | ||
|
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. Do we have tests for this? |
||
| if (snapshotId == null) { | ||
| if (table.currentSnapshot() == null) { | ||
| return CloseableIterable.of(List.of()); | ||
| } | ||
|
|
||
| snapshotId = table.currentSnapshot().snapshotId(); | ||
| } | ||
|
|
||
| Optional<PartitionStatisticsFile> statsFile = | ||
| table.partitionStatisticsFiles().stream() | ||
| .filter(f -> f.snapshotId() == snapshotId) | ||
| .findFirst(); | ||
|
|
||
| if (statsFile.isEmpty()) { | ||
| return CloseableIterable.of(List.of()); | ||
| } | ||
|
|
||
| Types.StructType partitionType = Partitioning.partitionType(table); | ||
| Schema schema = PartitionStatsHandler.schema(partitionType, TableUtil.formatVersion(table)); | ||
|
|
||
| FileFormat fileFormat = FileFormat.fromFileName(statsFile.get().path()); | ||
|
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. I still think that getting the file format from the name is brittle. Maybe not in this PR, but I would love to have this fixed
Collaborator
Author
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. I can take a look at this in a separate PR. Currently, |
||
| Preconditions.checkNotNull( | ||
| fileFormat != null, "Unable to determine format of file: %s", statsFile.get().path()); | ||
|
|
||
| CloseableIterable<StructLike> records = | ||
| InternalData.read(fileFormat, table.io().newInputFile(statsFile.get().path())) | ||
| .project(schema) | ||
| .build(); | ||
|
|
||
| return CloseableIterable.transform(records, PartitionStatsHandler::recordToPartitionStats); | ||
| } | ||
| } | ||
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.
Maybe describe what will happen with the
PartitionStatisticsattributes which are not part of the schema.