rdblue commented on code in PR #5268: URL: https://github.com/apache/iceberg/pull/5268#discussion_r925859764
########## api/src/main/java/org/apache/iceberg/metrics/ScanReport.java: ########## @@ -0,0 +1,251 @@ +/* + * 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.metrics; + +import java.io.Serializable; +import java.time.Duration; +import java.util.concurrent.TimeUnit; +import org.apache.iceberg.Schema; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.metrics.MetricsContext.Counter; +import org.apache.iceberg.relocated.com.google.common.base.MoreObjects; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; + +/** + * A Table Scan report that contains all relevant information from a Table Scan. + */ +public class ScanReport implements Serializable { + + private final String tableName; + private final long snapshotId; + private final Expression filter; + private final Schema projection; + private final int matchingDataFiles; + private final int matchingDataManifests; + private final int totalDataManifestsRead; + private final int addedDataFiles; + private final int deletedDataFiles; + private final Duration totalPlanningDuration; + private final long totalFileSizeBytes; + + private ScanReport( + String tableName, long snapshotId, Expression filter, Schema projection, + int matchingDataFiles, int matchingDataManifests, int totalDataManifestsRead, + int addedDataFiles, int deletedDataFiles, Duration totalPlanningDuration, + long totalFileSizeBytes) { + this.tableName = tableName; + this.snapshotId = snapshotId; + this.filter = filter; + this.projection = projection; + this.matchingDataFiles = matchingDataFiles; + this.matchingDataManifests = matchingDataManifests; + this.totalDataManifestsRead = totalDataManifestsRead; + this.addedDataFiles = addedDataFiles; + this.deletedDataFiles = deletedDataFiles; + this.totalPlanningDuration = totalPlanningDuration; + this.totalFileSizeBytes = totalFileSizeBytes; + } + + public String tableName() { + return tableName; + } + + public long snapshotId() { + return snapshotId; + } + + public Expression filter() { + return filter; + } + + public Schema projection() { + return projection; + } + + public int matchingDataFiles() { + return matchingDataFiles; + } + + public int matchingDataManifests() { + return matchingDataManifests; + } + + public int totalDataManifestsRead() { + return totalDataManifestsRead; + } + + public int addedDataFiles() { + return addedDataFiles; + } + + public int deletedDataFiles() { + return deletedDataFiles; + } + + public Duration totalPlanningDuration() { + return totalPlanningDuration; + } + + public long totalFileSizeBytes() { + return totalFileSizeBytes; + } + + public static Builder builder() { + return new Builder(); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("tableName", tableName) + .add("snapshotId", snapshotId) + .add("filter", filter) + .add("projection", projection) + .add("matchingDataFiles", matchingDataFiles) + .add("matchingDataManifests", matchingDataManifests) + .add("totalDataManifestsRead", totalDataManifestsRead) + .add("addedDataFiles", addedDataFiles) + .add("deletedDataFiles", deletedDataFiles) + .add("totalScanDuration", totalPlanningDuration) + .add("totalFileSizeBytes", totalFileSizeBytes) + .toString(); + } + + @SuppressWarnings("HiddenField") + public static class Builder { + private String tableName; + private long snapshotId = -1L; + private Expression filter; + private Schema projection; + private ScanMetrics scanMetrics; + + private Builder() { + } + + public Builder withTableName(String tableName) { + this.tableName = tableName; + return this; + } + + public Builder withSnapshotId(long snapshotId) { + this.snapshotId = snapshotId; + return this; + } + + public Builder withFilter(Expression filter) { + this.filter = filter; + return this; + } + + public Builder withProjection(Schema projection) { + this.projection = projection; + return this; + } + + public Builder fromScanMetrics(ScanMetrics scanMetrics) { + this.scanMetrics = scanMetrics; + return this; + } + + public ScanReport build() { + Preconditions.checkArgument(null != tableName, "TableName must be non-null"); + Preconditions.checkArgument(null != filter, "Expression filter must be non-null"); + Preconditions.checkArgument(null != projection, "Schema projection must be non-null"); + Preconditions.checkArgument(null != scanMetrics, "ScanMetrics must be non-null"); + return new ScanReport(tableName, snapshotId, filter, projection, + scanMetrics.matchingDataFiles().count().orElse(-1), Review Comment: Following up on [this comment](https://github.com/apache/iceberg/pull/5268#discussion_r922334117), I don't think that this should produce -1. The reason why `count` returns an `Optional` is that implementations, like Hadoop counters, may not be able to return the final value. Those counters aren't appropriate here, because we need to get the value to build a scan report. There are a couple options to fix. One is to log an error and not send the scan report, so this method would return null. I'm not a big fan of that, or of throwing an exception because metrics collection should not fail scans. The fix that I think makes the most sense is to let the `ScanReporter` implementation handle all of this internally. There's not really a need to have separate classes for `ScanMetrics` and `ScanReport`, when the report just tries to get the final metrics values. Separating the two classes creates a situation where the Iceberg library needs to interpret the `ScanMetrics` implementation that is provided by the `ScanReporter`. Instead, I think it's simpler just to pass the `ScanMetrics` (which were created by the `ScanReporter`) back as the final report. I think removing `ScanReport` and having just `ScanMetrics` is a cleaner in general as well. I don't see a clear purpose for a separate `ScanReport`, and a simpler API is generally better. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
