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 6b5b412fdaaf1..3c4c4dd0da75c 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 @@ -613,7 +613,7 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging { isStatement = statementInProgress(index) } - if (isStatement) { + if (beginIndex < line.length()) { 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 b404d77881137..11e6578260190 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 @@ -620,4 +620,17 @@ class CliSuite extends SparkFunSuite with BeforeAndAfterAll with Logging { |""".stripMargin -> "SELECT 1" ) } + + test("SPARK-37555: spark-sql should pass last unclosed comment to backend") { + runCliWithin(2.minute)( + // Only unclosed comment. + "/* SELECT /*+ HINT() 4; */;".stripMargin -> "mismatched input ';'", + // Unclosed nested bracketed comment. + "/* SELECT /*+ HINT() 4; */ SELECT 1;".stripMargin -> "1", + // Unclosed comment with query. + "/* Here is a unclosed bracketed comment SELECT 1;"-> "Unclosed bracketed comment", + // Whole comment. + "/* SELECT /*+ HINT() */ 4; */;".stripMargin -> "" + ) + } }