Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-6740][SQL] correctly parse NOT operator with comparison operations #8617

Closed
wants to merge 1 commit into from
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 @@ -218,7 +218,10 @@ object SqlParser extends AbstractSparkSQLParser with DataTypeParser {
andExpression * (OR ^^^ { (e1: Expression, e2: Expression) => Or(e1, e2) })

protected lazy val andExpression: Parser[Expression] =
comparisonExpression * (AND ^^^ { (e1: Expression, e2: Expression) => And(e1, e2) })
notExpression * (AND ^^^ { (e1: Expression, e2: Expression) => And(e1, e2) })

protected lazy val notExpression: Parser[Expression] =
NOT.? ~ comparisonExpression ^^ { case not ~ e => not.map(_ => Not(e)).getOrElse(e) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call it maybeNot ?


protected lazy val comparisonExpression: Parser[Expression] =
( termExpression ~ ("=" ~> termExpression) ^^ { case e1 ~ e2 => EqualTo(e1, e2) }
Expand Down Expand Up @@ -246,7 +249,6 @@ object SqlParser extends AbstractSparkSQLParser with DataTypeParser {
}
| termExpression <~ IS ~ NULL ^^ { case e => IsNull(e) }
| termExpression <~ IS ~ NOT ~ NULL ^^ { case e => IsNotNull(e) }
| NOT ~> termExpression ^^ {e => Not(e)}
| termExpression
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.apache.spark.sql.catalyst

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.expressions.Attribute
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.catalyst.plans.logical.Command
import org.apache.spark.sql.catalyst.analysis.UnresolvedAlias
import org.apache.spark.sql.catalyst.expressions.{Literal, GreaterThan, Not, Attribute}
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical.{OneRowRelation, Project, LogicalPlan, Command}

private[sql] case class TestCommand(cmd: String) extends LogicalPlan with Command {
override def output: Seq[Attribute] = Seq.empty
Expand Down Expand Up @@ -49,7 +49,7 @@ private[sql] class CaseInsensitiveTestParser extends AbstractSparkSQLParser {
}
}

class SqlParserSuite extends SparkFunSuite {
class SqlParserSuite extends PlanTest {

test("test long keyword") {
val parser = new SuperLongKeywordTestParser
Expand All @@ -63,4 +63,15 @@ class SqlParserSuite extends SparkFunSuite {
assert(TestCommand("NotRealCommand") === parser.parse("execute NotRealCommand"))
assert(TestCommand("NotRealCommand") === parser.parse("exEcute NotRealCommand"))
}

test("test NOT operator with comparison operations") {
val parsed = SqlParser.parse("SELECT NOT TRUE > TRUE")
val expected = Project(
UnresolvedAlias(
Not(
GreaterThan(Literal(true), Literal(true)))
) :: Nil,
OneRowRelation)
comparePlans(parsed, expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.apache.spark.sql.catalyst.util._
/**
* Provides helper methods for comparing plans.
*/
class PlanTest extends SparkFunSuite {
abstract class PlanTest extends SparkFunSuite {
/**
* Since attribute references are given globally unique ids during analysis,
* we must normalize them to check if two different queries are identical.
Expand Down