Skip to content

Commit 09fcf96

Browse files
cloud-fanDavies Liu
authored andcommitted
[SPARK-8371] [SQL] improve unit test for MaxOf and MinOf and fix bugs
a follow up of apache#6813 Author: Wenchen Fan <[email protected]> Closes apache#6825 from cloud-fan/cg and squashes the following commits: 43170cc [Wenchen Fan] fix bugs in code gen
1 parent 13ae806 commit 09fcf96

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeGenerator.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ class CodeGenContext {
175175
* Generate code for compare expression in Java
176176
*/
177177
def genComp(dataType: DataType, c1: String, c2: String): String = dataType match {
178+
// java boolean doesn't support > or < operator
179+
case BooleanType => s"($c1 == $c2 ? 0 : ($c1 ? 1 : -1))"
178180
// use c1 - c2 may overflow
179-
case dt: DataType if isPrimitiveType(dt) => s"(int)($c1 > $c2 ? 1 : $c1 < $c2 ? -1 : 0)"
181+
case dt: DataType if isPrimitiveType(dt) => s"($c1 > $c2 ? 1 : $c1 < $c2 ? -1 : 0)"
180182
case BinaryType => s"org.apache.spark.sql.catalyst.util.TypeUtils.compareBinary($c1, $c2)"
181183
case other => s"$c1.compare($c2)"
182184
}

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package org.apache.spark.sql.catalyst.expressions
1919

2020
import org.apache.spark.SparkFunSuite
2121
import org.apache.spark.sql.catalyst.dsl.expressions._
22-
import org.apache.spark.sql.types.{Decimal, DoubleType, IntegerType}
22+
import org.apache.spark.sql.types.Decimal
2323

2424

2525
class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
@@ -123,23 +123,39 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
123123
}
124124
}
125125

126-
test("MaxOf") {
127-
checkEvaluation(MaxOf(1, 2), 2)
128-
checkEvaluation(MaxOf(2, 1), 2)
129-
checkEvaluation(MaxOf(1L, 2L), 2L)
130-
checkEvaluation(MaxOf(2L, 1L), 2L)
126+
test("MaxOf basic") {
127+
testNumericDataTypes { convert =>
128+
val small = Literal(convert(1))
129+
val large = Literal(convert(2))
130+
checkEvaluation(MaxOf(small, large), convert(2))
131+
checkEvaluation(MaxOf(large, small), convert(2))
132+
checkEvaluation(MaxOf(Literal.create(null, small.dataType), large), convert(2))
133+
checkEvaluation(MaxOf(large, Literal.create(null, small.dataType)), convert(2))
134+
}
135+
}
131136

132-
checkEvaluation(MaxOf(Literal.create(null, IntegerType), 2), 2)
133-
checkEvaluation(MaxOf(2, Literal.create(null, IntegerType)), 2)
137+
test("MaxOf for atomic type") {
138+
checkEvaluation(MaxOf(true, false), true)
139+
checkEvaluation(MaxOf("abc", "bcd"), "bcd")
140+
checkEvaluation(MaxOf(Array(1.toByte, 2.toByte), Array(1.toByte, 3.toByte)),
141+
Array(1.toByte, 3.toByte))
134142
}
135143

136-
test("MinOf") {
137-
checkEvaluation(MinOf(1, 2), 1)
138-
checkEvaluation(MinOf(2, 1), 1)
139-
checkEvaluation(MinOf(1L, 2L), 1L)
140-
checkEvaluation(MinOf(2L, 1L), 1L)
144+
test("MinOf basic") {
145+
testNumericDataTypes { convert =>
146+
val small = Literal(convert(1))
147+
val large = Literal(convert(2))
148+
checkEvaluation(MinOf(small, large), convert(1))
149+
checkEvaluation(MinOf(large, small), convert(1))
150+
checkEvaluation(MinOf(Literal.create(null, small.dataType), large), convert(2))
151+
checkEvaluation(MinOf(small, Literal.create(null, small.dataType)), convert(1))
152+
}
153+
}
141154

142-
checkEvaluation(MinOf(Literal.create(null, IntegerType), 1), 1)
143-
checkEvaluation(MinOf(1, Literal.create(null, IntegerType)), 1)
155+
test("MinOf for atomic type") {
156+
checkEvaluation(MinOf(true, false), false)
157+
checkEvaluation(MinOf("abc", "bcd"), "abc")
158+
checkEvaluation(MinOf(Array(1.toByte, 2.toByte), Array(1.toByte, 3.toByte)),
159+
Array(1.toByte, 2.toByte))
144160
}
145161
}

0 commit comments

Comments
 (0)