Skip to content
Open
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 @@ -20,6 +20,7 @@ import org.apache.gluten.config.GlutenIcebergConfig

import org.apache.spark.SparkConf
import org.apache.spark.sql.Row
import org.apache.spark.sql.catalyst.expressions.{DynamicPruningExpression, Literal}
import org.apache.spark.sql.execution.QueryExecution
import org.apache.spark.sql.execution.datasources.v2.BatchScanExec
import org.apache.spark.sql.util.QueryExecutionListener
Expand Down Expand Up @@ -289,6 +290,51 @@ abstract class IcebergSuite extends WholeStageTransformerSuite {
}
}

test("iceberg bucketed join with a runtime filter that removes every partition") {
withTable("spj_items", "spj_purchases") {
spark.sql("create table spj_items(id int, price double) using iceberg partitioned by (id)")
spark.sql("insert into spj_items values (1, 40.0), (1, 41.0), (2, 10.0), (3, 15.5)")
spark.sql(
"create table spj_purchases(item_id int, price double) using iceberg " +
"partitioned by (item_id)")
spark.sql("insert into spj_purchases values (1, 42.0), (1, 44.0), (2, 11.0), (3, 19.5)")
for {
pushDownValues <- Seq("true", "false")
partiallyClustered <- Seq("true", "false")
} {
withSQLConf(
"spark.sql.sources.v2.bucketing.enabled" -> "true",
"spark.sql.requireAllClusterKeysForCoPartition" -> "false",
"spark.sql.adaptive.enabled" -> "false",
"spark.sql.iceberg.planning.preserve-data-grouping" -> "true",
"spark.sql.autoBroadcastJoinThreshold" -> "-1",
"spark.sql.optimizer.dynamicPartitionPruning.enabled" -> "true",
"spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly" -> "false",
"spark.sql.optimizer.dynamicPartitionPruning.fallbackFilterRatio" -> "10",
"spark.sql.sources.v2.bucketing.pushPartValues.enabled" -> pushDownValues,
"spark.sql.sources.v2.bucketing.partiallyClusteredDistribution.enabled" ->
partiallyClustered
) {
// No item is priced over 50, so dynamic filtering removes every purchases partition.
val df = spark.sql(
"select p.price from spj_items i, spj_purchases p " +
"where i.id = p.item_id and i.price > 50.0")
assert(df.collect().isEmpty)
// The purchases scan is offloaded, key grouped and dynamically filtered.
assert(getExecutedPlan(df).exists {
case s: IcebergScanTransformer =>
s.getKeyGroupPartitioning.isDefined &&
s.runtimeFilters.exists {
case DynamicPruningExpression(e) => e != Literal.TrueLiteral
case _ => false
}
case _ => false
})
}
}
}
}

