Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add positive identify function
  • Loading branch information
zhichao-li committed Jun 16, 2015
commit 624d4384c0fccfbaca0407c7210ab9ae16f41172
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just name this Positive

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ object DefaultOptimizer extends Optimizer {
LikeSimplification,
BooleanSimplification,
PushPredicateThroughJoin,
RemovePositive,
SimplifyFilters,
SimplifyCasts,
SimplifyCaseConversionExpressions) ::
Expand Down Expand Up @@ -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(data) => data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change data => child?

}
}

/**
* Combines two adjacent [[Limit]] operators into one, merging the
* expressions into one single expression.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.spark.sql

import org.apache.spark.sql.execution.ExplainCommand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import?

import org.apache.spark.sql.functions._
import org.apache.spark.sql.functions.{log => logarithm}

Expand Down Expand Up @@ -262,4 +263,10 @@ class MathExpressionsSuite extends QueryTest {
ctx.sql("SELECT negative(1), negative(0), negative(-1)"),
Row(-1, 0, 1))
}

test("positive") {
checkAnswer(
ctx.sql("""SELECT positive(1), positive(-1), positive("abc")"""),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use selectExpr instead, we'd better keep the same pattern with the other test case in the same file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just clone from the negative test, would change that later.

Row(1, -1, "abc"))
}
}