Skip to content

Commit

Permalink
[sql] Rename Expression.apply to eval for better readability.
Browse files Browse the repository at this point in the history
Also used this opportunity to add a bunch of override's and made some members private.

Author: Reynold Xin <[email protected]>

Closes apache#340 from rxin/eval and squashes the following commits:

a7c7ca7 [Reynold Xin] Fixed conflicts in merge.
9069de6 [Reynold Xin] Merge branch 'master' into eval
3ccc313 [Reynold Xin] Merge branch 'master' into eval
1a47e10 [Reynold Xin] Renamed apply to eval for generators and added a bunch of override's.
ea061de [Reynold Xin] Rename Expression.apply to eval for better readability.
  • Loading branch information
rxin authored and pdeyhim committed Jun 25, 2014
1 parent 5e81608 commit f50c3b8
Show file tree
Hide file tree
Showing 24 changed files with 156 additions and 159 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)
}

/**
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 @@ -17,8 +17,8 @@

package org.apache.spark.sql.catalyst.expressions

import org.apache.spark.sql.catalyst.trees
import org.apache.spark.sql.catalyst.errors.TreeNodeException
import org.apache.spark.sql.catalyst.trees
import org.apache.spark.sql.catalyst.trees.TreeNode
import org.apache.spark.sql.catalyst.types.{DataType, FractionalType, IntegralType, NumericType, NativeType}

Expand Down 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 All @@ -231,7 +231,7 @@ abstract class BinaryExpression extends Expression with trees.BinaryNode[Express

override def foldable = left.foldable && right.foldable

def references = left.references ++ right.references
override def references = left.references ++ right.references

override def toString = s"($left $symbol $right)"
}
Expand All @@ -243,5 +243,5 @@ abstract class LeafExpression extends Expression with trees.LeafNode[Expression]
abstract class UnaryExpression extends Expression with trees.UnaryNode[Expression] {
self: Product =>

def references = child.references
override def references = child.references
}
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.length)
var i = 0
while (i < exprArray.length) {
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.length) {
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
Loading

0 comments on commit f50c3b8

Please sign in to comment.