szehon-ho commented on code in PR #56472:
URL: https://github.com/apache/spark/pull/56472#discussion_r3417822891


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala:
##########
@@ -947,6 +947,20 @@ trait CheckAnalysis extends LookupCatalog with 
QueryErrorsBase with PlanToString
               messageParameters = Map("checkCondition" -> 
a.checkConstraint.condition)
             )
 
+          // Spark currently only supports session/temporary SQL variables
+          // (TempVariableManager); every VariableReference is therefore 
session-scoped and
+          // invalid in a persisted CHECK constraint. If persistent SQL 
variables are added

Review Comment:
   nit: the comment is too wordy, i would simplify it  (ie, no need to mention 
the permanent variables that dont exist yet)



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/CheckConstraintSuite.scala:
##########
@@ -78,6 +78,93 @@ class CheckConstraintSuite extends QueryTest with 
CommandSuiteBase with DDLComma
     }
   }
 
+  test("SPARK-57379: Temporary variable in check constraint -- alter table") {
+    withNamespaceAndTable("ns", "tbl", nonPartitionCatalog) { t =>
+      sql(s"CREATE TABLE $t (i INT) $defaultUsing")
+      withSessionVariable("my_var") {
+        sql("DECLARE OR REPLACE VARIABLE my_var INT DEFAULT 1")
+        Seq(
+          s"ALTER TABLE $t ADD CONSTRAINT c1 CHECK (i > my_var)",
+          s"ALTER TABLE $t ADD CONSTRAINT c1 CHECK (i > CAST(my_var AS 
BIGINT))"
+        ).foreach { query =>
+          val error = intercept[AnalysisException] {
+            sql(query)
+          }
+          checkError(
+            exception = error,
+            condition = "INVALID_TEMP_OBJ_REFERENCE",
+            sqlState = "42K0F",
+            parameters = Map(
+              "obj" -> "CHECK CONSTRAINT",
+              "objName" -> "`c1`",
+              "tempObj" -> "VARIABLE",
+              "tempObjName" -> "`my_var`")
+          )
+        }
+      }
+    }
+  }
+
+  test("SPARK-57379: Temporary variable in check constraint -- create table") {
+    withSessionVariable("my_var") {
+      sql("DECLARE OR REPLACE VARIABLE my_var INT DEFAULT 1")
+      Seq(
+        ("CREATE TABLE t(i INT CHECK (i > my_var))", "``"),
+        ("CREATE TABLE t(i INT, CONSTRAINT c1 CHECK (i > my_var))", "`c1`"),
+        ("REPLACE TABLE t(i INT CHECK (i > my_var))", "``"),
+        ("REPLACE TABLE t(i INT, CONSTRAINT c1 CHECK (i > my_var))", "`c1`"),
+        ("CREATE TABLE t(i INT CHECK (i > CAST(my_var AS BIGINT)))", "``")
+      ).foreach { case (query, expectedObjName) =>
+        withTable("t") {
+          val error = intercept[AnalysisException] {
+            sql(query)
+          }
+          checkError(
+            exception = error,
+            condition = "INVALID_TEMP_OBJ_REFERENCE",
+            sqlState = "42K0F",
+            parameters = Map(
+              "obj" -> "CHECK CONSTRAINT",
+              "objName" -> expectedObjName,
+              "tempObj" -> "VARIABLE",
+              "tempObjName" -> "`my_var`")
+          )
+        }
+      }
+    }
+  }
+
+  test("SPARK-57379: Temporary variable in check constraint -- qualified 
variable name") {
+    withNamespaceAndTable("ns", "tbl", nonPartitionCatalog) { t =>
+      sql(s"CREATE TABLE $t (i INT) $defaultUsing")
+      withSessionVariable("my_var") {
+        sql("DECLARE OR REPLACE VARIABLE my_var INT DEFAULT 1")
+        val error = intercept[AnalysisException] {
+          sql(s"ALTER TABLE $t ADD CONSTRAINT c1 CHECK (i > session.my_var)")
+        }
+        checkError(
+          exception = error,
+          condition = "INVALID_TEMP_OBJ_REFERENCE",
+          sqlState = "42K0F",
+          parameters = Map(
+            "obj" -> "CHECK CONSTRAINT",
+            "objName" -> "`c1`",
+            "tempObj" -> "VARIABLE",
+            "tempObjName" -> "`session`.`my_var`")
+        )
+      }
+    }
+  }
+
+  test("SPARK-57379: Temporary variable in check constraint -- happy path 
without variable") {
+    withNamespaceAndTable("ns", "tbl", nonPartitionCatalog) { t =>
+      sql(s"CREATE TABLE $t (i INT, CONSTRAINT c1 CHECK (i > 0)) 
$defaultUsing")
+      sql(s"ALTER TABLE $t ADD CONSTRAINT c2 CHECK (i > -1)")
+    }
+  }
+
+  // CTAS/RTAS cannot carry CHECK constraints -- the parser rejects them -- so 
no coverage needed.

Review Comment:
   remove the comment



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to