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

Commit 512b31d

Browse files
nappelsonapavlo
authored andcommitted
Move parse tree binding from optimizer to catalog (#1133)
* Move parse tree binding from optimizer to catalog * remove use of using directive from binder_test * remove the ||true from make check requested by @pervazea * Move parse tree binding from optimizer to catalog * stack allocation of BindNodeVisitor
1 parent 54b02a4 commit 512b31d

File tree

15 files changed

+286
-123
lines changed

15 files changed

+286
-123
lines changed

src/catalog/abstract_catalog.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

1313
#include "catalog/abstract_catalog.h"
1414

15+
#include "binder/bind_node_visitor.h"
16+
1517
#include "common/statement.h"
1618

17-
#include "catalog/catalog.h"
1819
#include "catalog/database_catalog.h"
1920
#include "catalog/table_catalog.h"
2021

@@ -55,25 +56,38 @@ AbstractCatalog::AbstractCatalog(oid_t catalog_table_oid,
5556

5657
AbstractCatalog::AbstractCatalog(const std::string &catalog_table_ddl,
5758
concurrency::TransactionContext *txn) {
58-
// get catalog table schema
59+
// Get catalog table schema
5960
auto &peloton_parser = parser::PostgresParser::GetInstance();
61+
62+
// Build the parse tree
63+
const auto parse_tree_list = peloton_parser.BuildParseTree(catalog_table_ddl);
64+
if (parse_tree_list->GetStatements().empty()) {
65+
throw CatalogException(
66+
"Parse tree list has no parse trees. Cannot build plan");
67+
}
68+
// TODO: support multi-statement queries
69+
auto parse_tree = parse_tree_list->GetStatement(0);
70+
71+
// Run binder
72+
auto bind_node_visitor = binder::BindNodeVisitor(txn, DATABASE_CATALOG_NAME);
73+
bind_node_visitor.BindNameToNode(parse_tree);
74+
75+
// Create the plan tree
6076
auto create_plan = std::dynamic_pointer_cast<planner::CreatePlan>(
61-
optimizer::Optimizer().BuildPelotonPlanTree(
62-
peloton_parser.BuildParseTree(catalog_table_ddl),
63-
DATABASE_CATALOG_NAME, txn));
77+
optimizer::Optimizer().BuildPelotonPlanTree(parse_tree_list, txn));
6478
auto catalog_table_schema = create_plan->GetSchema();
6579
auto catalog_table_name = create_plan->GetTableName();
6680

67-
// create catalog table
81+
// Create catalog table
6882
Catalog::GetInstance()->CreateTable(
6983
CATALOG_DATABASE_NAME, catalog_table_name,
7084
std::unique_ptr<catalog::Schema>(catalog_table_schema), txn, true);
7185

72-
// get catalog table oid
86+
// Get catalog table oid
7387
auto catalog_table_object = Catalog::GetInstance()->GetTableObject(
7488
CATALOG_DATABASE_NAME, catalog_table_name, txn);
7589

76-
// set catalog_table_
90+
// Set catalog_table_
7791
try {
7892
catalog_table_ = storage::StorageManager::GetInstance()->GetTableWithOid(
7993
CATALOG_DATABASE_OID, catalog_table_object->GetTableOid());

src/include/optimizer/abstract_optimizer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ class AbstractOptimizer {
5050
virtual ~AbstractOptimizer();
5151

5252
virtual std::shared_ptr<planner::AbstractPlan> BuildPelotonPlanTree(
53-
const std::unique_ptr<parser::SQLStatementList> &parse_tree,
54-
const std::string default_database_name,
53+
const std::unique_ptr<parser::SQLStatementList> &parse_tree,
5554
concurrency::TransactionContext *txn) = 0;
5655

5756
virtual void Reset(){};

src/include/optimizer/optimizer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ class Optimizer : public AbstractOptimizer {
7272
Optimizer();
7373

7474
std::shared_ptr<planner::AbstractPlan> BuildPelotonPlanTree(
75-
const std::unique_ptr<parser::SQLStatementList> &parse_tree,
76-
const std::string default_database_name,
75+
const std::unique_ptr<parser::SQLStatementList> &parse_tree_list,
7776
concurrency::TransactionContext *txn) override;
7877

7978
void OptimizeLoop(int root_group_id,

src/optimizer/optimizer.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444

4545
#include "storage/data_table.h"
4646

47-
#include "binder/bind_node_visitor.h"
48-
4947
using std::vector;
5048
using std::unordered_map;
5149
using std::shared_ptr;
@@ -92,21 +90,18 @@ void Optimizer::OptimizeLoop(int root_group_id,
9290
}
9391

9492
shared_ptr<planner::AbstractPlan> Optimizer::BuildPelotonPlanTree(
95-
const unique_ptr<parser::SQLStatementList> &parse_tree_list,
96-
const std::string default_database_name,
93+
const std::unique_ptr<parser::SQLStatementList> &parse_tree_list,
9794
concurrency::TransactionContext *txn) {
98-
// Base Case
99-
if (parse_tree_list->GetStatements().size() == 0) return nullptr;
95+
if (parse_tree_list->GetStatements().empty()) {
96+
// TODO: create optimizer exception
97+
throw CatalogException(
98+
"Parse tree list has no parse trees. Cannot build plan");
99+
}
100+
// TODO: support multi-statement queries
101+
auto parse_tree = parse_tree_list->GetStatement(0);
100102

101103
unique_ptr<planner::AbstractPlan> child_plan = nullptr;
102104

103-
auto parse_tree = parse_tree_list->GetStatements().at(0).get();
104-
105-
// Run binder
106-
auto bind_node_visitor =
107-
make_shared<binder::BindNodeVisitor>(txn, default_database_name);
108-
bind_node_visitor->BindNameToNode(parse_tree);
109-
110105
// Handle ddl statement
111106
bool is_ddl_stmt;
112107
auto ddl_plan = HandleDDLStatement(parse_tree, is_ddl_stmt, txn);

src/traffic_cop/traffic_cop.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,13 @@ std::shared_ptr<Statement> TrafficCop::PrepareStatement(
312312
// TODO(Tianyi) Move Statement Planing into Statement's method
313313
// to increase coherence
314314
try {
315+
// Run binder
316+
auto bind_node_visitor = binder::BindNodeVisitor(
317+
tcop_txn_state_.top().first, default_database_name_);
318+
bind_node_visitor.BindNameToNode(
319+
statement->GetStmtParseTreeList()->GetStatement(0));
315320
auto plan = optimizer_->BuildPelotonPlanTree(
316-
statement->GetStmtParseTreeList(), default_database_name_,
317-
tcop_txn_state_.top().first);
321+
statement->GetStmtParseTreeList(), tcop_txn_state_.top().first);
318322
statement->SetPlanTree(plan);
319323
// Get the tables that our plan references so that we know how to
320324
// invalidate it at a later point when the catalog changes
@@ -362,8 +366,8 @@ void TrafficCop::ProcessInvalidStatement() {
362366
}
363367

364368
bool TrafficCop::BindParamsForCachePlan(
365-
const std::vector<std::unique_ptr<expression::AbstractExpression>> &
366-
parameters,
369+
const std::vector<std::unique_ptr<expression::AbstractExpression>>
370+
&parameters,
367371
const size_t thread_id UNUSED_ATTRIBUTE) {
368372
if (tcop_txn_state_.empty()) {
369373
single_statement_txn_ = true;
@@ -377,8 +381,8 @@ bool TrafficCop::BindParamsForCachePlan(
377381
tcop_txn_state_.emplace(txn, ResultType::SUCCESS);
378382
}
379383
// Run binder
380-
auto bind_node_visitor = std::make_shared<binder::BindNodeVisitor>(
381-
tcop_txn_state_.top().first, default_database_name_);
384+
auto bind_node_visitor = binder::BindNodeVisitor(tcop_txn_state_.top().first,
385+
default_database_name_);
382386

383387
std::vector<type::Value> param_values;
384388
for (const std::unique_ptr<expression::AbstractExpression> &param :
@@ -387,7 +391,7 @@ bool TrafficCop::BindParamsForCachePlan(
387391
error_message_ = "Invalid Expression Type";
388392
return false;
389393
}
390-
param->Accept(bind_node_visitor.get());
394+
param->Accept(&bind_node_visitor);
391395
// TODO(Yuchen): need better check for nullptr argument
392396
param_values.push_back(param->Evaluate(nullptr, nullptr, nullptr));
393397
}
@@ -580,9 +584,12 @@ ResultType TrafficCop::ExecuteStatement(
580584
if (statement->GetNeedsReplan()) {
581585
// TODO(Tianyi) Move Statement Replan into Statement's method
582586
// to increase coherence
587+
auto bind_node_visitor = binder::BindNodeVisitor(
588+
tcop_txn_state_.top().first, default_database_name_);
589+
bind_node_visitor.BindNameToNode(
590+
statement->GetStmtParseTreeList()->GetStatement(0));
583591
auto plan = optimizer_->BuildPelotonPlanTree(
584-
statement->GetStmtParseTreeList(), default_database_name_,
585-
tcop_txn_state_.top().first);
592+
statement->GetStmtParseTreeList(), tcop_txn_state_.top().first);
586593
statement->SetPlanTree(plan);
587594
statement->SetNeedsReplan(true);
588595
}

test/binder/binder_test.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ using std::string;
3232
using std::unique_ptr;
3333
using std::vector;
3434
using std::make_tuple;
35+
using std::make_shared;
3536

3637
namespace peloton {
3738
namespace test {
@@ -77,9 +78,14 @@ void SetupTables(std::string database_name) {
7778
vector<ResultValue> result;
7879
vector<int> result_format;
7980
unique_ptr<Statement> statement(new Statement("CREATE", sql));
80-
auto parse_tree = parser.BuildParseTree(sql);
81+
82+
auto parse_tree_list = parser.BuildParseTree(sql);
83+
auto parse_tree = parse_tree_list->GetStatement(0);
84+
auto bind_node_visitor = binder::BindNodeVisitor(txn, database_name);
85+
bind_node_visitor.BindNameToNode(parse_tree);
86+
8187
statement->SetPlanTree(
82-
optimizer.BuildPelotonPlanTree(parse_tree, database_name, txn));
88+
optimizer.BuildPelotonPlanTree(parse_tree_list, txn));
8389
TestingSQLUtil::counter_.store(1);
8490
auto status = traffic_cop.ExecuteHelper(statement->GetPlanTree(), params,
8591
result, result_format);
@@ -319,7 +325,8 @@ TEST_F(BinderCorrectnessTest, BindDepthTest) {
319325
auto exists_sub_expr_select =
320326
dynamic_cast<const expression::SubqueryExpression *>(exists_sub_expr)
321327
->GetSubSelect();
322-
auto exists_sub_expr_select_where = exists_sub_expr_select->where_clause.get();
328+
auto exists_sub_expr_select_where =
329+
exists_sub_expr_select->where_clause.get();
323330
auto exists_sub_expr_select_ele =
324331
exists_sub_expr_select->select_list[0].get();
325332
auto in_tv_expr = in_expr->GetChild(0);
@@ -337,8 +344,7 @@ TEST_F(BinderCorrectnessTest, BindDepthTest) {
337344
in_sub_expr_select_where_right->GetChild(1);
338345
auto in_sub_expr_select_where_right_sub_select =
339346
dynamic_cast<const expression::SubqueryExpression *>(
340-
in_sub_expr_select_where_right_sub)
341-
->GetSubSelect();
347+
in_sub_expr_select_where_right_sub)->GetSubSelect();
342348
auto in_sub_expr_select_where_right_sub_select_where =
343349
in_sub_expr_select_where_right_sub_select->where_clause.get();
344350
auto in_sub_expr_select_where_right_sub_select_ele =

test/executor/copy_test.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <cstdio>
1414
#include "sql/testing_sql_util.h"
1515

16+
#include "binder/bind_node_visitor.h"
1617
#include "catalog/catalog.h"
1718
#include "common/harness.h"
1819
#include "common/logger.h"
@@ -41,7 +42,8 @@ TEST_F(CopyTests, Copying) {
4142
auto catalog = catalog::Catalog::GetInstance();
4243
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
4344
auto txn = txn_manager.BeginTransaction();
44-
catalog->CreateDatabase("emp_db", txn);
45+
const auto &db_name = "emp_db";
46+
catalog->CreateDatabase(db_name, txn);
4547
txn_manager.CommitTransaction(txn);
4648

4749
std::unique_ptr<optimizer::AbstractOptimizer> optimizer(
@@ -119,9 +121,14 @@ TEST_F(CopyTests, Copying) {
119121
auto &peloton_parser = parser::PostgresParser::GetInstance();
120122
auto copy_stmt = peloton_parser.BuildParseTree(copy_sql);
121123

124+
LOG_TRACE("Binding parse tree...");
125+
auto parse_tree = copy_stmt->GetStatement(0);
126+
auto bind_node_visitor = binder::BindNodeVisitor(txn, db_name);
127+
bind_node_visitor.BindNameToNode(parse_tree);
128+
LOG_TRACE("Binding parse tree completed!");
129+
122130
LOG_TRACE("Building plan tree...");
123-
auto copy_plan =
124-
optimizer->BuildPelotonPlanTree(copy_stmt, DEFAULT_DB_NAME, txn);
131+
auto copy_plan = optimizer->BuildPelotonPlanTree(copy_stmt, txn);
125132
statement->SetPlanTree(copy_plan);
126133

127134
LOG_TRACE("Building executor tree...");

test/executor/create_index_test.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <cstdio>
1515
#include "sql/testing_sql_util.h"
1616

17+
#include "binder/bind_node_visitor.h"
1718
#include "catalog/catalog.h"
1819
#include "common/harness.h"
1920
#include "common/logger.h"
@@ -80,9 +81,14 @@ TEST_F(CreateIndexTests, CreatingIndex) {
8081
"dept_name TEXT);");
8182
LOG_INFO("Building parse tree completed!");
8283

84+
LOG_INFO("Binding parse tree...");
85+
auto parse_tree = create_stmt->GetStatement(0);
86+
auto bind_node_visitor = binder::BindNodeVisitor(txn, DEFAULT_DB_NAME);
87+
bind_node_visitor.BindNameToNode(parse_tree);
88+
LOG_INFO("Binding parse tree completed!");
89+
8390
LOG_INFO("Building plan tree...");
84-
statement->SetPlanTree(
85-
optimizer->BuildPelotonPlanTree(create_stmt, DEFAULT_DB_NAME, txn));
91+
statement->SetPlanTree(optimizer->BuildPelotonPlanTree(create_stmt, txn));
8692
LOG_INFO("Building plan tree completed!");
8793

8894
std::vector<type::Value> params;
@@ -131,9 +137,14 @@ TEST_F(CreateIndexTests, CreatingIndex) {
131137
"(1,52,'hello_1');");
132138
LOG_INFO("Building parse tree completed!");
133139

140+
LOG_INFO("Binding parse tree...");
141+
parse_tree = insert_stmt->GetStatement(0);
142+
bind_node_visitor = binder::BindNodeVisitor(txn, DEFAULT_DB_NAME);
143+
bind_node_visitor.BindNameToNode(parse_tree);
144+
LOG_INFO("Binding parse tree completed!");
145+
134146
LOG_INFO("Building plan tree...");
135-
statement->SetPlanTree(
136-
optimizer->BuildPelotonPlanTree(insert_stmt, DEFAULT_DB_NAME, txn));
147+
statement->SetPlanTree(optimizer->BuildPelotonPlanTree(insert_stmt, txn));
137148
LOG_INFO("Building plan tree completed!\n%s",
138149
planner::PlanUtil::GetInfo(statement->GetPlanTree().get()).c_str());
139150

@@ -168,9 +179,14 @@ TEST_F(CreateIndexTests, CreatingIndex) {
168179
"CREATE INDEX saif ON department_table (student_id);");
169180
LOG_INFO("Building parse tree completed!");
170181

182+
LOG_INFO("Binding parse tree...");
183+
parse_tree = update_stmt->GetStatement(0);
184+
bind_node_visitor = binder::BindNodeVisitor(txn, DEFAULT_DB_NAME);
185+
bind_node_visitor.BindNameToNode(parse_tree);
186+
LOG_INFO("Binding parse tree completed!");
187+
171188
LOG_INFO("Building plan tree...");
172-
statement->SetPlanTree(
173-
optimizer->BuildPelotonPlanTree(update_stmt, DEFAULT_DB_NAME, txn));
189+
statement->SetPlanTree(optimizer->BuildPelotonPlanTree(update_stmt, txn));
174190
LOG_INFO("Building plan tree completed!\n%s",
175191
planner::PlanUtil::GetInfo(statement->GetPlanTree().get()).c_str());
176192

0 commit comments

Comments
 (0)