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

Commit 70dc9cc

Browse files
authored
Merge branch 'master' into optimizer_doc
2 parents c7525aa + 7cbedfb commit 70dc9cc

File tree

95 files changed

+3578
-2674
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+3578
-2674
lines changed

cmake/External/capnproto.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ if (NOT __CAPNP_INCLUDED) # guard against multiple includes
4444
LOG_INSTALL 1
4545
)
4646

47+
set(CAPNP_FOUND TRUE)
48+
set(CAPNP_EXECUTABLE ${capnp_INSTALL}/bin/capnp)
49+
set(CAPNPC_CXX_EXECUTABLE ${capnp_INSTALL}/bin/capnpc-c++)
50+
set(CAPNP_INCLUDE_DIRS ${capnp_INSTALL}/include)
51+
set(CAPNP_INCLUDE_DIRECTORY ${capnp_INSTALL}/include)
52+
set(CAPNP_EXTERNAL TRUE)
53+
4754
list(APPEND external_project_dependencies capnp)
4855

4956
endif()

script/installation/packages.sh

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,13 @@ if [ "$DISTRO" = "UBUNTU" ]; then
4848
fi
4949
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 15CF4D18AF4F7421
5050
sudo apt-get update -qq
51+
CMAKE_NAME="cmake3"
52+
FORCE_Y="--force-yes"
53+
else
54+
CMAKE_NAME="cmake"
55+
FORCE_Y=""
5156
fi
5257

53-
# Fix for cmake3 name change in Ubuntu 15.x and 16.x plus --force-yes deprecation
54-
CMAKE_NAME="cmake3"
55-
FORCE_Y="--force-yes"
56-
MAJOR_VER=$(echo "$DISTRO_VER" | cut -d '.' -f 1)
57-
for version in "15" "16"
58-
do
59-
if [ "$MAJOR_VER" = "$version" ]
60-
then
61-
FORCE_Y=""
62-
CMAKE_NAME="cmake"
63-
break
64-
fi
65-
done
66-
6758
sudo apt-get -qq $FORCE_Y --ignore-missing -y install \
6859
git \
6960
g++ \

src/brain/query_logger.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// query_logger.cpp
6+
//
7+
// Identification: src/brain/query_logger.cpp
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "brain/query_logger.h"
14+
#include "catalog/query_history_catalog.h"
15+
#include "concurrency/transaction_context.h"
16+
#include "concurrency/transaction_manager_factory.h"
17+
#include "parser/pg_query.h"
18+
19+
namespace peloton {
20+
namespace brain {
21+
22+
void QueryLogger::LogQuery(std::string query_string, uint64_t timestamp) {
23+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
24+
auto txn = txn_manager.BeginTransaction();
25+
std::string fingerprint = pg_query_fingerprint(query_string.c_str()).hexdigest;
26+
27+
catalog::QueryHistoryCatalog::GetInstance()->InsertQueryHistory(
28+
query_string, fingerprint, timestamp, nullptr, txn);
29+
30+
txn_manager.CommitTransaction(txn);
31+
}
32+
33+
} // namespace brain
34+
} // namespace peloton

