Skip to content
Closed
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
42 changes: 18 additions & 24 deletions sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2336,46 +2336,40 @@ class DataFrameSuite extends QueryTest with SharedSQLContext {

val sourceDF = spark.createDataFrame(rows, schema)

val structWhenDF = sourceDF
def structWhenDF: DataFrame = sourceDF
.select(when('cond, struct(lit("a").as("val1"), lit(10).as("val2"))).otherwise('s) as "res")
.select('res.getField("val1"))
val arrayWhenDF = sourceDF
def arrayWhenDF: DataFrame = sourceDF
.select(when('cond, array(lit("a"), lit("b"))).otherwise('a) as "res")
.select('res.getItem(0))
val mapWhenDF = sourceDF
def mapWhenDF: DataFrame = sourceDF
.select(when('cond, map(lit(0), lit("a"))).otherwise('m) as "res")
.select('res.getItem(0))

val structIfDF = sourceDF
def structIfDF: DataFrame = sourceDF
.select(expr("if(cond, struct('a' as val1, 10 as val2), s)") as "res")
.select('res.getField("val1"))
val arrayIfDF = sourceDF
def arrayIfDF: DataFrame = sourceDF
.select(expr("if(cond, array('a', 'b'), a)") as "res")
.select('res.getItem(0))
val mapIfDF = sourceDF
def mapIfDF: DataFrame = sourceDF
.select(expr("if(cond, map(0, 'a'), m)") as "res")
.select('res.getItem(0))

def checkResult(df: DataFrame, codegenExpected: Boolean): Unit = {
assert(df.queryExecution.executedPlan.isInstanceOf[WholeStageCodegenExec] == codegenExpected)
checkAnswer(df, Seq(Row("a"), Row(null)))
def checkResult(): Unit = {
checkAnswer(structWhenDF, Seq(Row("a"), Row(null)))
checkAnswer(arrayWhenDF, Seq(Row("a"), Row(null)))
checkAnswer(mapWhenDF, Seq(Row("a"), Row(null)))
checkAnswer(structIfDF, Seq(Row("a"), Row(null)))
checkAnswer(arrayIfDF, Seq(Row("a"), Row(null)))
checkAnswer(mapIfDF, Seq(Row("a"), Row(null)))
}

// without codegen
checkResult(structWhenDF, false)
checkResult(arrayWhenDF, false)
checkResult(mapWhenDF, false)
checkResult(structIfDF, false)
checkResult(arrayIfDF, false)
checkResult(mapIfDF, false)

// with codegen
checkResult(structWhenDF.filter('cond.isNotNull), true)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

no matter we add filter or not, the Project will always be evaluated without codegen, because it's above local relation and the optimizer will evaluate it eagerly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If it's the case why the assert didn't fail?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hmm, this is the execution plan I see for structWhenDF.filter('cond.isNotNull):

*(1) Project [CASE WHEN cond#77042 THEN [a,10] ELSE s#77043 END.val1 AS res.val1#77054]
+- *(1) Filter isnotnull(cond#77042)
   +- LocalTableScan [cond#77042, s#77043, a#77044, m#77045]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is the execution plan for structWhenDF:

LocalTableScan [res.val1#77079]      

@cloud-fan cloud-fan Jul 18, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ah that's tricky. Because filter pushdown runs first, the local relation optimization can't be applied.

To prevent confusions like this, how about we use local/cached relation to test it?

BTW if the local relation optimization includes filter in the future, this test will be broken.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I saw some tests using similar dummy filters in DataFrameFunctionsSuite. Should we fix them as well?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@cloud-fan Thanks for the clarification and this PR!

Btw, there are many tests in DataFrameFunctionsSuite that test only the scenarios without codgen. WDYT about adding a generic checkAnswer method to QueryTest that would evaluate a dataframe for both cases similarly like ExressionEvalHelper.checkEvaluation does for expressions? If it's possible, of course.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it will be very hard to write a general checkAnswer, because the local relation optimization can only handle Project. I'd like to wait for the general codegen config.

checkResult(arrayWhenDF.filter('cond.isNotNull), true)
checkResult(mapWhenDF.filter('cond.isNotNull), true)
checkResult(structIfDF.filter('cond.isNotNull), true)
checkResult(arrayIfDF.filter('cond.isNotNull), true)
checkResult(mapIfDF.filter('cond.isNotNull), true)
// Test with local relation, the Project will be evaluated without codegen
checkResult()
sourceDF.cache()
// Test with cached relation, the Project will be evaluated with codegen
checkResult()
}

test("Uuid expressions should produce same results at retries in the same DataFrame") {
Expand Down