Github user yhuai commented on a diff in the pull request:
https://github.com/apache/spark/pull/15189#discussion_r80834862
--- Diff:
sql/core/src/test/scala/org/apache/spark/sql/execution/columnar/InMemoryColumnarQuerySuite.scala
---
@@ -232,4 +232,29 @@ class InMemoryColumnarQuerySuite extends QueryTest
with SharedSQLContext {
val columnTypes2 = List.fill(length2)(IntegerType)
val columnarIterator2 = GenerateColumnAccessor.generate(columnTypes2)
}
+
+ test("SPARK-17549: cached table size should be correctly calculated") {
+ val data = spark.sparkContext.parallelize(1 to 10, 5).map { i => (i,
i.toLong) }
+ .toDF("col1", "col2")
+ val plan = spark.sessionState.executePlan(data.logicalPlan).sparkPlan
+ val cached = InMemoryRelation(true, 5, MEMORY_ONLY, plan, None)
+
+ // Materialize the data.
+ val expectedAnswer = data.collect()
+ checkAnswer(cached, expectedAnswer)
+
+ // Check that the right size was calculated.
+ val expectedColSizes = expectedAnswer.size * (INT.defaultSize +
LONG.defaultSize)
+ assert(cached.statistics.sizeInBytes === expectedColSizes)
+
+ // Create a projection of the cached data and make sure the statistics
are correct.
+ val projected = cached.withOutput(Seq(plan.output.last))
+ assert(projected.statistics.sizeInBytes === expectedAnswer.size *
LONG.defaultSize)
--- End diff --
I double checked the code. The `output` of an `InMemoryRelation` always
represent the materialized dataset. So, it should not be a set of the
underlying dataset's column set. When we scan this relation in
`InMemoryTableScanExec`, we will push the selection to the scan.
So, even we use `withOutput` in `CacheManager`'s `useCachedData`, we should
be fine to still use the original stats because we are not changing the
dataset. If you look at the implementation of this method
```
def useCachedData(plan: LogicalPlan): LogicalPlan = {
plan transformDown {
case currentFragment =>
lookupCachedData(currentFragment)
.map(_.cachedRepresentation.withOutput(currentFragment.output))
.getOrElse(currentFragment)
}
}
```
`lookupCachedData` is implemented using `sameResult`. So, we are just
applying a equivalent output (attributes in this output list may have cosmetic
variations but they should be equivalent to the original attributes of this
dataset).
Although we may have different outputs, they are still representing the
same dataset. So, seems it is fine if they have the same accumulator.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]