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-20718][SQL][followup] Fix canonicalization for HiveTableScanExec #17962

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -423,7 +423,7 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] extends TreeNode[PlanT
lazy val allAttributes: AttributeSeq = children.flatMap(_.output)
}

object QueryPlan {
object QueryPlan extends PredicateHelper {
/**
* Normalize the exprIds in the given expression, by updating the exprId in `AttributeReference`
* with its referenced ordinal from input attributes. It's similar to `BindReferences` but we
Expand All @@ -442,4 +442,14 @@ object QueryPlan {
}
}.canonicalized.asInstanceOf[T]
}

/** Normalize and reorder the expressions in the given sequence. */
Copy link
Member

Choose a reason for hiding this comment

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

Where do we reorder the predicates?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It links predicates with AND and pass it to def normalizeExprId, in which Canonicalize.execute is called. That's where we do the reordering.

Copy link
Member

Choose a reason for hiding this comment

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

Got it. Could you update the above comment, since this is a little bit confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. I'll update the comments.

def normalizePredicates(predicates: Seq[Expression], output: AttributeSeq): Seq[Expression] = {
if (predicates.nonEmpty) {
val normalized = normalizeExprId(predicates.reduce(And), output)
splitConjunctivePredicates(normalized)
} else {
Nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import org.apache.spark.sql.sources.BaseRelation
import org.apache.spark.sql.types.StructType
import org.apache.spark.util.Utils

trait DataSourceScanExec extends LeafExecNode with CodegenSupport with PredicateHelper {
trait DataSourceScanExec extends LeafExecNode with CodegenSupport {
val relation: BaseRelation
val metastoreTableIdentifier: Option[TableIdentifier]

Expand Down Expand Up @@ -519,18 +519,8 @@ case class FileSourceScanExec(
relation,
output.map(QueryPlan.normalizeExprId(_, output)),
requiredSchema,
canonicalizeFilters(partitionFilters, output),
canonicalizeFilters(dataFilters, output),
QueryPlan.normalizePredicates(partitionFilters, output),
QueryPlan.normalizePredicates(dataFilters, output),
None)
}

private def canonicalizeFilters(filters: Seq[Expression], output: Seq[Attribute])
: Seq[Expression] = {
if (filters.nonEmpty) {
val normalizedFilters = QueryPlan.normalizeExprId(filters.reduce(And), output)
splitConjunctivePredicates(normalizedFilters)
} else {
Nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ case class HiveTableScanExec(
HiveTableScanExec(
requestedAttributes.map(QueryPlan.normalizeExprId(_, input)),
relation.canonicalized.asInstanceOf[CatalogRelation],
partitionPruningPred.map(QueryPlan.normalizeExprId(_, input)))(sparkSession)
QueryPlan.normalizePredicates(partitionPruningPred, input))(sparkSession)
}

override def otherCopyArgs: Seq[AnyRef] = Seq(sparkSession)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,30 @@ class HiveTableScanSuite extends HiveComparisonTest with SQLTestUtils with TestH
|PARTITION (p1='a',p2='c',p3='c',p4='d',p5='e')
|SELECT v.id
""".stripMargin)
val plan = sql(
s"""
|SELECT * FROM $table
""".stripMargin).queryExecution.sparkPlan
val scan = plan.collectFirst {
case p: HiveTableScanExec => p
}.get
val scan = getHiveTableScanExec(s"SELECT * FROM $table")
val numDataCols = scan.relation.dataCols.length
scan.rawPartitions.foreach(p => assert(p.getCols.size == numDataCols))
}
}
}

test("HiveTableScanExec canonicalization for different orders of partition filters") {
val table = "hive_tbl_part"
withTable(table) {
sql(
s"""
|CREATE TABLE $table (id int)
|PARTITIONED BY (a int, b int)
""".stripMargin)
val scan1 = getHiveTableScanExec(s"SELECT * FROM $table WHERE a = 1 AND b = 2")
val scan2 = getHiveTableScanExec(s"SELECT * FROM $table WHERE b = 2 AND a = 1")
assert(scan1.sameResult(scan2))
}
}

private def getHiveTableScanExec(query: String): HiveTableScanExec = {
sql(query).queryExecution.sparkPlan.collectFirst {
case p: HiveTableScanExec => p
}.get
}
}