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 @@ -24,6 +24,7 @@ import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.analysis.MultiInstanceRelation
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.QueryPlan
import org.apache.spark.sql.catalyst.plans.logical
import org.apache.spark.sql.catalyst.plans.logical.{HintInfo, Statistics}
import org.apache.spark.sql.execution.SparkPlan
Expand Down Expand Up @@ -68,6 +69,15 @@ case class InMemoryRelation(

override protected def innerChildren: Seq[SparkPlan] = Seq(child)

override def doCanonicalize(): logical.LogicalPlan =
copy(output = output.map(QueryPlan.normalizeExprId(_, child.output)),
storageLevel = StorageLevel.NONE,
Copy link
Contributor

Choose a reason for hiding this comment

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

can we follow the parameter order in the constructor?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is followed. I just ignored useCompression, batchSize as they are just primitives and don't need to be canonicalized here.

child = child.canonicalized,
tableName = None)(
_cachedColumnBuffers,
sizeInBytesStats,
statsOfPlanToCache)
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 copy these cached data?

Copy link
Member Author

Choose a reason for hiding this comment

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

_cachedColumnBuffers can't be null. If it is null, copy will trigger buildBuffers.

Copy link
Member

Choose a reason for hiding this comment

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

statsOfPlanToCache and sizeInBytesStats, too? For instance, ResolveHint drops hints in canonicalization:

override def doCanonicalize(): LogicalPlan = child.canonicalized

Copy link
Member Author

Choose a reason for hiding this comment

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

cachedColumnBuffers, sizeInBytesStats, statsOfPlanToCache won't be considered when comparing two InMemoryRelation. So instead of create empty instances of statistics, I just use the original values.

Copy link
Member

Choose a reason for hiding this comment

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

aha, ok. Thanks!


override def producedAttributes: AttributeSet = outputSet

@transient val partitionStatistics = new PartitionStatistics(output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.QueryPlan
import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, Partitioning}
import org.apache.spark.sql.execution.{ColumnarBatchScan, LeafExecNode, WholeStageCodegenExec}
import org.apache.spark.sql.execution.{ColumnarBatchScan, LeafExecNode, SparkPlan, WholeStageCodegenExec}
import org.apache.spark.sql.execution.vectorized._
import org.apache.spark.sql.types._
import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector}
Expand All @@ -38,6 +38,11 @@ case class InMemoryTableScanExec(

override protected def innerChildren: Seq[QueryPlan[_]] = Seq(relation) ++ super.innerChildren

override def doCanonicalize(): SparkPlan =
copy(attributes = attributes.map(QueryPlan.normalizeExprId(_, relation.output)),
predicates = predicates.map(QueryPlan.normalizeExprId(_, relation.output)),
relation = relation.canonicalized.asInstanceOf[InMemoryRelation])

override def vectorTypes: Option[Seq[String]] =
Option(Seq.fill(attributes.length)(
if (!conf.offHeapColumnVectorEnabled) {
Expand Down Expand Up @@ -169,11 +174,13 @@ case class InMemoryTableScanExec(
override def outputOrdering: Seq[SortOrder] =
relation.child.outputOrdering.map(updateAttribute(_).asInstanceOf[SortOrder])

private def statsFor(a: Attribute) = relation.partitionStatistics.forAttribute(a)
// Keeps relation's partition statistics because we don't serialize relation.
private val stats = relation.partitionStatistics
private def statsFor(a: Attribute) = stats.forAttribute(a)

// Returned filter predicate should return false iff it is impossible for the input expression
// to evaluate to `true' based on statistics collected about this partition batch.
@transient val buildFilter: PartialFunction[Expression, Expression] = {
@transient lazy val buildFilter: PartialFunction[Expression, Expression] = {
case And(lhs: Expression, rhs: Expression)
if buildFilter.isDefinedAt(lhs) || buildFilter.isDefinedAt(rhs) =>
(buildFilter.lift(lhs) ++ buildFilter.lift(rhs)).reduce(_ && _)
Expand Down Expand Up @@ -213,14 +220,14 @@ case class InMemoryTableScanExec(
l.asInstanceOf[Literal] <= statsFor(a).upperBound).reduce(_ || _)
}

val partitionFilters: Seq[Expression] = {
lazy val partitionFilters: Seq[Expression] = {
predicates.flatMap { p =>
val filter = buildFilter.lift(p)
val boundFilter =
filter.map(
BindReferences.bindReference(
_,
relation.partitionStatistics.schema,
stats.schema,
allowFailures = true))

boundFilter.foreach(_ =>
Expand All @@ -243,7 +250,7 @@ case class InMemoryTableScanExec(
private def filteredCachedBatches(): RDD[CachedBatch] = {
// Using these variables here to avoid serialization of entire objects (if referenced directly)
// within the map Partitions closure.
val schema = relation.partitionStatistics.schema
val schema = stats.schema
val schemaIndex = schema.zipWithIndex
val buffers = relation.cachedColumnBuffers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1446,8 +1446,17 @@ class DatasetSuite extends QueryTest with SharedSQLContext {
val data = Seq(("a", null))
checkDataset(data.toDS(), data: _*)
}

test("SPARK-23614: Union produces incorrect results when caching is used") {
val cached = spark.createDataset(Seq(TestDataUnion(1, 2, 3), TestDataUnion(4, 5, 6))).cache()
val group1 = cached.groupBy("x").agg(min(col("y")) as "value")
val group2 = cached.groupBy("x").agg(min(col("z")) as "value")
checkAnswer(group1.union(group2), Row(4, 5) :: Row(1, 2) :: Row(4, 6) :: Row(1, 3) :: Nil)
}
}

case class TestDataUnion(x: Int, y: Int, z: Int)

case class SingleData(id: Int)
case class DoubleData(id: Int, val1: String)
case class TripleData(id: Int, val1: String, val2: Long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,11 @@ class ExchangeSuite extends SparkPlanTest with SharedSQLContext {
assertConsistency(spark.range(10000).map(i => Random.nextInt(1000).toLong))
}
}

test("SPARK-23614: Fix incorrect reuse exchange when caching is used") {
val cached = spark.createDataset(Seq((1, 2, 3), (4, 5, 6))).cache()
val projection1 = cached.select("_1", "_2").queryExecution.executedPlan
val projection2 = cached.select("_1", "_3").queryExecution.executedPlan
assert(!projection1.sameResult(projection2))
}
}