You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/08/03 01:37:24 UTC

[GitHub] [iceberg] rdblue commented on a diff in pull request #5268: API/Core: Initial Table Scan Reporting support

rdblue commented on code in PR #5268:
URL: https://github.com/apache/iceberg/pull/5268#discussion_r936160673


##########
data/src/test/java/org/apache/iceberg/TestScanPlanningAndReporting.java:
##########
@@ -0,0 +1,261 @@
+/*
+ * 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 static org.apache.iceberg.Files.localInput;
+import static org.apache.iceberg.types.Types.NestedField.required;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.iceberg.data.GenericRecord;
+import org.apache.iceberg.data.parquet.GenericParquetWriter;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.metrics.LoggingScanReporter;
+import org.apache.iceberg.metrics.ScanReport;
+import org.apache.iceberg.metrics.ScanReporter;
+import org.apache.iceberg.parquet.Parquet;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Types;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestScanPlanningAndReporting extends TableTestBase {
+
+  private final Schema schema =
+      new Schema(
+          required(1, "id", Types.IntegerType.get()), required(2, "x", Types.StringType.get()));
+
+  PartitionSpec partitionSpec = PartitionSpec.builderFor(schema).build();
+
+  private Table table;
+  private TestScanReporter reporter;
+
+  public TestScanPlanningAndReporting() {
+    super(2);
+  }
+
+  @Before
+  @Override
+  public void setupTable() throws Exception {
+    super.setupTable();
+    reporter = new TestScanReporter();
+    table =
+        TestTables.create(
+            tableDir,
+            "scan-planning-x",
+            schema,
+            partitionSpec,
+            SortOrder.unsorted(),
+            formatVersion,
+            reporter);
+    GenericRecord record = GenericRecord.create(schema);
+    record.setField("id", 1);
+    record.setField("x", "23");
+    GenericRecord record2 = GenericRecord.create(schema);
+    record2.setField("id", 2);
+    record2.setField("x", "30");
+    GenericRecord record3 = GenericRecord.create(schema);
+    record3.setField("id", 3);
+    record3.setField("x", "45");
+    GenericRecord record4 = GenericRecord.create(schema);
+    record4.setField("id", 3);
+    record4.setField("x", "51");
+    DataFile dataFile = writeParquetFile(table, Arrays.asList(record, record3));
+    DataFile dataFile2 = writeParquetFile(table, Arrays.asList(record2));
+    DataFile dataFile3 = writeParquetFile(table, Arrays.asList(record4));
+    table.newFastAppend().appendFile(dataFile).appendFile(dataFile2).appendFile(dataFile3).commit();
+    table.refresh();
+  }
+
+  @Test
+  public void testScanPlanningWithReport() throws IOException {
+    TableScan tableScan = table.newScan();
+
+    // should be 3 files
+    try (CloseableIterable<FileScanTask> fileScanTasks = tableScan.planFiles()) {
+      fileScanTasks.forEach(task -> {});
+    }
+
+    ScanReport scanReport = reporter.lastReport();
+    assertThat(scanReport).isNotNull();
+    assertThat(scanReport.tableName()).isEqualTo("scan-planning-x");
+    assertThat(scanReport.snapshotId()).isEqualTo(1L);
+    assertThat(scanReport.filter()).isEqualTo(Expressions.alwaysTrue());
+    assertThat(scanReport.scanMetrics().totalPlanningDuration().totalDuration())
+        .isGreaterThan(Duration.ZERO);
+    assertThat(scanReport.scanMetrics().resultDataFiles().value()).isEqualTo(3);
+    assertThat(scanReport.scanMetrics().resultDeleteFiles().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().scannedDataManifests().value()).isEqualTo(1);
+    assertThat(scanReport.scanMetrics().skippedDataManifests().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().totalDataManifests().value()).isEqualTo(1);
+    assertThat(scanReport.scanMetrics().totalDeleteManifests().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().totalFileSizeInBytes().value()).isGreaterThan(0L);
+    assertThat(scanReport.scanMetrics().totalDeleteFileSizeInBytes().value()).isEqualTo(0L);
+
+    // should be two files
+    try (CloseableIterable<FileScanTask> fileScanTasks =
+        tableScan.filter(Expressions.greaterThan("x", "30")).planFiles()) {
+      fileScanTasks.forEach(task -> {});
+    }
+
+    scanReport = reporter.lastReport();
+    assertThat(scanReport).isNotNull();
+    assertThat(scanReport.tableName()).isEqualTo("scan-planning-x");
+    assertThat(scanReport.snapshotId()).isEqualTo(1L);
+    assertThat(scanReport.scanMetrics().totalPlanningDuration().totalDuration())
+        .isGreaterThan(Duration.ZERO);
+    assertThat(scanReport.scanMetrics().resultDataFiles().value()).isEqualTo(2);
+    assertThat(scanReport.scanMetrics().resultDeleteFiles().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().scannedDataManifests().value()).isEqualTo(1);
+    assertThat(scanReport.scanMetrics().skippedDataManifests().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().totalDataManifests().value()).isEqualTo(1);
+    assertThat(scanReport.scanMetrics().totalDeleteManifests().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().totalFileSizeInBytes().value()).isGreaterThan(0L);
+    assertThat(scanReport.scanMetrics().totalDeleteFileSizeInBytes().value()).isEqualTo(0L);
+
+    // should be 1 file
+    try (CloseableIterable<FileScanTask> fileScanTasks =
+        tableScan.filter(Expressions.lessThan("x", "30")).planFiles()) {
+      fileScanTasks.forEach(task -> {});
+    }
+
+    scanReport = reporter.lastReport();
+    assertThat(scanReport).isNotNull();
+    assertThat(scanReport.tableName()).isEqualTo("scan-planning-x");
+    assertThat(scanReport.snapshotId()).isEqualTo(1L);
+    assertThat(scanReport.scanMetrics().totalPlanningDuration().totalDuration())
+        .isGreaterThan(Duration.ZERO);
+    assertThat(scanReport.scanMetrics().resultDataFiles().value()).isEqualTo(1);
+    assertThat(scanReport.scanMetrics().resultDeleteFiles().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().scannedDataManifests().value()).isEqualTo(1);
+    assertThat(scanReport.scanMetrics().skippedDataManifests().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().totalDataManifests().value()).isEqualTo(1);
+    assertThat(scanReport.scanMetrics().totalDeleteManifests().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().totalFileSizeInBytes().value()).isGreaterThan(0L);
+    assertThat(scanReport.scanMetrics().totalDeleteFileSizeInBytes().value()).isEqualTo(0L);
+
+    // all files
+    try (CloseableIterable<FileScanTask> fileScanTasks =
+        tableScan.filter(Expressions.lessThan("x", "52")).planFiles()) {
+      fileScanTasks.forEach(task -> {});
+    }
+
+    scanReport = reporter.lastReport();
+    assertThat(scanReport).isNotNull();
+    assertThat(scanReport.tableName()).isEqualTo("scan-planning-x");
+    assertThat(scanReport.snapshotId()).isEqualTo(1L);
+    assertThat(scanReport.scanMetrics().totalPlanningDuration().totalDuration())
+        .isGreaterThan(Duration.ZERO);
+    assertThat(scanReport.scanMetrics().resultDataFiles().value()).isEqualTo(3);
+    assertThat(scanReport.scanMetrics().resultDeleteFiles().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().scannedDataManifests().value()).isEqualTo(1);
+    assertThat(scanReport.scanMetrics().skippedDataManifests().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().totalDataManifests().value()).isEqualTo(1);
+    assertThat(scanReport.scanMetrics().totalDeleteManifests().value()).isEqualTo(0);
+    assertThat(scanReport.scanMetrics().totalFileSizeInBytes().value()).isGreaterThan(0L);
+    assertThat(scanReport.scanMetrics().totalDeleteFileSizeInBytes().value()).isEqualTo(0L);
+  }
+
+  @Test
+  public void deleteScanning() throws IOException {
+    Table tbl =
+        TestTables.create(
+            tableDir,
+            "scan-planning-with-deletes",
+            SCHEMA,
+            SPEC,
+            SortOrder.unsorted(),
+            formatVersion,
+            reporter);
+
+    tbl.newAppend().appendFile(FILE_A).appendFile(FILE_B).appendFile(FILE_C).commit();
+    tbl.newRowDelta().addDeletes(FILE_A_DELETES).addDeletes(FILE_B_DELETES).commit();
+    TableScan tableScan = tbl.newScan();
+
+    try (CloseableIterable<FileScanTask> fileScanTasks = tableScan.planFiles()) {
+      fileScanTasks.forEach(task -> {});
+    }
+
+    ScanReport scanReport = reporter.lastReport();
+    assertThat(scanReport).isNotNull();
+    assertThat(scanReport.tableName()).isEqualTo("scan-planning-with-deletes");
+    assertThat(scanReport.snapshotId()).isEqualTo(2L);
+    assertThat(scanReport.scanMetrics().totalPlanningDuration().totalDuration())
+        .isGreaterThan(Duration.ZERO);
+    assertThat(scanReport.scanMetrics().resultDataFiles().value()).isEqualTo(3);
+    assertThat(scanReport.scanMetrics().resultDeleteFiles().value()).isEqualTo(2);
+    assertThat(scanReport.scanMetrics().scannedDataManifests().value()).isEqualTo(1);
+    assertThat(scanReport.scanMetrics().skippedDataManifests().value()).isEqualTo(0);

Review Comment:
   I think we are going to need a test for skipped data manifests. I think you could probably achieve that with the current tests by splitting the write across 2 manifests and using a truncate(50) partition transform or just duplicating the test and truncating id instead of x. You may have to adjust values in the files a bit, but it's pretty close.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org