Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ class SqlParser extends AbstractSparkSQLParser {
| termExpression ~ (">=" ~> termExpression) ^^ { case e1 ~ e2 => GreaterThanOrEqual(e1, e2) }
| termExpression ~ ("!=" ~> termExpression) ^^ { case e1 ~ e2 => Not(EqualTo(e1, e2)) }
| termExpression ~ ("<>" ~> termExpression) ^^ { case e1 ~ e2 => Not(EqualTo(e1, e2)) }
| termExpression ~ (BETWEEN ~> termExpression) ~ (AND ~> termExpression) ^^ {
case e ~ el ~ eu => And(GreaterThanOrEqual(e, el), LessThanOrEqual(e, eu))
| termExpression ~ NOT.? ~ (BETWEEN ~> termExpression) ~ (AND ~> termExpression) ^^ {
case e ~ not ~ el ~ eu =>
val betweenExpr: Expression = And(GreaterThanOrEqual(e, el), LessThanOrEqual(e, eu))
not.fold(betweenExpr)(f=> Not(betweenExpr))
}
| termExpression ~ (RLIKE ~> termExpression) ^^ { case e1 ~ e2 => RLike(e1, e2) }
| termExpression ~ (REGEXP ~> termExpression) ^^ { case e1 ~ e2 => RLike(e1, e2) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,4 +909,9 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
""".stripMargin),
(1 to 100).map(i => Seq(i, i, i)))
}

test("SPARK-4154 Query does not work if it has 'not between' in Spark SQL and HQL") {
checkAnswer(sql("SELECT key FROM testData WHERE key not between 0 and 10 order by key"),
(11 to 100).map(i => Seq(i)))
}
}
13 changes: 9 additions & 4 deletions sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -985,15 +985,20 @@ private[hive] object HiveQl {
In(nodeToExpr(value), list.map(nodeToExpr))
case Token("TOK_FUNCTION",
Token(BETWEEN(), Nil) ::
Token("KW_FALSE", Nil) ::
kw ::
target ::
minValue ::
maxValue :: Nil) =>

val targetExpression = nodeToExpr(target)
And(
GreaterThanOrEqual(targetExpression, nodeToExpr(minValue)),
LessThanOrEqual(targetExpression, nodeToExpr(maxValue)))
val betweenExpr =
And(
GreaterThanOrEqual(targetExpression, nodeToExpr(minValue)),
LessThanOrEqual(targetExpression, nodeToExpr(maxValue)))
kw match {
case Token("KW_FALSE", Nil) => betweenExpr
case Token("KW_TRUE", Nil) => Not(betweenExpr)
}

/* Boolean Logic */
case Token(AND(), left :: right:: Nil) => And(nodeToExpr(left), nodeToExpr(right))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,9 @@ class SQLQuerySuite extends QueryTest {
sql("SELECT case when ~1=-2 then 1 else 0 end FROM src"),
sql("SELECT 1 FROM src").collect().toSeq)
}

test("SPARK-4154 Query does not work if it has 'not between' in Spark SQL and HQL") {
checkAnswer(sql("SELECT key FROM src WHERE key not between 0 and 10 order by key"),
sql("SELECT key FROM src WHERE key between 11 and 500 order by key").collect().toSeq)
}
}