Skip to content

Commit fca5e62

Browse files
committed
return long
1 parent 02a2369 commit fca5e62

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,18 @@ case class Divide(left: Expression, right: Expression) extends DivModLike {
325325
case class IntegralDivide(left: Expression, right: Expression) extends DivModLike {
326326

327327
override def inputType: AbstractDataType = IntegralType
328+
override def dataType: DataType = LongType
328329

329330
override def symbol: String = "/"
330331
override def sqlOperator: String = "div"
331332

332-
private lazy val div: (Any, Any) => Any = dataType match {
333-
case i: IntegralType => i.integral.asInstanceOf[Integral[Any]].quot
333+
private lazy val div: (Any, Any) => Long = left.dataType match {
334+
case i: IntegralType =>
335+
val divide = i.integral.asInstanceOf[Integral[Any]].quot _
336+
val toLong = i.integral.asInstanceOf[Integral[Any]].toLong _
337+
(x, y) => toLong(divide(x, y))
334338
}
339+
335340
override def evalOperation(left: Any, right: Any): Any = div(left, right)
336341
}
337342

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
144144
}
145145

146146
test("/ (Divide) for integral type") {
147-
checkEvaluation(IntegralDivide(Literal(1.toByte), Literal(2.toByte)), 0.toByte)
148-
checkEvaluation(IntegralDivide(Literal(1.toShort), Literal(2.toShort)), 0.toShort)
149-
checkEvaluation(IntegralDivide(Literal(1), Literal(2)), 0)
150-
checkEvaluation(IntegralDivide(Literal(1.toLong), Literal(2.toLong)), 0.toLong)
151-
checkEvaluation(IntegralDivide(positiveShortLit, negativeShortLit), 0.toShort)
152-
checkEvaluation(IntegralDivide(positiveIntLit, negativeIntLit), 0)
147+
checkEvaluation(IntegralDivide(Literal(1.toByte), Literal(2.toByte)), 0L)
148+
checkEvaluation(IntegralDivide(Literal(1.toShort), Literal(2.toShort)), 0L)
149+
checkEvaluation(IntegralDivide(Literal(1), Literal(2)), 0L)
150+
checkEvaluation(IntegralDivide(Literal(1.toLong), Literal(2.toLong)), 0L)
151+
checkEvaluation(IntegralDivide(positiveShortLit, negativeShortLit), 0L)
152+
checkEvaluation(IntegralDivide(positiveIntLit, negativeIntLit), 0L)
153153
checkEvaluation(IntegralDivide(positiveLongLit, negativeLongLit), 0L)
154154
}
155155

sql/core/src/test/resources/sql-tests/results/operators.sql.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,31 +157,31 @@ NULL
157157
-- !query 19
158158
select 5 div 2
159159
-- !query 19 schema
160-
struct<(5 div 2):int>
160+
struct<(5 div 2):bigint>
161161
-- !query 19 output
162162
2
163163

164164

165165
-- !query 20
166166
select 5 div 0
167167
-- !query 20 schema
168-
struct<(5 div 0):int>
168+
struct<(5 div 0):bigint>
169169
-- !query 20 output
170170
NULL
171171

172172

173173
-- !query 21
174174
select 5 div null
175175
-- !query 21 schema
176-
struct<(5 div CAST(NULL AS INT)):int>
176+
struct<(5 div CAST(NULL AS INT)):bigint>
177177
-- !query 21 output
178178
NULL
179179

180180

181181
-- !query 22
182182
select null div 5
183183
-- !query 22 schema
184-
struct<(CAST(NULL AS INT) div 5):int>
184+
struct<(CAST(NULL AS INT) div 5):bigint>
185185
-- !query 22 output
186186
NULL
187187

0 commit comments

Comments
 (0)