Skip to content

Commit 21c5262

Browse files
committed
add conf
1 parent 9bf23cc commit 21c5262

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ object OptimizeIn extends Rule[LogicalPlan] {
265265
val (convertible, nonConvertible) = list.partition(_.isInstanceOf[Literal])
266266
if (convertible.nonEmpty && nonConvertible.isEmpty) {
267267
optimizeIn(expr, v, list)
268-
} else if (convertible.nonEmpty && nonConvertible.nonEmpty) {
268+
} else if (convertible.nonEmpty && nonConvertible.nonEmpty &&
269+
SQLConf.get.optimizerInExtractLiteralPart) {
269270
val optimizedIn = optimizeIn(In(v, convertible), v, convertible)
270271
Or(optimizedIn, In(v, nonConvertible))
271272
} else {

sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ object SQLConf {
197197
.intConf
198198
.createWithDefault(100)
199199

200+
val OPTIMIZER_IN_EXTRACT_LITERAL_PART =
201+
buildConf("spark.sql.optimizer.inExtractLiteralPart")
202+
.internal()
203+
.doc("When true, we will extract and optimize the literal part of in if not all are literal.")
204+
.version("3.1.0")
205+
.booleanConf
206+
.createWithDefault(true)
207+
200208
val OPTIMIZER_INSET_CONVERSION_THRESHOLD =
201209
buildConf("spark.sql.optimizer.inSetConversionThreshold")
202210
.internal()
@@ -2761,6 +2769,8 @@ class SQLConf extends Serializable with Logging {
27612769

27622770
def optimizerMaxIterations: Int = getConf(OPTIMIZER_MAX_ITERATIONS)
27632771

2772+
def optimizerInExtractLiteralPart: Boolean = getConf(OPTIMIZER_IN_EXTRACT_LITERAL_PART)
2773+
27642774
def optimizerInSetConversionThreshold: Int = getConf(OPTIMIZER_INSET_CONVERSION_THRESHOLD)
27652775

27662776
def optimizerInSetSwitchThreshold: Int = getConf(OPTIMIZER_INSET_SWITCH_THRESHOLD)

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeInSuite.scala

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.apache.spark.sql.catalyst.expressions._
2424
import org.apache.spark.sql.catalyst.plans.PlanTest
2525
import org.apache.spark.sql.catalyst.plans.logical.{Filter, LocalRelation, LogicalPlan}
2626
import org.apache.spark.sql.catalyst.rules.RuleExecutor
27+
import org.apache.spark.sql.internal.SQLConf.OPTIMIZER_IN_EXTRACT_LITERAL_PART
2728
import org.apache.spark.sql.internal.SQLConf.OPTIMIZER_INSET_CONVERSION_THRESHOLD
2829
import org.apache.spark.sql.types._
2930

@@ -225,19 +226,32 @@ class OptimizeInSuite extends PlanTest {
225226
}
226227

227228
test("SPARK-32196: Extract In convertible part if it is not convertible") {
228-
val originalQuery1 =
229-
testRelation
230-
.where(In(UnresolvedAttribute("a"), Seq(Literal(1), UnresolvedAttribute("b"))))
231-
.analyze
232-
val optimized1 = Optimize.execute(originalQuery1)
233-
val correctAnswer1 =
234-
testRelation
235-
.where(
236-
Or(EqualTo(UnresolvedAttribute("a"), Literal(1)),
237-
In(UnresolvedAttribute("a"), Seq(UnresolvedAttribute("b"))))
238-
)
239-
.analyze
240-
comparePlans(optimized1, correctAnswer1)
229+
Seq("true", "false").foreach { enable =>
230+
withSQLConf(OPTIMIZER_IN_EXTRACT_LITERAL_PART.key -> enable) {
231+
val originalQuery1 =
232+
testRelation
233+
.where(In(UnresolvedAttribute("a"), Seq(Literal(1), UnresolvedAttribute("b"))))
234+
.analyze
235+
val optimized1 = Optimize.execute(originalQuery1)
236+
237+
if (enable.toBoolean) {
238+
val correctAnswer1 =
239+
testRelation
240+
.where(
241+
Or(EqualTo(UnresolvedAttribute("a"), Literal(1)),
242+
In(UnresolvedAttribute("a"), Seq(UnresolvedAttribute("b"))))
243+
)
244+
.analyze
245+
comparePlans(optimized1, correctAnswer1)
246+
} else {
247+
val correctAnswer1 =
248+
testRelation
249+
.where(In(UnresolvedAttribute("a"), Seq(Literal(1), UnresolvedAttribute("b"))))
250+
.analyze
251+
comparePlans(optimized1, correctAnswer1)
252+
}
253+
}
254+
}
241255

242256
val originalQuery2 =
243257
testRelation

0 commit comments

Comments
 (0)