Skip to content

Commit

Permalink
Modify the code of POWER and ABS. Move them to the file arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
bomeng authored and xinyunh committed Aug 29, 2014
1 parent ff8e51e commit 39f0309
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
protected val SUBSTRING = Keyword("SUBSTRING")
protected val SQRT = Keyword("SQRT")
protected val ABS = Keyword("ABS")
protected val POW = Keyword("POW")
protected val POWER = Keyword("POWER")

// Use reflection to find the reserved words defined in this class.
protected val reservedWords =
Expand Down Expand Up @@ -330,6 +332,9 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
} |
SQRT ~> "(" ~> expression <~ ")" ^^ { case exp => Sqrt(exp) } |
ABS ~> "(" ~> expression <~ ")" ^^ { case exp => Abs(exp) } |
(POW | POWER) ~> "(" ~> expression ~ "," ~ expression <~ ")" ^^ {
case s ~ "," ~ p => Power(s,p)
} |
ident ~ "(" ~ repsep(expression, ",") <~ ")" ^^ {
case udfName ~ _ ~ exprs => UnresolvedFunction(udfName, exprs)
}
Expand Down
56 changes: 54 additions & 2 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,58 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
}


test("SPARK-3176 Added Parser of SQL ABS()") {
checkAnswer(
sql("SELECT ABS(-1.3)"),
1.3)
checkAnswer(
sql("SELECT ABS(0.0)"),
0.0)
checkAnswer(
sql("SELECT ABS(2.5)"),
2.5)
}

test("SPARK-3176 Added Parser of SQL POWER()") {
checkAnswer(
sql("SELECT POWER(0, 512.0)"),
0.0)
checkAnswer(
sql("SELECT POW(1.0, 256.0)"),
1.0)
checkAnswer(
sql("SELECT POWER(1, -128)"),
1.0)
checkAnswer(
sql("SELECT POW(-1.0, -63)"),
-1.0)
checkAnswer(
sql("SELECT POWER(-1, 32.0)"),
1.0)
checkAnswer(
sql("SELECT POW(2, 8)"),
256.0)
checkAnswer(
sql("SELECT POWER(0.5, 2)"),
0.25)
checkAnswer(
sql("SELECT POW(2, -2)"),
0.25)
checkAnswer(
sql("SELECT POWER(8, 1)"),
8.0)
checkAnswer(
sql("SELECT POW(16, 0.5)"),
4.0)
}

test("SPARK-3176 Added Parser of SQL LAST()") {
checkAnswer(
sql("SELECT LAST(n) FROM lowerCaseData"),
4)
}


test("SPARK-2041 column name equals tablename") {
checkAnswer(
sql("SELECT tableName FROM tableName"),
Expand All @@ -53,14 +105,14 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
(1 to 100).map(x => Row(math.sqrt(x.toDouble))).toSeq
)
}

test("SQRT with automatic string casts") {
checkAnswer(
sql("SELECT SQRT(CAST(key AS STRING)) FROM testData"),
(1 to 100).map(x => Row(math.sqrt(x.toDouble))).toSeq
)
}

test("SPARK-2407 Added Parser of SQL SUBSTR()") {
checkAnswer(
sql("SELECT substr(tableName, 1, 2) FROM tableName"),
Expand Down

0 comments on commit 39f0309

Please sign in to comment.