Skip to content

Commit 61668ad

Browse files
ganeshas-dbcloud-fan
authored andcommitted
[SPARK-54030][SQL][FOLLOWUP] Refactor test to use withBasicCatalog for consistency
### What changes were proposed in this pull request? This PR refactors the test "corrupted view metadata: mismatch between viewQueryColumnNames and schema" in `SessionCatalogSuite.scala` to use the `withBasicCatalog` helper method instead of manually creating a `SessionCatalog` instance with `newBasicCatalog()`. ### Why are the changes needed? **Consistency**: All other tests in `SessionCatalogSuite` use the `withBasicCatalog` helper pattern ### Does this PR introduce _any_ user-facing change? No. This is a test-only refactoring with no functional changes. ### How was this patch tested? - Verified no linter errors in the modified file - The test logic remains identical, only the catalog initialization pattern changed - Existing test validates the same corrupted view metadata error message ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Sonnet 4.5 Closes #53126 from ganeshashree/SPARK-54030-2. Authored-by: Ganesha S <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent d299684 commit 61668ad

File tree

1 file changed

+59
-58
lines changed

1 file changed

+59
-58
lines changed

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,66 +2030,67 @@ abstract class SessionCatalogSuite extends AnalysisTest with Eventually {
20302030

20312031
test("corrupted view metadata: mismatch between viewQueryColumnNames and schema") {
20322032
withSQLConf("spark.sql.viewSchemaBinding.enabled" -> "true") {
2033-
val catalog = new SessionCatalog(newBasicCatalog())
2034-
val db = "test_db"
2035-
catalog.createDatabase(newDb(db), ignoreIfExists = false)
2036-
2037-
// First create a base table for the view to reference
2038-
val baseTable = CatalogTable(
2039-
identifier = TableIdentifier("base_table", Some(db)),
2040-
tableType = CatalogTableType.MANAGED,
2041-
storage = CatalogStorageFormat.empty,
2042-
schema = new StructType()
2043-
.add("id", IntegerType)
2044-
.add("name", StringType)
2045-
.add("value", DoubleType)
2046-
)
2047-
catalog.createTable(baseTable, ignoreIfExists = false)
2048-
2049-
// Create a view with corrupted metadata where viewQueryColumnNames length
2050-
// doesn't match schema length
2051-
// We need to set the properties to define viewQueryColumnNames
2052-
val properties = Map(
2053-
"view.query.out.numCols" -> "2",
2054-
"view.query.out.col.0" -> "id",
2055-
"view.query.out.col.1" -> "name",
2056-
"view.schema.mode" -> "binding" // Ensure it's not SchemaEvolution
2057-
)
2058-
val corruptedView = CatalogTable(
2059-
identifier = TableIdentifier("corrupted_view", Some(db)),
2060-
tableType = CatalogTableType.VIEW,
2061-
storage = CatalogStorageFormat.empty,
2062-
schema = new StructType()
2063-
.add("id", IntegerType)
2064-
.add("name", StringType)
2065-
.add("value", DoubleType),
2066-
viewText = Some("SELECT * FROM test_db.base_table"),
2067-
provider = Some("spark"), // Ensure it's not Hive-created
2068-
properties = properties // Only 2 query column names but schema has 3 columns
2069-
)
2070-
2071-
catalog.createTable(corruptedView, ignoreIfExists = false)
2072-
2073-
// Verify the view was created with corrupted metadata
2074-
val retrievedView = catalog.getTableMetadata(TableIdentifier("corrupted_view", Some(db)))
2075-
assert(retrievedView.viewQueryColumnNames.length == 2)
2076-
assert(retrievedView.schema.length == 3)
2033+
withBasicCatalog { catalog =>
2034+
val db = "test_db"
2035+
catalog.createDatabase(newDb(db), ignoreIfExists = false)
2036+
2037+
// First create a base table for the view to reference
2038+
val baseTable = CatalogTable(
2039+
identifier = TableIdentifier("base_table", Some(db)),
2040+
tableType = CatalogTableType.MANAGED,
2041+
storage = CatalogStorageFormat.empty,
2042+
schema = new StructType()
2043+
.add("id", IntegerType)
2044+
.add("name", StringType)
2045+
.add("value", DoubleType)
2046+
)
2047+
catalog.createTable(baseTable, ignoreIfExists = false)
2048+
2049+
// Create a view with corrupted metadata where viewQueryColumnNames length
2050+
// doesn't match schema length
2051+
// We need to set the properties to define viewQueryColumnNames
2052+
val properties = Map(
2053+
"view.query.out.numCols" -> "2",
2054+
"view.query.out.col.0" -> "id",
2055+
"view.query.out.col.1" -> "name",
2056+
"view.schema.mode" -> "binding" // Ensure it's not SchemaEvolution
2057+
)
2058+
val corruptedView = CatalogTable(
2059+
identifier = TableIdentifier("corrupted_view", Some(db)),
2060+
tableType = CatalogTableType.VIEW,
2061+
storage = CatalogStorageFormat.empty,
2062+
schema = new StructType()
2063+
.add("id", IntegerType)
2064+
.add("name", StringType)
2065+
.add("value", DoubleType),
2066+
viewText = Some("SELECT * FROM test_db.base_table"),
2067+
provider = Some("spark"), // Ensure it's not Hive-created
2068+
properties = properties // Only 2 query column names but schema has 3 columns
2069+
)
20772070

2078-
// Attempting to look up the view should throw an assertion error with detailed message
2079-
val exception = intercept[AssertionError] {
2080-
catalog.lookupRelation(TableIdentifier("corrupted_view", Some(db)))
2071+
catalog.createTable(corruptedView, ignoreIfExists = false)
2072+
2073+
// Verify the view was created with corrupted metadata
2074+
val retrievedView = catalog.getTableMetadata(TableIdentifier("corrupted_view", Some(db)))
2075+
assert(retrievedView.viewQueryColumnNames.length == 2)
2076+
assert(retrievedView.schema.length == 3)
2077+
2078+
// Attempting to look up the view should throw an assertion error with detailed message
2079+
val exception = intercept[AssertionError] {
2080+
catalog.lookupRelation(TableIdentifier("corrupted_view", Some(db)))
2081+
}
2082+
2083+
// The expected message pattern allows for optional catalog prefix
2084+
val expectedPattern =
2085+
"assertion failed: Corrupted view metadata detected for view " +
2086+
"(\\`\\w+\\`\\.)?\\`test_db\\`\\.\\`corrupted_view\\`\\. " +
2087+
"The number of view query column names 2 " +
2088+
"does not match the number of columns in the view schema 3\\. " +
2089+
"View query column names: \\[id, name\\], " +
2090+
"View schema columns: \\[id, name, value\\]\\. " +
2091+
"This indicates corrupted view metadata that needs to be repaired\\."
2092+
assert(exception.getMessage.matches(expectedPattern))
20812093
}
2082-
2083-
// The expected message pattern allows for optional catalog prefix
2084-
val expectedPattern =
2085-
"assertion failed: Corrupted view metadata detected for view " +
2086-
"(\\`\\w+\\`\\.)?\\`test_db\\`\\.\\`corrupted_view\\`\\. " +
2087-
"The number of view query column names 2 " +
2088-
"does not match the number of columns in the view schema 3\\. " +
2089-
"View query column names: \\[id, name\\], " +
2090-
"View schema columns: \\[id, name, value\\]\\. " +
2091-
"This indicates corrupted view metadata that needs to be repaired\\."
2092-
assert(exception.getMessage.matches(expectedPattern))
20932094
}
20942095
}
20952096

0 commit comments

Comments
 (0)