You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by we...@apache.org on 2025/11/12 13:50:53 UTC
(spark) branch master updated: [SPARK-54130][SQL] Add detailed error messages for catalog assertion failures
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 0c715b4de87f [SPARK-54130][SQL] Add detailed error messages for catalog assertion failures
0c715b4de87f is described below
commit 0c715b4de87fb7a6da2b9e2cf1a1ca68f37885a1
Author: Ganesha S <ga...@databricks.com>
AuthorDate: Wed Nov 12 05:50:40 2025 -0800
[SPARK-54130][SQL] Add detailed error messages for catalog assertion failures
### What changes were proposed in this pull request?
This PR enhances error reporting in the catalog layer by adding detailed, user-friendly assertion messages for various metadata validation failures. The changes add informative error messages to 11 bare `assert()` statements across the catalog codebase.
**Files modified:**
- `SessionCatalog.scala`: Enhanced 5 assertions with detailed error messages
- `interface.scala`: Enhanced 2 assertions (UnresolvedCatalogRelation, HiveTableRelation)
- `SQLFunction.scala`: Enhanced 2 assertions for function validation
- `InMemoryCatalog.scala`: Enhanced 2 assertions for table operations
- `SessionCatalogSuite.scala`: Added 6 new unit tests to verify error messages
**Enhanced assertions include:**
- Table identifier validation (missing database names)
- SQL function body validation (missing expression/query text)
- SQL function type validation (mismatched function type and return type)
- Function plan method validation (wrong method called for function type)
- Table function return parameter validation
### Why are the changes needed?
Currently, when metadata validation fails, assertions fail with generic "assertion failed" messages that provide no context about:
- Which table/function has the problem
- What the actual vs expected values are
- What specific validation failed
- How to resolve the issue
This makes debugging production issues very difficult, especially for:
- Missing database names in table identifiers
- Malformed SQL function definitions
- Type mismatches in SQL functions
- Internal method call errors
The enhanced error messages provide all necessary information to quickly identify and resolve issues.
### Does this PR introduce _any_ user-facing change?
Yes. Users will now see detailed error messages instead of generic assertion failures.
**Before:**
assertion failed
**After:**
assertion failed: Table identifier test_table is missing database name.
UnresolvedCatalogRelation requires a fully qualified table identifier with database.
**Example for SQL functions:**
assertion failed: SQL function 'test_func' is missing function body.
Either exprText or queryText must be defined. Found: exprText=None, queryText=None.
### How was this patch tested?
Added 6 new unit tests in `SessionCatalogSuite.scala` that:
- Create scenarios that trigger each assertion
- Verify that AssertionError is thrown
- Validate the complete error message matches expected format
**Test coverage:**
- `test("UnresolvedCatalogRelation requires database in identifier")`
- `test("HiveTableRelation requires database in identifier")`
- `test("SQLFunction requires either exprText or queryText")`
- `test("SQLFunction return type must match function type")`
- `test("InMemoryCatalog.createTable requires database in identifier")`
- `test("InMemoryCatalog.alterTable requires database in identifier")`
All existing tests continue to pass.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude 3.5 Sonnet (Anthropic)
Closes #52826 from ganeshashree/SPARK-54130.
Authored-by: Ganesha S <ga...@databricks.com>
Signed-off-by: Wenchen Fan <we...@databricks.com>
---
.../sql/catalyst/catalog/InMemoryCatalog.scala | 10 +-
.../spark/sql/catalyst/catalog/SQLFunction.scala | 11 +-
.../sql/catalyst/catalog/SessionCatalog.scala | 29 +++-
.../spark/sql/catalyst/catalog/interface.scala | 8 +-
.../sql/catalyst/catalog/SessionCatalogSuite.scala | 178 +++++++++++++++++++++
5 files changed, 224 insertions(+), 12 deletions(-)
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/InMemoryCatalog.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/InMemoryCatalog.scala
index 5d0184579faa..f5c732ee1412 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/InMemoryCatalog.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/InMemoryCatalog.scala
@@ -192,7 +192,10 @@ class InMemoryCatalog(
override def createTable(
tableDefinition: CatalogTable,
ignoreIfExists: Boolean): Unit = synchronized {
- assert(tableDefinition.identifier.database.isDefined)
+ assert(tableDefinition.identifier.database.isDefined,
+ "Table identifier " + tableDefinition.identifier.quotedString +
+ " is missing database name. " +
+ "Cannot create table without a database specified.")
val db = tableDefinition.identifier.database.get
requireDbExists(db)
val table = tableDefinition.identifier.table
@@ -313,7 +316,10 @@ class InMemoryCatalog(
}
override def alterTable(tableDefinition: CatalogTable): Unit = synchronized {
- assert(tableDefinition.identifier.database.isDefined)
+ assert(tableDefinition.identifier.database.isDefined,
+ "Table identifier " + tableDefinition.identifier.quotedString +
+ " is missing database name. " +
+ "Cannot alter table without a database specified.")
val db = tableDefinition.identifier.database.get
requireTableExists(db, tableDefinition.identifier.table)
val updatedProperties = tableDefinition.properties.filter(kv => kv._1 != "comment")
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SQLFunction.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SQLFunction.scala
index f2fd3b90f646..84d87fab8b06 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SQLFunction.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SQLFunction.scala
@@ -61,8 +61,15 @@ case class SQLFunction(
owner: Option[String] = None,
createTimeMs: Long = System.currentTimeMillis) extends UserDefinedFunction {
- assert(exprText.nonEmpty || queryText.nonEmpty)
- assert((isTableFunc && returnType.isRight) || (!isTableFunc && returnType.isLeft))
+ assert(exprText.nonEmpty || queryText.nonEmpty,
+ "SQL function '" + name + "' is missing function body. " +
+ "Either exprText or queryText must be defined. " +
+ "Found: exprText=" + exprText + ", queryText=" + queryText + ".")
+ assert((isTableFunc && returnType.isRight) || (!isTableFunc && returnType.isLeft),
+ "SQL function '" + name + "' has mismatched function type and return type. " +
+ "isTableFunc=" + isTableFunc + ", returnType.isRight=" + returnType.isRight + ", " +
+ "returnType.isLeft=" + returnType.isLeft + ". " +
+ "Table functions require Right[StructType] and scalar functions require Left[DataType].")
import SQLFunction._
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala
index c351aacd45a4..be90c7ad3656 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala
@@ -1663,7 +1663,9 @@ class SessionCatalog(
.putString("__funcInputAlias", "true")
.build()
}
- assert(!function.isTableFunc)
+ assert(!function.isTableFunc,
+ "Function '" + function.name + "' is a table function. " +
+ "Use makeSQLTableFunctionPlan() instead of makeSQLFunctionPlan().")
val funcName = function.name.funcName
// Use captured SQL configs when parsing a SQL function.
@@ -1674,7 +1676,10 @@ class SessionCatalog(
val inputParam = function.inputParam
val returnType = function.getScalarFuncReturnType
val (expression, query) = function.getExpressionAndQuery(parser, isTableFunc = false)
- assert(expression.isDefined || query.isDefined)
+ assert(expression.isDefined || query.isDefined,
+ "SQL function '" + function.name + "' could not be parsed. " +
+ "Neither expression nor query could be extracted from function body. " +
+ "exprText=" + function.exprText + ", queryText=" + function.queryText + ".")
// Check function arguments
val paramSize = inputParam.map(_.size).getOrElse(0)
@@ -1763,12 +1768,17 @@ class SessionCatalog(
function: SQLFunction,
input: Seq[Expression],
outputAttrs: Seq[Attribute]): LogicalPlan = {
- assert(function.isTableFunc)
+ assert(function.isTableFunc,
+ "Function '" + function.name + "' is a scalar function. " +
+ "Use makeSQLFunctionPlan() instead of makeSQLTableFunctionPlan().")
val funcName = function.name.funcName
val inputParam = function.inputParam
val returnParam = function.getTableFuncReturnCols
val (_, query) = function.getExpressionAndQuery(parser, isTableFunc = true)
- assert(query.isDefined)
+ assert(query.isDefined,
+ "SQL table function '" + function.name + "' could not be parsed. " +
+ "Query could not be extracted from function body. " +
+ "queryText=" + function.queryText + ".")
// Check function arguments
val paramSize = inputParam.map(_.size).getOrElse(0)
@@ -1807,7 +1817,12 @@ class SessionCatalog(
query.get
}
- assert(returnParam.length == outputAttrs.length)
+ assert(returnParam.length == outputAttrs.length,
+ "SQL table function '" + function.name + "' has mismatched return columns. " +
+ "Expected " + outputAttrs.length + " output attributes but found " +
+ returnParam.length + " return parameters. " +
+ "Return parameters: [" + returnParam.fieldNames.mkString(", ") + "], " +
+ "Output attributes: [" + outputAttrs.map(_.name).mkString(", ") + "].")
val output = returnParam.fields.zipWithIndex.map { case (param, i) =>
// Since we cannot get the output of a unresolved logical plan, we need
// to reference the output column of the lateral join by its position.
@@ -2390,7 +2405,9 @@ class SessionCatalog(
requireTableNotExists(newName)
val oldTable = getTableMetadata(oldName)
if (oldTable.tableType == CatalogTableType.MANAGED) {
- assert(oldName.database.nonEmpty)
+ assert(oldName.database.nonEmpty,
+ "Table identifier " + oldName.quotedString + " is missing database name. " +
+ "Managed tables must have a database defined.")
val databaseLocation =
externalCatalog.getDatabase(oldName.database.get).locationUri
val newTableLocation = new Path(new Path(databaseLocation), format(newName.table))
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/interface.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/interface.scala
index 07d26813be94..eab99a96f4c3 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/interface.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/interface.scala
@@ -1070,7 +1070,9 @@ case class UnresolvedCatalogRelation(
tableMeta: CatalogTable,
options: CaseInsensitiveStringMap = CaseInsensitiveStringMap.empty(),
override val isStreaming: Boolean = false) extends UnresolvedLeafNode {
- assert(tableMeta.identifier.database.isDefined)
+ assert(tableMeta.identifier.database.isDefined,
+ "Table identifier " + tableMeta.identifier.quotedString + " is missing database name. " +
+ "UnresolvedCatalogRelation requires a fully qualified table identifier with database.")
}
/**
@@ -1097,7 +1099,9 @@ case class HiveTableRelation(
tableStats: Option[Statistics] = None,
@transient prunedPartitions: Option[Seq[CatalogTablePartition]] = None)
extends LeafNode with MultiInstanceRelation with NormalizeableRelation {
- assert(tableMeta.identifier.database.isDefined)
+ assert(tableMeta.identifier.database.isDefined,
+ "Table identifier " + tableMeta.identifier.quotedString + " is missing database name. " +
+ "HiveTableRelation requires a fully qualified table identifier with database.")
assert(DataTypeUtils.sameType(tableMeta.partitionSchema, partitionCols.toStructType))
assert(DataTypeUtils.sameType(tableMeta.dataSchema, dataCols.toStructType))
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala
index 309d518f5ef4..92a7154c59cb 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala
@@ -34,6 +34,7 @@ import org.apache.spark.sql.connector.catalog.CatalogManager.SESSION_CATALOG_NAM
import org.apache.spark.sql.connector.catalog.SupportsNamespaces.PROP_OWNER
import org.apache.spark.sql.internal.{SQLConf, StaticSQLConf}
import org.apache.spark.sql.types._
+import org.apache.spark.util.Utils
class InMemorySessionCatalogSuite extends SessionCatalogSuite {
protected val utils = new CatalogTestUtils {
@@ -2091,4 +2092,181 @@ abstract class SessionCatalogSuite extends AnalysisTest with Eventually {
assert(exception.getMessage.matches(expectedPattern))
}
}
+
+ test("UnresolvedCatalogRelation requires database in identifier") {
+ val catalog = new SessionCatalog(newEmptyCatalog())
+ catalog.createDatabase(newDb("default"), ignoreIfExists = true)
+ val db = "test_db"
+ catalog.createDatabase(newDb(db), ignoreIfExists = true)
+
+ // Create a table with database
+ val validTable = CatalogTable(
+ identifier = TableIdentifier("test_table", Some(db)),
+ tableType = CatalogTableType.MANAGED,
+ storage = CatalogStorageFormat.empty,
+ schema = new StructType().add("id", IntegerType)
+ )
+ catalog.createTable(validTable, ignoreIfExists = false)
+
+ // Try to create UnresolvedCatalogRelation without database - should fail
+ val tableMetaWithoutDb = validTable.copy(
+ identifier = TableIdentifier("test_table", None)
+ )
+
+ val exception = intercept[AssertionError] {
+ UnresolvedCatalogRelation(tableMetaWithoutDb)
+ }
+
+ val expectedMessage =
+ "assertion failed: Table identifier `test_table` is missing database name. " +
+ "UnresolvedCatalogRelation requires a fully qualified table identifier with database."
+ assert(exception.getMessage === expectedMessage)
+ }
+
+ test("HiveTableRelation requires database in identifier") {
+ val catalog = new SessionCatalog(newEmptyCatalog())
+ catalog.createDatabase(newDb("default"), ignoreIfExists = true)
+ val db = "test_db"
+ catalog.createDatabase(newDb(db), ignoreIfExists = true)
+
+ // Create a table with database
+ val validTable = CatalogTable(
+ identifier = TableIdentifier("test_table", Some(db)),
+ tableType = CatalogTableType.MANAGED,
+ storage = CatalogStorageFormat.empty,
+ schema = new StructType()
+ .add("id", IntegerType)
+ .add("name", StringType)
+ )
+
+ // Try to create HiveTableRelation without database - should fail
+ val tableMetaWithoutDb = validTable.copy(
+ identifier = TableIdentifier("test_table", None)
+ )
+
+ val exception = intercept[AssertionError] {
+ HiveTableRelation(
+ tableMetaWithoutDb,
+ Seq(AttributeReference("id", IntegerType)()),
+ Seq.empty
+ )
+ }
+
+ val expectedMessage =
+ "assertion failed: Table identifier `test_table` is missing database name. " +
+ "HiveTableRelation requires a fully qualified table identifier with database."
+ assert(exception.getMessage === expectedMessage)
+ }
+
+ test("SQLFunction requires either exprText or queryText") {
+ // Test case 1: Neither exprText nor queryText provided
+ val exception1 = intercept[AssertionError] {
+ SQLFunction(
+ name = FunctionIdentifier("test_func"),
+ inputParam = None,
+ returnType = scala.util.Left(IntegerType),
+ exprText = None,
+ queryText = None,
+ comment = None,
+ deterministic = Some(true),
+ containsSQL = Some(false),
+ isTableFunc = false,
+ properties = Map.empty
+ )
+ }
+
+ val expectedMessage = "assertion failed: SQL function 'test_func' is missing function body. " +
+ "Either exprText or queryText must be defined. " +
+ "Found: exprText=None, queryText=None."
+ assert(exception1.getMessage === expectedMessage)
+ }
+
+ test("SQLFunction return type must match function type") {
+ // Test case: isTableFunc=true but returnType is Left (scalar type)
+ val exception = intercept[AssertionError] {
+ SQLFunction(
+ name = FunctionIdentifier("test_func"),
+ inputParam = None,
+ returnType = scala.util.Left(IntegerType), // Scalar return type
+ exprText = Some("SELECT 1"),
+ queryText = None,
+ comment = None,
+ deterministic = Some(true),
+ containsSQL = Some(true),
+ isTableFunc = true, // But marked as table function
+ properties = Map.empty
+ )
+ }
+
+ val expectedMessage =
+ "assertion failed: SQL function 'test_func' has mismatched function type " +
+ "and return type. " +
+ "isTableFunc=true, returnType.isRight=false, returnType.isLeft=true. " +
+ "Table functions require Right[StructType] and scalar functions require Left[DataType]."
+ assert(exception.getMessage === expectedMessage)
+ }
+
+ test("InMemoryCatalog.createTable requires database in identifier") {
+ val catalog = new InMemoryCatalog()
+ val db = "test_db"
+ val dbDefinition = CatalogDatabase(
+ name = db,
+ description = "test database",
+ locationUri = Utils.createTempDir().toURI,
+ properties = Map.empty
+ )
+ catalog.createDatabase(dbDefinition, ignoreIfExists = false)
+
+ // Try to create table without database - should fail
+ val tableWithoutDb = CatalogTable(
+ identifier = TableIdentifier("test_table", None),
+ tableType = CatalogTableType.MANAGED,
+ storage = CatalogStorageFormat.empty,
+ schema = new StructType().add("id", IntegerType)
+ )
+
+ val exception = intercept[AssertionError] {
+ catalog.createTable(tableWithoutDb, ignoreIfExists = false)
+ }
+
+ val expectedMessage =
+ "assertion failed: Table identifier `test_table` is missing database name. " +
+ "Cannot create table without a database specified."
+ assert(exception.getMessage === expectedMessage)
+ }
+
+ test("InMemoryCatalog.alterTable requires database in identifier") {
+ val catalog = new InMemoryCatalog()
+ val db = "test_db"
+ val dbDefinition = CatalogDatabase(
+ name = db,
+ description = "test database",
+ locationUri = Utils.createTempDir().toURI,
+ properties = Map.empty
+ )
+ catalog.createDatabase(dbDefinition, ignoreIfExists = false)
+
+ // First create a valid table
+ val validTable = CatalogTable(
+ identifier = TableIdentifier("test_table", Some(db)),
+ tableType = CatalogTableType.MANAGED,
+ storage = CatalogStorageFormat.empty,
+ schema = new StructType().add("id", IntegerType)
+ )
+ catalog.createTable(validTable, ignoreIfExists = false)
+
+ // Try to alter table with identifier without database - should fail
+ val tableWithoutDb = validTable.copy(
+ identifier = TableIdentifier("test_table", None)
+ )
+
+ val exception = intercept[AssertionError] {
+ catalog.alterTable(tableWithoutDb)
+ }
+
+ val expectedMessage =
+ "assertion failed: Table identifier `test_table` is missing database name. " +
+ "Cannot alter table without a database specified."
+ assert(exception.getMessage === expectedMessage)
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org