Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@
import org.apache.iceberg.spark.SparkReadConf;
import org.apache.iceberg.spark.SparkSchemaUtil;
import org.apache.iceberg.spark.SparkUtil;
import org.apache.iceberg.spark.source.metrics.NumSplits;
import org.apache.iceberg.spark.source.metrics.TaskNumSplits;
import org.apache.iceberg.util.PropertyUtil;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.broadcast.Broadcast;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.connector.metric.CustomMetric;
import org.apache.spark.sql.connector.metric.CustomTaskMetric;
import org.apache.spark.sql.connector.read.Batch;
import org.apache.spark.sql.connector.read.InputPartition;
import org.apache.spark.sql.connector.read.PartitionReader;
Expand Down Expand Up @@ -162,6 +166,11 @@ public String description() {
return String.format("%s [filters=%s]", table, filters);
}

@Override
public CustomMetric[] supportedCustomMetrics() {
return new CustomMetric[] { new NumSplits() };
}

static class ReaderFactory implements PartitionReaderFactory {
private final int batchSize;

Expand Down Expand Up @@ -194,14 +203,32 @@ public boolean supportColumnarReads(InputPartition partition) {
}

private static class RowReader extends RowDataReader implements PartitionReader<InternalRow> {
private long numSplits;

RowReader(ReadTask task) {
super(task.task, task.table(), task.expectedSchema(), task.isCaseSensitive());
numSplits = task.task.files().size();
LOG.debug("Reading {} file split(s) for table {} using RowReader", numSplits, task.table().name());
}

@Override
public CustomTaskMetric[] currentMetricsValues() {
return new CustomTaskMetric[] { new TaskNumSplits(numSplits) };
}
}

private static class BatchReader extends BatchDataReader implements PartitionReader<ColumnarBatch> {
private long numSplits;

BatchReader(ReadTask task, int batchSize) {
super(task.task, task.table(), task.expectedSchema(), task.isCaseSensitive(), batchSize);
numSplits = task.task.files().size();
LOG.debug("Reading {} file split(s) for table {} using BatchReader", numSplits, task.table().name());
}

@Override
public CustomTaskMetric[] currentMetricsValues() {
return new CustomTaskMetric[] { new TaskNumSplits(numSplits) };
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 java.text.NumberFormat;
import org.apache.spark.sql.connector.metric.CustomMetric;

public class NumSplits implements CustomMetric {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CustomSumMetric fits this a bit better and you will not need to reimplement the Aggregate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explained in #4588 why there is no benefit to extend CustomSumMetric.


@Override
public String name() {
return "numSplits";
}

@Override
public String description() {
return "number of file splits read";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start with a Capital leter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explained in #4588 that I lowercase "number" to be consistent with the standard metrics shown in the Spark UI.

}

@Override
public String aggregateTaskMetrics(long[] taskMetrics) {
long sum = initialValue;
for (int i = 0; i < taskMetrics.length; i++) {
sum += taskMetrics[i];
}
return NumberFormat.getIntegerInstance().format(sum);
}
}
Original file line number Diff line number Diff line change
@@ -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 TaskNumSplits implements CustomTaskMetric {
private final long value;

public TaskNumSplits(long value) {
this.value = value;
}

@Override
public String name() {
return "numSplits";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we say numFileSplits? I'm always worried about how overloaded the term "split" is especially in Spark. I believe you used this in the debug statement above.

Like here https://github.com/apache/spark/blob/master/core/src/main/scala/org/apache/spark/rdd/RDD.scala#L71 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is internal. The name in the CustomTaskMetric (in this case TaskNumSplits) needs to match the name in the CustomMetric (in this case NumSplits).
The user facing part is the description in the CustomMetric, in this case "number of file splits read". This appears in the Spark UI.
The debug logging does not use the name. (It uses the value of a numSplits variable.) It would log something like
"Reading 1835 file split(s) for table spark_catalog.tpcds_iceberg.catalog_sales using RowReader".

I had previously changed the numFiles/NumFiles in names to numSplits/NumSplits at your suggestion. Do you really think it is necessary to change all those names again?

}

@Override
public long value() {
return value;
}
}