Skip to content

Commit

Permalink
support using keyword in column name
Browse files Browse the repository at this point in the history
  • Loading branch information
cloud-fan committed Jul 6, 2015
1 parent 86768b7 commit e7b49bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,18 @@ class SqlParser extends AbstractSparkSQLParser with DataTypeParser {
throw new AnalysisException(s"invalid function approximate($floatLit) $udfName")
}
}
| CASE ~> expression.? ~ rep1(WHEN ~> expression ~ (THEN ~> expression)) ~
(ELSE ~> expression).? <~ END ^^ {
case casePart ~ altPart ~ elsePart =>
val branches = altPart.flatMap { case whenExpr ~ thenExpr =>
Seq(whenExpr, thenExpr)
} ++ elsePart
casePart.map(CaseKeyWhen(_, branches)).getOrElse(CaseWhen(branches))
}
)
| CASE ~> whenThenElse ^^ CaseWhen
| CASE ~> expression ~ whenThenElse ^^
{ case keyPart ~ branches => CaseKeyWhen(keyPart, branches) }
)

protected lazy val whenThenElse: Parser[List[Expression]] =
rep1(WHEN ~> expression ~ (THEN ~> expression)) ~ (ELSE ~> expression).? <~ END ^^ {
case altPart ~ elsePart =>
altPart.flatMap { case whenExpr ~ thenExpr =>
Seq(whenExpr, thenExpr)
} ++ elsePart
}

protected lazy val cast: Parser[Expression] =
CAST ~ "(" ~> expression ~ (AS ~> dataType) <~ ")" ^^ {
Expand Down Expand Up @@ -354,6 +357,11 @@ class SqlParser extends AbstractSparkSQLParser with DataTypeParser {
protected lazy val signedPrimary: Parser[Expression] =
sign ~ primary ^^ { case s ~ e => if (s == "-") UnaryMinus(e) else e}

protected lazy val attributeName: Parser[String] = acceptMatch("attribute name", {
case lexical.Identifier(str) => str
case lexical.Keyword(str) if !lexical.delimiters.contains(str) => str
})

protected lazy val primary: PackratParser[Expression] =
( literal
| expression ~ ("[" ~> expression <~ "]") ^^
Expand All @@ -364,9 +372,9 @@ class SqlParser extends AbstractSparkSQLParser with DataTypeParser {
| "(" ~> expression <~ ")"
| function
| dotExpressionHeader
| ident ^^ {case i => UnresolvedAttribute.quoted(i)}
| signedPrimary
| "~" ~> expression ^^ BitwiseNot
| attributeName ^^ UnresolvedAttribute.quoted
)

protected lazy val dotExpressionHeader: Parser[Expression] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1458,4 +1458,13 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll with SQLTestUtils {
checkAnswer(sql("SELECT * FROM t ORDER BY NULL"), Seq(Row(1, 2), Row(1, 2)))
}
}

test("SPARK-8837: use keyword in column name") {
withTempTable("t") {
val df = Seq(1 -> "a").toDF("count", "sort")
checkAnswer(df.filter("count > 0"), Row(1, "a"))
df.registerTempTable("t")
checkAnswer(sql("select count, sort from t"), Row(1, "a"))
}
}
}

0 comments on commit e7b49bb

Please sign in to comment.