Skip to content
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 @@ -267,6 +267,7 @@ object FunctionRegistry {
expression[Subtract]("-"),
expression[Multiply]("*"),
expression[Divide]("/"),
expression[IntegralDivide]("div"),
expression[Remainder]("%"),

// aggregate functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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)
Copy link
Member

Choose a reason for hiding this comment

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

The failure looks like relevant.

org.scalatest.exceptions.TestFailedException: 
Expected "struct<[CAST((CAST(5 AS DOUBLE) / CAST(2 AS DOUBLE)) AS BIGINT):big]int>",
but got "struct<[(5 div 2):]int>" Schema did not match for query #19 select 5 div 2

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 @@ -314,6 +314,34 @@ case class Divide(left: Expression, right: Expression) extends DivModLike {
override def evalOperation(left: Any, right: Any): Any = div(left, right)
}

// scalastyle:off line.size.limit
@ExpressionDescription(
usage = "expr1 _FUNC_ expr2 - Divide `expr1` by `expr2` rounded to the long integer. It returns NULL if an operand is NULL or `expr2` is 0.",
examples = """
Examples:
> SELECT 3 _FUNC_ 2;
1
""",
since = "2.5.0")
// scalastyle:on line.size.limit
case class IntegralDivide(left: Expression, right: Expression) extends DivModLike {

override def inputType: AbstractDataType = IntegralType
override def dataType: DataType = LongType

override def symbol: String = "/"
Copy link
Member

Choose a reason for hiding this comment

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

What is the reason we are using / here? Any benefit?

Copy link
Member

Choose a reason for hiding this comment

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

used in doGenCode?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, exactly, it is used there

override def sqlOperator: String = "div"

private lazy val div: (Any, Any) => Long = left.dataType match {
case i: IntegralType =>
val divide = i.integral.asInstanceOf[Integral[Any]].quot _
val toLong = i.integral.asInstanceOf[Integral[Any]].toLong _
(x, y) => toLong(divide(x, y))
}

override def evalOperation(left: Any, right: Any): Any = div(left, right)
}

@ExpressionDescription(
usage = "expr1 _FUNC_ expr2 - Returns the remainder after `expr1`/`expr2`.",
examples = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
case SqlBaseParser.PERCENT =>
Remainder(left, right)
case SqlBaseParser.DIV =>
Cast(Divide(left, right), LongType)
IntegralDivide(left, right)
case SqlBaseParser.PLUS =>
Add(left, right)
case SqlBaseParser.MINUS =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,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)), 0L)
checkEvaluation(IntegralDivide(Literal(1.toShort), Literal(2.toShort)), 0L)
checkEvaluation(IntegralDivide(Literal(1), Literal(2)), 0L)
checkEvaluation(IntegralDivide(Literal(1.toLong), Literal(2.toLong)), 0L)
checkEvaluation(IntegralDivide(positiveShortLit, negativeShortLit), 0L)
checkEvaluation(IntegralDivide(positiveIntLit, negativeIntLit), 0L)
checkEvaluation(IntegralDivide(positiveLongLit, negativeLongLit), 0L)
Copy link
Member

@dongjoon-hyun dongjoon-hyun Sep 17, 2018

Choose a reason for hiding this comment

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

Could you add a test case for divide by zero like test("/ (Divide) basic")?

For now, this PR seems to follow the behavior of Spark / instead of Hive div. We had better be clear on our decision and prevent future unintended behavior changes.

scala> sql("select 2 / 0, 2 div 0").show()
+---------------------------------------+---------+
|(CAST(2 AS DOUBLE) / CAST(0 AS DOUBLE))|(2 div 0)|
+---------------------------------------+---------+
|                                   null|     null|
+---------------------------------------+---------+
0: jdbc:hive2://ctr-e138-1518143905142-477481> select 2 / 0;
+-------+
|  _c0  |
+-------+
| NULL  |
+-------+

0: jdbc:hive2://ctr-e138-1518143905142-477481> select 2 div 0;
Error: Error while compiling statement: FAILED:
SemanticException [Error 10014]: Line 1:7 Wrong arguments '0':
org.apache.hadoop.hive.ql.metadata.HiveException:
Unable to execute method public org.apache.hadoop.io.LongWritable org.apache.hadoop.hive.ql.udf.UDFOPLongDivide.evaluate(org.apache.hadoop.io.LongWritable,org.apache.hadoop.io.LongWritable)
with arguments {2,0}:/ by zero (state=42000,code=10014)

Copy link
Contributor

Choose a reason for hiding this comment

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

good catch! We should clearly define the behavior in the doc string too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test for this case is present in operators.sql (anyway, if you prefer me to add a case here too, just let me know and I'll add it). And since we already have this function in our code indeed - it is just translated to a normal divide + a cast - currently we are returning null and throwing an exception for it would be a behavior change (and a quite disruptive too). Do we really want to follow Hive's behavior on this?

Copy link
Member

Choose a reason for hiding this comment

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

I think we don't really need to change current behavior, but it is worth describing this in the doc string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with you @viirya. I updated the doc string with the current behavior. Thanks.

}

test("% (Remainder)") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,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 @@ -214,7 +214,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
Original file line number Diff line number Diff line change
Expand Up @@ -157,31 +157,31 @@ NULL
-- !query 19
select 5 div 2
-- !query 19 schema
struct<CAST((CAST(5 AS DOUBLE) / CAST(2 AS DOUBLE)) AS BIGINT):bigint>
struct<(5 div 2):bigint>
-- !query 19 output
2


-- !query 20
select 5 div 0
-- !query 20 schema
struct<CAST((CAST(5 AS DOUBLE) / CAST(0 AS DOUBLE)) AS BIGINT):bigint>
struct<(5 div 0):bigint>
-- !query 20 output
NULL


-- !query 21
select 5 div null
-- !query 21 schema
struct<CAST((CAST(5 AS DOUBLE) / CAST(NULL AS DOUBLE)) AS BIGINT):bigint>
struct<(5 div CAST(NULL AS INT)):bigint>
-- !query 21 output
NULL


-- !query 22
select null div 5
-- !query 22 schema
struct<CAST((CAST(NULL AS DOUBLE) / CAST(5 AS DOUBLE)) AS BIGINT):bigint>
struct<(CAST(NULL AS INT) div 5):bigint>
-- !query 22 output
NULL

Expand Down