Skip to content

Commit 56efb79

Browse files
committed
ensure bitwise NOT over byte and short persist data type
1 parent f55fbae commit 56efb79

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ case class BitwiseNot(child: Expression) extends UnaryExpression {
184184
null
185185
} else {
186186
dataType match {
187-
case ByteType => ~evalE.asInstanceOf[Byte]
188-
case ShortType => ~evalE.asInstanceOf[Short]
187+
case ByteType => (~evalE.asInstanceOf[Byte]).toByte
188+
case ShortType => (~evalE.asInstanceOf[Short]).toShort
189189
case IntegerType => ~evalE.asInstanceOf[Int]
190190
case LongType => ~evalE.asInstanceOf[Long]
191191
case other => sys.error(s"Unsupported bitwise ~ operation on $other")

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ class ExpressionEvaluationSuite extends FunSuite {
4242
checkEvaluation(Literal(1) + Literal(1), 2)
4343
}
4444

45+
test("unary BitwiseNOT") {
46+
checkEvaluation(BitwiseNot(1), -2)
47+
assert(BitwiseNot(1).dataType === IntegerType)
48+
assert(BitwiseNot(1).eval(EmptyRow).isInstanceOf[Int])
49+
checkEvaluation(BitwiseNot(1.toLong), -2)
50+
assert(BitwiseNot(1.toLong).dataType === LongType)
51+
assert(BitwiseNot(1.toLong).eval(EmptyRow).isInstanceOf[Long])
52+
checkEvaluation(BitwiseNot(1.toShort), -2)
53+
assert(BitwiseNot(1.toShort).dataType === ShortType)
54+
assert(BitwiseNot(1.toShort).eval(EmptyRow).isInstanceOf[Short])
55+
checkEvaluation(BitwiseNot(1.toByte), -2)
56+
assert(BitwiseNot(1.toByte).dataType === ByteType)
57+
assert(BitwiseNot(1.toByte).eval(EmptyRow).isInstanceOf[Byte])
58+
}
59+
4560
/**
4661
* Checks for three-valued-logic. Based on:
4762
* http://en.wikipedia.org/wiki/Null_(SQL)#Comparisons_with_NULL_and_the_three-valued_logic_.283VL.29

0 commit comments

Comments
 (0)