@@ -56,7 +56,7 @@ abstract class LeafMathExpression(c: Double, name: String)
5656 * @param name The short name of the function
5757 */
5858abstract class UnaryMathExpression (f : Double => Double , name : String )
59- extends UnaryExpression with Serializable with ExpectsInputTypes {
59+ extends UnaryExpression with Serializable with AutoCastInputTypes {
6060 self : Product =>
6161
6262 override def expectedChildTypes : Seq [DataType ] = Seq (DoubleType )
@@ -99,7 +99,7 @@ abstract class UnaryMathExpression(f: Double => Double, name: String)
9999 * @param name The short name of the function
100100 */
101101abstract class BinaryMathExpression (f : (Double , Double ) => Double , name : String )
102- extends BinaryExpression with Serializable with ExpectsInputTypes { self : Product =>
102+ extends BinaryExpression with Serializable with AutoCastInputTypes { self : Product =>
103103
104104 override def expectedChildTypes : Seq [DataType ] = Seq (DoubleType , DoubleType )
105105
@@ -211,19 +211,11 @@ case class ToRadians(child: Expression) extends UnaryMathExpression(math.toRadia
211211}
212212
213213case class Bin (child : Expression )
214- extends UnaryExpression with Serializable with ExpectsInputTypes {
215-
216- val name : String = " BIN"
217-
218- override def foldable : Boolean = child.foldable
219- override def nullable : Boolean = true
220- override def toString : String = s " $name( $child) "
214+ extends UnaryExpression with Serializable with AutoCastInputTypes {
221215
222216 override def expectedChildTypes : Seq [DataType ] = Seq (LongType )
223217 override def dataType : DataType = StringType
224218
225- def funcName : String = name.toLowerCase
226-
227219 override def eval (input : InternalRow ): Any = {
228220 val evalE = child.eval(input)
229221 if (evalE == null ) {
@@ -239,61 +231,13 @@ case class Bin(child: Expression)
239231 }
240232}
241233
242- // //////////////////////////////////////////////////////////////////////////////////////////////////
243- // //////////////////////////////////////////////////////////////////////////////////////////////////
244- // Binary math functions
245- // //////////////////////////////////////////////////////////////////////////////////////////////////
246- // //////////////////////////////////////////////////////////////////////////////////////////////////
247-
248-
249- case class Atan2 (left : Expression , right : Expression )
250- extends BinaryMathExpression (math.atan2, " ATAN2" ) {
251-
252- override def eval (input : InternalRow ): Any = {
253- val evalE1 = left.eval(input)
254- if (evalE1 == null ) {
255- null
256- } else {
257- val evalE2 = right.eval(input)
258- if (evalE2 == null ) {
259- null
260- } else {
261- // With codegen, the values returned by -0.0 and 0.0 are different. Handled with +0.0
262- val result = math.atan2(evalE1.asInstanceOf [Double ] + 0.0 ,
263- evalE2.asInstanceOf [Double ] + 0.0 )
264- if (result.isNaN) null else result
265- }
266- }
267- }
268-
269- override def genCode (ctx : CodeGenContext , ev : GeneratedExpressionCode ): String = {
270- defineCodeGen(ctx, ev, (c1, c2) => s " java.lang.Math.atan2( $c1 + 0.0, $c2 + 0.0) " ) + s """
271- if (Double.valueOf( ${ev.primitive}).isNaN()) {
272- ${ev.isNull} = true;
273- }
274- """
275- }
276- }
277-
278- case class Pow (left : Expression , right : Expression )
279- extends BinaryMathExpression (math.pow, " POWER" ) {
280- override def genCode (ctx : CodeGenContext , ev : GeneratedExpressionCode ): String = {
281- defineCodeGen(ctx, ev, (c1, c2) => s " java.lang.Math.pow( $c1, $c2) " ) + s """
282- if (Double.valueOf( ${ev.primitive}).isNaN()) {
283- ${ev.isNull} = true;
284- }
285- """
286- }
287- }
288234
289235/**
290236 * If the argument is an INT or binary, hex returns the number as a STRING in hexadecimal format.
291- * Otherwise if the number is a STRING,
292- * it converts each character into its hexadecimal representation and returns the resulting STRING.
293- * Negative numbers would be treated as two's complement.
237+ * Otherwise if the number is a STRING, it converts each character into its hex representation
238+ * and returns the resulting STRING. Negative numbers would be treated as two's complement.
294239 */
295- case class Hex (child : Expression )
296- extends UnaryExpression with Serializable {
240+ case class Hex (child : Expression ) extends UnaryExpression with Serializable {
297241
298242 override def dataType : DataType = StringType
299243
@@ -337,7 +281,7 @@ case class Hex(child: Expression)
337281 private def doHex (bytes : Array [Byte ], length : Int ): UTF8String = {
338282 val value = new Array [Byte ](length * 2 )
339283 var i = 0
340- while (i < length) {
284+ while (i < length) {
341285 value(i * 2 ) = Character .toUpperCase(Character .forDigit(
342286 (bytes(i) & 0xF0 ) >>> 4 , 16 )).toByte
343287 value(i * 2 + 1 ) = Character .toUpperCase(Character .forDigit(
@@ -362,6 +306,54 @@ case class Hex(child: Expression)
362306 }
363307}
364308
309+
310+ // //////////////////////////////////////////////////////////////////////////////////////////////////
311+ // //////////////////////////////////////////////////////////////////////////////////////////////////
312+ // Binary math functions
313+ // //////////////////////////////////////////////////////////////////////////////////////////////////
314+ // //////////////////////////////////////////////////////////////////////////////////////////////////
315+
316+
317+ case class Atan2 (left : Expression , right : Expression )
318+ extends BinaryMathExpression (math.atan2, " ATAN2" ) {
319+
320+ override def eval (input : InternalRow ): Any = {
321+ val evalE1 = left.eval(input)
322+ if (evalE1 == null ) {
323+ null
324+ } else {
325+ val evalE2 = right.eval(input)
326+ if (evalE2 == null ) {
327+ null
328+ } else {
329+ // With codegen, the values returned by -0.0 and 0.0 are different. Handled with +0.0
330+ val result = math.atan2(evalE1.asInstanceOf [Double ] + 0.0 ,
331+ evalE2.asInstanceOf [Double ] + 0.0 )
332+ if (result.isNaN) null else result
333+ }
334+ }
335+ }
336+
337+ override def genCode (ctx : CodeGenContext , ev : GeneratedExpressionCode ): String = {
338+ defineCodeGen(ctx, ev, (c1, c2) => s " java.lang.Math.atan2( $c1 + 0.0, $c2 + 0.0) " ) + s """
339+ if (Double.valueOf( ${ev.primitive}).isNaN()) {
340+ ${ev.isNull} = true;
341+ }
342+ """
343+ }
344+ }
345+
346+ case class Pow (left : Expression , right : Expression )
347+ extends BinaryMathExpression (math.pow, " POWER" ) {
348+ override def genCode (ctx : CodeGenContext , ev : GeneratedExpressionCode ): String = {
349+ defineCodeGen(ctx, ev, (c1, c2) => s " java.lang.Math.pow( $c1, $c2) " ) + s """
350+ if (Double.valueOf( ${ev.primitive}).isNaN()) {
351+ ${ev.isNull} = true;
352+ }
353+ """
354+ }
355+ }
356+
365357case class Hypot (left : Expression , right : Expression )
366358 extends BinaryMathExpression (math.hypot, " HYPOT" )
367359
0 commit comments