Repository: spark
Updated Branches:
  refs/heads/branch-1.0 ff47cdc0c -> 386b31cbc


[SQL] Implement between in hql

Author: Michael Armbrust <[email protected]>

Closes #804 from marmbrus/between and squashes the following commits:

ae24672 [Michael Armbrust] add golden answer.
d9997ef [Michael Armbrust] Implement between in hql.
9bd4433 [Michael Armbrust] Better error on parse failures.

(cherry picked from commit 032d6632ad4ab88c97c9e568b63169a114220a02)
Signed-off-by: Reynold Xin <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/386b31cb
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/386b31cb
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/386b31cb

Branch: refs/heads/branch-1.0
Commit: 386b31cbc5dd9ef1e9d989a3c6a3ac587c3684c1
Parents: ff47cdc
Author: Michael Armbrust <[email protected]>
Authored: Fri May 16 11:47:00 2014 -0700
Committer: Reynold Xin <[email protected]>
Committed: Fri May 16 11:47:07 2014 -0700

----------------------------------------------------------------------
 .../scala/org/apache/spark/sql/hive/HiveQl.scala    | 16 ++++++++++++++++
 .../between-0-df3cf89fcf2ef64199a582fae14a3321      |  1 +
 .../spark/sql/hive/execution/HiveQuerySuite.scala   |  4 ++++
 3 files changed, 21 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/386b31cb/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala
----------------------------------------------------------------------
diff --git a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala 
b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala
index 1f688fe..93b9057 100644
--- a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala
+++ b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala
@@ -233,6 +233,11 @@ private[hive] object HiveQl {
       }
     } catch {
       case e: Exception => throw new ParseException(sql, e)
+      case e: NotImplementedError => sys.error(
+        s"""
+          |Unsupported language features in query: $sql
+          |${dumpTree(getAst(sql))}
+        """.stripMargin)
     }
   }
 
@@ -865,6 +870,17 @@ private[hive] object HiveQl {
       IsNull(nodeToExpr(child))
     case Token("TOK_FUNCTION", Token("IN", Nil) :: value :: list) =>
       In(nodeToExpr(value), list.map(nodeToExpr))
+    case Token("TOK_FUNCTION",
+           Token("between", Nil) ::
+           Token("KW_FALSE", Nil) ::
+           target ::
+           minValue ::
+           maxValue :: Nil) =>
+
+      val targetExpression = nodeToExpr(target)
+      And(
+        GreaterThanOrEqual(targetExpression, nodeToExpr(minValue)),
+        LessThanOrEqual(targetExpression, nodeToExpr(maxValue)))
 
     /* Boolean Logic */
     case Token(AND(), left :: right:: Nil) => And(nodeToExpr(left), 
nodeToExpr(right))

http://git-wip-us.apache.org/repos/asf/spark/blob/386b31cb/sql/hive/src/test/resources/golden/between-0-df3cf89fcf2ef64199a582fae14a3321
----------------------------------------------------------------------
diff --git 
a/sql/hive/src/test/resources/golden/between-0-df3cf89fcf2ef64199a582fae14a3321 
b/sql/hive/src/test/resources/golden/between-0-df3cf89fcf2ef64199a582fae14a3321
new file mode 100644
index 0000000..dcd1d86
--- /dev/null
+++ 
b/sql/hive/src/test/resources/golden/between-0-df3cf89fcf2ef64199a582fae14a3321
@@ -0,0 +1 @@
+2      val_2

http://git-wip-us.apache.org/repos/asf/spark/blob/386b31cb/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala
----------------------------------------------------------------------
diff --git 
a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala
 
b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala
index 87a92d8..1a2b2f8 100644
--- 
a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala
+++ 
b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala
@@ -24,6 +24,10 @@ import org.apache.spark.sql.hive.test.TestHive._
  */
 class HiveQuerySuite extends HiveComparisonTest {
 
+  createQueryTest("between",
+    "SELECT * FROM src WHERE key between 1 and 2"
+  )
+
   test("Query expressed in SQL") {
     assert(sql("SELECT 1").collect() === Array(Seq(1)))
   }

Reply via email to