Github user xinyunh commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2099#discussion_r16923739
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/numericOperations.scala
 ---
    @@ -0,0 +1,78 @@
    +package org.apache.spark.sql.catalyst.expressions
    +
    +import org.apache.spark.sql.catalyst.types.{DoubleType, StringType, 
DataType}
    +import org.apache.spark.sql.catalyst.analysis.UnresolvedException
    +import scala.math.pow
    +
    +trait NumberConversionExpression {
    +  self: UnaryExpression =>
    +
    +  type EvaluatedType = Any
    +
    +  def convert(v: String): Any
    +
    +  override def foldable: Boolean = child.foldable
    +  def nullable: Boolean = child.nullable
    +  def dataType: DataType = StringType
    +
    +  override def eval(input: Row): Any = {
    +    val evaluated = child.eval(input)
    +    if (evaluated == null) {
    +      null
    +    } else {
    +      convert(evaluated.toString)
    +    }
    +  }
    +}
    +/**
    + * A function that get the absolute value of the numeric value.
    + */
    +case class Abs(child: Expression) extends UnaryExpression with 
NumberConversionExpression {
    +  def parseDouble(s: String) = try { Some(s.toDouble) } catch { case _ => 
None }
    +
    +  def convert(v: String): Any = {
    +    parseDouble(v)  match {
    +      case Some(s) => s.abs
    +      case None => null
    +    }
    +  }
    +
    +  override def toString() = s"Abs($child)"
    +}
    +
    +/**
    + * A function that get the power value of two parameters.
    + * First one is taken as base while second one taken as exponent
    + */
    +case class Power(base: Expression, exponent: Expression) extends 
Expression {
    +
    +  type EvaluatedType = Any
    +
    +  def nullable: Boolean = base.nullable || exponent.nullable
    +
    +  override def children = base :: exponent :: Nil
    +  def references = children.flatMap(_.references).toSet
    +
    +  def dataType: DataType = {
    +    if (!resolved) throw new UnresolvedException(this, s"Cannot resolve 
since $children are not resolved")
    +    DoubleType
    +  }
    +
    +  override def eval(input: Row): Any = {
    +    def convertToDouble(num: EvaluatedType): Double = {
    +      num match {
    +        case d:Double => d
    +        case i:Integer => i.doubleValue()
    +        case f:Float => f.toDouble
    +      }
    +    }
    +
    +    val base_v = base.eval(input)
    +    val exponent_v = exponent.eval(input)
    +
    +    if ((base_v == null) || (exponent_v == null))  null
    +    else pow(convertToDouble(base_v), convertToDouble(exponent_v))
    +  }
    +
    +  override def toString = s"Power($base, $exponent)"
    +}
    --- End diff --
    
    Ok, will do it.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to