|
25 | 25 | #include "paimon/common/data/blob_utils.h" |
26 | 26 | #include "paimon/common/utils/path_util.h" |
27 | 27 | #include "paimon/core/core_options.h" |
| 28 | +#include "paimon/core/schema/table_schema.h" |
| 29 | +#include "paimon/core/table/system/system_table_schema.h" |
28 | 30 | #include "paimon/defs.h" |
29 | 31 | #include "paimon/fs/file_system.h" |
30 | 32 | #include "paimon/fs/file_system_factory.h" |
@@ -149,6 +151,65 @@ TEST(FileSystemCatalogTest, TestCreateTable) { |
149 | 151 | ArrowSchemaRelease(&schema); |
150 | 152 | } |
151 | 153 |
|
| 154 | +TEST(FileSystemCatalogTest, TestOptionsSystemTableCatalog) { |
| 155 | + std::map<std::string, std::string> options; |
| 156 | + options[Options::FILE_SYSTEM] = "local"; |
| 157 | + options[Options::FILE_FORMAT] = "orc"; |
| 158 | + options["custom.option"] = "custom-value"; |
| 159 | + ASSERT_OK_AND_ASSIGN(auto core_options, CoreOptions::FromMap(options)); |
| 160 | + auto dir = UniqueTestDirectory::Create(); |
| 161 | + ASSERT_TRUE(dir); |
| 162 | + FileSystemCatalog catalog(core_options.GetFileSystem(), dir->Str()); |
| 163 | + ASSERT_OK(catalog.CreateDatabase("db1", options, /*ignore_if_exists=*/true)); |
| 164 | + |
| 165 | + auto typed_schema = arrow::schema({arrow::field("f0", arrow::int32())}); |
| 166 | + ::ArrowSchema schema; |
| 167 | + ASSERT_TRUE(arrow::ExportSchema(*typed_schema, &schema).ok()); |
| 168 | + ASSERT_OK(catalog.CreateTable(Identifier("db1", "tbl1"), &schema, |
| 169 | + /*partition_keys=*/{}, /*primary_keys=*/{}, options, |
| 170 | + /*ignore_if_exists=*/false)); |
| 171 | + ArrowSchemaRelease(&schema); |
| 172 | + |
| 173 | + Identifier options_identifier("db1", "tbl1$options"); |
| 174 | + ASSERT_OK_AND_ASSIGN(bool exists, catalog.TableExists(options_identifier)); |
| 175 | + ASSERT_TRUE(exists); |
| 176 | + ASSERT_OK_AND_ASSIGN(exists, catalog.TableExists(Identifier("db1", "tbl1$unknown"))); |
| 177 | + ASSERT_FALSE(exists); |
| 178 | + ASSERT_OK_AND_ASSIGN(exists, catalog.TableExists(Identifier("db1", "missing$options"))); |
| 179 | + ASSERT_FALSE(exists); |
| 180 | + ASSERT_EQ(catalog.GetTableLocation(options_identifier), |
| 181 | + PathUtil::JoinPath(PathUtil::JoinPath(dir->Str(), "db1.db"), "tbl1$options")); |
| 182 | + |
| 183 | + ASSERT_OK_AND_ASSIGN(std::shared_ptr<Schema> system_schema, |
| 184 | + catalog.LoadTableSchema(options_identifier)); |
| 185 | + ASSERT_TRUE(std::dynamic_pointer_cast<SystemSchema>(system_schema) != nullptr); |
| 186 | + ASSERT_TRUE(std::dynamic_pointer_cast<SystemTableSchema>(system_schema) != nullptr); |
| 187 | + ASSERT_OK_AND_ASSIGN(auto c_schema, system_schema->GetArrowSchema()); |
| 188 | + auto loaded_schema_result = arrow::ImportSchema(c_schema.get()); |
| 189 | + ASSERT_TRUE(loaded_schema_result.ok()) << loaded_schema_result.status().ToString(); |
| 190 | + auto loaded_schema = loaded_schema_result.ValueUnsafe(); |
| 191 | + ASSERT_EQ(loaded_schema->field_names(), (std::vector<std::string>{"key", "value"})); |
| 192 | + ASSERT_EQ(loaded_schema->field(0)->type()->id(), arrow::Type::STRING); |
| 193 | + ASSERT_EQ(loaded_schema->field(1)->type()->id(), arrow::Type::STRING); |
| 194 | + ASSERT_FALSE(loaded_schema->field(0)->nullable()); |
| 195 | + ASSERT_FALSE(loaded_schema->field(1)->nullable()); |
| 196 | + |
| 197 | + ASSERT_OK_AND_ASSIGN(auto system_table, catalog.GetTable(options_identifier)); |
| 198 | + ASSERT_EQ(system_table->Name(), "tbl1$options"); |
| 199 | + ASSERT_NOK_WITH_MSG(catalog.LoadTableSchema(Identifier("db1", "tbl1$unknown")), "not exist"); |
| 200 | + ASSERT_NOK_WITH_MSG(catalog.LoadTableSchema(Identifier("db1", "missing$options")), "not exist"); |
| 201 | + |
| 202 | + ::ArrowSchema system_create_schema; |
| 203 | + ASSERT_TRUE(arrow::ExportSchema(*typed_schema, &system_create_schema).ok()); |
| 204 | + ASSERT_NOK_WITH_MSG( |
| 205 | + catalog.CreateTable(options_identifier, &system_create_schema, {}, {}, options, false), |
| 206 | + "Cannot create table for system table"); |
| 207 | + ArrowSchemaRelease(&system_create_schema); |
| 208 | + ASSERT_NOK_WITH_MSG(catalog.DropTable(options_identifier, false), "Cannot drop system table"); |
| 209 | + ASSERT_NOK_WITH_MSG(catalog.RenameTable(options_identifier, Identifier("db1", "tbl2"), false), |
| 210 | + "Cannot rename system table"); |
| 211 | +} |
| 212 | + |
152 | 213 | TEST(FileSystemCatalogTest, TestCreateTableWithBlob) { |
153 | 214 | std::map<std::string, std::string> options; |
154 | 215 | options[Options::FILE_SYSTEM] = "local"; |
@@ -188,6 +249,8 @@ TEST(FileSystemCatalogTest, TestCreateTableWithBlob) { |
188 | 249 | ASSERT_EQ(table_names[0], "tbl1"); |
189 | 250 | ASSERT_OK_AND_ASSIGN(std::shared_ptr<Schema> table_schema, |
190 | 251 | catalog.LoadTableSchema(Identifier("db1", "tbl1"))); |
| 252 | + ASSERT_TRUE(std::dynamic_pointer_cast<DataSchema>(table_schema) != nullptr); |
| 253 | + ASSERT_TRUE(std::dynamic_pointer_cast<TableSchema>(table_schema) != nullptr); |
191 | 254 | ASSERT_OK_AND_ASSIGN(auto arrow_schema, table_schema->GetArrowSchema()); |
192 | 255 | auto loaded_schema = arrow::ImportSchema(arrow_schema.get()).ValueOrDie(); |
193 | 256 | ASSERT_TRUE(typed_schema.Equals(loaded_schema)); |
@@ -358,11 +421,13 @@ TEST(FileSystemCatalogTest, TestValidateTableSchema) { |
358 | 421 | ASSERT_NOK_WITH_MSG(catalog.LoadTableSchema(Identifier("db0", "tbl0")), |
359 | 422 | "Identifier{database=\'db0\', table=\'tbl0\'} not exist"); |
360 | 423 | ASSERT_OK_AND_ASSIGN(std::shared_ptr<Schema> table_schema, catalog.LoadTableSchema(identifier)); |
361 | | - ASSERT_EQ(0, table_schema->Id()); |
362 | | - ASSERT_EQ(3, table_schema->HighestFieldId()); |
363 | | - ASSERT_EQ(1, table_schema->PartitionKeys().size()); |
364 | | - ASSERT_EQ(0, table_schema->PrimaryKeys().size()); |
365 | | - ASSERT_EQ(-1, table_schema->NumBuckets()); |
| 424 | + auto data_schema = std::dynamic_pointer_cast<DataSchema>(table_schema); |
| 425 | + ASSERT_TRUE(data_schema != nullptr); |
| 426 | + ASSERT_EQ(0, data_schema->Id()); |
| 427 | + ASSERT_EQ(3, data_schema->HighestFieldId()); |
| 428 | + ASSERT_EQ(1, data_schema->PartitionKeys().size()); |
| 429 | + ASSERT_EQ(0, data_schema->PrimaryKeys().size()); |
| 430 | + ASSERT_EQ(-1, data_schema->NumBuckets()); |
366 | 431 | ASSERT_FALSE(table_schema->Comment().has_value()); |
367 | 432 | std::vector<std::string> field_names = table_schema->FieldNames(); |
368 | 433 | std::vector<std::string> expected_field_names = {"f0", "f1", "f2", "f3"}; |
@@ -396,8 +461,7 @@ TEST(FileSystemCatalogTest, TestValidateTableSchema) { |
396 | 461 | ASSERT_NOK_WITH_MSG(catalog.LoadTableSchema(identifier), |
397 | 462 | "Identifier{database=\'db1\', table=\'tbl1\'} not exist"); |
398 | 463 |
|
399 | | - ASSERT_NOK_WITH_MSG(catalog.LoadTableSchema(Identifier("db1", "tbl$11")), |
400 | | - "do not support checking TableSchemaExists for system table."); |
| 464 | + ASSERT_NOK_WITH_MSG(catalog.LoadTableSchema(Identifier("db1", "tbl$11")), "not exist"); |
401 | 465 | ArrowSchemaRelease(&schema); |
402 | 466 | } |
403 | 467 |
|
|
0 commit comments