src/catalog/catalog.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "catalog/index_metrics_catalog.h"
2020
#include "catalog/language_catalog.h"
2121
#include "catalog/proc_catalog.h"
22+
#include "catalog/query_history_catalog.h"
2223
#include "catalog/query_metrics_catalog.h"
2324
#include "catalog/settings_catalog.h"
2425
#include "catalog/table_catalog.h"
@@ -30,6 +31,7 @@
3031
#include "function/old_engine_string_functions.h"
3132
#include "function/timestamp_functions.h"
3233
#include "index/index_factory.h"
34+
#include "settings/settings_manager.h"
3335
#include "storage/storage_manager.h"
3436
#include "storage/table_factory.h"
3537
#include "type/ephemeral_pool.h"
@@ -146,11 +148,15 @@ void Catalog::Bootstrap() {
146148
DatabaseMetricsCatalog::GetInstance(txn);
147149
TableMetricsCatalog::GetInstance(txn);
148150
IndexMetricsCatalog::GetInstance(txn);
149-
QueryMetricsCatalog::GetInstance(txn);
151+
QueryMetricsCatalog::GetInstance(txn);
150152
SettingsCatalog::GetInstance(txn);
151153
TriggerCatalog::GetInstance(txn);
152154
LanguageCatalog::GetInstance(txn);
153155
ProcCatalog::GetInstance(txn);
156+
157+
if (settings::SettingsManager::GetBool(settings::SettingId::brain)) {
158+
QueryHistoryCatalog::GetInstance(txn);
159+
}
154160

155161
txn_manager.CommitTransaction(txn);
156162

src/catalog/query_history_catalog.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// query_history_catalog.cpp
6+
//
7+
// Identification: src/catalog/query_history_catalog.cpp
8+
//
9+
// Copyright (c) 2015-18, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "catalog/query_history_catalog.h"
14+
15+
#include "catalog/catalog.h"
16+
#include "executor/logical_tile.h"
17+
#include "parser/pg_query.h"
18+
#include "storage/data_table.h"
19+
#include "type/value_factory.h"
20+
21+
namespace peloton {
22+
namespace catalog {
23+
24+
QueryHistoryCatalog *QueryHistoryCatalog::GetInstance(
25+
concurrency::TransactionContext *txn) {
26+
static QueryHistoryCatalog query_history_catalog{txn};
27+
return &query_history_catalog;
28+
}
29+
30+
QueryHistoryCatalog::QueryHistoryCatalog(concurrency::TransactionContext *txn)
31+
: AbstractCatalog("CREATE TABLE " CATALOG_DATABASE_NAME
32+
"." QUERY_HISTORY_CATALOG_NAME
33+
" ("
34+
"query_string VARCHAR NOT NULL, "
35+
"fingerprint VARCHAR NOT NULL, "
36+
"timestamp TIMESTAMP NOT NULL);",
37+
txn) {}
38+
39+
QueryHistoryCatalog::~QueryHistoryCatalog() {}
40+
41+
bool QueryHistoryCatalog::InsertQueryHistory(const std::string &query_string,
42+
std::string &fingerprint, uint64_t timestamp,
43+
type::AbstractPool *pool,
44+
concurrency::TransactionContext *txn) {
45+
std::unique_ptr<storage::Tuple> tuple(
46+
new storage::Tuple(catalog_table_->GetSchema(), true));
47+
48+
auto val0 = type::ValueFactory::GetVarcharValue(query_string, pool);
49+
auto val1 = type::ValueFactory::GetVarcharValue(fingerprint, pool);
50+
auto val2 = type::ValueFactory::GetTimestampValue(timestamp);
51+
52+
tuple->SetValue(ColumnId::QUERY_STRING, val0, pool);
53+
tuple->SetValue(ColumnId::FINGERPRINT, val1, pool);
54+
tuple->SetValue(ColumnId::TIMESTAMP, val2, pool);
55+
56+
// Insert the tuple
57+
return InsertTuple(std::move(tuple), txn);
58+
}
59+
60+
} // namespace catalog
61+
} // namespace peloton

src/common/init.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ void PelotonInit::Initialize() {
4242
// start worker pool
4343
threadpool::MonoQueuePool::GetInstance().Startup();
4444

45+
// start brain thread pool
46+
if (settings::SettingsManager::GetBool(settings::SettingId::brain)) {
47+
threadpool::MonoQueuePool::GetBrainInstance().Startup();
48+
}
49+
4550
int parallelism = (std::thread::hardware_concurrency() + 3) / 4;
4651
storage::DataTable::SetActiveTileGroupCount(parallelism);
4752
storage::DataTable::SetActiveIndirectionArrayCount(parallelism);
@@ -107,6 +112,11 @@ void PelotonInit::Shutdown() {
107112
// stop worker pool
108113
threadpool::MonoQueuePool::GetInstance().Shutdown();
109114

115+
// stop brain thread pool
116+
if (settings::SettingsManager::GetBool(settings::SettingId::brain)) {
117+
threadpool::MonoQueuePool::GetBrainInstance().Shutdown();
118+
}
119+
110120
thread_pool.Shutdown();
111121

112122
// shutdown protocol buf library

src/concurrency/transaction_manager.cpp

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

1515
#include "catalog/manager.h"
1616
#include "concurrency/transaction_context.h"
17+
#include "function/date_functions.h"
1718
#include "gc/gc_manager_factory.h"
1819
#include "logging/log_manager.h"
1920
#include "settings/settings_manager.h"
@@ -72,6 +73,8 @@ TransactionContext *TransactionManager::BeginTransaction(
7273
.StartTimer();
7374
}
7475

76+
txn->SetTimestamp(function::DateFunctions::Now());
77+
7578
return txn;
7679
}
7780

src/executor/create_executor.cpp

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,25 +99,26 @@ bool CreateExecutor::CreateDatabase(const planner::CreatePlan &node) {
9999
}
100100

101101
bool CreateExecutor::CreateTable(const planner::CreatePlan &node) {
102-
auto txn = context_->GetTransaction();
102+
auto current_txn = context_->GetTransaction();
103103
std::string table_name = node.GetTableName();
104104
auto database_name = node.GetDatabaseName();
105105
std::unique_ptr<catalog::Schema> schema(node.GetSchema());
106106

107107
ResultType result = catalog::Catalog::GetInstance()->CreateTable(
108-
database_name, table_name, std::move(schema), txn);
109-
txn->SetResult(result);
108+
database_name, table_name, std::move(schema), current_txn);
109+
current_txn->SetResult(result);
110110

111-
if (txn->GetResult() == ResultType::SUCCESS) {
111+
if (current_txn->GetResult() == ResultType::SUCCESS) {
112112
LOG_TRACE("Creating table succeeded!");
113113

114114
// Add the foreign key constraint (or other multi-column constraints)
115115
if (node.GetForeignKeys().empty() == false) {
116116
auto catalog = catalog::Catalog::GetInstance();
117-
auto db = catalog->GetDatabaseWithName(database_name, txn);
117+
auto db = catalog->GetDatabaseWithName(database_name, current_txn);
118118

119119
auto source_table = db->GetTableWithName(table_name);
120120
int count = 1;
121+
121122
for (auto fk : node.GetForeignKeys()) {
122123
auto sink_table = db->GetTableWithName(fk.sink_table_name);
123124

@@ -139,7 +140,7 @@ bool CreateExecutor::CreateTable(const planner::CreatePlan &node) {
139140
// Sink Column Offsets
140141
std::vector<oid_t> sink_col_ids;
141142
for (auto col_name : fk.foreign_key_sinks) {
142-
oid_t col_id = source_table->GetSchema()->GetColumnID(col_name);
143+
oid_t col_id = sink_table->GetSchema()->GetColumnID(col_name);
143144
if (col_id == INVALID_OID) {
144145
std::string error = StringUtil::Format(
145146
"Invalid sink column name '%s.%s' for foreign key '%s'",
@@ -152,43 +153,51 @@ bool CreateExecutor::CreateTable(const planner::CreatePlan &node) {
152153
PL_ASSERT(sink_col_ids.size() == fk.foreign_key_sinks.size());
153154

154155
// Create the catalog object and shove it into the table
155-
auto catalog_fk = new catalog::ForeignKey(
156+
auto catalog_fk = new catalog::ForeignKey(INVALID_OID,
156157
sink_table->GetOid(), sink_col_ids, source_col_ids,
157158
fk.upd_action, fk.del_action, fk.constraint_name);
158159
source_table->AddForeignKey(catalog_fk);
159160

160161
// Register FK with the sink table for delete/update actions
161-
sink_table->RegisterForeignKeySource(table_name);
162+
catalog_fk = new catalog::ForeignKey(source_table->GetOid(),
163+
INVALID_OID,
164+
sink_col_ids,
165+
source_col_ids,
166+
fk.upd_action,
167+
fk.del_action,
168+
fk.constraint_name);
169+
sink_table->RegisterForeignKeySource(catalog_fk);
162170

163171
// Add a non-unique index on the source table if needed
164-
if (catalog_fk->GetUpdateAction() != FKConstrActionType::NOACTION ||
165-
catalog_fk->GetDeleteAction() != FKConstrActionType::NOACTION) {
166-
std::vector<std::string> source_col_names =
167-
fk.foreign_key_sources;
168-
std::string index_name =
169-
source_table->GetName() + "_FK_" + std::to_string(count);
170-
catalog->CreateIndex(database_name, source_table->GetName(),
171-
source_col_ids, index_name, false,
172-
IndexType::BWTREE, txn);
173-
count++;
172+
std::vector<std::string> source_col_names =
173+
fk.foreign_key_sources;
174+
std::string index_name =
175+
source_table->GetName() + "_FK_" + sink_table->GetName() + "_"
176+
+ std::to_string(count);
177+
catalog->CreateIndex(database_name, source_table->GetName(),
178+
source_col_ids, index_name, false,
179+
IndexType::BWTREE, current_txn);
180+
count++;
174181

175182
#ifdef LOG_DEBUG_ENABLED
176-
LOG_DEBUG("Added a FOREIGN index on in %s.", table_name.c_str());
177-
LOG_DEBUG("Foreign key column names: ");
178-
for (auto c : source_col_names) {
179-
LOG_DEBUG("FK col name: %s", c.c_str());
180-
}
181-
#endif
183+
LOG_DEBUG("Added a FOREIGN index on in %s.\n", table_name.c_str());
184+
LOG_DEBUG("Foreign key column names: \n");
185+
for (auto c : source_col_names) {
186+
LOG_DEBUG("FK col name: %s\n", c.c_str());
187+
}
188+
for (auto c : fk.foreign_key_sinks) {
189+
LOG_DEBUG("FK sink col name: %s\n", c.c_str());
182190
}
191+
#endif
183192
}
184193
}
185-
} else if (txn->GetResult() == ResultType::FAILURE) {
194+
} else if (current_txn->GetResult() == ResultType::FAILURE) {
186195
LOG_TRACE("Creating table failed!");
187-
return (false);
188196
} else {
189197
LOG_TRACE("Result is: %s",
190-
ResultTypeToString(txn->GetResult()).c_str());
198+
ResultTypeToString(current_txn->GetResult()).c_str());
191199
}
200+
192201
return (true);
193202
}
194203

src/executor/delete_executor.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ bool DeleteExecutor::DExecute() {
133133
physical_tuple_id = old_location.offset;
134134
}
135135

136+
ContainerTuple<storage::TileGroup> old_tuple(tile_group, physical_tuple_id);
137+
storage::Tuple prev_tuple(target_table_->GetSchema(), true);
138+
139+
// Get a copy of the old tuple
140+
for (oid_t column_itr = 0; column_itr < target_table_schema->GetColumnCount(); column_itr++) {
141+
type::Value val = (old_tuple.GetValue(column_itr));
142+
prev_tuple.SetValue(column_itr, val, executor_context_->GetPool());
143+
}
144+
145+
// Check the foreign key source table
146+
if (target_table_->CheckForeignKeySrcAndCascade(&prev_tuple,
147+
nullptr,
148+
current_txn,
149+
executor_context_,
150+
false) == false)
151+
{
152+
transaction_manager.SetTransactionResult(current_txn,
153+
peloton::ResultType::FAILURE);
154+
return false;
155+
}
156+
136157
bool is_owner = transaction_manager.IsOwner(current_txn, tile_group_header,
137158
physical_tuple_id);
138159

src/executor/update_executor.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "storage/data_table.h"
2323
#include "storage/tile_group_header.h"
2424
#include "storage/tile.h"
25+
#include "storage/storage_manager.h"
26+
#include "catalog/foreign_key.h"
2527

2628
namespace peloton {
2729
namespace executor {
@@ -112,6 +114,27 @@ bool UpdateExecutor::PerformUpdatePrimaryKey(
112114
return false;
113115
}
114116

117+
// Check the source table of any foreign key constraint
118+
if (target_table_->GetForeignKeySrcCount() > 0) {
119+
storage::Tuple prev_tuple(target_table_schema, true);
120+
// Get a copy of the old tuple
121+
for (oid_t column_itr = 0; column_itr < target_table_schema->GetColumnCount(); column_itr++) {
122+
type::Value val = (old_tuple.GetValue(column_itr));
123+
prev_tuple.SetValue(column_itr, val, executor_context_->GetPool());
124+
}
125+
126+
if (target_table_->CheckForeignKeySrcAndCascade(&prev_tuple,
127+
&new_tuple,
128+
current_txn,
129+
executor_context_,
130+
true) == false)
131+
{
132+
transaction_manager.SetTransactionResult(current_txn,
133+
peloton::ResultType::FAILURE);
134+
return false;
135+
}
136+
}
137+
115138
transaction_manager.PerformInsert(current_txn, location, index_entry_ptr);
116139

117140
return true;

0 commit comments

Comments
 (0)