Skip to content

Commit 6ef3b37

Browse files
committed
add unittest and comments
1 parent 78d9836 commit 6ef3b37

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ case class Cos(child: Expression) extends UnaryMathExpression(math.cos, "COS")
140140

141141
case class Cosh(child: Expression) extends UnaryMathExpression(math.cosh, "COSH")
142142

143+
/**
144+
* Convert a num from one base to another
145+
* @param numExpr the number to be converted
146+
* @param fromBaseExpr from which base
147+
* @param toBaseExpr to which base
148+
*/
143149
case class Conv(numExpr: Expression, fromBaseExpr: Expression, toBaseExpr: Expression)
144150
extends Expression with ImplicitCastInputTypes{
145151

@@ -272,7 +278,7 @@ case class Conv(numExpr: Expression, fromBaseExpr: Expression, toBaseExpr: Expre
272278
* NB: This logic is borrowed from org.apache.hadoop.hive.ql.ud.UDFConv
273279
*/
274280
private def conv(n: Array[Byte] , fromBase: Int, toBase: Int ): UTF8String = {
275-
if (n == null || fromBase == null || toBase == null) {
281+
if (n == null || fromBase == null || toBase == null || n.isEmpty) {
276282
return null
277283
}
278284

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
103103
checkEvaluation(Conv(Literal("3"), Literal(null), Literal(16)), null)
104104
checkEvaluation(
105105
Conv(Literal("1234"), Literal(10), Literal(37)), null)
106+
checkEvaluation(
107+
Conv(Literal(""), Literal(10), Literal(16)), null)
106108
checkEvaluation(
107109
Conv(Literal("9223372036854775807"), Literal(36), Literal(16)), "FFFFFFFFFFFFFFFF")
110+
// If there is an invalid digit in the number, the longest valid prefix should be converted.
111+
checkEvaluation(
112+
Conv(Literal("11abc"), Literal(10), Literal(16)), "B")
108113
}
109114

110115
test("e") {

0 commit comments

Comments
 (0)