Skip to content

[SPARK-33482][SQL] Fix FileScan canonicalization #31820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 @@ -49,6 +49,15 @@ case class BatchScanExec(
}

override def doCanonicalize(): BatchScanExec = {
this.copy(output = output.map(QueryPlan.normalizeExpressions(_, output)))
val canonicalizedScan = scan match {
case s: FileScan =>
s.withFilters(
QueryPlan.normalizePredicates(s.partitionFilters, output),
QueryPlan.normalizePredicates(s.dataFilters, output))
Copy link
Contributor

Choose a reason for hiding this comment

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

This works, but is a bit hacky as it doesn't apply to all the Scan implementations.

I think we should add doc in the Scan interface to explain how the hashCode/equals should be implemented.

case _ => scan
}
this.copy(
output = output.map(QueryPlan.normalizeExpressions(_, output)),
scan = canonicalizedScan)
}
}
24 changes: 24 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import org.apache.spark.sql.execution.datasources.{LogicalRelation, SchemaColumn
import org.apache.spark.sql.execution.datasources.v2.BatchScanExec
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.exchange.ReusedExchangeExec
import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, CartesianProductExec, SortMergeJoinExec}
import org.apache.spark.sql.functions._
import org.apache.spark.sql.internal.SQLConf
Expand Down Expand Up @@ -4065,6 +4066,29 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
}
}
}

test("SPARK-33482: Fix FileScan canonicalization") {
withSQLConf(SQLConf.USE_V1_SOURCE_LIST.key -> "") {
withTempPath { path =>
spark.range(5).toDF().write.mode("overwrite").parquet(path.toString)
withTempView("t") {
spark.read.parquet(path.toString).createOrReplaceTempView("t")
val df = sql(
"""
|SELECT *
|FROM t AS t1
|JOIN t AS t2 ON t2.id = t1.id
|JOIN t AS t3 ON t3.id = t2.id
|""".stripMargin)
df.collect()
Copy link
Contributor

Choose a reason for hiding this comment

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

do we really need df.collect() here? shouldn't AdaptiveSparkPlanHelper.collect() below take care of going through query plan properly with AQE being enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we do need to run the query first and then check the plan as this is an AQE compatible query where ReusedExchangeExec nodes are inserted during execution.

val reusedExchanges = collect(df.queryExecution.executedPlan) {
case r: ReusedExchangeExec => r
}
assert(reusedExchanges.size == 1)
}
}
}
}
}

case class Foo(bar: Option[String])