This is an automated email from the ASF dual-hosted git repository.
dongjoon-hyun 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 718e56f4ea92 [SPARK-60823][SQL] Fix NoSuchElementException when adding
a CHECK constraint to a path
718e56f4ea92 is described below
commit 718e56f4ea92db18a30c24ac79e205d9d4d77070
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Tue Nov 3 10:36:09 2026 -0800
[SPARK-60823][SQL] Fix NoSuchElementException when adding a CHECK
constraint to a path
### What changes were proposed in this pull request?
This PR aims to fix `ALTER TABLE ... ADD CONSTRAINT ... CHECK` to fail with
`TABLE_OR_VIEW_NOT_FOUND` instead of a raw `java.util.NoSuchElementException:
None.get` when the target is a path, like ``parquet.`/some/path` ``.
`ResolveSQLOnFile` now leaves the target of `AddCheckConstraint`
unresolved, so `CheckAnalysis` reports it as a missing table.
### Why are the changes needed?
Unlike the other constraint types, which use `UnresolvedTable`, a CHECK
constraint reads its target through an `UnresolvedRelation` to validate the
existing rows. `ResolveSQLOnFile` resolves it to a `LogicalRelation` without a
catalog table, and `ResolveSessionCatalog` calls `catalogTable.get` on it.
```sql
ALTER TABLE parquet.`/some/path` ADD CONSTRAINT c1 CHECK (id > 0);
```
**BEFORE**
```
java.util.NoSuchElementException: None.get
```
**AFTER**
```
[TABLE_OR_VIEW_NOT_FOUND] The table or view `parquet`.`/some/path` cannot
be found. ...
```
The same statement with `UNIQUE (id)` or `PRIMARY KEY (id)` already fails
with `TABLE_OR_VIEW_NOT_FOUND`, like the other `ALTER TABLE` commands on a path.
### Does this PR introduce _any_ user-facing change?
Yes. The statement fails with `TABLE_OR_VIEW_NOT_FOUND` instead of
`java.util.NoSuchElementException`. It failed before as well.
For the same reason, a CHECK constraint on a missing path or on a non-file
source, like ``jdbc.`/some/path` ``, now fails with `TABLE_OR_VIEW_NOT_FOUND`
instead of `PATH_NOT_FOUND` or `UNSUPPORTED_DATASOURCE_FOR_DIRECT_QUERY`, as
the other constraint types do.
### How was this patch tested?
Pass the CIs with the newly added test case.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
Closes #60041 from dongjoon-hyun/SPARK-60823.
Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
---
.../org/apache/spark/sql/execution/datasources/rules.scala | 10 +++++++++-
.../sql/execution/command/v2/CheckConstraintSuite.scala | 14 ++++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
index 7fbf5e9083a8..4e23db80d017 100644
---
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
+++
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
@@ -117,7 +117,15 @@ class ResolveSQLOnFile(sparkSession: SparkSession) extends
Rule[LogicalPlan] {
dataSource
}
- def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ def apply(plan: LogicalPlan): LogicalPlan = plan match {
+ // Like the other ALTER TABLE commands, which take an `UnresolvedTable`,
ADD CONSTRAINT with a
+ // CHECK constraint does not run on a file. Its target is an
`UnresolvedRelation` only to
+ // validate the existing rows, so leave it unresolved to be reported as
not found.
+ case _: AddCheckConstraint => plan
+ case _ => resolveRelations(plan)
+ }
+
+ private def resolveRelations(plan: LogicalPlan): LogicalPlan = plan
resolveOperators {
case r @ RelationTimeTravel(u: UnresolvedRelation, timestamp, _)
if maybeSQLFile(u) && timestamp.forall(_.resolved) =>
// If we successfully look up the data source, then this is a path-based
table, so we should
diff --git
a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/CheckConstraintSuite.scala
b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/CheckConstraintSuite.scala
index 96049e1d180d..9e44aa9b8bd0 100644
---
a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/CheckConstraintSuite.scala
+++
b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/CheckConstraintSuite.scala
@@ -189,6 +189,20 @@ class CheckConstraintSuite extends QueryTest with
CommandSuiteBase with DDLComma
}
}
+ test("SPARK-60823: Alter table add check constraint on a path") {
+ withTempPath { dir =>
+ spark.range(1).write.parquet(dir.getCanonicalPath)
+ val path = s"parquet.`${dir.getCanonicalPath}`"
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(s"ALTER TABLE $path ADD CONSTRAINT c1 CHECK (id > 0)")
+ },
+ condition = "TABLE_OR_VIEW_NOT_FOUND",
+ parameters = Map("relationName" ->
s"`parquet`.`${dir.getCanonicalPath}`"),
+ context = ExpectedContext(fragment = path))
+ }
+ }
+
private def getCheckConstraint(table: Table): Check = {
assert(table.constraints.length == 1)
assert(table.constraints.head.isInstanceOf[Check])
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]