Skip to content

Commit

Permalink
Add Exceptional case for constant folding
Browse files Browse the repository at this point in the history
  • Loading branch information
chenghao-intel committed Apr 28, 2014
1 parent 3c045c7 commit 536c005
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ case class GetItem(child: Expression, ordinal: Expression) extends Expression {
val children = child :: ordinal :: Nil
/** `Null` is returned for invalid ordinals. */
override def nullable = true
override def foldable = child.foldable && ordinal.foldable
override def references = children.flatMap(_.references).toSet
def dataType = child.dataType match {
case ArrayType(dt) => dt
Expand Down Expand Up @@ -69,7 +70,8 @@ case class GetField(child: Expression, fieldName: String) extends UnaryExpressio
type EvaluatedType = Any

def dataType = field.dataType
def nullable = field.nullable
override def nullable = field.nullable
override def foldable = child.foldable

protected def structType = child.dataType match {
case s: StructType => s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ object ColumnPruning extends Rule[LogicalPlan] {
*/
object ConstantFolding extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
case q: LogicalPlan => q transformExpressionsDown {
case q: LogicalPlan => q transformExpressionsUp {
// Skip redundant folding of literals.
case l: Literal => l
// if it's foldable
case e if e.foldable => Literal(e.eval(null), e.dataType)
case e @ Count(Literal(null, _)) => Literal(null, e.dataType)
case e @ Sum(Literal(null, _)) => Literal(null, e.dataType)
case e @ Average(Literal(null, _)) => Literal(null, e.dataType)
Expand Down Expand Up @@ -124,15 +126,18 @@ object ConstantFolding extends Rule[LogicalPlan] {
case Literal(candidate, _) if(candidate == v) => true
case _ => false
})) => Literal(true, BooleanType)
// TODO put exceptional cases(Unary & Binary Expression) before here.

case e @ SortOrder(_, _) => e
// put exceptional cases(Unary & Binary Expression) before here.
case e: UnaryExpression => e.child match {
case Literal(null, _) => Literal(null, e.dataType)
case _ => e
}
case e: BinaryExpression => e.children match {
case Literal(null, _) :: right :: Nil => Literal(null, e.dataType)
case left :: Literal(null, _) :: Nil => Literal(null, e.dataType)
case _ => e
}
case e if e.foldable => Literal(e.eval(null), e.dataType)
}
}
}
Expand Down

0 comments on commit 536c005

Please sign in to comment.