Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix HiveTableScanExec canonicalization
  • Loading branch information
wangzhenhua committed May 12, 2017
commit a61e865b84e76575698679fc12e2a5a8b9c5b0fd
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 canonicalizeExprSeq(exprSeq: Seq[Expression], output: AttributeSeq): Seq[Expression] = {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: normalizePredicates

if (exprSeq.nonEmpty) {
val normalizedExprs = QueryPlan.normalizeExprId(exprSeq.reduce(And), output)
Copy link
Member

Choose a reason for hiding this comment

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

nit: QueryPlan.normalizeExprId -> normalizeExprId

splitConjunctivePredicates(normalizedExprs)
} 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.canonicalizeExprSeq(partitionFilters, output),
QueryPlan.canonicalizeExprSeq(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.canonicalizeExprSeq(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
}
}