Skip to content

Commit 032d663

Browse files
marmbrusrxin
authored andcommitted
[SQL] Implement between in hql
Author: Michael Armbrust <michael@databricks.com> 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.
1 parent fa6de40 commit 032d663

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ private[hive] object HiveQl {
233233
}
234234
} catch {
235235
case e: Exception => throw new ParseException(sql, e)
236+
case e: NotImplementedError => sys.error(
237+
s"""
238+
|Unsupported language features in query: $sql
239+
|${dumpTree(getAst(sql))}
240+
""".stripMargin)
236241
}
237242
}
238243

@@ -865,6 +870,17 @@ private[hive] object HiveQl {
865870
IsNull(nodeToExpr(child))
866871
case Token("TOK_FUNCTION", Token("IN", Nil) :: value :: list) =>
867872
In(nodeToExpr(value), list.map(nodeToExpr))
873+
case Token("TOK_FUNCTION",
874+
Token("between", Nil) ::
875+
Token("KW_FALSE", Nil) ::
876+
target ::
877+
minValue ::
878+
maxValue :: Nil) =>
879+
880+
val targetExpression = nodeToExpr(target)
881+
And(
882+
GreaterThanOrEqual(targetExpression, nodeToExpr(minValue)),
883+
LessThanOrEqual(targetExpression, nodeToExpr(maxValue)))
868884

869885
/* Boolean Logic */
870886
case Token(AND(), left :: right:: Nil) => And(nodeToExpr(left), nodeToExpr(right))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2 val_2

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import org.apache.spark.sql.hive.test.TestHive._
2424
*/
2525
class HiveQuerySuite extends HiveComparisonTest {
2626

27+
createQueryTest("between",
28+
"SELECT * FROM src WHERE key between 1 and 2"
29+
)
30+
2731
test("Query expressed in SQL") {
2832
assert(sql("SELECT 1").collect() === Array(Seq(1)))
2933
}

0 commit comments

Comments
 (0)