Skip to content

Commit b063327

Browse files
authored
feat(binder,sqllogictest): support extra options (#431)
Signed-off-by: Alex Chi <[email protected]>
1 parent 78c8f61 commit b063327

File tree

18 files changed

+232
-51
lines changed

18 files changed

+232
-51
lines changed

src/binder/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_library(
77
bind_create.cpp
88
bind_insert.cpp
99
bind_select.cpp
10+
bind_variable.cpp
1011
bound_statement.cpp
1112
fmt_impl.cpp
1213
keyword_helper.cpp

src/binder/bind_variable.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#include <memory>
3+
#include "binder/binder.h"
4+
#include "binder/bound_expression.h"
5+
#include "binder/expressions/bound_constant.h"
6+
#include "binder/statement/set_show_statement.h"
7+
#include "common/exception.h"
8+
namespace bustub {
9+
10+
auto Binder::BindVariableSet(duckdb_libpgquery::PGVariableSetStmt *stmt) -> std::unique_ptr<VariableSetStatement> {
11+
auto expr = BindExpressionList(stmt->args);
12+
if (expr.size() != 1) {
13+
throw bustub::NotImplementedException("Only exactly one arg is supported");
14+
}
15+
if (expr[0]->type_ != ExpressionType::CONSTANT) {
16+
throw bustub::NotImplementedException("Only constant is supported");
17+
}
18+
const auto &const_expr = dynamic_cast<const BoundConstant &>(*expr[0]);
19+
return std::make_unique<VariableSetStatement>(stmt->name, const_expr.val_.ToString());
20+
}
21+
22+
auto Binder::BindVariableShow(duckdb_libpgquery::PGVariableShowStmt *stmt) -> std::unique_ptr<VariableShowStatement> {
23+
return std::make_unique<VariableShowStatement>(stmt->name);
24+
}
25+
26+
} // namespace bustub

src/binder/statement/index_statement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ IndexStatement::IndexStatement(std::string index_name, std::unique_ptr<BoundBase
1414
cols_(std::move(cols)) {}
1515

1616
auto IndexStatement::ToString() const -> std::string {
17-
return fmt::format("Index {{ index_name={}, table={}, cols={} }}", index_name_, *table_, cols_);
17+
return fmt::format("BoundIndex {{ index_name={}, table={}, cols={} }}", index_name_, *table_, cols_);
1818
}
1919

2020
} // namespace bustub

src/binder/transformer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ auto Binder::BindStatement(duckdb_libpgquery::PGNode *stmt) -> std::unique_ptr<B
6464
return BindDelete(reinterpret_cast<duckdb_libpgquery::PGDeleteStmt *>(stmt));
6565
case duckdb_libpgquery::T_PGIndexStmt:
6666
return BindIndex(reinterpret_cast<duckdb_libpgquery::PGIndexStmt *>(stmt));
67+
case duckdb_libpgquery::T_PGVariableSetStmt:
68+
return BindVariableSet(reinterpret_cast<duckdb_libpgquery::PGVariableSetStmt *>(stmt));
69+
case duckdb_libpgquery::T_PGVariableShowStmt:
70+
return BindVariableShow(reinterpret_cast<duckdb_libpgquery::PGVariableShowStmt *>(stmt));
6771
case duckdb_libpgquery::T_PGUpdateStmt:
6872
default:
6973
throw NotImplementedException(NodeTagToString(stmt->type));

src/common/bustub_instance.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <optional>
12
#include <string>
23
#include <tuple>
34

@@ -8,6 +9,7 @@
89
#include "binder/statement/explain_statement.h"
910
#include "binder/statement/index_statement.h"
1011
#include "binder/statement/select_statement.h"
12+
#include "binder/statement/set_show_statement.h"
1113
#include "buffer/buffer_pool_manager_instance.h"
1214
#include "catalog/schema.h"
1315
#include "catalog/table_generator.h"
@@ -201,6 +203,17 @@ void BustubInstance::ExecuteSql(const std::string &sql, ResultWriter &writer) {
201203
WriteOneCell(fmt::format("Index created with id = {}", info->index_oid_), writer);
202204
continue;
203205
}
206+
case StatementType::VARIABLE_SHOW_STATEMENT: {
207+
const auto &show_stmt = dynamic_cast<const VariableShowStatement &>(*statement);
208+
auto content = GetSessionVariable(show_stmt.variable_);
209+
WriteOneCell(fmt::format("{}={}", show_stmt.variable_, content), writer);
210+
continue;
211+
}
212+
case StatementType::VARIABLE_SET_STATEMENT: {
213+
const auto &set_stmt = dynamic_cast<const VariableSetStatement &>(*statement);
214+
session_variables_[set_stmt.variable_] = set_stmt.value_;
215+
continue;
216+
}
204217
case StatementType::EXPLAIN_STATEMENT: {
205218
const auto &explain_stmt = dynamic_cast<const ExplainStatement &>(*statement);
206219
std::string output;
@@ -227,7 +240,7 @@ void BustubInstance::ExecuteSql(const std::string &sql, ResultWriter &writer) {
227240
}
228241

229242
// Print optimizer result.
230-
bustub::Optimizer optimizer(*catalog_);
243+
bustub::Optimizer optimizer(*catalog_, IsForceStarterRule());
231244
auto optimized_plan = optimizer.Optimize(planner.plan_);
232245

233246
if ((explain_stmt.options_ & ExplainOptions::OPTIMIZER) != 0) {
@@ -250,7 +263,7 @@ void BustubInstance::ExecuteSql(const std::string &sql, ResultWriter &writer) {
250263
planner.PlanQuery(*statement);
251264

252265
// Optimize the query.
253-
bustub::Optimizer optimizer(*catalog_);
266+
bustub::Optimizer optimizer(*catalog_, IsForceStarterRule());
254267
auto optimized_plan = optimizer.Optimize(planner.plan_);
255268

256269
// Execute the query.

src/include/binder/binder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include "binder/simplified_token.h"
4040
#include "binder/statement/select_statement.h"
41+
#include "binder/statement/set_show_statement.h"
4142
#include "binder/tokens.h"
4243
#include "catalog/catalog.h"
4344
#include "catalog/column.h"
@@ -186,6 +187,10 @@ class Binder {
186187

187188
auto BindCTE(duckdb_libpgquery::PGWithClause *node) -> std::vector<std::unique_ptr<BoundSubqueryRef>>;
188189

190+
auto BindVariableSet(duckdb_libpgquery::PGVariableSetStmt *stmt) -> std::unique_ptr<VariableSetStatement>;
191+
192+
auto BindVariableShow(duckdb_libpgquery::PGVariableShowStmt *stmt) -> std::unique_ptr<VariableShowStatement>;
193+
189194
class ContextGuard {
190195
public:
191196
explicit ContextGuard(const BoundTableRef **scope, const CTEList **cte_scope) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===----------------------------------------------------------------------===//
2+
// BusTub
3+
//
4+
// binder/set_get_statement.h
5+
//
6+
//===----------------------------------------------------------------------===//
7+
8+
#pragma once
9+
10+
#include <string>
11+
#include <utility>
12+
#include <vector>
13+
14+
#include "binder/bound_statement.h"
15+
#include "common/enums/statement_type.h"
16+
#include "fmt/format.h"
17+
18+
namespace duckdb_libpgquery {
19+
struct PGVariableSetStmt;
20+
struct PGVariableShowStmt;
21+
} // namespace duckdb_libpgquery
22+
23+
namespace bustub {
24+
25+
class VariableSetStatement : public BoundStatement {
26+
public:
27+
explicit VariableSetStatement(std::string variable, std::string value)
28+
: BoundStatement(StatementType::VARIABLE_SET_STATEMENT),
29+
variable_(std::move(variable)),
30+
value_(std::move(value)) {}
31+
32+
std::string variable_;
33+
std::string value_;
34+
35+
auto ToString() const -> std::string override {
36+
return fmt::format("BoundVariableSet {{ variable={}, value={} }}", variable_, value_);
37+
}
38+
};
39+
40+
class VariableShowStatement : public BoundStatement {
41+
public:
42+
explicit VariableShowStatement(std::string variable)
43+
: BoundStatement(StatementType::VARIABLE_SHOW_STATEMENT), variable_(std::move(variable)) {}
44+
45+
std::string variable_;
46+
47+
auto ToString() const -> std::string override {
48+
return fmt::format("BoundVariableShow {{ variable={} }}", variable_);
49+
}
50+
};
51+
52+
} // namespace bustub

src/include/common/bustub_instance.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414

1515
#include <iostream>
1616
#include <memory>
17+
#include <optional>
1718
#include <sstream>
1819
#include <string>
20+
#include <unordered_map>
1921
#include <utility>
2022
#include <vector>
2123

2224
#include "catalog/catalog.h"
2325
#include "common/config.h"
26+
#include "common/util/string_util.h"
2427
#include "libfort/lib/fort.hpp"
2528
#include "type/value.h"
2629

@@ -229,11 +232,24 @@ class BustubInstance {
229232
Catalog *catalog_;
230233
ExecutionEngine *execution_engine_;
231234

235+
auto GetSessionVariable(const std::string &key) -> std::string {
236+
if (session_variables_.find(key) != session_variables_.end()) {
237+
return session_variables_[key];
238+
}
239+
return "";
240+
}
241+
242+
auto IsForceStarterRule() -> bool {
243+
auto variable = StringUtil::Lower(GetSessionVariable("force_optimizer_starter_rule"));
244+
return variable == "1" || variable == "true" || variable == "yes";
245+
}
246+
232247
private:
233248
void CmdDisplayTables(ResultWriter &writer);
234249
void CmdDisplayIndices(ResultWriter &writer);
235250
void CmdDisplayHelp(ResultWriter &writer);
236251
void WriteOneCell(const std::string &cell, ResultWriter &writer);
252+
std::unordered_map<std::string, std::string> session_variables_;
237253
};
238254

239255
} // namespace bustub

src/include/common/enums/statement_type.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ namespace bustub {
2121
// Statement Types
2222
//===--------------------------------------------------------------------===//
2323
enum class StatementType : uint8_t {
24-
INVALID_STATEMENT, // invalid statement type
25-
SELECT_STATEMENT, // select statement type
26-
INSERT_STATEMENT, // insert statement type
27-
UPDATE_STATEMENT, // update statement type
28-
CREATE_STATEMENT, // create statement type
29-
DELETE_STATEMENT, // delete statement type
30-
EXPLAIN_STATEMENT, // explain statement type
31-
DROP_STATEMENT, // drop statement type
32-
INDEX_STATEMENT, // index statement type
24+
INVALID_STATEMENT, // invalid statement type
25+
SELECT_STATEMENT, // select statement type
26+
INSERT_STATEMENT, // insert statement type
27+
UPDATE_STATEMENT, // update statement type
28+
CREATE_STATEMENT, // create statement type
29+
DELETE_STATEMENT, // delete statement type
30+
EXPLAIN_STATEMENT, // explain statement type
31+
DROP_STATEMENT, // drop statement type
32+
INDEX_STATEMENT, // index statement type
33+
VARIABLE_SET_STATEMENT, // set variable statement type
34+
VARIABLE_SHOW_STATEMENT, // show variable statement type
3335
};
3436

3537
} // namespace bustub
@@ -67,6 +69,12 @@ struct fmt::formatter<bustub::StatementType> : formatter<string_view> {
6769
case bustub::StatementType::INDEX_STATEMENT:
6870
name = "Index";
6971
break;
72+
case bustub::StatementType::VARIABLE_SHOW_STATEMENT:
73+
name = "VariableShow";
74+
break;
75+
case bustub::StatementType::VARIABLE_SET_STATEMENT:
76+
name = "VariableSet";
77+
break;
7078
}
7179
return formatter<string_view>::format(name, ctx);
7280
}

src/include/optimizer/optimizer.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ namespace bustub {
1919
*/
2020
class Optimizer {
2121
public:
22-
explicit Optimizer(const Catalog &catalog) : catalog_(catalog) {}
22+
explicit Optimizer(const Catalog &catalog, bool force_starter_rule)
23+
: catalog_(catalog), force_starter_rule_(force_starter_rule) {}
2324

2425
auto Optimize(const AbstractPlanNodeRef &plan) -> AbstractPlanNodeRef;
2526

27+
auto OptimizeCustom(const AbstractPlanNodeRef &plan) -> AbstractPlanNodeRef;
28+
2629
private:
2730
/**
2831
* @brief merge projections that do identical project.
@@ -83,6 +86,8 @@ class Optimizer {
8386
* OPTIMIZER, otherwise it's a dangling reference.
8487
*/
8588
const Catalog &catalog_;
89+
90+
const bool force_starter_rule_;
8691
};
8792

8893
} // namespace bustub

0 commit comments

Comments
 (0)