Skip to content

Commit 6704d0e

Browse files
authored
Merge pull request #1 from DeanChensj/catlog
Change column catalog
2 parents 39fa518 + 75ac84d commit 6704d0e

File tree

8 files changed

+352
-49
lines changed

8 files changed

+352
-49
lines changed

src/catalog/catalog.cpp

+275-42
Large diffs are not rendered by default.

src/catalog/table_catalog.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,15 @@ std::shared_ptr<ColumnCatalogObject> TableCatalogObject::GetColumnObject(
287287
return nullptr;
288288
}
289289

290+
void TableCatalogObject::ChangeColumnName(
291+
const std::vector<oid_t> &columns,
292+
UNUSED_ATTRIBUTE const std::vector<std::string> &names) {
293+
// TODO: Change column_names
294+
// TODO: Change column_objects
295+
TableCatalogObject::EvictColumnObject(columns[0]);
296+
return;
297+
}
298+
290299
TableCatalog *TableCatalog::GetInstance(storage::Database *pg_catalog,
291300
type::AbstractPool *pool,
292301
concurrency::TransactionContext *txn) {
@@ -378,7 +387,8 @@ bool TableCatalog::InsertTable(oid_t table_oid, const std::string &table_name,
378387
* @param txn TransactionContext
379388
* @return Whether deletion is Successful
380389
*/
381-
bool TableCatalog::DeleteTable(oid_t table_oid, concurrency::TransactionContext *txn) {
390+
bool TableCatalog::DeleteTable(oid_t table_oid,
391+
concurrency::TransactionContext *txn) {
382392
oid_t index_offset = IndexId::PRIMARY_KEY; // Index of table_oid
383393
std::vector<type::Value> values;
384394
values.push_back(type::ValueFactory::GetIntegerValue(table_oid).Copy());

src/include/catalog/catalog.h

+25
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <mutex>
1616

1717
#include "catalog/catalog_defaults.h"
18+
#include "catalog/column_catalog.h"
1819
#include "function/functions.h"
1920

2021
namespace peloton {
@@ -176,6 +177,30 @@ class Catalog {
176177
std::shared_ptr<TableCatalogObject> GetTableObject(
177178
oid_t database_oid, oid_t table_oid,
178179
concurrency::TransactionContext *txn);
180+
181+
//===--------------------------------------------------------------------===//
182+
// ALTER TABLE
183+
//===--------------------------------------------------------------------===//
184+
ResultType AlterTable(oid_t database_oid, oid_t table_oid,
185+
std::unique_ptr<catalog::Schema> new_schema,
186+
concurrency::TransactionContext *txn);
187+
188+
ResultType AddColumn(const std::string &database_name,
189+
const std::string &table_name,
190+
const std::vector<Column> &columns,
191+
concurrency::TransactionContext *txn);
192+
193+
ResultType DropColumn(const std::string &database_name,
194+
const std::string &table_name,
195+
const std::vector<Column> &columns,
196+
concurrency::TransactionContext *txn);
197+
198+
ResultType ChangeColumnName(const std::string &database_name,
199+
const std::string &table_name,
200+
const std::vector<Column> &old_columns,
201+
const std::vector<std::string> &names,
202+
concurrency::TransactionContext *txn);
203+
179204
//===--------------------------------------------------------------------===//
180205
// DEPRECATED FUNCTIONS
181206
//===--------------------------------------------------------------------===//

src/include/catalog/schema.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ class Schema : public Printable {
144144
return index;
145145
}
146146

147+
inline void ChangeColumnName(const oid_t column_id, std::string new_name) {
148+
columns[column_id].column_name = new_name;
149+
}
150+
147151
inline oid_t GetUninlinedColumn(const oid_t column_id) const {
148152
return uninlined_columns[column_id];
149153
}
@@ -191,7 +195,7 @@ class Schema : public Printable {
191195
}
192196

193197
// Get the default value for the column
194-
inline type::Value* GetDefaultValue(const oid_t column_id) const {
198+
inline type::Value *GetDefaultValue(const oid_t column_id) const {
195199
for (auto constraint : columns[column_id].GetConstraints()) {
196200
if (constraint.GetType() == ConstraintType::DEFAULT) {
197201
return constraint.getDefaultValue();

src/include/catalog/table_catalog.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class TableCatalogObject {
4545
friend class ColumnCatalog;
4646

4747
public:
48-
TableCatalogObject(executor::LogicalTile *tile, concurrency::TransactionContext *txn,
49-
int tupleId = 0);
48+
TableCatalogObject(executor::LogicalTile *tile,
49+
concurrency::TransactionContext *txn, int tupleId = 0);
5050

5151
public:
5252
// Get indexes
@@ -71,6 +71,9 @@ class TableCatalogObject {
7171
std::shared_ptr<ColumnCatalogObject> GetColumnObject(
7272
const std::string &column_name, bool cached_only = false);
7373

74+
void ChangeColumnName(const std::vector<oid_t> &columns,
75+
const std::vector<std::string> &names);
76+
7477
inline oid_t GetTableOid() { return table_oid; }
7578
inline const std::string &GetTableName() { return table_name; }
7679
inline oid_t GetDatabaseOid() { return database_oid; }
@@ -119,9 +122,10 @@ class TableCatalog : public AbstractCatalog {
119122
~TableCatalog();
120123

121124
// Global Singleton, only the first call requires passing parameters.
122-
static TableCatalog *GetInstance(storage::Database *pg_catalog = nullptr,
123-
type::AbstractPool *pool = nullptr,
124-
concurrency::TransactionContext *txn = nullptr);
125+
static TableCatalog *GetInstance(
126+
storage::Database *pg_catalog = nullptr,
127+
type::AbstractPool *pool = nullptr,
128+
concurrency::TransactionContext *txn = nullptr);
125129

126130
inline oid_t GetNextOid() { return oid_++ | TABLE_OID_MASK; }
127131

src/include/storage/database.h

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class Database : public Printable {
7272
std::string GetDBName();
7373
void setDBName(const std::string &database_name);
7474

75+
storage::DataTable *ReplaceTableWithOid(const oid_t table_oid,
76+
storage::DataTable *new_table);
77+
7578
protected:
7679
//===--------------------------------------------------------------------===//
7780
// MEMBERS

src/include/storage/tile.h

+4
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ class Tile : public Printable {
139139
return schema.GetColumn(column_index).GetName();
140140
}
141141

142+
void ChangeColumnName(const oid_t column_index, const std::string name) {
143+
schema.ChangeColumnName(column_index, name);
144+
}
145+
142146
inline oid_t GetColumnCount() const { return column_count; };
143147

144148
inline TileGroupHeader *GetHeader() const { return tile_group_header; }

src/storage/database.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,25 @@ void Database::setDBName(const std::string &database_name) {
184184
Database::database_name = database_name;
185185
}
186186

187+
storage::DataTable *Database::ReplaceTableWithOid(
188+
const oid_t table_oid, storage::DataTable *new_table) {
189+
{
190+
std::lock_guard<std::mutex> lock(database_mutex);
191+
192+
oid_t table_offset = 0;
193+
for (auto table : tables) {
194+
if (table->GetOid() == table_oid) {
195+
break;
196+
}
197+
table_offset++;
198+
}
199+
PL_ASSERT(table_offset < tables.size());
200+
201+
auto old_table = tables.at(table_offset);
202+
tables[table_offset] = new_table;
203+
return old_table;
204+
}
205+
}
206+
187207
} // namespace storage
188208
} // namespace peloton

0 commit comments

Comments
 (0)