Skip to content

Commit fb3fe62

Browse files
committed
Added Remainder.
1 parent 3b266ba commit fb3fe62

File tree

2 files changed

+17
-45
lines changed

2 files changed

+17
-45
lines changed

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,20 +244,16 @@ case class Divide(left: Expression, right: Expression) extends BinaryArithmetic
244244
} else {
245245
s"${eval2.primitive} == 0"
246246
}
247-
val method = if (left.dataType.isInstanceOf[DecimalType]) {
248-
s".$decimalMethod"
249-
} else {
250-
s"$symbol"
251-
}
247+
val method = if (left.dataType.isInstanceOf[DecimalType]) s".$decimalMethod" else s" $symbol "
248+
val javaType = ctx.javaType(left.dataType)
252249
eval1.code + eval2.code +
253250
s"""
254251
boolean ${ev.isNull} = false;
255252
${ctx.javaType(left.dataType)} ${ev.primitive} = ${ctx.defaultValue(left.dataType)};
256253
if (${eval1.isNull} || ${eval2.isNull} || $test) {
257254
${ev.isNull} = true;
258255
} else {
259-
${ev.primitive} =
260-
(${ctx.javaType(left.dataType)})(${eval1.primitive}$method(${eval2.primitive}));
256+
${ev.primitive} = ($javaType) (${eval1.primitive}$method(${eval2.primitive}));
261257
}
262258
"""
263259
}
@@ -305,19 +301,16 @@ case class Remainder(left: Expression, right: Expression) extends BinaryArithmet
305301
} else {
306302
s"${eval2.primitive} == 0"
307303
}
308-
val method = if (left.dataType.isInstanceOf[DecimalType]) {
309-
s".$decimalMethod"
310-
} else {
311-
s"$symbol"
312-
}
304+
val method = if (left.dataType.isInstanceOf[DecimalType]) s".$decimalMethod" else s" $symbol "
305+
val javaType = ctx.javaType(left.dataType)
313306
eval1.code + eval2.code +
314307
s"""
315308
boolean ${ev.isNull} = false;
316309
${ctx.javaType(left.dataType)} ${ev.primitive} = ${ctx.defaultValue(left.dataType)};
317310
if (${eval1.isNull} || ${eval2.isNull} || $test) {
318311
${ev.isNull} = true;
319312
} else {
320-
${ev.primitive} = ${eval1.primitive}$method(${eval2.primitive});
313+
${ev.primitive} = ($javaType) (${eval1.primitive}$method(${eval2.primitive}));
321314
}
322315
"""
323316
}

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

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
104104
checkEvaluation(Divide(Literal(Decimal(1.0)), Literal(Decimal(2.0))), Decimal(0.5))
105105
}
106106

107+
test("% (Remainder)") {
108+
testNumericDataTypes { convert =>
109+
val left = Literal(convert(1))
110+
val right = Literal(convert(2))
111+
checkEvaluation(Remainder(left, right), convert(1))
112+
checkEvaluation(Remainder(Literal.create(null, left.dataType), right), null)
113+
checkEvaluation(Remainder(left, Literal.create(null, right.dataType)), null)
114+
checkEvaluation(Remainder(left, Literal(convert(0))), null) // mod by 0
115+
}
116+
}
117+
107118
test("Abs") {
108119
testNumericDataTypes { convert =>
109120
checkEvaluation(Abs(Literal(convert(0))), convert(0))
@@ -112,38 +123,6 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
112123
}
113124
}
114125

115-
test("Divide") {
116-
checkEvaluation(Divide(Literal(2), Literal(1)), 2)
117-
checkEvaluation(Divide(Literal(1.0), Literal(2.0)), 0.5)
118-
checkEvaluation(Divide(Literal(1), Literal(2)), 0)
119-
checkEvaluation(Divide(Literal(1), Literal(0)), null)
120-
checkEvaluation(Divide(Literal(1.0), Literal(0.0)), null)
121-
checkEvaluation(Divide(Literal(0.0), Literal(0.0)), null)
122-
checkEvaluation(Divide(Literal(0), Literal.create(null, IntegerType)), null)
123-
checkEvaluation(Divide(Literal(1), Literal.create(null, IntegerType)), null)
124-
checkEvaluation(Divide(Literal.create(null, IntegerType), Literal(0)), null)
125-
checkEvaluation(Divide(Literal.create(null, DoubleType), Literal(0.0)), null)
126-
checkEvaluation(Divide(Literal.create(null, IntegerType), Literal(1)), null)
127-
checkEvaluation(Divide(Literal.create(null, IntegerType), Literal.create(null, IntegerType)),
128-
null)
129-
}
130-
131-
test("Remainder") {
132-
checkEvaluation(Remainder(Literal(2), Literal(1)), 0)
133-
checkEvaluation(Remainder(Literal(1.0), Literal(2.0)), 1.0)
134-
checkEvaluation(Remainder(Literal(1), Literal(2)), 1)
135-
checkEvaluation(Remainder(Literal(1), Literal(0)), null)
136-
checkEvaluation(Remainder(Literal(1.0), Literal(0.0)), null)
137-
checkEvaluation(Remainder(Literal(0.0), Literal(0.0)), null)
138-
checkEvaluation(Remainder(Literal(0), Literal.create(null, IntegerType)), null)
139-
checkEvaluation(Remainder(Literal(1), Literal.create(null, IntegerType)), null)
140-
checkEvaluation(Remainder(Literal.create(null, IntegerType), Literal(0)), null)
141-
checkEvaluation(Remainder(Literal.create(null, DoubleType), Literal(0.0)), null)
142-
checkEvaluation(Remainder(Literal.create(null, IntegerType), Literal(1)), null)
143-
checkEvaluation(Remainder(Literal.create(null, IntegerType), Literal.create(null, IntegerType)),
144-
null)
145-
}
146-
147126
test("MaxOf") {
148127
checkEvaluation(MaxOf(1, 2), 2)
149128
checkEvaluation(MaxOf(2, 1), 2)

0 commit comments

Comments
 (0)