Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -192,8 +192,17 @@ case object ParseErrorListener extends BaseErrorListener {
charPositionInLine: Int,
msg: String,
e: RecognitionException): Unit = {
val position = Origin(Some(line), Some(charPositionInLine))
throw new ParseException(None, msg, position, position)
val (start, stop) = offendingSymbol match {
case token: CommonToken =>
val start = Origin (Some (line), Some (token.getCharPositionInLine) )
val length = token.getStopIndex - token.getStartIndex + 1
val stop = Origin (Some (line), Some (token.getCharPositionInLine + length) )
(start, stop)
case _ =>
val start = Origin (Some (line), Some (charPositionInLine) )
(start, start)
}
throw new ParseException (None, msg, start, stop)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ import org.apache.spark.SparkFunSuite
* Test various parser errors.
*/
class ErrorParserSuite extends SparkFunSuite {
def intercept(sql: String, line: Int, startPosition: Int, messages: String*): Unit = {
def intercept(sql: String, line: Int, startPosition: Int, stopPosition: Int,
messages: String*): Unit = {
Copy link
Member

Choose a reason for hiding this comment

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

nit:

  def intercept(
      sql: String,
      line: Int,
      startPosition: Int,
      stopPosition: Int,
      messages: String*): Unit = {

val e = intercept[ParseException](CatalystSqlParser.parsePlan(sql))

// Check position.
assert(e.line.isDefined)
assert(e.line.get === line)
assert(e.startPosition.isDefined)
assert(e.startPosition.get === startPosition)
assert(e.stop.startPosition.isDefined)
assert(e.stop.startPosition.get === stopPosition)

// Check messages.
val error = e.getMessage
Expand All @@ -39,26 +42,27 @@ class ErrorParserSuite extends SparkFunSuite {
}

test("no viable input") {
intercept("select ((r + 1) ", 1, 16, "no viable alternative at input", "----------------^^^")
intercept("select ((r + 1) ", 1, 16, 16,
"no viable alternative at input", "----------------^^^")
}

test("extraneous input") {
intercept("select 1 1", 1, 9, "extraneous input '1' expecting", "---------^^^")
intercept("select *\nfrom r as q t", 2, 12, "extraneous input", "------------^^^")
intercept("select 1 1", 1, 9, 10, "extraneous input '1' expecting", "---------^^^")
intercept("select *\nfrom r as q t", 2, 12, 13, "extraneous input", "------------^^^")
}

test("mismatched input") {
intercept("select * from r order by q from t", 1, 27,
intercept("select * from r order by q from t", 1, 27, 31,
"mismatched input",
"---------------------------^^^")
intercept("select *\nfrom r\norder by q\nfrom t", 4, 0, "mismatched input", "^^^")
intercept("select *\nfrom r\norder by q\nfrom t", 4, 0, 4, "mismatched input", "^^^")
}

test("semantic errors") {
intercept("select *\nfrom r\norder by q\ncluster by q", 3, 0,
intercept("select *\nfrom r\norder by q\ncluster by q", 3, 0, 11,
"Combination of ORDER BY/SORT BY/DISTRIBUTE BY/CLUSTER BY is not supported",
"^^^")
intercept("select * from r except all select * from t", 1, 0,
intercept("select * from r except all select * from t", 1, 0, 41,
"EXCEPT ALL is not supported",
"^^^")
}
Expand Down