Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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 @@ -71,6 +71,7 @@ package object dsl {
def - (other: Expression): Expression = Subtract(expr, other)
def * (other: Expression): Expression = Multiply(expr, other)
def / (other: Expression): Expression = Divide(expr, other)
def div (other: Expression): Expression = IntegralDivide(expr, other)
def % (other: Expression): Expression = Remainder(expr, other)
def & (other: Expression): Expression = BitwiseAnd(expr, other)
def | (other: Expression): Expression = BitwiseOr(expr, other)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,12 @@ case class Multiply(left: Expression, right: Expression)
protected override def nullSafeEval(input1: Any, input2: Any): Any = numeric.times(input1, input2)
}

@ExpressionDescription(
usage = "a _FUNC_ b - Divides a by b.",
extended = "> SELECT 3 _FUNC_ 2;\n 1.5")
case class Divide(left: Expression, right: Expression)
extends BinaryArithmetic with NullIntolerant {

override def inputType: AbstractDataType = TypeCollection(DoubleType, DecimalType)

override def symbol: String = "/"
override def decimalMethod: String = "$div"
abstract class DivideBase extends BinaryArithmetic with NullIntolerant {
override def nullable: Boolean = true

private lazy val div: (Any, Any) => Any = dataType match {
case ft: FractionalType => ft.fractional.asInstanceOf[Fractional[Any]].div
case i: IntegralType => i.integral.asInstanceOf[Integral[Any]].quot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cloud-fan how about make this line in IntegralDivide that can be more readable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DivideBase implement codegen for all types, so I think it's fine for it to implement eval for all types.

}

override def eval(input: InternalRow): Any = {
Expand Down Expand Up @@ -250,10 +242,11 @@ case class Divide(left: Expression, right: Expression)
}
val javaType = ctx.javaType(dataType)
val divide = if (dataType.isInstanceOf[DecimalType]) {
s"${eval1.value}.$decimalMethod(${eval2.value})"
s"${eval1.value}.$$div(${eval2.value})"
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just keep it? Then the 2 implementations can share the same codegen.

s"($javaType)(${eval1.value} $symbol ${eval2.value})"
s"($javaType)(${eval1.value} / ${eval2.value})"
}

if (!left.nullable && !right.nullable) {
ev.copy(code = s"""
${eval2.code}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it already cover both fraction and integral division?

Expand Down Expand Up @@ -284,6 +277,26 @@ case class Divide(left: Expression, right: Expression)
}
}

@ExpressionDescription(
usage = "a _FUNC_ b - Fraction Division a by b.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about Divides a by b of fraction type?

extended = "> SELECT 3 _FUNC_ 2;\n 1.5")
case class Divide(left: Expression, right: Expression) extends DivideBase {

override def inputType: AbstractDataType = TypeCollection(DoubleType, DecimalType)

override def symbol: String = "/"
}

@ExpressionDescription(
usage = "a _FUNC_ b - Divides a by b.",
Copy link
Contributor

@cloud-fan cloud-fan Jul 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about Divides a by b of integral type?

extended = "> SELECT 3 _FUNC_ 2;\n 1")
case class IntegralDivide(left: Expression, right: Expression) extends DivideBase {

override def inputType: AbstractDataType = IntegralType

override def symbol: String = "div"
}

@ExpressionDescription(
usage = "a _FUNC_ b - Returns the remainder when dividing a by b.")
case class Remainder(left: Expression, right: Expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with Logging {
case SqlBaseParser.PERCENT =>
Remainder(left, right)
case SqlBaseParser.DIV =>
Cast(Divide(left, right), LongType)
IntegralDivide(left, right)
Copy link
Contributor

@lianhuiwang lianhuiwang Jul 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add SqlBaseParser.DIVIDE for '/'. BTW: SqlBaseParser.DIV for 'div' .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's ok because I find SparkSQL has SqlBaseParser.SLASH for '/' .

case SqlBaseParser.PLUS =>
Add(left, right)
case SqlBaseParser.MINUS =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,14 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
}
}

// By fixing SPARK-15776, Divide's inputType is required to be DoubleType of DecimalType.
// TODO: in future release, we should add a IntegerDivide to support integral types.
ignore("/ (Divide) for integral type") {
checkEvaluation(Divide(Literal(1.toByte), Literal(2.toByte)), 0.toByte)
checkEvaluation(Divide(Literal(1.toShort), Literal(2.toShort)), 0.toShort)
checkEvaluation(Divide(Literal(1), Literal(2)), 0)
checkEvaluation(Divide(Literal(1.toLong), Literal(2.toLong)), 0.toLong)
checkEvaluation(Divide(positiveShortLit, negativeShortLit), 0.toShort)
checkEvaluation(Divide(positiveIntLit, negativeIntLit), 0)
checkEvaluation(Divide(positiveLongLit, negativeLongLit), 0L)
test("/ (Divide) for integral type") {
checkEvaluation(IntegralDivide(Literal(1.toByte), Literal(2.toByte)), 0.toByte)
checkEvaluation(IntegralDivide(Literal(1.toShort), Literal(2.toShort)), 0.toShort)
checkEvaluation(IntegralDivide(Literal(1), Literal(2)), 0)
checkEvaluation(IntegralDivide(Literal(1.toLong), Literal(2.toLong)), 0.toLong)
checkEvaluation(IntegralDivide(positiveShortLit, negativeShortLit), 0.toShort)
checkEvaluation(IntegralDivide(positiveIntLit, negativeIntLit), 0)
checkEvaluation(IntegralDivide(positiveLongLit, negativeLongLit), 0L)
}

test("% (Remainder)") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class ExpressionParserSuite extends PlanTest {
// Simple operations
assertEqual("a * b", 'a * 'b)
assertEqual("a / b", 'a / 'b)
assertEqual("a DIV b", ('a / 'b).cast(LongType))
assertEqual("a DIV b", ('a div 'b))
assertEqual("a % b", 'a % 'b)
assertEqual("a + b", 'a + 'b)
assertEqual("a - b", 'a - 'b)
Expand All @@ -180,7 +180,7 @@ class ExpressionParserSuite extends PlanTest {
// Check precedences
assertEqual(
"a * t | b ^ c & d - e + f % g DIV h / i * k",
'a * 't | ('b ^ ('c & ('d - 'e + (('f % 'g / 'h).cast(LongType) / 'i * 'k)))))
'a * 't | ('b ^ ('c & ('d - 'e + (('f % 'g div 'h) / 'i * 'k)))))
}

test("unary arithmetic expressions") {
Expand Down