Skip to content

Commit

Permalink
Rename Expression.apply to eval for better readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
rxin committed Apr 7, 2014
1 parent e258e50 commit ea061de
Show file tree
Hide file tree
Showing 23 changed files with 106 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ case class BoundReference(ordinal: Int, baseReference: Attribute)

override def toString = s"$baseReference:$ordinal"

override def apply(input: Row): Any = input(ordinal)
override def eval(input: Row): Any = input(ordinal)
}

class BindReferences[TreeNode <: QueryPlan[TreeNode]] extends Rule[TreeNode] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {
case DoubleType => castToDouble
}

override def apply(input: Row): Any = {
val evaluated = child.apply(input)
override def eval(input: Row): Any = {
val evaluated = child.eval(input)
if (evaluated == null) {
null
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract class Expression extends TreeNode[Expression] {
def references: Set[Attribute]

/** Returns the result of evaluating this expression on a given input Row */
def apply(input: Row = null): EvaluatedType =
def eval(input: Row = null): EvaluatedType =
throw new TreeNodeException(this, s"No function to evaluate expression. type: ${this.nodeName}")

/**
Expand All @@ -73,7 +73,7 @@ abstract class Expression extends TreeNode[Expression] {
*/
@inline
def n1(e: Expression, i: Row, f: ((Numeric[Any], Any) => Any)): Any = {
val evalE = e.apply(i)
val evalE = e.eval(i)
if (evalE == null) {
null
} else {
Expand Down Expand Up @@ -102,11 +102,11 @@ abstract class Expression extends TreeNode[Expression] {
throw new TreeNodeException(this, s"Types do not match ${e1.dataType} != ${e2.dataType}")
}

val evalE1 = e1.apply(i)
val evalE1 = e1.eval(i)
if(evalE1 == null) {
null
} else {
val evalE2 = e2.apply(i)
val evalE2 = e2.eval(i)
if (evalE2 == null) {
null
} else {
Expand Down Expand Up @@ -135,11 +135,11 @@ abstract class Expression extends TreeNode[Expression] {
throw new TreeNodeException(this, s"Types do not match ${e1.dataType} != ${e2.dataType}")
}

val evalE1 = e1.apply(i: Row)
val evalE1 = e1.eval(i: Row)
if(evalE1 == null) {
null
} else {
val evalE2 = e2.apply(i: Row)
val evalE2 = e2.eval(i: Row)
if (evalE2 == null) {
null
} else {
Expand Down Expand Up @@ -168,11 +168,11 @@ abstract class Expression extends TreeNode[Expression] {
throw new TreeNodeException(this, s"Types do not match ${e1.dataType} != ${e2.dataType}")
}

val evalE1 = e1.apply(i)
val evalE1 = e1.eval(i)
if(evalE1 == null) {
null
} else {
val evalE2 = e2.apply(i)
val evalE2 = e2.eval(i)
if (evalE2 == null) {
null
} else {
Expand Down Expand Up @@ -205,11 +205,11 @@ abstract class Expression extends TreeNode[Expression] {
throw new TreeNodeException(this, s"Types do not match ${e1.dataType} != ${e2.dataType}")
}

val evalE1 = e1.apply(i)
val evalE1 = e1.eval(i)
if(evalE1 == null) {
null
} else {
val evalE2 = e2.apply(i)
val evalE2 = e2.eval(i)
if (evalE2 == null) {
null
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ class Projection(expressions: Seq[Expression]) extends (Row => Row) {
this(expressions.map(BindReferences.bindReference(_, inputSchema)))

protected val exprArray = expressions.toArray

def apply(input: Row): Row = {
val outputArray = new Array[Any](exprArray.size)
var i = 0
while (i < exprArray.size) {
outputArray(i) = exprArray(i).apply(input)
outputArray(i) = exprArray(i).eval(input)
i += 1
}
new GenericRow(outputArray)
Expand All @@ -58,7 +59,7 @@ case class MutableProjection(expressions: Seq[Expression]) extends (Row => Row)
def apply(input: Row): Row = {
var i = 0
while (i < exprArray.size) {
mutableRow(i) = exprArray(i).apply(input)
mutableRow(i) = exprArray(i).eval(input)
i += 1
}
mutableRow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ class RowOrdering(ordering: Seq[SortOrder]) extends Ordering[Row] {
var i = 0
while (i < ordering.size) {
val order = ordering(i)
val left = order.child.apply(a)
val right = order.child.apply(b)
val left = order.child.eval(a)
val right = order.child.eval(b)

if (left == null && right == null) {
// Both null, continue looking.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ case class ScalaUdf(function: AnyRef, dataType: DataType, children: Seq[Expressi
def references = children.flatMap(_.references).toSet
def nullable = true

override def apply(input: Row): Any = {
override def eval(input: Row): Any = {
children.size match {
case 1 => function.asInstanceOf[(Any) => Any](children(0).apply(input))
case 1 => function.asInstanceOf[(Any) => Any](children(0).eval(input))
case 2 =>
function.asInstanceOf[(Any, Any) => Any](
children(0).apply(input),
children(1).apply(input))
children(0).eval(input),
children(1).eval(input))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ case class WrapDynamic(children: Seq[Attribute]) extends Expression {
def references = children.toSet
def dataType = DynamicType

override def apply(input: Row): DynamicRow = input match {
override def eval(input: Row): DynamicRow = input match {
// Avoid copy for generic rows.
case g: GenericRow => new DynamicRow(children, g.values)
case otherRowType => new DynamicRow(children, otherRowType.toArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ abstract class AggregateFunction
def dataType = base.dataType

def update(input: Row): Unit
override def apply(input: Row): Any
override def eval(input: Row): Any

// Do we really need this?
def newInstance = makeCopy(productIterator.map { case a: AnyRef => a }.toArray)
Expand Down Expand Up @@ -169,15 +169,15 @@ case class AverageFunction(expr: Expression, base: AggregateExpression)
def this() = this(null, null) // Required for serialization.

private var count: Long = _
private val sum = MutableLiteral(Cast(Literal(0), expr.dataType).apply(EmptyRow))
private val sum = MutableLiteral(Cast(Literal(0), expr.dataType).eval(EmptyRow))
private val sumAsDouble = Cast(sum, DoubleType)



private val addFunction = Add(sum, expr)

override def apply(input: Row): Any =
sumAsDouble.apply(EmptyRow).asInstanceOf[Double] / count.toDouble
override def eval(input: Row): Any =
sumAsDouble.eval(EmptyRow).asInstanceOf[Double] / count.toDouble

def update(input: Row): Unit = {
count += 1
Expand All @@ -191,27 +191,27 @@ case class CountFunction(expr: Expression, base: AggregateExpression) extends Ag
var count: Int = _

def update(input: Row): Unit = {
val evaluatedExpr = expr.map(_.apply(input))
val evaluatedExpr = expr.map(_.eval(input))
if (evaluatedExpr.map(_ != null).reduceLeft(_ || _)) {
count += 1
}
}

override def apply(input: Row): Any = count
override def eval(input: Row): Any = count
}

case class SumFunction(expr: Expression, base: AggregateExpression) extends AggregateFunction {
def this() = this(null, null) // Required for serialization.

private val sum = MutableLiteral(Cast(Literal(0), expr.dataType).apply(null))
private val sum = MutableLiteral(Cast(Literal(0), expr.dataType).eval(null))

private val addFunction = Add(sum, expr)

def update(input: Row): Unit = {
sum.update(addFunction, input)
}

override def apply(input: Row): Any = sum.apply(null)
override def eval(input: Row): Any = sum.eval(null)
}

case class SumDistinctFunction(expr: Expression, base: AggregateExpression)
Expand All @@ -222,13 +222,13 @@ case class SumDistinctFunction(expr: Expression, base: AggregateExpression)
val seen = new scala.collection.mutable.HashSet[Any]()

def update(input: Row): Unit = {
val evaluatedExpr = expr.apply(input)
val evaluatedExpr = expr.eval(input)
if (evaluatedExpr != null) {
seen += evaluatedExpr
}
}

override def apply(input: Row): Any =
override def eval(input: Row): Any =
seen.reduceLeft(base.dataType.asInstanceOf[NumericType].numeric.asInstanceOf[Numeric[Any]].plus)
}

Expand All @@ -240,13 +240,13 @@ case class CountDistinctFunction(expr: Seq[Expression], base: AggregateExpressio
val seen = new scala.collection.mutable.HashSet[Any]()

def update(input: Row): Unit = {
val evaluatedExpr = expr.map(_.apply(input))
val evaluatedExpr = expr.map(_.eval(input))
if (evaluatedExpr.map(_ != null).reduceLeft(_ && _)) {
seen += evaluatedExpr
}
}

override def apply(input: Row): Any = seen.size
override def eval(input: Row): Any = seen.size
}

case class FirstFunction(expr: Expression, base: AggregateExpression) extends AggregateFunction {
Expand All @@ -256,9 +256,9 @@ case class FirstFunction(expr: Expression, base: AggregateExpression) extends Ag

def update(input: Row): Unit = {
if (result == null) {
result = expr.apply(input)
result = expr.eval(input)
}
}

override def apply(input: Row): Any = result
override def eval(input: Row): Any = result
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ case class UnaryMinus(child: Expression) extends UnaryExpression {
def nullable = child.nullable
override def toString = s"-$child"

override def apply(input: Row): Any = {
override def eval(input: Row): Any = {
n1(child, input, _.negate(_))
}
}
Expand All @@ -55,25 +55,25 @@ abstract class BinaryArithmetic extends BinaryExpression {
case class Add(left: Expression, right: Expression) extends BinaryArithmetic {
def symbol = "+"

override def apply(input: Row): Any = n2(input, left, right, _.plus(_, _))
override def eval(input: Row): Any = n2(input, left, right, _.plus(_, _))
}

case class Subtract(left: Expression, right: Expression) extends BinaryArithmetic {
def symbol = "-"

override def apply(input: Row): Any = n2(input, left, right, _.minus(_, _))
override def eval(input: Row): Any = n2(input, left, right, _.minus(_, _))
}

case class Multiply(left: Expression, right: Expression) extends BinaryArithmetic {
def symbol = "*"

override def apply(input: Row): Any = n2(input, left, right, _.times(_, _))
override def eval(input: Row): Any = n2(input, left, right, _.times(_, _))
}

case class Divide(left: Expression, right: Expression) extends BinaryArithmetic {
def symbol = "/"

override def apply(input: Row): Any = dataType match {
override def eval(input: Row): Any = dataType match {
case _: FractionalType => f2(input, left, right, _.div(_, _))
case _: IntegralType => i2(input, left , right, _.quot(_, _))
}
Expand All @@ -83,5 +83,5 @@ case class Divide(left: Expression, right: Expression) extends BinaryArithmetic
case class Remainder(left: Expression, right: Expression) extends BinaryArithmetic {
def symbol = "%"

override def apply(input: Row): Any = i2(input, left, right, _.rem(_, _))
override def eval(input: Row): Any = i2(input, left, right, _.rem(_, _))
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ case class GetItem(child: Expression, ordinal: Expression) extends Expression {

override def toString = s"$child[$ordinal]"

override def apply(input: Row): Any = {
override def eval(input: Row): Any = {
if (child.dataType.isInstanceOf[ArrayType]) {
val baseValue = child.apply(input).asInstanceOf[Seq[_]]
val o = ordinal.apply(input).asInstanceOf[Int]
val baseValue = child.eval(input).asInstanceOf[Seq[_]]
val o = ordinal.eval(input).asInstanceOf[Int]
if (baseValue == null) {
null
} else if (o >= baseValue.size || o < 0) {
Expand All @@ -51,8 +51,8 @@ case class GetItem(child: Expression, ordinal: Expression) extends Expression {
baseValue(o)
}
} else {
val baseValue = child.apply(input).asInstanceOf[Map[Any, _]]
val key = ordinal.apply(input)
val baseValue = child.eval(input).asInstanceOf[Map[Any, _]]
val key = ordinal.eval(input)
if (baseValue == null) {
null
} else {
Expand Down Expand Up @@ -85,8 +85,8 @@ case class GetField(child: Expression, fieldName: String) extends UnaryExpressio

override lazy val resolved = childrenResolved && child.dataType.isInstanceOf[StructType]

override def apply(input: Row): Any = {
val baseValue = child.apply(input).asInstanceOf[Row]
override def eval(input: Row): Any = {
val baseValue = child.eval(input).asInstanceOf[Row]
if (baseValue == null) null else baseValue(ordinal)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ case class Explode(attributeNames: Seq[String], child: Expression)
override def apply(input: Row): TraversableOnce[Row] = {
child.dataType match {
case ArrayType(_) =>
val inputArray = child.apply(input).asInstanceOf[Seq[Any]]
val inputArray = child.eval(input).asInstanceOf[Seq[Any]]
if (inputArray == null) Nil else inputArray.map(v => new GenericRow(Array(v)))
case MapType(_, _) =>
val inputMap = child.apply(input).asInstanceOf[Map[Any,Any]]
val inputMap = child.eval(input).asInstanceOf[Map[Any,Any]]
if (inputMap == null) Nil else inputMap.map { case (k,v) => new GenericRow(Array(k,v)) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ case class Literal(value: Any, dataType: DataType) extends LeafExpression {
override def toString = if (value != null) value.toString else "null"

type EvaluatedType = Any
override def apply(input: Row):Any = value
override def eval(input: Row):Any = value
}

// TODO: Specialize
Expand All @@ -69,8 +69,8 @@ case class MutableLiteral(var value: Any, nullable: Boolean = true) extends Leaf
def references = Set.empty

def update(expression: Expression, input: Row) = {
value = expression.apply(input)
value = expression.eval(input)
}

override def apply(input: Row) = value
override def eval(input: Row) = value
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ case class Alias(child: Expression, name: String)

type EvaluatedType = Any

override def apply(input: Row) = child.apply(input)
override def eval(input: Row) = child.eval(input)

def dataType = child.dataType
def nullable = child.nullable
Expand Down
Loading

0 comments on commit ea061de

Please sign in to comment.