Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 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,8 +937,12 @@ 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 =>
e.toCodegen()
case e: CaseWhen if canCodeGen(e) => 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.

Sorry for nitpicking, but could you use canCodegen instead (to follow the name of the method to call)? Thanks!

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, let me fix it. Thanks!

}

private def canCodeGen(e: CaseWhen): Boolean = {
val numBranches = e.branches.size + e.elseValue.size
numBranches <= conf.maxCaseBranchesForCodegen
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.sql.internal

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 {
Expand Down Expand Up @@ -219,4 +220,41 @@ class SQLConfSuite extends QueryTest with SharedSQLContext {
}
}

test("MAX_CASES_BRANCHES") {
import testImplicits._

val original = spark.conf.get(SQLConf.MAX_CASES_BRANCHES)
try {
withTable("tab1") {
spark
.range(10)
.select('id as 'a, 'id as 'b, 'id as 'c, 'id as 'd)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we only need a? or just spark.range(10).write.saveAsTable, then we can use id in the case when

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 it. Thanks!

.write
.saveAsTable("tab1")

val sql_one_branch_caseWhen = "SELECT CASE WHEN a = 1 THEN 1 END FROM tab1"
val sql_two_branch_caseWhen = "SELECT CASE WHEN a = 1 THEN 1 ELSE 0 END FROM tab1"

spark.conf.set(SQLConf.MAX_CASES_BRANCHES.key, "0")
Copy link
Contributor

Choose a reason for hiding this comment

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

how about:

withTable {
  spark.range(10)....
  val oneBranchCaseWhen = ....
  val twoBranchCaseWhen = ...
  withConf {
    assert(...)
  }
  withConf {
    assert(...)
  }
  withConf {
    assert(...)
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, will do it.

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")
}
}
}