Skip to content

Commit

Permalink
Fix issues according to PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chenghao-intel committed Apr 3, 2014
1 parent e529168 commit 9cb505c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ class SqlParser extends StandardTokenParsers {
protected val RIGHT = Keyword("RIGHT")
protected val SELECT = Keyword("SELECT")
protected val STRING = Keyword("STRING")
protected val FLOAT = Keyword("FLOAT")
protected val SMALLINT = Keyword("SMALLINT")
protected val TINYINT = Keyword("TINYINT")
protected val INT = Keyword("INT")
protected val BIGINT = Keyword("BIGINT")
protected val DECIMAL = Keyword("DECIMAL")
protected val DOUBLE = Keyword("DOUBLE")
protected val BOOLEAN = Keyword("BOOLEAN")
protected val BINARY = Keyword("BINARY")
protected val TIMESTAMP = Keyword("TIMESTAMP")
protected val SUM = Keyword("SUM")
protected val TRUE = Keyword("TRUE")
protected val UNION = Keyword("UNION")
Expand Down Expand Up @@ -348,15 +338,5 @@ class SqlParser extends StandardTokenParsers {
literal

protected lazy val dataType: Parser[DataType] =
STRING ^^^ StringType |
FLOAT ^^^ FloatType |
TINYINT ^^^ ByteType |
SMALLINT ^^^ ShortType |
INT ^^^ IntegerType |
BIGINT ^^^ LongType |
DECIMAL ^^^ DecimalType |
DOUBLE ^^^ DoubleType |
BOOLEAN ^^^ BooleanType |
BINARY ^^^ BinaryType |
TIMESTAMP ^^^ TimestampType
STRING ^^^ StringType
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,37 +113,37 @@ package object dsl {
def expr = attr
def attr = analysis.UnresolvedAttribute(s)

/** Creates a new typed attributes of type boolean */
/** Creates a new AttributeReference of type boolean */
def boolean = AttributeReference(s, BooleanType, nullable = false)()

/** Creates a new typed attributes of type byte */
/** Creates a new AttributeReference of type byte */
def byte = AttributeReference(s, ByteType, nullable = false)()

/** Creates a new typed attributes of type short */
/** Creates a new AttributeReference of type short */
def short = AttributeReference(s, ShortType, nullable = false)()

/** Creates a new typed attributes of type int */
/** Creates a new AttributeReference of type int */
def int = AttributeReference(s, IntegerType, nullable = false)()

/** Creates a new typed attributes of type long */
/** Creates a new AttributeReference of type long */
def long = AttributeReference(s, LongType, nullable = false)()

/** Creates a new typed attributes of type float */
/** Creates a new AttributeReference of type float */
def float = AttributeReference(s, FloatType, nullable = false)()

/** Creates a new typed attributes of type double */
/** Creates a new AttributeReference of type double */
def double = AttributeReference(s, DoubleType, nullable = false)()

/** Creates a new typed attributes of type string */
/** Creates a new AttributeReference of type string */
def string = AttributeReference(s, StringType, nullable = false)()

/** Creates a new typed attributes of type decimal */
/** Creates a new AttributeReference of type decimal */
def decimal = AttributeReference(s, DecimalType, nullable = false)()

/** Creates a new typed attributes of type timestamp */
/** Creates a new AttributeReference of type timestamp */
def timestamp = AttributeReference(s, TimestampType, nullable = false)()

/** Creates a new typed attributes of type binary */
/** Creates a new AttributeReference of type binary */
def binary = AttributeReference(s, BinaryType, nullable = false)()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.spark.sql.catalyst.expressions

import java.sql.Timestamp
import java.lang.{NumberFormatException => NFE}

import org.apache.spark.sql.catalyst.types._

Expand Down Expand Up @@ -105,7 +104,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {

def castToLong: Any => Long = child.dataType match {
case StringType => nullOrCast[String, Long](_, s => try s.toLong catch {
case _: NFE => null.asInstanceOf[Long]
case _: NumberFormatException => null.asInstanceOf[Long]
})
case BooleanType => nullOrCast[Boolean, Long](_, b => if(b) 1 else 0)
case TimestampType => nullOrCast[Timestamp, Long](_, t => timestampToDouble(t).toLong)
Expand All @@ -115,7 +114,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {

def castToInt: Any => Int = child.dataType match {
case StringType => nullOrCast[String, Int](_, s => try s.toInt catch {
case _: NFE => null.asInstanceOf[Int]
case _: NumberFormatException => null.asInstanceOf[Int]
})
case BooleanType => nullOrCast[Boolean, Int](_, b => if(b) 1 else 0)
case TimestampType => nullOrCast[Timestamp, Int](_, t => timestampToDouble(t).toInt)
Expand All @@ -125,7 +124,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {

def castToShort: Any => Short = child.dataType match {
case StringType => nullOrCast[String, Short](_, s => try s.toShort catch {
case _: NFE => null.asInstanceOf[Short]
case _: NumberFormatException => null.asInstanceOf[Short]
})
case BooleanType => nullOrCast[Boolean, Short](_, b => if(b) 1 else 0)
case TimestampType => nullOrCast[Timestamp, Short](_, t => timestampToDouble(t).toShort)
Expand All @@ -135,7 +134,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {

def castToByte: Any => Byte = child.dataType match {
case StringType => nullOrCast[String, Byte](_, s => try s.toByte catch {
case _: NFE => null.asInstanceOf[Byte]
case _: NumberFormatException => null.asInstanceOf[Byte]
})
case BooleanType => nullOrCast[Boolean, Byte](_, b => if(b) 1 else 0)
case TimestampType => nullOrCast[Timestamp, Byte](_, t => timestampToDouble(t).toByte)
Expand All @@ -145,7 +144,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {

def castToDecimal: Any => BigDecimal = child.dataType match {
case StringType => nullOrCast[String, BigDecimal](_, s => try s.toDouble catch {
case _: NFE => null.asInstanceOf[BigDecimal]
case _: NumberFormatException => null.asInstanceOf[BigDecimal]
})
case BooleanType => nullOrCast[Boolean, BigDecimal](_, b => if(b) 1 else 0)
case TimestampType => nullOrCast[Timestamp, BigDecimal](_, t => timestampToDouble(t))
Expand All @@ -154,7 +153,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {

def castToDouble: Any => Double = child.dataType match {
case StringType => nullOrCast[String, Double](_, s => try s.toDouble catch {
case _: NFE => null.asInstanceOf[Int]
case _: NumberFormatException => null.asInstanceOf[Int]
})
case BooleanType => nullOrCast[Boolean, Double](_, b => if(b) 1 else 0)
case TimestampType => nullOrCast[Timestamp, Double](_, t => timestampToDouble(t))
Expand All @@ -164,7 +163,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {

def castToFloat: Any => Float = child.dataType match {
case StringType => nullOrCast[String, Float](_, s => try s.toFloat catch {
case _: NFE => null.asInstanceOf[Int]
case _: NumberFormatException => null.asInstanceOf[Int]
})
case BooleanType => nullOrCast[Boolean, Float](_, b => if(b) 1 else 0)
case TimestampType => nullOrCast[Timestamp, Float](_, t => timestampToDouble(t).toFloat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ abstract class Expression extends TreeNode[Expression] {
}
}

/**
* Evaluation helper function for 2 Numeric children expressions. Those expressions are supposed
* to be in the same data type, and also the return type.
* Either one of the expressions result is null, the evaluation result should be null.
*/
@inline
protected final def n2(
i: Row,
Expand Down Expand Up @@ -115,6 +120,11 @@ abstract class Expression extends TreeNode[Expression] {
}
}

/**
* Evaluation helper function for 2 Fractional children expressions. Those expressions are
* supposed to be in the same data type, and also the return type.
* Either one of the expressions result is null, the evaluation result should be null.
*/
@inline
protected final def f2(
i: Row,
Expand Down Expand Up @@ -143,6 +153,11 @@ abstract class Expression extends TreeNode[Expression] {
}
}

/**
* Evaluation helper function for 2 Integral children expressions. Those expressions are
* supposed to be in the same data type, and also the return type.
* Either one of the expressions result is null, the evaluation result should be null.
*/
@inline
protected final def i2(
i: Row,
Expand Down Expand Up @@ -170,7 +185,16 @@ abstract class Expression extends TreeNode[Expression] {
}
}
}


/**
* Evaluation helper function for 2 Comparable children expressions. Those expressions are
* supposed to be in the same data type, and the return type should be Integer:
* Negative value: 1st argument less than 2nd argument
* Zero: 1st argument equals 2nd argument
* Positive value: 1st argument greater than 2nd argument
*
* Either one of the expressions result is null, the evaluation result should be null.
*/
@inline
protected final def c2(
i: Row,
Expand Down

0 comments on commit 9cb505c

Please sign in to comment.