Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support prediction push-down in scanNode and record the cost of query #10

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/server/include/query_engine/optimizer/optimizer.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#pragma once

#include "include/query_engine/planner/node/logical_node.h"
#include <memory>

#include "include/query_engine/planner/node/logical_node.h"
#include "rewriter.h"

class Optimizer{
public:
RC rewrite(std::unique_ptr<LogicalNode> &logical_operator);
private:
Rewriter rewriter_;
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,25 @@ class IndexScanPhysicalOperator : public PhysicalOperator

std::string param() const override;

void set_predicates(std::vector<std::unique_ptr<Expression>> &predicates) {
predicates_ = std::move(predicates);
}

private:
RC filter(RowTuple &tuple, bool &result);
Table *table_ = nullptr;
Index *index_ = nullptr;
IndexScanner *index_scanner_ = nullptr;
RecordFileHandler *record_handler_ = nullptr;
bool readonly_ = false;

RecordPageHandler record_page_handler_ =;
RecordPageHandler record_page_handler_;
Record current_record_;
RowTuple tuple_;

Value left_value_;
Value right_value_;
bool left_inclusive_ = false;
bool right_inclusive_ = false;
std::vector<std::unique_ptr<Expression>> predicates_;
};
3 changes: 1 addition & 2 deletions src/server/include/query_engine/structor/tuple/row_tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ class RowTuple : public Tuple
const FieldExpr *field_expr = species_[i];
const Field &field = field_expr->field();
if (0 == strcmp(table_name, field.table_name()) &&
0 == strcmp(field_name, field.field_name()) &&
0 == strcmp(spec.alias(), field.table_alias())) {
0 == strcmp(field_name, field.field_name())) {
return cell_at(i, cell);
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/server/query_engine/optimizer/optimzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

RC Optimizer::rewrite(std::unique_ptr<LogicalNode> &logical_operator)
{
//Currently we don't support optimization.
return RC::SUCCESS;
RC rc;
bool change = false;
rc = rewriter_.rewrite(logical_operator, change);
if(rc != RC::SUCCESS){
return rc;
}
return rc;
}
2 changes: 0 additions & 2 deletions src/server/query_engine/optimizer/rewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

Rewriter::Rewriter()
{
rewrite_rules_.emplace_back(new ExpressionRewriter);
rewrite_rules_.emplace_back(new PredicateRewriteRule);
rewrite_rules_.emplace_back(new PredicatePushdownRewriter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,24 @@ std::string IndexScanPhysicalOperator::param() const
{
return std::string(index_->index_meta().name()) + " ON " + table_->name();
}

RC IndexScanPhysicalOperator::filter(RowTuple &tuple, bool &result)
{
RC rc = RC::SUCCESS;
Value value;
for (std::unique_ptr<Expression> &expr : predicates_) {
rc = expr->get_value(tuple, value);
if (rc != RC::SUCCESS) {
return rc;
}

bool tmp_result = value.get_boolean();
if (!tmp_result) {
result = false;
return rc;
}
}

result = true;
return rc;
}
13 changes: 11 additions & 2 deletions src/server/query_engine/query_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#include "include/query_engine/analyzer/analyzer.h"
#include "include/session/communicator.h"

#include <chrono>
#include <memory>

// 处理从session传来的请求, 包含sql执行与结果写回
bool QueryEngine::process_session_request(SessionRequest *request) {
RC rc;
char *time_str = new char[64];
bool need_disconnect = true;

std::string sql = request->query();
Expand All @@ -22,6 +24,8 @@ bool QueryEngine::process_session_request(SessionRequest *request) {
request->session()->set_current_request(request);

QueryInfo query_info(request, sql);

auto start_time = std::chrono::high_resolution_clock::now();
rc = executeQuery(&query_info);
if(RC_FAIL(rc) && rc != RC::UNIMPLENMENT){
request->get_communicator()->write_state(request->sql_result(), need_disconnect);
Expand All @@ -30,10 +34,15 @@ bool QueryEngine::process_session_request(SessionRequest *request) {
}else{
executor_.execute(request, &query_info, need_disconnect);
}

auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time);
snprintf(time_str, 64, "Cost time: %ld ns\n", duration.count());

request->get_communicator()->write_result(time_str, strlen(time_str));
request->get_communicator()->flush();
request->session()->set_current_request(nullptr);
Session::set_current_session(nullptr);

delete[] time_str;
return need_disconnect;
}

Expand Down
8 changes: 4 additions & 4 deletions src/server/storage_engine/index/bplus_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1837,13 +1837,13 @@ RC BplusTreeScanner::next_entry(RID &rid, bool isdelete)
if (!first_emitted_) {
fetch_item(rid);
first_emitted_ = true;

if (!isdelete) {
iter_index_++;
}
return RC::SUCCESS;
}

// if (!isdelete) {
// iter_index_++;
// }

LeafIndexNodeHandler node(tree_handler_.file_header_, current_frame_);
if (iter_index_ < node.size()) {
if (touch_end()) {
Expand Down
1 change: 0 additions & 1 deletion test/unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ FOREACH (F ${ALL_SRC})
get_filename_component(prjName ${F} NAME_WE)
MESSAGE("Build ${prjName} according to ${F}")
ADD_EXECUTABLE(${prjName} ${F})
# 不是所有的单测都需要链接server_static
TARGET_LINK_LIBRARIES(${prjName} common pthread dl gtest gtest_main server_static)
gtest_discover_tests(${prjName})
ENDFOREACH (F)