manuzhang commented on code in PR #15104: URL: https://github.com/apache/iceberg/pull/15104#discussion_r2804411514
########## spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkWriteMetrics.java: ########## @@ -0,0 +1,203 @@ +/* + * 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.spark.source; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Map; +import org.apache.iceberg.ParameterizedTestExtension; +import org.apache.iceberg.spark.TestBaseWithCatalog; +import org.apache.spark.sql.Dataset; +import org.apache.spark.sql.Row; +import org.apache.spark.sql.catalyst.analysis.NoSuchTableException; +import org.apache.spark.sql.execution.SparkPlan; +import org.apache.spark.sql.execution.metric.SQLMetric; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; +import scala.collection.JavaConverters; + +@ExtendWith(ParameterizedTestExtension.class) +public class TestSparkWriteMetrics extends TestBaseWithCatalog { + + @AfterEach + public void removeTables() { + sql("DROP TABLE IF EXISTS %s", tableName); + } + + @TestTemplate + public void testWriteMetrics() throws NoSuchTableException { + sql("CREATE TABLE %s (id BIGINT) USING iceberg", tableName); + + String insertSql = String.format("INSERT INTO %s SELECT id FROM range(1000)", tableName); + Dataset<Row> result = spark.sql(insertSql); + result.collect(); + + SparkPlan plan = result.queryExecution().executedPlan(); + Map<String, SQLMetric> metricsMap = + JavaConverters.mapAsJavaMapConverter(plan.metrics()).asJava(); + + // If we are at the root, check if we have the metrics. + // Sometimes the plan structure is complex (e.g. AdaptiveSparkPlanExec). + // We might want to find the specific write node. + + if (!metricsMap.containsKey("addedDataFiles")) { + // Attempt to find a node with these metrics + metricsMap = findMetrics(plan, "addedDataFiles"); + } + + assertThat(metricsMap).isNotNull(); + + assertThat(metricsMap) + .hasEntrySatisfying("addedDataFiles", metric -> assertThat(metric.value()).isEqualTo(2)); + + assertThat(metricsMap) + .hasEntrySatisfying("addedRecords", metric -> assertThat(metric.value()).isEqualTo(1000)); + + assertThat(metricsMap) + .hasEntrySatisfying( + "addedFileSizeInBytes", metric -> assertThat(metric.value()).isGreaterThan(0)); + + assertThat(metricsMap) + .hasEntrySatisfying("totalDataFiles", metric -> assertThat(metric.value()).isEqualTo(2)); + + assertThat(metricsMap) + .hasEntrySatisfying("totalRecords", metric -> assertThat(metric.value()).isEqualTo(1000)); + + assertThat(metricsMap) + .hasEntrySatisfying( + "totalFileSizeInBytes", metric -> assertThat(metric.value()).isGreaterThan(0)); + + // Verify other metrics are 0 + String[] zeroMetrics = { + "addedDeleteFiles", + "addedEqualityDeleteFiles", + "addedPositionalDeleteFiles", + "addedEqualityDeletes", + "addedPositionalDeletes", + "removedDataFiles", + "removedDeleteFiles", + "removedEqualityDeleteFiles", + "removedPositionalDeleteFiles", + "removedEqualityDeletes", + "removedPositionalDeletes", + "removedRecords", + "removedFileSizeInBytes", + "totalDeleteFiles", + "totalEqualityDeletes", + "totalPositionalDeletes" + }; + + for (String metric : zeroMetrics) { + assertThat(metricsMap) + .hasEntrySatisfying(metric, m -> assertThat(m.value()).as(metric).isEqualTo(0)); + } + } + + @TestTemplate + public void testDeleteMetrics() throws NoSuchTableException { + sql( + "CREATE TABLE %s (id BIGINT) USING iceberg TBLPROPERTIES ('format-version'='2', 'write.delete.mode'='merge-on-read')", Review Comment: The current metrics are intended for v2 tables. -- 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]
