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] Fix NOT operator precedence. #6326

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 @@ -207,7 +207,12 @@ class 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) })
booleanFactor * (AND ^^^ { (e1: Expression, e2: Expression) => And(e1, e2) })
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of bind the rule booleanFactor with andExpression, a better place probably be expression rule. (https://github.com/smola/spark/blob/feature/operator-precedence/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/SqlParser.scala#L225)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chenghao-intel Not sure how. Adding the NOT clause in the expression rule would break precedence rules. Also, binding expression -> orExpression -> andExpression -> booleanFactor -> comparison is pretty much they way it is expressed in the grammars for standard SQL.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, sorry, my bad, I just picked up some corner case, it works.


protected lazy val booleanFactor: Parser[Expression] =
NOT.? ~ comparisonExpression ^^ {
case notOpt ~ expr => notOpt.map(s => Not(expr)).getOrElse(expr)
}

protected lazy val comparisonExpression: Parser[Expression] =
( termExpression ~ ("=" ~> termExpression) ^^ { case e1 ~ e2 => EqualTo(e1, e2) }
Expand Down Expand Up @@ -235,7 +240,6 @@ class 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 @@ -18,7 +18,10 @@
package org.apache.spark.sql.catalyst

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.analysis.{UnresolvedStar, UnresolvedRelation}
import org.apache.spark.sql.catalyst.expressions.Attribute
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.catalyst.plans.logical.Command

Expand Down Expand Up @@ -51,6 +54,15 @@ private[sql] class CaseInsensitiveTestParser extends AbstractSparkSQLParser {

class SqlParserSuite extends SparkFunSuite {

test("precedence of NOT operator") {
val parser = new SqlParser
val actual = parser.parse("SELECT * FROM t WHERE NOT c IS NULL")
val expected = UnresolvedRelation("t" :: Nil)
.where('c.isNull.unary_!)
.select(UnresolvedStar(None))
assertResult(expected)(actual)
}

test("test long keyword") {
val parser = new SuperLongKeywordTestParser
assert(TestCommand("NotRealCommand") ===
Expand Down