Skip to content
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

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

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -49,6 +49,13 @@ 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))
peter-toth marked this conversation as resolved.
Show resolved Hide resolved
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)
peter-toth marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Up @@ -86,7 +86,7 @@ trait FileScan extends Scan

override def equals(obj: Any): Boolean = obj match {
case f: FileScan =>
fileIndex == f.fileIndex && readSchema == f.readSchema
fileIndex == f.fileIndex && readSchema == f.readSchema &&
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 change is not related to this PR, but it looks like a && is missing from here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice catch. It seems that #27112 introduced this.
cc @dongjoon-hyun if we want to backport this to 3.1 and 3.0.

Copy link
Member

Choose a reason for hiding this comment

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

Oh... nice catch. cc: @gengliangwang , too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Credit goes to @bersprockets for catching this in SPARK-33482.

Copy link
Member

Choose a reason for hiding this comment

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

wow ..

ExpressionSet(partitionFilters) == ExpressionSet(f.partitionFilters) &&
ExpressionSet(dataFilters) == ExpressionSet(f.dataFilters)

Expand Down
28 changes: 28 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
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,33 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
}
}
}

test("SPARK-33482: Fix FileScan canonicalization") {
Seq(true, false).foreach { aqe =>
withSQLConf(SQLConf.USE_V1_SOURCE_LIST.key -> "",
SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> aqe.toString) {
Copy link
Member

Choose a reason for hiding this comment

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

We need to set this config for this test purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, reuse exchange code is very different in AQE/non-AQE paths, but I think you are right as we just need to test the canonicalization fix so I've removed this in c0fb9b2

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()
df.explain()
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 these two statements?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. I left explain() there accidentally, removed in: c0fb9b2

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

case class Foo(bar: Option[String])