Skip to content

[SPARK-4670] [SQL] wrong symbol for bitwise not #3528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
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 @@ -19,7 +19,6 @@ package org.apache.spark.sql.catalyst.expressions

import org.apache.spark.sql.catalyst.analysis.UnresolvedException
import org.apache.spark.sql.catalyst.types._
import scala.math.pow

case class UnaryMinus(child: Expression) extends UnaryExpression {
type EvaluatedType = Any
Expand All @@ -43,7 +42,7 @@ case class Sqrt(child: Expression) extends UnaryExpression {
override def toString = s"SQRT($child)"

override def eval(input: Row): Any = {
n1(child, input, ((na,a) => math.sqrt(na.toDouble(a))))
n1(child, input, (na,a) => math.sqrt(na.toDouble(a)))
}
}

Expand Down Expand Up @@ -134,7 +133,7 @@ case class BitwiseAnd(left: Expression, right: Expression) extends BinaryArithme
case ShortType => (evalE1.asInstanceOf[Short] & evalE2.asInstanceOf[Short]).toShort
case IntegerType => evalE1.asInstanceOf[Int] & evalE2.asInstanceOf[Int]
case LongType => evalE1.asInstanceOf[Long] & evalE2.asInstanceOf[Long]
case other => sys.error(s"Unsupported bitwise & operation on ${other}")
case other => sys.error(s"Unsupported bitwise & operation on $other")
}
}

Expand All @@ -149,7 +148,7 @@ case class BitwiseOr(left: Expression, right: Expression) extends BinaryArithmet
case ShortType => (evalE1.asInstanceOf[Short] | evalE2.asInstanceOf[Short]).toShort
case IntegerType => evalE1.asInstanceOf[Int] | evalE2.asInstanceOf[Int]
case LongType => evalE1.asInstanceOf[Long] | evalE2.asInstanceOf[Long]
case other => sys.error(s"Unsupported bitwise | operation on ${other}")
case other => sys.error(s"Unsupported bitwise | operation on $other")
}
}

Expand All @@ -164,7 +163,7 @@ case class BitwiseXor(left: Expression, right: Expression) extends BinaryArithme
case ShortType => (evalE1.asInstanceOf[Short] ^ evalE2.asInstanceOf[Short]).toShort
case IntegerType => evalE1.asInstanceOf[Int] ^ evalE2.asInstanceOf[Int]
case LongType => evalE1.asInstanceOf[Long] ^ evalE2.asInstanceOf[Long]
case other => sys.error(s"Unsupported bitwise ^ operation on ${other}")
case other => sys.error(s"Unsupported bitwise ^ operation on $other")
}
}

Expand All @@ -177,19 +176,19 @@ case class BitwiseNot(child: Expression) extends UnaryExpression {
def dataType = child.dataType
override def foldable = child.foldable
def nullable = child.nullable
override def toString = s"-$child"
override def toString = s"~$child"

override def eval(input: Row): Any = {
val evalE = child.eval(input)
if (evalE == null) {
null
} else {
dataType match {
case ByteType => (~(evalE.asInstanceOf[Byte])).toByte
case ShortType => (~(evalE.asInstanceOf[Short])).toShort
case IntegerType => ~(evalE.asInstanceOf[Int])
case LongType => ~(evalE.asInstanceOf[Long])
case other => sys.error(s"Unsupported bitwise ~ operation on ${other}")
case ByteType => (~evalE.asInstanceOf[Byte]).toByte
case ShortType => (~evalE.asInstanceOf[Short]).toShort
case IntegerType => ~evalE.asInstanceOf[Int]
case LongType => ~evalE.asInstanceOf[Long]
case other => sys.error(s"Unsupported bitwise ~ operation on $other")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ class ExpressionEvaluationSuite extends FunSuite {
checkEvaluation(Literal(1) + Literal(1), 2)
}

test("unary BitwiseNOT") {
checkEvaluation(BitwiseNot(1), -2)
assert(BitwiseNot(1).dataType === IntegerType)
assert(BitwiseNot(1).eval(EmptyRow).isInstanceOf[Int])
checkEvaluation(BitwiseNot(1.toLong), -2.toLong)
assert(BitwiseNot(1.toLong).dataType === LongType)
assert(BitwiseNot(1.toLong).eval(EmptyRow).isInstanceOf[Long])
checkEvaluation(BitwiseNot(1.toShort), -2.toShort)
assert(BitwiseNot(1.toShort).dataType === ShortType)
assert(BitwiseNot(1.toShort).eval(EmptyRow).isInstanceOf[Short])
checkEvaluation(BitwiseNot(1.toByte), -2.toByte)
assert(BitwiseNot(1.toByte).dataType === ByteType)
assert(BitwiseNot(1.toByte).eval(EmptyRow).isInstanceOf[Byte])
}

/**
* Checks for three-valued-logic. Based on:
* http://en.wikipedia.org/wiki/Null_(SQL)#Comparisons_with_NULL_and_the_three-valued_logic_.283VL.29
Expand Down