@@ -180,6 +180,12 @@ TEST(FileSystemCatalogTest, TestCreateTableWithBlob) {
180180 ASSERT_OK_AND_ASSIGN (std::vector<std::string> table_names, catalog.ListTables (" db1" ));
181181 ASSERT_EQ (1 , table_names.size ());
182182 ASSERT_EQ (table_names[0 ], " tbl1" );
183+ ASSERT_OK_AND_ASSIGN (std::optional<std::shared_ptr<Schema>> table_schema,
184+ catalog.LoadTableSchema (Identifier (" db1" , " tbl1" )));
185+ ASSERT_TRUE (table_schema.has_value ());
186+ ASSERT_OK_AND_ASSIGN (auto arrow_schema, (*table_schema)->GetArrowSchema ());
187+ auto loaded_schema = arrow::ImportSchema (arrow_schema.get ()).ValueOrDie ();
188+ ASSERT_TRUE (typed_schema.Equals (loaded_schema));
183189 ArrowSchemaRelease (&schema);
184190}
185191
@@ -309,4 +315,56 @@ TEST(FileSystemCatalogTest, TestInvalidList) {
309315 " do not support listing tables for system database." );
310316}
311317
318+ TEST (FileSystemCatalogTest, TestValidateTableSchema) {
319+ std::map<std::string, std::string> options;
320+ options[Options::FILE_SYSTEM] = " local" ;
321+ options[Options::FILE_FORMAT] = " orc" ;
322+ ASSERT_OK_AND_ASSIGN (auto core_options, CoreOptions::FromMap (options));
323+ auto dir = UniqueTestDirectory::Create ();
324+ ASSERT_TRUE (dir);
325+ FileSystemCatalog catalog (core_options.GetFileSystem (), dir->Str ());
326+ {
327+ ASSERT_OK (catalog.CreateDatabase (" db1" , options, /* ignore_if_exists=*/ true ));
328+ arrow::FieldVector fields = {
329+ arrow::field (" f0" , arrow::utf8 ()),
330+ arrow::field (" f1" , arrow::int32 ()),
331+ arrow::field (" f2" , arrow::int32 ()),
332+ arrow::field (" f3" , arrow::float64 ()),
333+ };
334+ arrow::Schema typed_schema (fields);
335+ ::ArrowSchema schema;
336+ ASSERT_TRUE (arrow::ExportSchema (typed_schema, &schema).ok ());
337+ ASSERT_OK (catalog.CreateTable (Identifier (" db1" , " tbl1" ), &schema, {" f1" }, {}, options,
338+ /* ignore_if_exists=*/ false ));
339+
340+ ASSERT_OK_AND_ASSIGN (std::optional<std::shared_ptr<Schema>> table_schema,
341+ catalog.LoadTableSchema (Identifier (" db0" , " tbl0" )));
342+ ASSERT_FALSE (table_schema.has_value ());
343+ ASSERT_OK_AND_ASSIGN (table_schema, catalog.LoadTableSchema (Identifier (" db1" , " tbl1" )));
344+ ASSERT_TRUE (table_schema.has_value ());
345+ ASSERT_EQ (0 , (*table_schema)->Id ());
346+ ASSERT_EQ (3 , (*table_schema)->HighestFieldId ());
347+ ASSERT_EQ (1 , (*table_schema)->PartitionKeys ().size ());
348+ ASSERT_EQ (0 , (*table_schema)->PrimaryKeys ().size ());
349+ ASSERT_EQ (-1 , (*table_schema)->NumBuckets ());
350+ ASSERT_FALSE ((*table_schema)->Comment ().has_value ());
351+ std::vector<std::string> field_names = (*table_schema)->FieldNames ();
352+ std::vector<std::string> expected_field_names = {" f0" , " f1" , " f2" , " f3" };
353+ ASSERT_EQ (field_names, expected_field_names);
354+
355+ ASSERT_OK_AND_ASSIGN (auto arrow_schema, (*table_schema)->GetArrowSchema ());
356+ auto loaded_schema = arrow::ImportSchema (arrow_schema.get ()).ValueOrDie ();
357+ ASSERT_TRUE (typed_schema.Equals (loaded_schema));
358+
359+ ASSERT_OK_AND_ASSIGN (auto fs, FileSystemFactory::Get (" local" , dir->Str (), {}));
360+ ASSERT_OK (fs->Delete (PathUtil::JoinPath (dir->Str (), " db1.db/tbl1/schema/schema-0" )));
361+ ASSERT_OK_AND_ASSIGN (table_schema, catalog.LoadTableSchema (Identifier (" db1" , " tbl1" )));
362+ ASSERT_FALSE (table_schema.has_value ());
363+
364+ ASSERT_NOK_WITH_MSG (catalog.LoadTableSchema (Identifier (" db1" , " tbl$11" )),
365+ " do not support loading schema for system table." );
366+ ArrowSchemaRelease (&schema);
367+ }
368+ }
369+
312370} // namespace paimon::test
0 commit comments