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 5ed0cb0407484..bffa24c469601 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 @@ -507,6 +507,9 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging { } // Adapted splitSemiColon from Hive 2.3's CliDriver.splitSemiColon. + // Note: [SPARK-31595] if there is a `'` in a double quoted string, or a `"` in a single quoted + // string, the origin implementation from Hive will not drop the trailing semicolon as expected, + // hence we refined this function a little bit. private def splitSemiColon(line: String): JList[String] = { var insideSingleQuote = false var insideDoubleQuote = false @@ -519,13 +522,15 @@ private[hive] class SparkSQLCLIDriver extends CliDriver with Logging { for (index <- 0 until line.length) { if (line.charAt(index) == '\'' && !insideComment) { // take a look to see if it is escaped - if (!escape) { + // See the comment above about SPARK-31595 + if (!escape && !insideDoubleQuote) { // flip the boolean variable insideSingleQuote = !insideSingleQuote } } else if (line.charAt(index) == '\"' && !insideComment) { // take a look to see if it is escaped - if (!escape) { + // See the comment above about SPARK-31595 + if (!escape && !insideSingleQuote) { // flip the boolean variable insideDoubleQuote = !insideDoubleQuote } 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 abefb46b32140..265e7772a691c 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 @@ -500,4 +500,13 @@ class CliSuite extends SparkFunSuite with BeforeAndAfterAll with BeforeAndAfterE | ;""".stripMargin -> "testcomment" ) } + + test("SPARK-31595 Should allow unescaped quote mark in quoted string") { + runCliWithin(1.minute)( + "SELECT '\"legal string a';select 1 + 234;".stripMargin -> "235" + ) + runCliWithin(1.minute)( + "SELECT \"legal 'string b\";select 22222 + 1;".stripMargin -> "22223" + ) + } }