Skip to content

Commit

Permalink
modified to add modulus support to fractional types float,double,decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
gvramana committed Sep 18, 2014
1 parent 6772afe commit 296d253
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ abstract class Expression extends TreeNode[Expression] {
case i: IntegralType =>
f.asInstanceOf[(Integral[i.JvmType], i.JvmType, i.JvmType) => i.JvmType](
i.integral, evalE1.asInstanceOf[i.JvmType], evalE2.asInstanceOf[i.JvmType])
case i: FractionalType =>
f.asInstanceOf[(Integral[i.JvmType], i.JvmType, i.JvmType) => i.JvmType](
i.asIntegral, evalE1.asInstanceOf[i.JvmType], evalE2.asInstanceOf[i.JvmType])
case other => sys.error(s"Type $other does not support numeric operations")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.apache.spark.sql.catalyst.types

import java.sql.Timestamp

import scala.math.Numeric.{FloatAsIfIntegral, BigDecimalAsIfIntegral, DoubleAsIfIntegral}
import scala.reflect.ClassTag
import scala.reflect.runtime.universe.{typeTag, TypeTag, runtimeMirror}
import scala.util.parsing.combinator.RegexParsers
Expand Down Expand Up @@ -240,6 +241,7 @@ object FractionalType {
}
abstract class FractionalType extends NumericType {
private[sql] val fractional: Fractional[JvmType]
private[sql] val asIntegral: Integral[JvmType]
}

case object DecimalType extends FractionalType {
Expand All @@ -248,6 +250,7 @@ case object DecimalType extends FractionalType {
private[sql] val numeric = implicitly[Numeric[BigDecimal]]
private[sql] val fractional = implicitly[Fractional[BigDecimal]]
private[sql] val ordering = implicitly[Ordering[JvmType]]
private[sql] val asIntegral = BigDecimalAsIfIntegral
def simpleString: String = "decimal"
}

Expand All @@ -257,6 +260,7 @@ case object DoubleType extends FractionalType {
private[sql] val numeric = implicitly[Numeric[Double]]
private[sql] val fractional = implicitly[Fractional[Double]]
private[sql] val ordering = implicitly[Ordering[JvmType]]
private[sql] val asIntegral = DoubleAsIfIntegral
def simpleString: String = "double"
}

Expand All @@ -266,6 +270,7 @@ case object FloatType extends FractionalType {
private[sql] val numeric = implicitly[Numeric[Float]]
private[sql] val fractional = implicitly[Fractional[Float]]
private[sql] val ordering = implicitly[Ordering[JvmType]]
private[sql] val asIntegral = FloatAsIfIntegral
def simpleString: String = "float"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,30 @@ class ExpressionEvaluationSuite extends FunSuite {
checkEvaluation(c1 % c2, 1, row)
}

test("arithmetic fractional") {
val row = new GenericRow(Array[Any](1.1, 2, 3.1, null))
val c1 = 'a.double.at(0)
val c2 = 'a.int.at(1)
val c3 = 'a.double.at(2)
val c4 = 'a.double.at(3)

checkEvaluation(UnaryMinus(c1), -1, row)
checkEvaluation(UnaryMinus(Literal(100, IntegerType)), -100)

checkEvaluation(Add(c1, c4), null, row)
checkEvaluation(Add(c1, c2), 3.1, row)
checkEvaluation(Add(c1, Literal(null, DoubleType)), null, row)
checkEvaluation(Add(Literal(null, DoubleType), c2), null, row)
checkEvaluation(Add(Literal(null, DoubleType), Literal(null, DoubleType)), null, row)

checkEvaluation(-c1, -1.1, row)
checkEvaluation(c1 + c2, 3.1, row)
checkEvaluation(c1 - c2, -0.8, row)
checkEvaluation(c1 * c2, 2.2, row)
checkEvaluation(c1 / c2, 0.55, row)
checkEvaluation(c3 % c2, 1.1, row)
}

test("BinaryComparison") {
val row = new GenericRow(Array[Any](1, 2, 3, null, 3, null))
val c1 = 'a.int.at(0)
Expand Down

0 comments on commit 296d253

Please sign in to comment.