This is an automated email from the ASF dual-hosted git repository.

wenchen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new 6eb556fa9921 [SPARK-50409][SQL] Fix set statement to ignore `;` at the 
end of `SET;`, `SET -v;` and `SET key;`
6eb556fa9921 is described below

commit 6eb556fa99216fa2ed7a9dcd7d711e097abd4de7
Author: Mihailo Milosevic <[email protected]>
AuthorDate: Wed Dec 25 15:52:46 2024 +0800

    [SPARK-50409][SQL] Fix set statement to ignore `;` at the end of `SET;`, 
`SET -v;` and `SET key;`
    
    ### What changes were proposed in this pull request?
    This PR aims to fix the problem of trailing `;` in set statement.
    
    ### Why are the changes needed?
    We missed to add `;*` in the regex rules in SparkSqlParser.scala.
    Several queries were failing:
    
    ```
    ---------------------------------------
    set;
    ---------------------------------------
    [INVALID_SET_SYNTAX] Expected format is 'SET', 'SET key', or 'SET 
key=value'. If you want to include special characters in key, or include 
semicolon in value, please use backquotes, e.g., SET `key`=`value`. SQLSTATE: 
42000
    == SQL (line 1, position 1) ==
    set;
    ^^^
    ---------------------------------------
    set -v;
    ---------------------------------------
    [INVALID_SET_SYNTAX] Expected format is 'SET', 'SET key', or 'SET 
key=value'. If you want to include special characters in key, or include 
semicolon in value, please use backquotes, e.g., SET `key`=`value`. SQLSTATE: 
42000
    == SQL (line 1, position 1) ==
    set -v;
    ^^^^^^^
    ---------------------------------------
    set spark.sql.ansi.enabled;
    ---------------------------------------
    [INVALID_SET_SYNTAX] Expected format is 'SET', 'SET key', or 'SET 
key=value'. If you want to include special characters in key, or include 
semicolon in value, please use backquotes, e.g., SET `key`=`value`. SQLSTATE: 
42000
    == SQL (line 1, position 1) ==
    set spark.sql.ansi.enabled;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    ```
    
    All of these would get the `;` at the end and fail due to impossible match.
    
    ### Does this PR introduce _any_ user-facing change?
    Yes. This is a bug that we had for a long time.
    
    ### How was this patch tested?
    Added test.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    No.
    
    Closes #48951 from mihailom-db/fixreminder.
    
    Authored-by: Mihailo Milosevic <[email protected]>
    Signed-off-by: Wenchen Fan <[email protected]>
---
 .../scala/org/apache/spark/sql/execution/SparkSqlParser.scala  | 10 +++++-----
 .../org/apache/spark/sql/execution/SparkSqlParserSuite.scala   |  9 +++++++++
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkSqlParser.scala 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkSqlParser.scala
index 9fbe400a555f..e15250eb46b5 100644
--- 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkSqlParser.scala
+++ 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkSqlParser.scala
@@ -63,7 +63,7 @@ class SparkSqlAstBuilder extends AstBuilder {
   import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
 
   private val configKeyValueDef = """([a-zA-Z_\d\\.:]+)\s*=([^;]*);*""".r
-  private val configKeyDef = """([a-zA-Z_\d\\.:]+)$""".r
+  private val configKeyDef = """([a-zA-Z_\d\\.:]+)\s*$""".r
   private val configValueDef = """([^;]*);*""".r
   private val strLiteralDef = """(".*?[^\\]"|'.*?[^\\]'|[^ \n\r\t"']+)""".r
 
@@ -106,14 +106,14 @@ class SparkSqlAstBuilder extends AstBuilder {
         SetCommand(Some(keyStr -> None))
       }
     } else {
-      remainder(ctx.SET.getSymbol).trim match {
+      remainder(ctx.SET.getSymbol).trim.replaceAll(";+$", "") match {
         case configKeyValueDef(key, value) =>
           SetCommand(Some(key -> Option(value.trim)))
         case configKeyDef(key) =>
           SetCommand(Some(key -> None))
-        case s if s == "-v" =>
+        case s if s.trim == "-v" =>
           SetCommand(Some("-v" -> None))
-        case s if s.isEmpty =>
+        case s if s.trim.isEmpty =>
           SetCommand(None)
         case _ => throw 
QueryParsingErrors.unexpectedFormatForSetConfigurationError(ctx)
       }
@@ -146,7 +146,7 @@ class SparkSqlAstBuilder extends AstBuilder {
    */
   override def visitResetConfiguration(
       ctx: ResetConfigurationContext): LogicalPlan = withOrigin(ctx) {
-    remainder(ctx.RESET.getSymbol).trim match {
+    remainder(ctx.RESET.getSymbol).trim.replaceAll(";+$", "") match {
       case configKeyDef(key) =>
         ResetCommand(Some(key))
       case s if s.trim.isEmpty =>
diff --git 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/SparkSqlParserSuite.scala
 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/SparkSqlParserSuite.scala
index d4a0f987d499..acc3cdb01bf3 100644
--- 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/SparkSqlParserSuite.scala
+++ 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/SparkSqlParserSuite.scala
@@ -93,6 +93,15 @@ class SparkSqlParserSuite extends AnalysisTest with 
SharedSparkSession {
       parameters = Map.empty)
   }
 
+  test("SET with semi-colons") {
+    assertEqual(s"SET;", SetCommand(None))
+    assertEqual(s"SET    ;", SetCommand(None))
+    assertEqual(s"SET -v;", SetCommand(Some("-v" -> None)))
+    assertEqual(s"SET -v    ;", SetCommand(Some("-v" -> None)))
+    assertEqual(s"SET spark.sql.ansi.enabled;", 
SetCommand(Some("spark.sql.ansi.enabled" -> None)))
+    assertEqual(s"SET spark.sql.ansi.enabled ;", 
SetCommand(Some("spark.sql.ansi.enabled" -> None)))
+  }
+
   test("Report Error for invalid usage of SET command") {
     assertEqual("SET", SetCommand(None))
     assertEqual("SET -v", SetCommand(Some("-v", None)))


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to