Skip to content
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 @@ -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,17 @@ object QueryPlan {
}
}.canonicalized.asInstanceOf[T]
}

/**
* Composes the given predicates into a conjunctive predicate, which is normalized and reordered.
* Then returns a new sequence of predicates by splitting the conjunctive predicate.
*/
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
}
}