Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -937,7 +937,8 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
*/
case class OptimizeCodegen(conf: CatalystConf) extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions {
case e @ CaseWhen(branches, _) if branches.size < conf.maxCaseBranchesForCodegen =>
case e @ CaseWhen(branches, elseBranch)
if branches.size + elseBranch.size <= conf.maxCaseBranchesForCodegen =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Reading the case takes a while and and I think it'd greatly benefit from introducing a local def - a predicate - for the condition (I can't figure out a name for this, sorry)

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not good at naming. How about canCodegen? : )

e.toCodegen()
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this can fit in the previous line?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, will do. Thanks!

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,36 @@

package org.apache.spark.sql.internal

import org.scalatest.BeforeAndAfterAll

import org.apache.spark.sql.{QueryTest, Row, SparkSession, SQLContext}
import org.apache.spark.sql.execution.WholeStageCodegenExec
import org.apache.spark.sql.test.{SharedSQLContext, TestSQLContext}

class SQLConfSuite extends QueryTest with SharedSQLContext {
class SQLConfSuite extends QueryTest with SharedSQLContext with BeforeAndAfterAll {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if this is the proper suite to test this, how about OptimizeCodegenSuite?

Copy link
Member Author

Choose a reason for hiding this comment

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

Initially, I tried it. The behavior is controlled by the configuration SQLConf.MAX_CASES_BRANCHES. However, I am not sure how to change the default conf value of SQLConf.MAX_CASES_BRANCHES in OptimizeCodegenSuite.

At the same time, we do not have a test case to verify the configuration MAX_CASES_BRANCHES.

That is why I added the test case here. Let me know if you have any idea. Thanks!

import testImplicits._

private val testKey = "test.key.0"
private val testVal = "test.val.0"

override def beforeAll() {
super.beforeAll()
sql("DROP TABLE IF EXISTS testData")
spark
.range(10)
.select('id as 'a, 'id as 'b, 'id as 'c, 'id as 'd)
.write
.saveAsTable("testData")
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of creating this table in beforeAll, can we create it just in the test case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, will do. Thanks!

}

override def afterAll(): Unit = {
try {
sql("DROP TABLE IF EXISTS testData")
} finally {
super.afterAll()
}
}

test("propagate from spark conf") {
// We create a new context here to avoid order dependence with other tests that might call
// clear().
Expand Down Expand Up @@ -219,4 +242,32 @@ class SQLConfSuite extends QueryTest with SharedSQLContext {
}
}

test("MAX_CASES_BRANCHES") {
val original = spark.conf.get(SQLConf.MAX_CASES_BRANCHES)
try {
val sql_one_branch_caseWhen = "SELECT CASE WHEN a = 1 THEN 1 END FROM testData"
val sql_two_branch_caseWhen = "SELECT CASE WHEN a = 1 THEN 1 ELSE 0 END FROM testData"

spark.conf.set(SQLConf.MAX_CASES_BRANCHES.key, "0")
assert(!sql(sql_one_branch_caseWhen)
.queryExecution.executedPlan.isInstanceOf[WholeStageCodegenExec])
assert(!sql(sql_two_branch_caseWhen)
.queryExecution.executedPlan.isInstanceOf[WholeStageCodegenExec])

spark.conf.set(SQLConf.MAX_CASES_BRANCHES.key, "1")
assert(sql(sql_one_branch_caseWhen)
.queryExecution.executedPlan.isInstanceOf[WholeStageCodegenExec])
assert(!sql(sql_two_branch_caseWhen)
.queryExecution.executedPlan.isInstanceOf[WholeStageCodegenExec])

spark.conf.set(SQLConf.MAX_CASES_BRANCHES.key, "2")
assert(sql(sql_one_branch_caseWhen)
.queryExecution.executedPlan.isInstanceOf[WholeStageCodegenExec])
assert(sql(sql_two_branch_caseWhen)
.queryExecution.executedPlan.isInstanceOf[WholeStageCodegenExec])
} finally {
spark.conf.set(SQLConf.MAX_CASES_BRANCHES.key, s"$original")
}
}

}