test("iceberg bucketed join with partition") {
val leftTable = "p_str_tb"
val rightTable = "p_int_tb"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class AbstractBatchScanExec(
@transient protected lazy val inputPartitionsShim: Seq[InputPartition] =
batch.planInputPartitions()

@transient private lazy val filteredPartitions: Seq[Seq[InputPartition]] = {
@transient protected lazy val filteredPartitions: Seq[Seq[InputPartition]] = {
val dataSourceFilters = runtimeFilters.flatMap {
case DynamicPruningExpression(e) => DataSourceV2Strategy.translateRuntimeFilterV2(e)
case _ => None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
*/
package org.apache.spark.sql.execution.datasources.v2

import org.apache.spark.SparkException
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.physical.KeyGroupedPartitioning
import org.apache.spark.sql.catalyst.util.InternalRowComparableWrapper
import org.apache.spark.sql.connector.catalog.Table
import org.apache.spark.sql.connector.expressions.aggregate.Aggregation
import org.apache.spark.sql.connector.read.{HasPartitionKey, InputPartition, Scan, SupportsRuntimeV2Filtering}
import org.apache.spark.sql.connector.read.Scan
import org.apache.spark.sql.execution.datasources.v2.orc.OrcScan
import org.apache.spark.sql.execution.datasources.v2.parquet.ParquetScan
import org.apache.spark.sql.execution.metric.SQLMetric
Expand Down Expand Up @@ -77,69 +74,6 @@ abstract class BatchScanExecShim(
throw new UnsupportedOperationException("Need to implement this method")
}

@transient protected lazy val filteredPartitions: Seq[Seq[InputPartition]] = {
val dataSourceFilters = runtimeFilters.flatMap {
case DynamicPruningExpression(e) => DataSourceV2Strategy.translateRuntimeFilterV2(e)
case _ => None
}

if (dataSourceFilters.nonEmpty) {
val originalPartitioning = outputPartitioning

// the cast is safe as runtime filters are only assigned if the scan can be filtered
val filterableScan = scan.asInstanceOf[SupportsRuntimeV2Filtering]
filterableScan.filter(dataSourceFilters.toArray)

// call toBatch again to get filtered partitions
val newPartitions = scan.toBatch.planInputPartitions()

originalPartitioning match {
case p: KeyGroupedPartitioning =>
if (newPartitions.exists(!_.isInstanceOf[HasPartitionKey])) {
throw new SparkException(
"Data source must have preserved the original partitioning " +
"during runtime filtering: not all partitions implement HasPartitionKey after " +
"filtering")
}
val newPartitionValues = newPartitions
.map(
partition =>
InternalRowComparableWrapper(
partition.asInstanceOf[HasPartitionKey],
p.expressions))
.toSet
val oldPartitionValues = p.partitionValues
.map(partition => InternalRowComparableWrapper(partition, p.expressions))
.toSet
// We require the new number of partition values to be equal or less than the old number
// of partition values here. In the case of less than, empty partitions will be added for
// those missing values that are not present in the new input partitions.
if (oldPartitionValues.size < newPartitionValues.size) {
throw new SparkException(
"During runtime filtering, data source must either report " +
"the same number of partition values, or a subset of partition values from the " +
s"original. Before: ${oldPartitionValues.size} partition values. " +
s"After: ${newPartitionValues.size} partition values")
}

if (!newPartitionValues.forall(oldPartitionValues.contains)) {
throw new SparkException(
"During runtime filtering, data source must not report new " +
"partition values that are not present in the original partitioning.")
}

groupPartitions(newPartitions).get.map(_._2)

case _ =>
// no validation is needed as the data source did not report any specific partitioning
newPartitions.map(Seq(_))
}

} else {
partitions
}
}

@transient lazy val pushedAggregate: Option[Aggregation] = {
scan match {
case s: ParquetScan => s.pushedAggregate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ abstract class AbstractBatchScanExec(
@transient protected lazy val inputPartitionsShim: Seq[InputPartition] =
batch.planInputPartitions()

@transient private lazy val filteredPartitions: Seq[Seq[InputPartition]] = {
@transient protected lazy val filteredPartitions: Seq[Seq[InputPartition]] = {
val dataSourceFilters = runtimeFilters.flatMap {
case DynamicPruningExpression(e) => DataSourceV2Strategy.translateRuntimeFilterV2(e)
case _ => None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
*/
package org.apache.spark.sql.execution.datasources.v2

import org.apache.spark.SparkException
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.physical.KeyGroupedPartitioning
import org.apache.spark.sql.catalyst.util.InternalRowComparableWrapper
import org.apache.spark.sql.connector.catalog.Table
import org.apache.spark.sql.connector.expressions.aggregate.Aggregation
import org.apache.spark.sql.connector.read.{HasPartitionKey, InputPartition, Scan, SupportsRuntimeV2Filtering}
import org.apache.spark.sql.connector.read.Scan
import org.apache.spark.sql.execution.datasources.v2.orc.OrcScan
import org.apache.spark.sql.execution.datasources.v2.parquet.ParquetScan
import org.apache.spark.sql.execution.metric.SQLMetric
Expand Down Expand Up @@ -79,69 +76,6 @@ abstract class BatchScanExecShim(
throw new UnsupportedOperationException("Need to implement this method")
}

@transient protected lazy val filteredPartitions: Seq[Seq[InputPartition]] = {
val dataSourceFilters = runtimeFilters.flatMap {
case DynamicPruningExpression(e) => DataSourceV2Strategy.translateRuntimeFilterV2(e)
case _ => None
}

if (dataSourceFilters.nonEmpty) {
val originalPartitioning = outputPartitioning

// the cast is safe as runtime filters are only assigned if the scan can be filtered
val filterableScan = scan.asInstanceOf[SupportsRuntimeV2Filtering]
filterableScan.filter(dataSourceFilters.toArray)

// call toBatch again to get filtered partitions
val newPartitions = scan.toBatch.planInputPartitions()

originalPartitioning match {
case p: KeyGroupedPartitioning =>
if (newPartitions.exists(!_.isInstanceOf[HasPartitionKey])) {
throw new SparkException(
"Data source must have preserved the original partitioning " +
"during runtime filtering: not all partitions implement HasPartitionKey after " +
"filtering")
}
val newPartitionValues = newPartitions
.map(
partition =>
InternalRowComparableWrapper(
partition.asInstanceOf[HasPartitionKey],
p.expressions))
.toSet
val oldPartitionValues = p.partitionValues
.map(partition => InternalRowComparableWrapper(partition, p.expressions))
.toSet
// We require the new number of partition values to be equal or less than the old number
// of partition values here. In the case of less than, empty partitions will be added for
// those missing values that are not present in the new input partitions.
if (oldPartitionValues.size < newPartitionValues.size) {
throw new SparkException(
"During runtime filtering, data source must either report " +
"the same number of partition values, or a subset of partition values from the " +
s"original. Before: ${oldPartitionValues.size} partition values. " +
s"After: ${newPartitionValues.size} partition values")
}

if (!newPartitionValues.forall(oldPartitionValues.contains)) {
throw new SparkException(
"During runtime filtering, data source must not report new " +
"partition values that are not present in the original partitioning.")
}

groupPartitions(newPartitions).get.map(_._2)

case _ =>
// no validation is needed as the data source did not report any specific partitioning
newPartitions.map(Seq(_))
}

} else {
partitions
}
}

@transient lazy val pushedAggregate: Option[Aggregation] = {
scan match {
case s: ParquetScan => s.pushedAggregate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ abstract class AbstractBatchScanExec(
@transient protected lazy val inputPartitionsShim: Seq[InputPartition] =
batch.planInputPartitions()

@transient private lazy val filteredPartitions: Seq[Seq[InputPartition]] = {
@transient protected lazy val filteredPartitions: Seq[Seq[InputPartition]] = {
val dataSourceFilters = runtimeFilters.flatMap {
case DynamicPruningExpression(e) => DataSourceV2Strategy.translateRuntimeFilterV2(e)
case _ => None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@
*/
package org.apache.spark.sql.execution.datasources.v2

import org.apache.spark.SparkException
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.physical.KeyGroupedPartitioning
import org.apache.spark.sql.catalyst.util.InternalRowComparableWrapper
import org.apache.spark.sql.connector.catalog.Table
import org.apache.spark.sql.connector.catalog.functions.Reducer
import org.apache.spark.sql.connector.expressions.aggregate.Aggregation
import org.apache.spark.sql.connector.read.{HasPartitionKey, InputPartition, Scan, SupportsRuntimeV2Filtering}
import org.apache.spark.sql.connector.read.Scan
import org.apache.spark.sql.execution.datasources.v2.orc.OrcScan
import org.apache.spark.sql.execution.datasources.v2.parquet.ParquetScan
import org.apache.spark.sql.execution.metric.SQLMetric
import org.apache.spark.sql.vectorized.ColumnarBatch
import org.apache.spark.util.ArrayImplicits._

abstract class BatchScanExecShim(
output: Seq[AttributeReference],
Expand Down Expand Up @@ -85,71 +81,6 @@ abstract class BatchScanExecShim(
throw new UnsupportedOperationException("Need to implement this method")
}

@transient protected lazy val filteredPartitions: Seq[Seq[InputPartition]] = {
val dataSourceFilters = runtimeFilters.flatMap {
case DynamicPruningExpression(e) => DataSourceV2Strategy.translateRuntimeFilterV2(e)
case _ => None
}

if (dataSourceFilters.nonEmpty) {
val originalPartitioning = outputPartitioning

// the cast is safe as runtime filters are only assigned if the scan can be filtered
val filterableScan = scan.asInstanceOf[SupportsRuntimeV2Filtering]
filterableScan.filter(dataSourceFilters.toArray)

// call toBatch again to get filtered partitions
val newPartitions = scan.toBatch.planInputPartitions()

originalPartitioning match {
case p: KeyGroupedPartitioning =>
if (newPartitions.exists(!_.isInstanceOf[HasPartitionKey])) {
throw new SparkException(
"Data source must have preserved the original partitioning " +
"during runtime filtering: not all partitions implement HasPartitionKey after " +
"filtering")
}
val newPartitionValues = newPartitions
.map(
partition =>
InternalRowComparableWrapper(
partition.asInstanceOf[HasPartitionKey],
p.expressions))
.toSet
val oldPartitionValues = p.partitionValues
.map(partition => InternalRowComparableWrapper(partition, p.expressions))
.toSet
// We require the new number of partition values to be equal or less than the old number
// of partition values here. In the case of less than, empty partitions will be added for
// those missing values that are not present in the new input partitions.
if (oldPartitionValues.size < newPartitionValues.size) {
throw new SparkException(
"During runtime filtering, data source must either report " +
"the same number of partition values, or a subset of partition values from the " +
s"original. Before: ${oldPartitionValues.size} partition values. " +
s"After: ${newPartitionValues.size} partition values")
}

if (!newPartitionValues.forall(oldPartitionValues.contains)) {
throw new SparkException(
"During runtime filtering, data source must not report new " +
"partition values that are not present in the original partitioning.")
}

groupPartitions(newPartitions.toImmutableArraySeq)
.map(_.groupedParts.map(_.parts))
.getOrElse(Seq.empty)

case _ =>
// no validation is needed as the data source did not report any specific partitioning
newPartitions.map(Seq(_))
}

} else {
partitions
}
}

@transient lazy val pushedAggregate: Option[Aggregation] = {
scan match {
case s: ParquetScan => s.pushedAggregate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ abstract class AbstractBatchScanExec(
@transient protected lazy val inputPartitionsShim: Seq[InputPartition] =
batch.planInputPartitions()

@transient private lazy val filteredPartitions: Seq[Seq[InputPartition]] = {
@transient protected lazy val filteredPartitions: Seq[Seq[InputPartition]] = {
val dataSourceFilters = runtimeFilters.flatMap {
case DynamicPruningExpression(e) => DataSourceV2Strategy.translateRuntimeFilterV2(e)
case _ => None
Expand Down
Loading