Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 39f1f0f

Browse files
Failing LayoutCatalog test because of deserialization
1 parent 2ecd160 commit 39f1f0f

File tree

8 files changed

+152
-32
lines changed

8 files changed

+152
-32
lines changed

src/catalog/catalog.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ void Catalog::BootstrapSystemCatalogs(storage::Database *database,
181181
system_catalogs->GetTableCatalog()->InsertTable(
182182
COLUMN_CATALOG_OID, COLUMN_CATALOG_NAME, CATALOG_SCHEMA_NAME,
183183
database_oid, pool_.get(), txn);
184+
system_catalogs->GetTableCatalog()->InsertTable(
185+
LAYOUT_CATALOG_OID, LAYOUT_CATALOG_NAME, CATALOG_SCHEMA_NAME,
186+
database_oid, pool_.get(), txn);
184187
}
185188

186189
void Catalog::Bootstrap() {
@@ -557,18 +560,6 @@ ResultType Catalog::CreateIndex(
557560
return ResultType::SUCCESS;
558561
}
559562

560-
/*
561-
*
562-
// Create a new layout
563-
ResultType CreateLayout(oid_t database_oid, oid_t table_oid,
564-
const column_map_type &column_map,
565-
concurrency::TransactionContext *txn);
566-
// Create a new layout and set it as the default for the table
567-
ResultType CreateDefaultLayout(oid_t database_oid, oid_t table_oid,
568-
const column_map_type &column_map,
569-
concurrency::TransactionContext *txn);
570-
*/
571-
572563
/*
573564
* @brief create a new layout for a table
574565
* @param database_oid database to which the table belongs to
@@ -580,8 +571,8 @@ ResultType Catalog::CreateIndex(
580571
*/
581572
std::shared_ptr<const storage::Layout>
582573
Catalog::CreateLayout(oid_t database_oid, oid_t table_oid,
583-
const column_map_type &column_map,
584-
concurrency::TransactionContext *txn) {
574+
const column_map_type &column_map,
575+
concurrency::TransactionContext *txn) {
585576

586577
auto storage_manager = storage::StorageManager::GetInstance();
587578
auto database = storage_manager->GetDatabaseWithOid(database_oid);
@@ -617,8 +608,8 @@ Catalog::CreateLayout(oid_t database_oid, oid_t table_oid,
617608
*/
618609
std::shared_ptr<const storage::Layout>
619610
Catalog::CreateDefaultLayout(oid_t database_oid, oid_t table_oid,
620-
const column_map_type &column_map,
621-
concurrency::TransactionContext *txn) {
611+
const column_map_type &column_map,
612+
concurrency::TransactionContext *txn) {
622613
auto new_layout = CreateLayout(database_oid, table_oid, column_map, txn);
623614
// If the layout creation was successful, set it as the default
624615
if (new_layout != nullptr) {

src/catalog/layout_catalog.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "catalog/catalog.h"
1616
#include "catalog/system_catalogs.h"
17-
#include "catalog/table_catalog.h"
1817
#include "concurrency/transaction_context.h"
1918
#include "storage/data_table.h"
2019
#include "storage/layout.h"
@@ -97,10 +96,10 @@ bool LayoutCatalog::InsertLayout(oid_t table_oid,
9796
auto val3 = type::ValueFactory::GetVarcharValue(layout->SerializeColumnMap(),
9897
nullptr);
9998

100-
tuple->SetValue(ColumnId::TABLE_OID, val0, pool);
101-
tuple->SetValue(ColumnId::LAYOUT_OID, val1, pool);
102-
tuple->SetValue(ColumnId::NUM_COLUMNS, val2, pool);
103-
tuple->SetValue(ColumnId::COLUMN_MAP, val3, pool);
99+
tuple->SetValue(LayoutCatalog::ColumnId::TABLE_OID, val0, pool);
100+
tuple->SetValue(LayoutCatalog::ColumnId::LAYOUT_OID, val1, pool);
101+
tuple->SetValue(LayoutCatalog::ColumnId::NUM_COLUMNS, val2, pool);
102+
tuple->SetValue(LayoutCatalog::ColumnId::COLUMN_MAP, val3, pool);
104103

105104
// Insert the tuple
106105
return InsertTuple(std::move(tuple), txn);
@@ -167,13 +166,16 @@ LayoutCatalog::GetLayouts(oid_t table_oid,
167166

168167
for (auto &tile : (*result_tiles)) {
169168
for (auto tuple_id : *tile) {
170-
oid_t layout_oid = tile->GetValue(tuple_id, ColumnId::LAYOUT_OID)
169+
oid_t layout_oid =
170+
tile->GetValue(tuple_id, LayoutCatalog::ColumnId::LAYOUT_OID)
171171
.GetAs<oid_t>();
172-
oid_t num_coulmns = tile->GetValue(tuple_id, ColumnId::NUM_COLUMNS)
172+
oid_t num_coulmns =
173+
tile->GetValue(tuple_id, LayoutCatalog::ColumnId::NUM_COLUMNS)
173174
.GetAs<oid_t>();
174175

175-
std::string column_map_str = tile->GetValue(
176-
tuple_id, ColumnId::COLUMN_MAP).GetAs<std::string>();
176+
std::string column_map_str =
177+
tile->GetValue(tuple_id, LayoutCatalog::ColumnId::COLUMN_MAP)
178+
.GetAs<std::string>();
177179
auto column_map = storage::Layout::DeserializeColumnMap(num_coulmns,
178180
column_map_str);
179181
auto layout_object =
@@ -185,5 +187,17 @@ LayoutCatalog::GetLayouts(oid_t table_oid,
185187
return table_object->GetLayouts();
186188
}
187189

190+
std::shared_ptr<const storage::Layout>
191+
LayoutCatalog::GetLayoutWithOid(oid_t table_oid, oid_t layout_oid,
192+
concurrency::TransactionContext *txn) {
193+
auto table_layouts = GetLayouts(table_oid, txn);
194+
for (const auto &layout_entry : table_layouts) {
195+
if (layout_entry.second->GetOid() == layout_oid) {
196+
return layout_entry.second;
197+
}
198+
}
199+
return nullptr;
200+
}
201+
188202
} // namespace catalog
189203
} // namespace peloton

src/include/catalog/layout_catalog.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class Layout;
3838
namespace catalog {
3939

4040
class LayoutCatalog : public AbstractCatalog {
41-
friend class TableCatalogObject;
42-
friend class Catalog;
4341

4442
public:
4543
// Global Singleton, only the first call requires passing parameters.
@@ -65,14 +63,18 @@ class LayoutCatalog : public AbstractCatalog {
6563

6664
bool DeleteLayouts(oid_t table_oid, concurrency::TransactionContext *txn);
6765

68-
private:
69-
7066
//===--------------------------------------------------------------------===//
7167
// Read Related API
7268
//===--------------------------------------------------------------------===//
7369
const std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>
7470
GetLayouts(oid_t table_oid, concurrency::TransactionContext *txn);
7571

72+
std::shared_ptr<const storage::Layout>
73+
GetLayoutWithOid(oid_t table_oid, oid_t layout_oid,
74+
concurrency::TransactionContext *txn);
75+
76+
private:
77+
7678
std::unique_ptr<catalog::Schema> InitializeSchema();
7779

7880
enum ColumnId {

src/include/storage/layout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Layout : public Printable {
6060

6161
std::vector<catalog::Schema> GetLayoutSchemas(catalog::Schema* const schema) const;
6262

63-
std::map<oid_t, oid_t> GetColumnLayoutStats() const;
63+
std::map<oid_t, oid_t> GetLayoutStats() const;
6464

6565
std::string SerializeColumnMap() const;
6666

src/storage/layout.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ std::vector<catalog::Schema> Layout::GetLayoutSchemas(
186186
return schemas;
187187
}
188188

189-
std::map<oid_t, oid_t> Layout::GetColumnLayoutStats() const {
189+
std::map<oid_t, oid_t> Layout::GetLayoutStats() const {
190190
std::map<oid_t, oid_t> column_map_stats;
191191

192192
// Cluster per-tile column count

test/catalog/catalog_test.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "catalog/database_catalog.h"
1616
#include "catalog/database_metrics_catalog.h"
1717
#include "catalog/index_catalog.h"
18+
#include "catalog/layout_catalog.h"
1819
#include "catalog/query_metrics_catalog.h"
1920
#include "catalog/system_catalogs.h"
2021
#include "catalog/table_catalog.h"
@@ -303,5 +304,105 @@ TEST_F(CatalogTests, DroppingCatalog) {
303304
EXPECT_NE(nullptr, catalog);
304305
}
305306

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+
306407
} // namespace test
307408
} // namespace peloton

test/include/executor/testing_executor_util.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ class TestingExecutorUtil {
7575
int tuples_per_tilegroup_count = TESTS_TUPLES_PER_TILEGROUP,
7676
bool indexes = true, oid_t table_oid = INVALID_OID);
7777

78-
/** @brief Creates a basic table and adds its entry to the catalog */
78+
/**
79+
* @brief Creates a basic table and adds its entry to the catalog
80+
*
81+
* @return A pointer to the DataTable created.
82+
*/
7983
static storage::DataTable *CreateTableUpdateCatalog(
8084
int tuples_per_tilegroup_count, std::string &db_name);
8185

test/tuning/layout_tuner_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ TEST_F(LayoutTunerTests, BasicTest) {
125125
EXPECT_EQ(new_default_layout.GetTileIdFromColumnId(2),0);
126126
EXPECT_EQ(new_default_layout.GetTileIdFromColumnId(3),1);
127127

128+
// Check the per tile stats of the new layout
129+
// The layout must contain 2 tiles with the following stats
130+
// 0 -> 3
131+
// 1 -> 1
132+
auto layout_stats = new_default_layout.GetLayoutStats();
133+
EXPECT_EQ(layout_stats[0], 3);
134+
EXPECT_EQ(layout_stats[1], 1);
135+
128136
TestingExecutorUtil::DeleteDatabase(db_name);
129137
}
130138

0 commit comments

Comments
 (0)