|
15 | 15 | #include "catalog/database_catalog.h" |
16 | 16 | #include "catalog/database_metrics_catalog.h" |
17 | 17 | #include "catalog/index_catalog.h" |
| 18 | +#include "catalog/layout_catalog.h" |
18 | 19 | #include "catalog/query_metrics_catalog.h" |
19 | 20 | #include "catalog/system_catalogs.h" |
20 | 21 | #include "catalog/table_catalog.h" |
@@ -303,5 +304,105 @@ TEST_F(CatalogTests, DroppingCatalog) { |
303 | 304 | EXPECT_NE(nullptr, catalog); |
304 | 305 | } |
305 | 306 |
|
| 307 | +TEST_F(CatalogTests, LayoutCatalogTest) { |
| 308 | + // This test creates a table, changes its layout. |
| 309 | + // Create another additional layout. |
| 310 | + // Ensure that default is not changed. |
| 311 | + // Drops layout and verifies that the default_layout is reset. |
| 312 | + // It also queries pg_layout to ensure that the entry is removed. |
| 313 | + |
| 314 | + auto db_name = "temp_db"; |
| 315 | + auto table_name = "temp_table"; |
| 316 | + auto catalog = catalog::Catalog::GetInstance(); |
| 317 | + // Create database. |
| 318 | + auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); |
| 319 | + auto txn = txn_manager.BeginTransaction(); |
| 320 | + catalog->CreateDatabase(db_name, txn); |
| 321 | + |
| 322 | + // Create table. |
| 323 | + auto val0 = catalog::Column( |
| 324 | + type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), |
| 325 | + "val0", true); |
| 326 | + auto val1 = catalog::Column( |
| 327 | + type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), |
| 328 | + "val1", true); |
| 329 | + auto val2 = catalog::Column( |
| 330 | + type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), |
| 331 | + "val2", true); |
| 332 | + auto val3 = catalog::Column( |
| 333 | + type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), |
| 334 | + "val3", true); |
| 335 | + std::unique_ptr<catalog::Schema> table_schema( |
| 336 | + new catalog::Schema({val0, val1, val2, val3})); |
| 337 | + catalog->CreateTable(db_name, DEFAULT_SCHEMA_NAME, |
| 338 | + table_name, std::move(table_schema), txn); |
| 339 | + txn_manager.CommitTransaction(txn); |
| 340 | + |
| 341 | + txn = txn_manager.BeginTransaction(); |
| 342 | + auto table = catalog->GetTableWithName(db_name, DEFAULT_SCHEMA_NAME, |
| 343 | + table_name, txn); |
| 344 | + auto table_oid = table->GetOid(); |
| 345 | + auto database_oid = table->GetDatabaseOid(); |
| 346 | + txn_manager.CommitTransaction(txn); |
| 347 | + |
| 348 | + // Change default layout. |
| 349 | + |
| 350 | + std::map<oid_t, std::pair<oid_t, oid_t>> default_map; |
| 351 | + default_map[0] = std::make_pair(0, 0); |
| 352 | + default_map[1] = std::make_pair(0, 1); |
| 353 | + default_map[2] = std::make_pair(1, 0); |
| 354 | + default_map[3] = std::make_pair(1, 1); |
| 355 | + |
| 356 | + txn = txn_manager.BeginTransaction(); |
| 357 | + auto default_layout = catalog->CreateDefaultLayout(database_oid, table_oid, |
| 358 | + default_map, txn); |
| 359 | + txn_manager.CommitTransaction(txn); |
| 360 | + EXPECT_NE(nullptr, default_layout); |
| 361 | + |
| 362 | + // Create additional layout. |
| 363 | + std::map<oid_t, std::pair<oid_t, oid_t>> non_default_map; |
| 364 | + non_default_map[0] = std::make_pair(0, 0); |
| 365 | + non_default_map[1] = std::make_pair(0, 1); |
| 366 | + non_default_map[2] = std::make_pair(1, 0); |
| 367 | + non_default_map[3] = std::make_pair(1, 1); |
| 368 | + |
| 369 | + txn = txn_manager.BeginTransaction(); |
| 370 | + auto other_layout = catalog->CreateLayout(database_oid, table_oid, |
| 371 | + non_default_map, txn); |
| 372 | + txn_manager.CommitTransaction(txn); |
| 373 | + |
| 374 | + // Check that the default layout is still the same. |
| 375 | + EXPECT_NE(*other_layout.get(), table->GetDefaultLayout()); |
| 376 | + |
| 377 | + // Drop the default layout. |
| 378 | + auto default_layout_oid = default_layout->GetOid(); |
| 379 | + txn = txn_manager.BeginTransaction(); |
| 380 | + EXPECT_EQ(ResultType::SUCCESS, catalog->DropLayout( |
| 381 | + database_oid, table_oid, default_layout_oid, txn)); |
| 382 | + txn_manager.CommitTransaction(txn); |
| 383 | + |
| 384 | + // Check that default layout is reset and set to row_store. |
| 385 | + EXPECT_NE(*default_layout.get(), table->GetDefaultLayout()); |
| 386 | + EXPECT_TRUE(table->GetDefaultLayout().IsRowStore()); |
| 387 | + |
| 388 | + // Query pg_layout to ensure that the entry is dropped |
| 389 | + txn = txn_manager.BeginTransaction(); |
| 390 | + auto pg_layout = catalog-> |
| 391 | + GetSystemCatalogs(database_oid)->GetLayoutCatalog(); |
| 392 | + EXPECT_EQ(nullptr, |
| 393 | + pg_layout->GetLayoutWithOid(table_oid, default_layout_oid, txn)); |
| 394 | + |
| 395 | + // The additional layout must be present in pg_layout |
| 396 | + auto other_layout_oid = other_layout->GetOid(); |
| 397 | + EXPECT_NE(nullptr, |
| 398 | + pg_layout->GetLayoutWithOid(table_oid, other_layout_oid, txn)); |
| 399 | + txn_manager.CommitTransaction(txn); |
| 400 | + |
| 401 | + // Drop database |
| 402 | + txn = txn_manager.BeginTransaction(); |
| 403 | + catalog->DropDatabaseWithName(db_name, txn); |
| 404 | + txn_manager.CommitTransaction(txn); |
| 405 | +} |
| 406 | + |
306 | 407 | } // namespace test |
307 | 408 | } // namespace peloton |
0 commit comments