Skip to content

Commit

Permalink
compare literal
Browse files Browse the repository at this point in the history
  • Loading branch information
cloud-fan committed Jun 7, 2015
1 parent ca8dafc commit f93c420
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Expand Up @@ -480,13 +480,15 @@ trait HiveTypeCoercion {
* Changes numeric values to booleans so that expressions like true = 1 can be evaluated.
*/
object BooleanEqualization extends Rule[LogicalPlan] {
private val trueValues = Seq(1.toByte, 1.toShort, 1, 1L, new java.math.BigDecimal(1))
private val falseValues = Seq(0.toByte, 0.toShort, 0, 0L, new java.math.BigDecimal(0))
private val trueValues =
Seq(1.toByte, 1.toShort, 1, 1L, new java.math.BigDecimal(1)).map(Literal(_))
private val falseValues =
Seq(0.toByte, 0.toShort, 0, 0L, new java.math.BigDecimal(0)).map(Literal(_))

private def buildCaseKeyWhen(booleanExpr: Expression, numericExpr: Expression) = {
CaseKeyWhen(numericExpr, Seq(
Literal(trueValues.head), booleanExpr,
Literal(falseValues.head), Not(booleanExpr),
trueValues.head, booleanExpr,
falseValues.head, Not(booleanExpr),
Literal(false)))
}

Expand Down
Expand Up @@ -148,6 +148,7 @@ class HiveTypeCoercionSuite extends PlanTest {

test("type coercion simplification for equal to") {
val be = new HiveTypeCoercion {}.BooleanEqualization

ruleTest(be,
EqualTo(Literal(true), Literal(1)),
Literal(true)
Expand All @@ -164,5 +165,22 @@ class HiveTypeCoercionSuite extends PlanTest {
EqualNullSafe(Literal(true), Literal(0)),
And(IsNotNull(Literal(true)), Not(Literal(true)))
)

ruleTest(be,
EqualTo(Literal(true), Literal(1L)),
Literal(true)
)
ruleTest(be,
EqualTo(Literal(new java.math.BigDecimal(1)), Literal(true)),
Literal(true)
)
ruleTest(be,
EqualTo(Literal(BigDecimal(0)), Literal(true)),
Not(Literal(true))
)
ruleTest(be,
EqualTo(Literal(Decimal(1)), Literal(true)),
Literal(true)
)
}
}

0 comments on commit f93c420

Please sign in to comment.