This is an automated email from the ASF dual-hosted git repository.
blackmwk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git
The following commit(s) were added to refs/heads/main by this push:
new 90a1e71287 Spark: Report effective split target size as a driver
metric (#18709)
90a1e71287 is described below
commit 90a1e71287ac9b1e63b4d21689b90447159b8753
Author: Gera Shegalov <[email protected]>
AuthorDate: Sun Nov 15 19:33:34 2026 -0800
Spark: Report effective split target size as a driver metric (#18709)
* Spark: Report effective split target size in driver metrics
Expose the resolved grouping target to scan analysis tools, including for
staged rewrite scans.
Generated-by: Codex
Signed-off-by: Gera Shegalov <[email protected]>
* Spark: Make effective split target metric loadable by Spark UI
Spark's SQLAppStatusListener instantiates custom metrics reflectively by
class name and requires a public zero-argument constructor. The private
nested metric class could not be loaded, so the value was missing from
the SQL tab. Move it to a public top-level class in the metrics package,
label it "effective split target size (bytes)", and test that every
declared scan metric can be loaded the way Spark loads it.
Generated-by: Claude Code
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Gera Shegalov <[email protected]>
* Spark: Address review feedback on effective split target metric
Initialize the metric to the configured split size so it always has a
value, which lets staged scans rely on it without an explicit record
call. Fold the metric class-loading check into the V1 and V2 read
metric tests, inline the test helpers, drop the test prefix from the
new tests, and add a runtime filter test.
Generated-by: Claude Code
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Gera Shegalov <[email protected]>
* Spark: Record effective split target at task-group planning
Record the split target where task groups are planned instead of as a
side effect of adjustSplitSize, and make the field volatile because
planning and metric reporting can run on different threads. Compute
expected test values from TableScanUtil, check metric loadability with
plain reflection instead of Spark internals, make new test methods
package-private, and assert the second staged-scan split size.
Generated-by: Claude Code
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Gera Shegalov <[email protected]>
* Spark: Test metric loadability in its own test
Keep the check that Spark can load every declared scan metric in a
dedicated test, following the project's one-behavior-per-test
convention, so a failure points directly at the broken behavior.
Generated-by: Claude Code
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Gera Shegalov <[email protected]>
* Spark: Report effective split target only from task-group planning
Leave the metric at 0 until task groups are planned, so a scan without a
snapshot no longer reports the configured split size. Record the staged
scan's split size where it plans task groups, and keep a single return
in reportDriverMetrics.
Generated-by: Claude Code
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Gera Shegalov <[email protected]>
* Spark: Report 0 split target for scans without data
Record 0 whenever planning produces no task groups, including scans whose
files are all pruned and tables whose data was deleted. Verify the value
the SQL UI renders instead of checking metric classes by reflection.
Generated-by: Claude Code
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Gera Shegalov <[email protected]>
* Spark: Reset split target with tasks and test runtime-filter change
Reset the effective split target when runtime filtering resets the task
groups, so it always reflects the current planning. Make the runtime
filter test prune large unread files so the target changes from the
pre-filter value.
Generated-by: Claude Code
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Gera Shegalov <[email protected]>
* Spark: Report the largest effective split target instead of the sum
Make EffectiveSplitTargetSize a CustomMetric, like NumSplits, because the
driver reports one non-additive value per scan. Name the runtime filter
test's sizes and drop the redundant positive checks from the V1/V2 read
metrics tests.
Generated-by: Claude Code
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Gera Shegalov <[email protected]>
---------
Signed-off-by: Gera Shegalov <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../spark/source/SparkPartitioningAwareScan.java | 9 +-
.../org/apache/iceberg/spark/source/SparkScan.java | 65 +++---
.../iceberg/spark/source/SparkStagedScan.java | 1 +
.../source/metrics/EffectiveSplitTargetSize.java | 47 +++++
.../metrics/TaskEffectiveSplitTargetSize.java | 40 ++++
.../iceberg/spark/source/TestSparkReadMetrics.java | 219 +++++++++++++++++++++
.../iceberg/spark/source/TestSparkStagedScan.java | 27 +++
7 files changed, 380 insertions(+), 28 deletions(-)
diff --git
a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkPartitioningAwareScan.java
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkPartitioningAwareScan.java
index f4b1c406d6..7ab0fd0b3e 100644
---
a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkPartitioningAwareScan.java
+++
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkPartitioningAwareScan.java
@@ -233,11 +233,13 @@ abstract class SparkPartitioningAwareScan<T extends
PartitionScanTask> extends S
@Override
protected synchronized List<ScanTaskGroup<T>> taskGroups() {
if (taskGroups == null) {
+ long splitSize = adjustSplitSize(tasks(), scan.targetSplitSize());
+
if (groupingKeyType().fields().isEmpty()) {
CloseableIterable<ScanTaskGroup<T>> plannedTaskGroups =
TableScanUtil.planTaskGroups(
CloseableIterable.withNoopClose(tasks()),
- adjustSplitSize(tasks(), scan.targetSplitSize()),
+ splitSize,
scan.splitLookback(),
scan.splitOpenFileCost());
this.taskGroups = Lists.newArrayList(plannedTaskGroups);
@@ -251,7 +253,7 @@ abstract class SparkPartitioningAwareScan<T extends
PartitionScanTask> extends S
List<ScanTaskGroup<T>> plannedTaskGroups =
TableScanUtil.planTaskGroups(
tasks(),
- adjustSplitSize(tasks(), scan.targetSplitSize()),
+ splitSize,
scan.splitLookback(),
scan.splitOpenFileCost(),
groupingKeyType());
@@ -266,6 +268,8 @@ abstract class SparkPartitioningAwareScan<T extends
PartitionScanTask> extends S
this.taskGroups = plannedTaskGroups;
}
+
+ setEffectiveSplitTargetSize(taskGroups, splitSize);
}
return taskGroups;
@@ -276,6 +280,7 @@ abstract class SparkPartitioningAwareScan<T extends
PartitionScanTask> extends S
protected synchronized void resetTasks(List<T> filteredTasks) {
this.taskGroups = null;
this.tasks = filteredTasks;
+ resetEffectiveSplitTargetSize();
}
private StructLikeSet collectGroupingKeys(Iterable<ScanTaskGroup<T>>
taskGroupIterable) {
diff --git
a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkScan.java
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkScan.java
index c54e9c2dab..5bec031489 100644
---
a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkScan.java
+++
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkScan.java
@@ -42,6 +42,7 @@ import
org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.spark.Spark3Util;
import org.apache.iceberg.spark.SparkReadConf;
import org.apache.iceberg.spark.SparkSchemaUtil;
+import org.apache.iceberg.spark.source.metrics.EffectiveSplitTargetSize;
import org.apache.iceberg.spark.source.metrics.EqualityDeleteFiles;
import org.apache.iceberg.spark.source.metrics.IndexedDeleteFiles;
import org.apache.iceberg.spark.source.metrics.NumDeletes;
@@ -56,6 +57,7 @@ import
org.apache.iceberg.spark.source.metrics.SkippedDataFiles;
import org.apache.iceberg.spark.source.metrics.SkippedDataManifests;
import org.apache.iceberg.spark.source.metrics.SkippedDeleteFiles;
import org.apache.iceberg.spark.source.metrics.SkippedDeleteManifests;
+import org.apache.iceberg.spark.source.metrics.TaskEffectiveSplitTargetSize;
import org.apache.iceberg.spark.source.metrics.TaskEqualityDeleteFiles;
import org.apache.iceberg.spark.source.metrics.TaskIndexedDeleteFiles;
import org.apache.iceberg.spark.source.metrics.TaskPositionalDeleteFiles;
@@ -111,6 +113,8 @@ abstract class SparkScan implements Scan,
SupportsReportStatistics {
private final Schema projection;
private final List<Expression> filters;
private final Supplier<ScanReport> scanReportSupplier;
+ // split target used to plan task groups; 0 when no task groups are planned
+ private volatile long effectiveSplitTargetSize;
// lazy variables
private StructType readSchema;
@@ -301,38 +305,37 @@ abstract class SparkScan implements Scan,
SupportsReportStatistics {
@Override
public CustomTaskMetric[] reportDriverMetrics() {
ScanReport scanReport = scanReportSupplier != null ?
scanReportSupplier.get() : null;
-
- if (scanReport == null) {
- return new CustomTaskMetric[0];
- }
-
List<CustomTaskMetric> driverMetrics = Lists.newArrayList();
- // common
- driverMetrics.add(TaskTotalPlanningDuration.from(scanReport));
+ driverMetrics.add(new
TaskEffectiveSplitTargetSize(effectiveSplitTargetSize));
- // data manifests
- driverMetrics.add(TaskTotalDataManifests.from(scanReport));
- driverMetrics.add(TaskScannedDataManifests.from(scanReport));
- driverMetrics.add(TaskSkippedDataManifests.from(scanReport));
+ if (scanReport != null) {
+ // common
+ driverMetrics.add(TaskTotalPlanningDuration.from(scanReport));
- // data files
- driverMetrics.add(TaskResultDataFiles.from(scanReport));
- driverMetrics.add(TaskSkippedDataFiles.from(scanReport));
- driverMetrics.add(TaskTotalDataFileSize.from(scanReport));
+ // data manifests
+ driverMetrics.add(TaskTotalDataManifests.from(scanReport));
+ driverMetrics.add(TaskScannedDataManifests.from(scanReport));
+ driverMetrics.add(TaskSkippedDataManifests.from(scanReport));
+
+ // data files
+ driverMetrics.add(TaskResultDataFiles.from(scanReport));
+ driverMetrics.add(TaskSkippedDataFiles.from(scanReport));
+ driverMetrics.add(TaskTotalDataFileSize.from(scanReport));
- // delete manifests
- driverMetrics.add(TaskTotalDeleteManifests.from(scanReport));
- driverMetrics.add(TaskScannedDeleteManifests.from(scanReport));
- driverMetrics.add(TaskSkippedDeleteManifests.from(scanReport));
+ // delete manifests
+ driverMetrics.add(TaskTotalDeleteManifests.from(scanReport));
+ driverMetrics.add(TaskScannedDeleteManifests.from(scanReport));
+ driverMetrics.add(TaskSkippedDeleteManifests.from(scanReport));
- // delete files
- driverMetrics.add(TaskTotalDeleteFileSize.from(scanReport));
- driverMetrics.add(TaskResultDeleteFiles.from(scanReport));
- driverMetrics.add(TaskEqualityDeleteFiles.from(scanReport));
- driverMetrics.add(TaskIndexedDeleteFiles.from(scanReport));
- driverMetrics.add(TaskPositionalDeleteFiles.from(scanReport));
- driverMetrics.add(TaskSkippedDeleteFiles.from(scanReport));
+ // delete files
+ driverMetrics.add(TaskTotalDeleteFileSize.from(scanReport));
+ driverMetrics.add(TaskResultDeleteFiles.from(scanReport));
+ driverMetrics.add(TaskEqualityDeleteFiles.from(scanReport));
+ driverMetrics.add(TaskIndexedDeleteFiles.from(scanReport));
+ driverMetrics.add(TaskPositionalDeleteFiles.from(scanReport));
+ driverMetrics.add(TaskSkippedDeleteFiles.from(scanReport));
+ }
return driverMetrics.toArray(new CustomTaskMetric[0]);
}
@@ -345,6 +348,7 @@ abstract class SparkScan implements Scan,
SupportsReportStatistics {
new NumDeletes(),
// common
+ new EffectiveSplitTargetSize(),
new TotalPlanningDuration(),
new ScanDuration(),
@@ -373,6 +377,15 @@ abstract class SparkScan implements Scan,
SupportsReportStatistics {
};
}
+ protected void setEffectiveSplitTargetSize(
+ List<? extends ScanTaskGroup<?>> plannedTaskGroups, long splitSize) {
+ this.effectiveSplitTargetSize = plannedTaskGroups.isEmpty() ? 0L :
splitSize;
+ }
+
+ protected void resetEffectiveSplitTargetSize() {
+ this.effectiveSplitTargetSize = 0L;
+ }
+
protected long adjustSplitSize(List<? extends ScanTask> tasks, long
splitSize) {
if (readConf.splitSizeOption() == null &&
readConf.adaptiveSplitSizeEnabled()) {
long scanSize = tasks.stream().mapToLong(ScanTask::sizeBytes).sum();
diff --git
a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkStagedScan.java
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkStagedScan.java
index 47481ec51c..8ce01eb969 100644
---
a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkStagedScan.java
+++
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkStagedScan.java
@@ -66,6 +66,7 @@ class SparkStagedScan extends SparkScan {
taskSetId);
this.taskGroups = TableScanUtil.planTaskGroups(tasks, splitSize,
splitLookback, openFileCost);
+ setEffectiveSplitTargetSize(taskGroups, splitSize);
}
return taskGroups;
}
diff --git
a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/metrics/EffectiveSplitTargetSize.java
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/metrics/EffectiveSplitTargetSize.java
new file mode 100644
index 0000000000..05f26de671
--- /dev/null
+++
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/metrics/EffectiveSplitTargetSize.java
@@ -0,0 +1,47 @@
+/*
+ * 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.metrics;
+
+import org.apache.spark.sql.connector.metric.CustomMetric;
+
+public class EffectiveSplitTargetSize implements CustomMetric {
+
+ public static final String NAME = "effectiveSplitTargetSize";
+
+ @Override
+ public String name() {
+ return NAME;
+ }
+
+ @Override
+ public String description() {
+ return "effective split target size (bytes)";
+ }
+
+ @Override
+ public String aggregateTaskMetrics(long[] taskMetrics) {
+ // the driver reports one value per scan, which is not additive
+ long max = 0L;
+ for (long taskMetric : taskMetrics) {
+ max = Math.max(max, taskMetric);
+ }
+
+ return String.valueOf(max);
+ }
+}
diff --git
a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/metrics/TaskEffectiveSplitTargetSize.java
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/metrics/TaskEffectiveSplitTargetSize.java
new file mode 100644
index 0000000000..a19a3f4338
--- /dev/null
+++
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/metrics/TaskEffectiveSplitTargetSize.java
@@ -0,0 +1,40 @@
+/*
+ * 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.metrics;
+
+import org.apache.spark.sql.connector.metric.CustomTaskMetric;
+
+public class TaskEffectiveSplitTargetSize implements CustomTaskMetric {
+
+ private final long value;
+
+ public TaskEffectiveSplitTargetSize(long value) {
+ this.value = value;
+ }
+
+ @Override
+ public String name() {
+ return EffectiveSplitTargetSize.NAME;
+ }
+
+ @Override
+ public long value() {
+ return value;
+ }
+}
diff --git
a/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkReadMetrics.java
b/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkReadMetrics.java
index 442c96ae6c..d3109365d8 100644
---
a/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkReadMetrics.java
+++
b/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkReadMetrics.java
@@ -19,20 +19,39 @@
package org.apache.iceberg.spark.source;
import static
org.apache.iceberg.spark.source.SparkSQLExecutionHelper.lastExecutedMetricValue;
+import static org.apache.spark.sql.functions.date_add;
+import static org.apache.spark.sql.functions.expr;
import static org.assertj.core.api.Assertions.assertThat;
import static scala.collection.JavaConverters.seqAsJavaListConverter;
+import java.io.IOException;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.iceberg.AppendFiles;
+import org.apache.iceberg.DataFiles;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
import org.apache.iceberg.ParameterizedTestExtension;
+import org.apache.iceberg.Table;
import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.spark.SparkReadOptions;
+import org.apache.iceberg.spark.SparkSQLProperties;
+import org.apache.iceberg.spark.SparkWriteOptions;
import org.apache.iceberg.spark.TestBaseWithCatalog;
+import org.apache.iceberg.spark.source.metrics.EffectiveSplitTargetSize;
import org.apache.iceberg.spark.source.metrics.ScanDuration;
+import org.apache.iceberg.util.TableScanUtil;
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.datasources.v2.BatchScanExec;
import org.apache.spark.sql.execution.metric.SQLMetric;
+import org.apache.spark.sql.internal.SQLConf;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -44,6 +63,205 @@ public class TestSparkReadMetrics extends
TestBaseWithCatalog {
@AfterEach
public void removeTables() {
sql("DROP TABLE IF EXISTS %s", tableName);
+ sql("DROP TABLE IF EXISTS dim");
+ }
+
+ @TestTemplate
+ void effectiveSplitTargetSize() throws NoSuchTableException, IOException {
+ long tableSplitSize = 128L * 1024 * 1024;
+ long optionSplitSize = 32L * 1024 * 1024;
+ int parallelism = 1000;
+ sql(
+ "CREATE TABLE %s (id BIGINT) USING iceberg TBLPROPERTIES
('read.split.target-size'='%s')",
+ tableName, tableSplitSize);
+ spark.range(100).coalesce(1).writeTo(tableName).append();
+
+ long scanSize = 0;
+ try (CloseableIterable<FileScanTask> tasks =
+ validationCatalog.loadTable(tableIdent).newScan().planFiles()) {
+ for (FileScanTask task : tasks) {
+ scanSize += task.sizeBytes();
+ }
+ }
+
+ long adaptiveSplitSize = TableScanUtil.adjustSplitSize(scanSize,
parallelism, tableSplitSize);
+ assertThat(adaptiveSplitSize).isNotEqualTo(tableSplitSize);
+
+ // adaptive sizing lowers the table target for a small scan
+ withSQLConf(
+ ImmutableMap.of(
+ SparkSQLProperties.READ_ADAPTIVE_SPLIT_SIZE_ENABLED,
+ "true",
+ SparkSQLProperties.READ_ADAPTIVE_SPLIT_SIZE_PARALLELISM,
+ String.valueOf(parallelism)),
+ () -> {
+ Dataset<Row> df = spark.sql(String.format("SELECT * FROM %s",
tableName));
+ df.collect();
+ SparkPlan scan =
df.queryExecution().executedPlan().collectLeaves().head();
+
assertThat(scan.metrics().get("effectiveSplitTargetSize").get().value())
+ .isEqualTo(adaptiveSplitSize);
+
+ // the value the UI renders, read back from the SQL status store
+ assertThat(lastExecutedMetricValue(spark, new
EffectiveSplitTargetSize().description()))
+ .isEqualTo(String.valueOf(adaptiveSplitSize));
+ });
+
+ // without adaptive sizing, the table property is the target
+ withSQLConf(
+ ImmutableMap.of(SparkSQLProperties.READ_ADAPTIVE_SPLIT_SIZE_ENABLED,
"false"),
+ () -> {
+ Dataset<Row> df = spark.sql(String.format("SELECT * FROM %s",
tableName));
+ df.collect();
+ SparkPlan scan =
df.queryExecution().executedPlan().collectLeaves().head();
+
assertThat(scan.metrics().get("effectiveSplitTargetSize").get().value())
+ .isEqualTo(tableSplitSize);
+ });
+
+ // an explicit read option is used as is, even with adaptive sizing enabled
+ withSQLConf(
+ ImmutableMap.of(SparkSQLProperties.READ_ADAPTIVE_SPLIT_SIZE_ENABLED,
"true"),
+ () -> {
+ Dataset<Row> df =
+ spark
+ .read()
+ .format("iceberg")
+ .option(SparkReadOptions.SPLIT_SIZE,
String.valueOf(optionSplitSize))
+ .load(tableName);
+ df.collect();
+ SparkPlan scan =
df.queryExecution().executedPlan().collectLeaves().head();
+
assertThat(scan.metrics().get("effectiveSplitTargetSize").get().value())
+ .isEqualTo(optionSplitSize);
+ });
+ }
+
+ @TestTemplate
+ void effectiveSplitTargetSizeWithoutData() throws NoSuchTableException {
+ sql("CREATE TABLE %s (id BIGINT) USING iceberg", tableName);
+
+ // no task groups are planned for a table without data
+ Dataset<Row> df = spark.sql(String.format("SELECT * FROM %s", tableName));
+ df.collect();
+ SparkPlan scan = df.queryExecution().executedPlan().collectLeaves().head();
+
assertThat(scan.metrics().get("effectiveSplitTargetSize").get().value()).isZero();
+
+ // or when a filter prunes every file
+ spark.range(100).coalesce(1).writeTo(tableName).append();
+ df = spark.sql(String.format("SELECT * FROM %s WHERE id > 100",
tableName));
+ df.collect();
+ scan = df.queryExecution().executedPlan().collectLeaves().head();
+
assertThat(scan.metrics().get("effectiveSplitTargetSize").get().value()).isZero();
+
+ // or after all data is deleted
+ sql("DELETE FROM %s", tableName);
+ df = spark.sql(String.format("SELECT * FROM %s", tableName));
+ df.collect();
+ scan = df.queryExecution().executedPlan().collectLeaves().head();
+
assertThat(scan.metrics().get("effectiveSplitTargetSize").get().value()).isZero();
+ }
+
+ @TestTemplate
+ void effectiveSplitTargetSizeWithRuntimeFilter() throws
NoSuchTableException, IOException {
+ long tableSplitSize = 128L * 1024 * 1024;
+ // pruned files whose total, spread over the parallelism, exceeds the 16
MB minimum target
+ int prunedFileCount = 4;
+ long prunedFileSize = 64L * 1024 * 1024;
+ int parallelism = prunedFileCount;
+ sql(
+ "CREATE TABLE %s (id BIGINT, date DATE) USING iceberg PARTITIONED BY
(date) "
+ + "TBLPROPERTIES ('read.split.target-size'='%s')",
+ tableName, tableSplitSize);
+ spark
+ .range(1, 100)
+ .withColumn("date", date_add(expr("DATE '1970-01-01'"), expr("CAST(id
% 4 AS INT)")))
+ .coalesce(1)
+ .writeTo(tableName)
+ .option(SparkWriteOptions.FANOUT_ENABLED, "true")
+ .append();
+
+ sql("CREATE TABLE dim (id BIGINT, date DATE) USING parquet");
+ spark
+ .range(1, 10)
+ .withColumn("date", expr("DATE '1970-01-02'"))
+ .coalesce(1)
+ .write()
+ .mode("append")
+ .insertInto("dim");
+
+ // large files in a partition the runtime filter prunes; they are never
read
+ Table table = validationCatalog.loadTable(tableIdent);
+ AppendFiles append = table.newAppend();
+ for (int i = 0; i < prunedFileCount; i++) {
+ append.appendFile(
+ DataFiles.builder(table.spec())
+ .withPath(table.location() + "/data/date=1970-01-01/unread-" + i
+ ".parquet")
+ .withFormat(FileFormat.PARQUET)
+ .withPartitionPath("date=1970-01-01")
+ .withFileSizeInBytes(prunedFileSize)
+ .withRecordCount(1)
+ .build());
+ }
+
+ append.commit();
+
+ long scanSize = 0;
+ try (CloseableIterable<FileScanTask> tasks = table.newScan().planFiles()) {
+ for (FileScanTask task : tasks) {
+ scanSize += task.sizeBytes();
+ }
+ }
+
+ long filteredScanSize = 0;
+ try (CloseableIterable<FileScanTask> tasks =
+ table.newScan().filter(Expressions.equal("date",
"1970-01-02")).planFiles()) {
+ for (FileScanTask task : tasks) {
+ filteredScanSize += task.sizeBytes();
+ }
+ }
+
+ long splitSizeBeforeFilter =
+ TableScanUtil.adjustSplitSize(scanSize, parallelism, tableSplitSize);
+ long expectedSplitSize =
+ TableScanUtil.adjustSplitSize(filteredScanSize, parallelism,
tableSplitSize);
+ assertThat(expectedSplitSize).isNotEqualTo(splitSizeBeforeFilter);
+
+ long expectedRows =
+ spark
+ .sql(String.format("SELECT * FROM %s WHERE date = DATE
'1970-01-02'", tableName))
+ .count();
+
+ // the metric reports the target of the task groups re-planned after the
runtime filter
+ withSQLConf(
+ ImmutableMap.of(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED().key(),
+ "false",
+ SparkSQLProperties.READ_ADAPTIVE_SPLIT_SIZE_ENABLED,
+ "true",
+ SparkSQLProperties.READ_ADAPTIVE_SPLIT_SIZE_PARALLELISM,
+ String.valueOf(parallelism)),
+ () -> {
+ Dataset<Row> df =
+ spark.sql(
+ String.format(
+ "SELECT f.* FROM %s f JOIN dim d ON f.date = d.date AND
d.id = 1",
+ tableName));
+ assertThat(df.collectAsList()).hasSize((int) expectedRows);
+
+ List<SparkPlan> scans =
+
seqAsJavaListConverter(df.queryExecution().executedPlan().collectLeaves())
+ .asJava()
+ .stream()
+ .filter(BatchScanExec.class::isInstance)
+ .collect(Collectors.toList());
+ assertThat(scans).hasSize(1);
+ assertThat(((BatchScanExec)
scans.get(0)).runtimeFilters().nonEmpty()).isTrue();
+
+ Map<String, SQLMetric> metricsMap =
+
JavaConverters.mapAsJavaMapConverter(scans.get(0).metrics()).asJava();
+ assertThat(metricsMap)
+ .hasEntrySatisfying(
+ "effectiveSplitTargetSize",
+ sqlMetric ->
assertThat(sqlMetric.value()).isEqualTo(expectedSplitSize));
+ });
}
@TestTemplate
@@ -62,6 +280,7 @@ public class TestSparkReadMetrics extends
TestBaseWithCatalog {
seqAsJavaListConverter(df.queryExecution().executedPlan().collectLeaves()).asJava();
Map<String, SQLMetric> metricsMap =
JavaConverters.mapAsJavaMapConverter(sparkPlans.get(0).metrics()).asJava();
+
// Common
assertThat(metricsMap)
.hasEntrySatisfying(
diff --git
a/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkStagedScan.java
b/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkStagedScan.java
index de07b7471f..3491489564 100644
---
a/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkStagedScan.java
+++
b/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkStagedScan.java
@@ -19,9 +19,11 @@
package org.apache.iceberg.spark.source;
import static org.assertj.core.api.Assertions.assertThat;
+import static scala.collection.JavaConverters.seqAsJavaListConverter;
import java.io.IOException;
import java.util.List;
+import java.util.Map;
import java.util.UUID;
import org.apache.iceberg.FileScanTask;
import org.apache.iceberg.ParameterizedTestExtension;
@@ -35,9 +37,12 @@ import org.apache.iceberg.spark.SparkTableCache;
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 TestSparkStagedScan extends CatalogTestBase {
@@ -109,6 +114,18 @@ public class TestSparkStagedScan extends CatalogTestBase {
.as("Num partitions should match")
.isEqualTo(2);
+ // the staged scan reports the split size it used to plan its task groups
+ scanDF.collect();
+ List<SparkPlan> sparkPlans =
+
seqAsJavaListConverter(scanDF.queryExecution().executedPlan().collectLeaves()).asJava();
+ Map<String, SQLMetric> metrics =
+
JavaConverters.mapAsJavaMapConverter(sparkPlans.get(0).metrics()).asJava();
+ assertThat(metrics)
+ .hasEntrySatisfying(
+ "effectiveSplitTargetSize",
+ metric ->
+
assertThat(metric.value()).isEqualTo(tasks.get(0).file().fileSizeInBytes()));
+
// load the staged file set and make sure we combine both files into a
single split
scanDF =
spark
@@ -119,6 +136,16 @@ public class TestSparkStagedScan extends CatalogTestBase {
assertThat(scanDF.javaRDD().getNumPartitions())
.as("Num partitions should match")
.isEqualTo(1);
+
+ // the staged scan reports the larger split size used for this read
+ scanDF.collect();
+ sparkPlans =
+
seqAsJavaListConverter(scanDF.queryExecution().executedPlan().collectLeaves()).asJava();
+ metrics =
JavaConverters.mapAsJavaMapConverter(sparkPlans.get(0).metrics()).asJava();
+ assertThat(metrics)
+ .hasEntrySatisfying(
+ "effectiveSplitTargetSize",
+ metric -> assertThat(metric.value()).isEqualTo(Long.MAX_VALUE));
}
}
}