aboutsummaryrefslogtreecommitdiff
path: root/sql/catalyst
diff options
context:
space:
mode:
authorMichael Armbrust <michael@databricks.com>2014-07-23 22:52:49 -0700
committerReynold Xin <rxin@apache.org>2014-07-23 22:52:49 -0700
commit78d18fdbaa62d8ed235c29b2e37fd6607263c639 (patch)
treeccdaf838b7206369649163212f4cec8493c0cfa3 /sql/catalyst
parent9e7725c86e70ffd3d2ff3a563460c2b7d0c9bbee (diff)
downloadspark-78d18fdbaa62d8ed235c29b2e37fd6607263c639.tar.gz
spark-78d18fdbaa62d8ed235c29b2e37fd6607263c639.tar.bz2
spark-78d18fdbaa62d8ed235c29b2e37fd6607263c639.zip
[SPARK-2658][SQL] Add rule for true = 1.
Author: Michael Armbrust <michael@databricks.com> Closes #1556 from marmbrus/fixBooleanEqualsOne and squashes the following commits: ad8edd4 [Michael Armbrust] Add rule for true = 1 and false = 0.
Diffstat (limited to 'sql/catalyst')
-rw-r--r--sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala12
1 files changed, 11 insertions, 1 deletions
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala
index 76ddeba9cb..9887856b9c 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala
@@ -231,10 +231,20 @@ trait HiveTypeCoercion {
* Changes Boolean values to Bytes so that expressions like true < false can be Evaluated.
*/
object BooleanComparisons extends Rule[LogicalPlan] {
+ val trueValues = Seq(1, 1L, 1.toByte, 1.toShort, BigDecimal(1)).map(Literal(_))
+ val falseValues = Seq(0, 0L, 0.toByte, 0.toShort, BigDecimal(0)).map(Literal(_))
+
def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions {
// Skip nodes who's children have not been resolved yet.
case e if !e.childrenResolved => e
- // No need to change EqualTo operators as that actually makes sense for boolean types.
+
+ // Hive treats (true = 1) as true and (false = 0) as true.
+ case EqualTo(l @ BooleanType(), r) if trueValues.contains(r) => l
+ case EqualTo(l, r @ BooleanType()) if trueValues.contains(l) => r
+ case EqualTo(l @ BooleanType(), r) if falseValues.contains(r) => Not(l)
+ case EqualTo(l, r @ BooleanType()) if falseValues.contains(l) => Not(r)
+
+ // No need to change other EqualTo operators as that actually makes sense for boolean types.
case e: EqualTo => e
// Otherwise turn them to Byte types so that there exists and ordering.
case p: BinaryComparison