Github user xinyunh commented on a diff in the pull request: https://github.com/apache/spark/pull/2099#discussion_r16923632 --- 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 --- End diff -- But there is a question, when I input, for example POWER(3, 0.5), the return result could not be the INT type. Should I try convert it to base's datatype as long as it could?
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org