Github user nchammas commented on a diff in the pull request:
https://github.com/apache/spark/pull/2099#discussion_r16928085
--- 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 --
Microsoft has some good documentation for how SQL Server handles these
things. As an established and very popular product, SQL Server could provide
y'all with a good reference implementation for this behavior.
From their [documentation on
`POWER()`](http://msdn.microsoft.com/en-us/library/ms174276.aspx):
> Returns the same type as submitted in _float_expression_. For example, if
a **decimal**(2,0) is submitted as _float_expression_, the result returned is
**decimal**(2,0).
There are a few good examples that follow.
Microsoft also has some good documentation on [how precision, scale, and
length are calculated for results of arithmetic
operations](http://msdn.microsoft.com/en-us/library/ms190476.aspx).
---
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]