/** * AddSamplesTable - Add a samples table into the 'samples_db'. * The table name is generated by concatenating db_id and table_id with '_'. */ void TupleSamplesStorage::AddSamplesTable( storage::DataTable *data_table, std::vector<std::unique_ptr<storage::Tuple>> &sampled_tuples) { auto schema = data_table->GetSchema(); auto schema_copy = catalog::Schema::CopySchema(schema); std::unique_ptr<catalog::Schema> schema_ptr(schema_copy); auto catalog = catalog::Catalog::GetInstance(); bool is_catalog = false; std::string samples_table_name = GenerateSamplesTableName( data_table->GetDatabaseOid(), data_table->GetOid()); auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); auto txn = txn_manager.BeginTransaction(); catalog->CreateTable(std::string(SAMPLES_DB_NAME), std::string(DEFAULT_SCHEMA_NAME), samples_table_name, std::move(schema_ptr), txn, is_catalog); auto samples_table = catalog->GetTableWithName( std::string(SAMPLES_DB_NAME), std::string(DEFAULT_SCHEMA_NAME), samples_table_name, txn); for (auto &tuple : sampled_tuples) { InsertSampleTuple(samples_table, std::move(tuple), txn); } txn_manager.CommitTransaction(txn); }
/** * GetColumnSamples - Query column samples by db_id, table_id and column_id. */ void TupleSamplesStorage::GetColumnSamples( oid_t database_id, oid_t table_id, oid_t column_id, std::vector<type::Value> &column_samples) { auto catalog = catalog::Catalog::GetInstance(); std::string samples_table_name = GenerateSamplesTableName(database_id, table_id); auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); auto txn = txn_manager.BeginTransaction(); auto data_table = catalog->GetTableWithName(std::string(SAMPLES_DB_NAME), std::string(DEFAULT_SCHEMA_NAME), samples_table_name, txn); std::vector<oid_t> column_ids({column_id}); auto result_tiles = GetTuplesWithSeqScan(data_table, column_ids, txn); txn_manager.CommitTransaction(txn); LOG_DEBUG("Result tiles count: %lu", result_tiles->size()); if (result_tiles->size() != 0) { auto tile = (*result_tiles)[0].get(); LOG_DEBUG("Tuple count: %lu", tile->GetTupleCount()); for (size_t tuple_id = 0; tuple_id < tile->GetTupleCount(); ++tuple_id) { column_samples.push_back(tile->GetValue(tuple_id, 0)); } } }
void CopyExecutor::InitParamColIds() { // If we're going to deserialize prepared statement, get the column ids for // the varbinary columns first auto catalog = catalog::Catalog::GetInstance(); try { auto query_metric_table = catalog->GetTableWithName(CATALOG_DATABASE_NAME, QUERY_METRIC_NAME); auto schema = query_metric_table->GetSchema(); auto &cols = schema->GetColumns(); for (unsigned int i = 0; i < cols.size(); i++) { auto col_name = cols[i].column_name.c_str(); if (std::strcmp(col_name, QUERY_PARAM_TYPE_COL_NAME) == 0) { param_type_col_id = i; } else if (std::strcmp(col_name, QUERY_PARAM_FORMAT_COL_NAME) == 0) { param_format_col_id = i; } else if (std::strcmp(col_name, QUERY_PARAM_VAL_COL_NAME) == 0) { param_val_col_id = i; } else if (std::strcmp(col_name, QUERY_NUM_PARAM_COL_NAME) == 0) { num_param_col_id = i; } } } catch (Exception &e) { e.PrintStackTrace(); } }
storage::DataTable *Manager::GetTableWithName( const oid_t database_oid, const std::string table_name) const { // Lookup DB auto database = GetDatabaseWithOid(database_oid); // Lookup table if (database != nullptr) { auto table = database->GetTableWithName(table_name); return table; } return nullptr; }
TEST_F(CatalogTests, BootstrappingCatalog) { auto catalog = catalog::Catalog::GetInstance(); catalog->Bootstrap(); EXPECT_EQ(1, storage::StorageManager::GetInstance()->GetDatabaseCount()); auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); auto txn = txn_manager.BeginTransaction(); storage::Database *database = catalog->GetDatabaseWithName(CATALOG_DATABASE_NAME, txn); // Check database metric table storage::DataTable *db_metric_table = catalog->GetTableWithName(CATALOG_DATABASE_NAME, CATALOG_SCHEMA_NAME, DATABASE_METRICS_CATALOG_NAME, txn); txn_manager.CommitTransaction(txn); EXPECT_NE(nullptr, database); EXPECT_NE(nullptr, db_metric_table); }
/** * GetTupleSamples - Query tuple samples by db_id and table_id. */ std::unique_ptr<std::vector<std::unique_ptr<executor::LogicalTile>>> TupleSamplesStorage::GetTupleSamples(oid_t database_id, oid_t table_id) { auto catalog = catalog::Catalog::GetInstance(); std::string samples_table_name = GenerateSamplesTableName(database_id, table_id); auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); auto txn = txn_manager.BeginTransaction(); auto data_table = catalog->GetTableWithName(std::string(SAMPLES_DB_NAME), std::string(DEFAULT_SCHEMA_NAME), samples_table_name, txn); auto col_count = data_table->GetSchema()->GetColumnCount(); std::vector<oid_t> column_ids; for (size_t col_id = 0; col_id < col_count; ++col_id) { column_ids.push_back(col_id); } auto result_tiles = GetTuplesWithSeqScan(data_table, column_ids, txn); txn_manager.CommitTransaction(txn); return result_tiles; }
/** * @brief Execute the vacuum stmt. * @param the parse tree * @return true if we handled it correctly, false otherwise */ bool DDLDatabase::ExecVacuumStmt(Node *parsetree) { VacuumStmt *vacuum = (VacuumStmt *)parsetree; std::string relation_name; if (vacuum->relation != NULL) relation_name = vacuum->relation->relname; // Get database oid oid_t database_oid = Bridge::GetCurrentDatabaseOid(); // Get data table based on dabase oid and table name auto &manager = catalog::Manager::GetInstance(); auto db = manager.GetDatabaseWithOid(database_oid); // Update every table and index if (relation_name.empty()) { db->UpdateStats(); } // Otherwise, update the specific table else { oid_t relation_oid = (db->GetTableWithName(relation_name))->GetOid(); db->UpdateStatsWithOid(relation_oid); } return true; }
TEST_F(CatalogTests, LayoutCatalogTest) { // This test creates a table, changes its layout. // Create another additional layout. // Ensure that default is not changed. // Drops layout and verifies that the default_layout is reset. // It also queries pg_layout to ensure that the entry is removed. auto db_name = "temp_db"; auto table_name = "temp_table"; auto catalog = catalog::Catalog::GetInstance(); // Create database. auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); auto txn = txn_manager.BeginTransaction(); EXPECT_EQ(ResultType::SUCCESS, catalog->CreateDatabase(db_name, txn)); // Create table. auto val0 = catalog::Column(type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), "val0", true); auto val1 = catalog::Column(type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), "val1", true); auto val2 = catalog::Column(type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), "val2", true); auto val3 = catalog::Column(type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), "val3", true); std::unique_ptr<catalog::Schema> table_schema( new catalog::Schema({val0, val1, val2, val3})); EXPECT_EQ(ResultType::SUCCESS, catalog->CreateTable(db_name, DEFAULT_SCHEMA_NAME, table_name, std::move(table_schema), txn)); txn_manager.CommitTransaction(txn); txn = txn_manager.BeginTransaction(); auto database_oid = catalog->GetDatabaseObject(db_name, txn)->GetDatabaseOid(); auto table_object = catalog->GetTableObject(db_name, DEFAULT_SCHEMA_NAME, table_name, txn); auto table_oid = table_object->GetTableOid(); auto table = catalog->GetTableWithName(db_name, DEFAULT_SCHEMA_NAME, table_name, txn); txn_manager.CommitTransaction(txn); // Change default layout. std::map<oid_t, std::pair<oid_t, oid_t>> default_map; default_map[0] = std::make_pair(0, 0); default_map[1] = std::make_pair(0, 1); default_map[2] = std::make_pair(1, 0); default_map[3] = std::make_pair(1, 1); txn = txn_manager.BeginTransaction(); auto default_layout = catalog->CreateDefaultLayout(database_oid, table_oid, default_map, txn); txn_manager.CommitTransaction(txn); EXPECT_NE(nullptr, default_layout); // Create additional layout. std::map<oid_t, std::pair<oid_t, oid_t>> non_default_map; non_default_map[0] = std::make_pair(0, 0); non_default_map[1] = std::make_pair(0, 1); non_default_map[2] = std::make_pair(1, 0); non_default_map[3] = std::make_pair(1, 1); txn = txn_manager.BeginTransaction(); auto other_layout = catalog->CreateLayout(database_oid, table_oid, non_default_map, txn); txn_manager.CommitTransaction(txn); // Check that the default layout is still the same. EXPECT_NE(*other_layout.get(), table->GetDefaultLayout()); // Drop the default layout. auto default_layout_oid = default_layout->GetOid(); txn = txn_manager.BeginTransaction(); EXPECT_EQ(ResultType::SUCCESS, catalog->DropLayout(database_oid, table_oid, default_layout_oid, txn)); txn_manager.CommitTransaction(txn); // Check that default layout is reset and set to row_store. EXPECT_NE(*default_layout.get(), table->GetDefaultLayout()); EXPECT_TRUE(table->GetDefaultLayout().IsRowStore()); // Query pg_layout to ensure that the entry is dropped txn = txn_manager.BeginTransaction(); auto pg_layout = catalog->GetSystemCatalogs(database_oid)->GetLayoutCatalog(); EXPECT_EQ(nullptr, pg_layout->GetLayoutWithOid(table_oid, default_layout_oid, txn)); // The additional layout must be present in pg_layout auto other_layout_oid = other_layout->GetOid(); EXPECT_EQ( *(other_layout.get()), *(pg_layout->GetLayoutWithOid(table_oid, other_layout_oid, txn).get())); txn_manager.CommitTransaction(txn); // Drop database txn = txn_manager.BeginTransaction(); catalog->DropDatabaseWithName(db_name, txn); txn_manager.CommitTransaction(txn); }