Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -76,7 +76,8 @@ trait CaseConversionExpression {
type EvaluatedType = Any

def convert(v: String): String


override def foldable: Boolean = child.foldable
def nullable: Boolean = child.nullable
def dataType: DataType = StringType

Expand Down Expand Up @@ -142,6 +143,8 @@ case class RLike(left: Expression, right: Expression)
case class Upper(child: Expression) extends UnaryExpression with CaseConversionExpression {

override def convert(v: String): String = v.toUpperCase()

override def toString() = s"Upper($child)"
}

/**
Expand All @@ -150,4 +153,6 @@ case class Upper(child: Expression) extends UnaryExpression with CaseConversionE
case class Lower(child: Expression) extends UnaryExpression with CaseConversionExpression {

override def convert(v: String): String = v.toLowerCase()

override def toString() = s"Lower($child)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ object Optimizer extends RuleExecutor[LogicalPlan] {
ConstantFolding,
BooleanSimplification,
SimplifyFilters,
SimplifyCasts) ::
SimplifyCasts,
SimplifyCaseConversionExpressions) ::
Batch("Filter Pushdown", FixedPoint(100),
CombineFilters,
PushPredicateThroughProject,
Expand Down Expand Up @@ -132,18 +133,6 @@ object NullPropagation extends Rule[LogicalPlan] {
case Literal(candidate, _) if candidate == v => true
case _ => false
})) => Literal(true, BooleanType)
case e: UnaryMinus => e.child match {
case Literal(null, _) => Literal(null, e.dataType)
case _ => e
}
case e: Cast => e.child match {
case Literal(null, _) => Literal(null, e.dataType)
case _ => e
}
case e: Not => e.child match {
case Literal(null, _) => Literal(null, e.dataType)
case _ => e
}
// Put exceptional cases above if any
case e: BinaryArithmetic => e.children match {
case Literal(null, _) :: right :: Nil => Literal(null, e.dataType)
Expand Down Expand Up @@ -375,3 +364,18 @@ object CombineLimits extends Rule[LogicalPlan] {
Limit(If(LessThan(ne, le), ne, le), grandChild)
}
}

/**
* Removes the inner [[catalyst.expressions.CaseConversionExpression]] that are unnecessary because
* the inner conversion is overwritten by the outer one.
*/
object SimplifyCaseConversionExpressions extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
case q: LogicalPlan => q transformExpressionsUp {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious - do you have a use case for this rule? I wonder how often it happens in practice.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi,
I just cared that the conversions were used redundantly like UPPER(LOWER(str)), which would be a mistake in most case, though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you actually have queries that trigger this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just use UPPER(LOWER(str)) or UPPER(UPPER(str)), or something like those.

case Upper(Upper(child)) => Upper(child)
case Upper(Lower(child)) => Upper(child)
case Lower(Upper(child)) => Lower(child)
case Lower(Lower(child)) => Lower(child)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.apache.spark.sql.catalyst.analysis.EliminateAnalysisOperators
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan}
import org.apache.spark.sql.catalyst.rules.RuleExecutor
import org.apache.spark.sql.catalyst.types.{DoubleType, IntegerType}
import org.apache.spark.sql.catalyst.types._

// For implicit conversions
import org.apache.spark.sql.catalyst.dsl.plans._
Expand Down Expand Up @@ -173,4 +173,63 @@ class ConstantFoldingSuite extends OptimizerTest {

comparePlans(optimized, correctAnswer)
}

test("Constant folding test: expressions have null literals") {
val originalQuery =
testRelation
.select(
IsNull(Literal(null)) as 'c1,
IsNotNull(Literal(null)) as 'c2,

GetItem(Literal(null, ArrayType(IntegerType)), 1) as 'c3,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is only foldable because both values are literals. if you make one of them an attribute (e.g., 'a.int) then it won't fold anymore and we'll need the NullPropagation rule still.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I see, that's right!
I'll move them back.

GetItem(Literal(Seq(1), ArrayType(IntegerType)), Literal(null, IntegerType)) as 'c4,
GetField(
Literal(null, StructType(Seq(StructField("a", IntegerType, true)))),
"a") as 'c5,

UnaryMinus(Literal(null, IntegerType)) as 'c6,
Cast(Literal(null), IntegerType) as 'c7,
Not(Literal(null, BooleanType)) as 'c8,

Add(Literal(null, IntegerType), 1) as 'c9,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here and all the expression with more than one child.

Add(1, Literal(null, IntegerType)) as 'c10,

Equals(Literal(null, IntegerType), 1) as 'c11,
Equals(1, Literal(null, IntegerType)) as 'c12,

Like(Literal(null, StringType), "abc") as 'c13,
Like("abc", Literal(null, StringType)) as 'c14,

Upper(Literal(null, StringType)) as 'c15)

val optimized = Optimize(originalQuery.analyze)

val correctAnswer =
testRelation
.select(
Literal(true) as 'c1,
Literal(false) as 'c2,

Literal(null, IntegerType) as 'c3,
Literal(null, IntegerType) as 'c4,
Literal(null, IntegerType) as 'c5,

Literal(null, IntegerType) as 'c6,
Literal(null, IntegerType) as 'c7,
Literal(null, BooleanType) as 'c8,

Literal(null, IntegerType) as 'c9,
Literal(null, IntegerType) as 'c10,

Literal(null, BooleanType) as 'c11,
Literal(null, BooleanType) as 'c12,

Literal(null, BooleanType) as 'c13,
Literal(null, BooleanType) as 'c14,

Literal(null, StringType) as 'c15)
.analyze

comparePlans(optimized, correctAnswer)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.catalyst.optimizer

import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules._

/* Implicit conversions */
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._

class SimplifyCaseConversionExpressionsSuite extends OptimizerTest {

object Optimize extends RuleExecutor[LogicalPlan] {
val batches =
Batch("Simplify CaseConversionExpressions", Once,
SimplifyCaseConversionExpressions) :: Nil
}

val testRelation = LocalRelation('a.string)

test("simplify UPPER(UPPER(str))") {
val originalQuery =
testRelation
.select(Upper(Upper('a)) as 'u)

val optimized = Optimize(originalQuery.analyze)
val correctAnswer =
testRelation
.select(Upper('a) as 'u)
.analyze

comparePlans(optimized, correctAnswer)
}

test("simplify UPPER(LOWER(str))") {
val originalQuery =
testRelation
.select(Upper(Lower('a)) as 'u)

val optimized = Optimize(originalQuery.analyze)
val correctAnswer =
testRelation
.select(Upper('a) as 'u)
.analyze

comparePlans(optimized, correctAnswer)
}

test("simplify LOWER(UPPER(str))") {
val originalQuery =
testRelation
.select(Lower(Upper('a)) as 'l)

val optimized = Optimize(originalQuery.analyze)
val correctAnswer = testRelation
.select(Lower('a) as 'l)
.analyze

comparePlans(optimized, correctAnswer)
}

test("simplify LOWER(LOWER(str))") {
val originalQuery =
testRelation
.select(Lower(Lower('a)) as 'l)

val optimized = Optimize(originalQuery.analyze)
val correctAnswer = testRelation
.select(Lower('a) as 'l)
.analyze

comparePlans(optimized, correctAnswer)
}
}