diff --git a/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 b/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 index d78f584aa9ae..0dd8725be705 100644 --- a/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 +++ b/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 @@ -1814,7 +1814,7 @@ fragment LETTER ; SIMPLE_COMMENT - : '--' ~[\r\n]* '\r'? '\n'? -> channel(HIDDEN) + : '--' ('\\\n' | ~[\r\n])* '\r'? '\n'? -> channel(HIDDEN) ; BRACKETED_COMMENT diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala index 0a591ad9cde7..a1d88232b037 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala @@ -55,11 +55,16 @@ class PlanParserSuite extends AnalysisTest { With(plan, ctes) } - test("single comment") { + test("single comment case one") { val plan = table("a").select(star()) assertEqual("-- single comment\nSELECT * FROM a", plan) } + test("single comment case two") { + val plan = table("a").select(star()) + assertEqual("-- single comment\\\nwith line continuity\nSELECT * FROM a", plan) + } + test("bracketed comment case one") { val plan = table("a").select(star()) assertEqual( diff --git a/sql/hive-thriftserver/src/main/scala/org/apache/spark/sql/hive/thriftserver/SparkSQLCLIDriver.scala b/sql/hive-thriftserver/src/main/scala/org/apache/spark/sql/hive/thriftserver/SparkSQLCLIDriver.scala index 5ed0cb040748..107cb8c9f814 100644 --- a/sql/hive-thriftserver/src/main/scala/org/apache/spark/sql/hive/thriftserver/SparkSQLCLIDriver.scala +++ b/sql/hive-thriftserver/src/main/scala/org/apache/spark/sql/hive/thriftserver/SparkSQLCLIDriver.scala @@ -513,7 +513,6 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging { var insideComment = false var escape = false var beginIndex = 0 - var endIndex = line.length val ret = new JArrayList[String] for (index <- 0 until line.length) { @@ -539,8 +538,6 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging { } else if (hasNext && line.charAt(index + 1) == '-') { // ignore quotes and ; insideComment = true - // ignore eol - endIndex = index } } else if (line.charAt(index) == ';') { if (insideSingleQuote || insideDoubleQuote || insideComment) { @@ -550,8 +547,11 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging { ret.add(line.substring(beginIndex, index)) beginIndex = index + 1 } - } else { - // nothing to do + } else if (line.charAt(index) == '\n') { + // with a new line the inline comment should end. + if (!escape) { + insideComment = false + } } // set the escape if (escape) { @@ -560,7 +560,7 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging { escape = true } } - ret.add(line.substring(beginIndex, endIndex)) + ret.add(line.substring(beginIndex)) ret } } diff --git a/sql/hive-thriftserver/src/test/scala/org/apache/spark/sql/hive/thriftserver/CliSuite.scala b/sql/hive-thriftserver/src/test/scala/org/apache/spark/sql/hive/thriftserver/CliSuite.scala index c393054051fc..5871728ccd58 100644 --- a/sql/hive-thriftserver/src/test/scala/org/apache/spark/sql/hive/thriftserver/CliSuite.scala +++ b/sql/hive-thriftserver/src/test/scala/org/apache/spark/sql/hive/thriftserver/CliSuite.scala @@ -460,18 +460,20 @@ class CliSuite extends SparkFunSuite with BeforeAndAfterAll with BeforeAndAfterE ) } - test("SPARK-30049 Should not complain for quotes in commented with multi-lines") { + test("SPARK-31102 spark-sql fails to parse when contains comment") { runCliWithin(1.minute)( - """SELECT concat('test', 'comment') -- someone's comment here \\ - | comment continues here with single ' quote \\ - | extra ' \\ - |;""".stripMargin -> "testcomment" + """SELECT concat('test', 'comment'), + | -- someone's comment here + | 2;""".stripMargin -> "testcomment" ) + } + + test("SPARK-30049 Should not complain for quotes in commented with multi-lines") { runCliWithin(1.minute)( - """SELECT concat('test', 'comment') -- someone's comment here \\ - | comment continues here with single ' quote \\ - | extra ' \\ - | ;""".stripMargin -> "testcomment" + """SELECT concat('test', 'comment') -- someone's comment here \ + | comment continues here with single ' quote \ + | extra ' \ + |;""".stripMargin -> "testcomment" ) } }