@@ -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