[SPARK-8220][SQL]Add positive identify function

chenghao-intel adrian-wang

Author: dragonli <lisurprise@gmail.com>
Author: zhichao.li <zhichao.li@intel.com>

Closes #6838 from zhichao-li/positive and squashes the following commits:

e1032a0 [dragonli] remove useless import and refactor code
624d438 [zhichao.li] add positive identify function
This commit is contained in:
dragonli 2015-06-16 23:44:10 -07:00 committed by Reynold Xin
parent 0b8c8fdc12
commit bedff7d532
4 changed files with 27 additions and 0 deletions

View file

@ -120,6 +120,7 @@ object FunctionRegistry {
expression[Log2]("log2"),
expression[Pow]("pow"),
expression[Pow]("power"),
expression[UnaryPositive]("positive"),
expression[Rint]("rint"),
expression[Signum]("sign"),
expression[Signum]("signum"),

View file

@ -58,6 +58,15 @@ case class UnaryMinus(child: Expression) extends UnaryArithmetic {
protected override def evalInternal(evalE: Any) = numeric.negate(evalE)
}
case class UnaryPositive(child: Expression) extends UnaryArithmetic {
override def toString: String = s"positive($child)"
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String =
defineCodeGen(ctx, ev, c => c)
protected override def evalInternal(evalE: Any) = evalE
}
case class Sqrt(child: Expression) extends UnaryArithmetic {
override def dataType: DataType = DoubleType
override def nullable: Boolean = true

View file

@ -52,6 +52,7 @@ object DefaultOptimizer extends Optimizer {
LikeSimplification,
BooleanSimplification,
PushPredicateThroughJoin,
RemovePositive,
SimplifyFilters,
SimplifyCasts,
SimplifyCaseConversionExpressions) ::
@ -632,6 +633,15 @@ object SimplifyCasts extends Rule[LogicalPlan] {
}
}
/**
* Removes [[UnaryPositive]] identify function
*/
object RemovePositive extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions {
case UnaryPositive(child) => child
}
}
/**
* Combines two adjacent [[Limit]] operators into one, merging the
* expressions into one single expression.

View file

@ -262,4 +262,11 @@ class MathExpressionsSuite extends QueryTest {
ctx.sql("SELECT negative(1), negative(0), negative(-1)"),
Row(-1, 0, 1))
}
test("positive") {
val df = Seq((1, -1, "abc")).toDF("a", "b", "c")
checkAnswer(df.selectExpr("positive(a)"), Row(1))
checkAnswer(df.selectExpr("positive(b)"), Row(-1))
checkAnswer(df.selectExpr("positive(c)"), Row("abc"))
}
}