gerashegalov commented on code in PR #19022:
URL: https://github.com/apache/iceberg/pull/19022#discussion_r4774771915
##########
spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkReadMetrics.java:
##########
@@ -493,4 +567,125 @@ public void testDeleteMetrics() throws
NoSuchTableException {
.hasEntrySatisfying(
"skippedDeleteFiles", sqlMetric ->
assertThat(sqlMetric.value()).isEqualTo(0));
}
+
+ @Test
Review Comment:
Commit-structure nit: in b41c842ea this is still `@TestTemplate`, copied
from 4.1 before 4.1 was converted. That doesn't compile on 3.5 since #19230.
The change to `@Test` is in the scanDuration commit, so if that commit is
dropped while rebasing after #19088, the test compile breaks here. Moving the
`@Test` change into b41c842ea would let each commit build on its own. This
doesn't matter if the PR becomes the combined backport and is squash-merged.
##########
spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/source/InheritedTaskMetrics.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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 java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.spark.sql.connector.metric.CustomTaskMetric;
+
+/**
+ * Task metrics reported by the readers that ran earlier in the same Spark
task.
+ *
+ * <p>When a task reads several input partitions, Spark opens one reader per
partition and passes
+ * the previous reader's metrics to the next through {@code
initMetricsValues}. Spark keeps only the
+ * last reader's values, so each reader must add the inherited values to its
own.
Review Comment:
Spark 3.5 only started calling `initMetricsValues` in 3.5.9 (SPARK-55302).
On 3.5.0 through 3.5.8, this is never called, and each task still reports only
its last reader's metrics. #18775 notes the 4.0.3 floor in the same place.
Could we note it here too, and in the PR description?
```suggestion
* last reader's values, so each reader must add the inherited values to its
own.
*
* <p>Spark calls {@code initMetricsValues} only from 3.5.9 on
(SPARK-55302). On 3.5.0 to 3.5.8,
* each task still reports only its last reader's metrics.
```
##########
spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkReadMetrics.java:
##########
@@ -493,4 +567,125 @@ public void testDeleteMetrics() throws
NoSuchTableException {
.hasEntrySatisfying(
"skippedDeleteFiles", sqlMetric ->
assertThat(sqlMetric.value()).isEqualTo(0));
}
+
+ @Test
+ public void metricsWhenTaskReadsSeveralPartitions() {
+ // open file cost and split size are set as 16 MB to produce a split per
file
+ sql(
+ "CREATE TABLE %s (id BIGINT, dep STRING) USING iceberg PARTITIONED BY
(dep) "
+ + "TBLPROPERTIES ('format-version'='2',
'write.delete.mode'='merge-on-read', "
+ + "'%s'='16777216', '%s'='16777216')",
+ tableName, TableProperties.SPLIT_SIZE,
TableProperties.SPLIT_OPEN_FILE_COST);
+ sql("INSERT INTO %s VALUES (1, 'hr'), (11, 'hr'), (4, 'ops'), (14,
'ops')", tableName);
+ sql("INSERT INTO %s VALUES (2, 'hr'), (12, 'hr'), (5, 'ops'), (15,
'ops')", tableName);
+ sql("INSERT INTO %s VALUES (3, 'hr'), (13, 'hr')", tableName);
+ // one deleted row in each of the 5 data files
+ sql("DELETE FROM %s WHERE id > 10", tableName);
+
+ // Spark groups the splits with the same partition key into one task, so
the 3 hr and the
+ // 2 ops splits are read by one task each and every task opens several
readers
+ Map<String, String> spjConf =
+ ImmutableMap.of(
+ SQLConf.V2_BUCKETING_ENABLED().key(),
+ "true",
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED().key(),
+ "false",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD().key(),
+ "-1",
+ SparkSQLProperties.PRESERVE_DATA_GROUPING,
+ "true");
+
+ for (String vectorized : new String[] {"true", "false"}) {
+ sql(
+ "ALTER TABLE %s SET TBLPROPERTIES ('%s'='%s')",
+ tableName, TableProperties.PARQUET_VECTORIZATION_ENABLED,
vectorized);
+
+ withSQLConf(
+ spjConf,
+ () -> {
+ Dataset<Row> df =
+ spark.sql(
+ String.format(
+ "SELECT * FROM %s a JOIN %s b ON a.dep = b.dep",
tableName, tableName));
+ df.collect();
+
+ List<SparkPlan> scans =
+
seqAsJavaListConverter(df.queryExecution().executedPlan().collectLeaves()).asJava();
+ assertThat(scans).hasSize(2);
+ for (SparkPlan scan : scans) {
+ assertThat(scan.metrics().get("numSplits").get().value())
+ .as("numSplits with vectorization %s", vectorized)
+ .isEqualTo(5L);
+ assertThat(scan.metrics().get("numDeletes").get().value())
+ .as("numDeletes with vectorization %s", vectorized)
+ .isEqualTo(5L);
+ }
+ });
+ }
+ }
+
+ @Test
+ public void batchReaderScanDurationIncludesPreviousReader() throws
IOException {
Review Comment:
nit: new tests can be package-private, like `effectiveSplitTargetSize` above.
```suggestion
void batchReaderScanDurationIncludesPreviousReader() throws IOException {
```
##########
spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkReadMetrics.java:
##########
@@ -493,4 +567,125 @@ public void testDeleteMetrics() throws
NoSuchTableException {
.hasEntrySatisfying(
"skippedDeleteFiles", sqlMetric ->
assertThat(sqlMetric.value()).isEqualTo(0));
}
+
+ @Test
+ public void metricsWhenTaskReadsSeveralPartitions() {
+ // open file cost and split size are set as 16 MB to produce a split per
file
+ sql(
+ "CREATE TABLE %s (id BIGINT, dep STRING) USING iceberg PARTITIONED BY
(dep) "
+ + "TBLPROPERTIES ('format-version'='2',
'write.delete.mode'='merge-on-read', "
+ + "'%s'='16777216', '%s'='16777216')",
+ tableName, TableProperties.SPLIT_SIZE,
TableProperties.SPLIT_OPEN_FILE_COST);
+ sql("INSERT INTO %s VALUES (1, 'hr'), (11, 'hr'), (4, 'ops'), (14,
'ops')", tableName);
+ sql("INSERT INTO %s VALUES (2, 'hr'), (12, 'hr'), (5, 'ops'), (15,
'ops')", tableName);
+ sql("INSERT INTO %s VALUES (3, 'hr'), (13, 'hr')", tableName);
+ // one deleted row in each of the 5 data files
+ sql("DELETE FROM %s WHERE id > 10", tableName);
+
+ // Spark groups the splits with the same partition key into one task, so
the 3 hr and the
+ // 2 ops splits are read by one task each and every task opens several
readers
+ Map<String, String> spjConf =
+ ImmutableMap.of(
+ SQLConf.V2_BUCKETING_ENABLED().key(),
+ "true",
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED().key(),
+ "false",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD().key(),
+ "-1",
+ SparkSQLProperties.PRESERVE_DATA_GROUPING,
+ "true");
+
+ for (String vectorized : new String[] {"true", "false"}) {
+ sql(
+ "ALTER TABLE %s SET TBLPROPERTIES ('%s'='%s')",
+ tableName, TableProperties.PARQUET_VECTORIZATION_ENABLED,
vectorized);
+
+ withSQLConf(
+ spjConf,
+ () -> {
+ Dataset<Row> df =
+ spark.sql(
+ String.format(
+ "SELECT * FROM %s a JOIN %s b ON a.dep = b.dep",
tableName, tableName));
+ df.collect();
+
+ List<SparkPlan> scans =
+
seqAsJavaListConverter(df.queryExecution().executedPlan().collectLeaves()).asJava();
+ assertThat(scans).hasSize(2);
+ for (SparkPlan scan : scans) {
+ assertThat(scan.metrics().get("numSplits").get().value())
+ .as("numSplits with vectorization %s", vectorized)
+ .isEqualTo(5L);
+ assertThat(scan.metrics().get("numDeletes").get().value())
+ .as("numDeletes with vectorization %s", vectorized)
+ .isEqualTo(5L);
+ }
+ });
+ }
+ }
+
+ @Test
+ public void batchReaderScanDurationIncludesPreviousReader() throws
IOException {
+ Table table = createTableForReaderMetrics();
+ ScanTaskGroup<FileScanTask> taskGroup = taskGroup(table);
+ ParquetBatchReadConf conf =
ImmutableParquetBatchReadConf.builder().batchSize(7).build();
+ try (BatchDataReader reader =
+ new BatchDataReader(
+ table,
+ table.io(),
+ taskGroup,
+ table.schema(),
+ table.schema(),
+ false,
+ conf,
+ null,
+ false)) {
+ reader.initMetricsValues(inheritedMetrics());
+ while (reader.next()) {
+ // drain
+ }
+
+ assertScanDurationIncludesInherited(reader.currentMetricsValues());
+ }
+ }
+
+ @Test
+ public void rowReaderScanDurationIncludesPreviousReader() throws IOException
{
Review Comment:
Same here.
```suggestion
void rowReaderScanDurationIncludesPreviousReader() throws IOException {
```
--
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]