Skip to content

Commit

Permalink
Make SQL keywords case-insensitive
Browse files Browse the repository at this point in the history
This is a bit of a hack that allows all variations of a keyword, but it
still seems to produce valid error messages and such.
  • Loading branch information
mateiz committed Mar 21, 2014
1 parent e09139d commit e3ed773
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class SqlParser extends StandardTokenParsers {
}

protected case class Keyword(str: String)
protected implicit def asParser(k: Keyword): Parser[String] = k.str

protected implicit def asParser(k: Keyword): Parser[String] =
allCaseVersions(k.str).map(x => x : Parser[String]).reduce(_ | _)

protected class SqlLexical extends StdLexical {
case class FloatLit(chars: String) extends Token {
Expand Down Expand Up @@ -133,7 +135,17 @@ class SqlParser extends StandardTokenParsers {
.filter(_.getReturnType == classOf[Keyword])
.map(_.invoke(this).asInstanceOf[Keyword])

lexical.reserved ++= reservedWords.map(_.str)
/** Generate all variations of upper and lower case of a given string */
private def allCaseVersions(s: String, prefix: String = ""): Stream[String] = {
if (s == "") {
Stream(prefix + "")
} else {
allCaseVersions(s.tail, prefix + s.head.toLower) ++
allCaseVersions(s.tail, prefix + s.head.toUpper)
}
}

lexical.reserved ++= reservedWords.flatMap(w => allCaseVersions(w.str))

lexical.delimiters += (
"@", "*", "+", "-", "<", "=", "<>", "!=", "<=", ">=", ">", "/", "(", ")",
Expand Down
25 changes: 21 additions & 4 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ class SQLQuerySuite extends QueryTest {
sql(
"""
|SELECT * FROM
| (SELECT * FROM upperCaseData WHERE N <= 4) left FULL OUTER JOIN
| (SELECT * FROM upperCaseData WHERE N >= 3) right
| ON left.N = right.N
| (SELECT * FROM upperCaseData WHERE N <= 4) leftTable FULL OUTER JOIN
| (SELECT * FROM upperCaseData WHERE N >= 3) rightTable
| ON leftTable.N = rightTable.N
""".stripMargin),
(1, "A", null, null) ::
(2, "B", null, null) ::
Expand All @@ -211,4 +211,21 @@ class SQLQuerySuite extends QueryTest {
(null, null, 5, "E") ::
(null, null, 6, "F") :: Nil)
}
}

test("mixed-case keywords") {
checkAnswer(
sql(
"""
|SeleCT * from
| (select * from upperCaseData WherE N <= 4) leftTable fuLL OUtER joiN
| (sElEcT * FROM upperCaseData whERe N >= 3) rightTable
| oN leftTable.N = rightTable.N
""".stripMargin),
(1, "A", null, null) ::
(2, "B", null, null) ::
(3, "C", 3, "C") ::
(4, "D", 4, "D") ::
(null, null, 5, "E") ::
(null, null, 6, "F") :: Nil)
}
}

0 comments on commit e3ed773

Please sign in to comment.