From 263a6e3ec822b053d29f9f4532e6f0fcd148c665 Mon Sep 17 00:00:00 2001 From: hnwyllmm Date: Fri, 1 Jul 2022 14:11:17 +0800 Subject: [PATCH] operator & tuple --- src/observer/sql/executor/delete_operator.cpp | 7 +- src/observer/sql/executor/delete_operator.h | 4 +- src/observer/sql/executor/execute_stage.cpp | 25 ++- ...cution_node.cpp => execution_node.cpp.bak} | 0 src/observer/sql/executor/operator.h | 3 +- .../sql/executor/predicate_operator.cpp | 28 +-- .../sql/executor/predicate_operator.h | 4 +- .../sql/executor/table_scan_operator.cpp | 13 +- .../sql/executor/table_scan_operator.h | 3 +- .../sql/executor/{tuple.cpp => tuple.cpp.bak} | 0 src/observer/sql/executor/tuple.h | 181 ++++++------------ src/observer/sql/stmt/filter_stmt.h | 2 +- src/observer/storage/common/field.cpp | 4 +- src/observer/storage/common/field.h | 10 +- src/observer/storage/common/record.cpp | 13 -- 15 files changed, 117 insertions(+), 180 deletions(-) rename src/observer/sql/executor/{execution_node.cpp => execution_node.cpp.bak} (100%) rename src/observer/sql/executor/{tuple.cpp => tuple.cpp.bak} (100%) diff --git a/src/observer/sql/executor/delete_operator.cpp b/src/observer/sql/executor/delete_operator.cpp index 2f53593..f6757e9 100644 --- a/src/observer/sql/executor/delete_operator.cpp +++ b/src/observer/sql/executor/delete_operator.cpp @@ -32,15 +32,16 @@ RC DeleteOperator::open() return rc; } - Record record; Table *table = delete_stmt_->table(); while (RC::SUCCESS == (rc = child->next())) { - rc = child->current_record(record); - if (rc != RC::SUCCESS) { + Tuple *tuple = child->current_tuple(); + if (nullptr == tuple) { LOG_WARN("failed to get current record: %s", strrc(rc)); return rc; } + RowTuple *row_tuple = static_cast(tuple); + Record &record = row_tuple->record(); rc = table->delete_record(nullptr, &record); if (rc != RC::SUCCESS) { LOG_WARN("failed to delete record: %s", strrc(rc)); diff --git a/src/observer/sql/executor/delete_operator.h b/src/observer/sql/executor/delete_operator.h index 6a28db3..6a4ac77 100644 --- a/src/observer/sql/executor/delete_operator.h +++ b/src/observer/sql/executor/delete_operator.h @@ -32,8 +32,8 @@ public: RC next() override; RC close() override; - RC current_record(Record &record) override { - return RC::GENERIC_ERROR; + Tuple * current_tuple() override { + return nullptr; } private: DeleteStmt *delete_stmt_ = nullptr; diff --git a/src/observer/sql/executor/execute_stage.cpp b/src/observer/sql/executor/execute_stage.cpp index 60fdcc4..0d830b6 100644 --- a/src/observer/sql/executor/execute_stage.cpp +++ b/src/observer/sql/executor/execute_stage.cpp @@ -25,7 +25,6 @@ See the Mulan PSL v2 for more details. */ #include "event/storage_event.h" #include "event/sql_event.h" #include "event/session_event.h" -#include "sql/executor/execution_node.h" #include "sql/executor/tuple.h" #include "sql/executor/table_scan_operator.h" #include "sql/executor/predicate_operator.h" @@ -43,8 +42,8 @@ See the Mulan PSL v2 for more details. */ using namespace common; -RC create_selection_executor( - Trx *trx, const Selects &selects, const char *db, const char *table_name, SelectExeNode &select_node); +//RC create_selection_executor( +// Trx *trx, const Selects &selects, const char *db, const char *table_name, SelectExeNode &select_node); //! Constructor ExecuteStage::ExecuteStage(const char *tag) : Stage(tag) @@ -217,15 +216,15 @@ void end_trx_if_need(Session *session, Trx *trx, bool all_right) } } -void record_to_string(std::ostream &os, const Record &record) +void tuple_to_string(std::ostream &os, const Tuple &tuple) { - Field field; + TupleCell cell; RC rc = RC::SUCCESS; bool first_field = true; - for (int i = 0; i < record.field_amount(); i++) { - rc = record.field_at(i, field); + for (int i = 0; i < tuple.cell_num(); i++) { + rc = tuple.cell_at(i, cell); if (rc != RC::SUCCESS) { - LOG_WARN("failed to fetch field of record. index=%d, rc=%s", i, strrc(rc)); + LOG_WARN("failed to fetch field of cell. index=%d, rc=%s", i, strrc(rc)); break; } @@ -234,7 +233,7 @@ void record_to_string(std::ostream &os, const Record &record) } else { first_field = false; } - field.to_string(os); + cell.to_string(os); } } RC ExecuteStage::do_select(SQLStageEvent *sql_event) @@ -257,17 +256,17 @@ RC ExecuteStage::do_select(SQLStageEvent *sql_event) } std::stringstream ss; - Record record; while ((rc = table_scan_operator.next()) == RC::SUCCESS) { // get current record // write to response - rc = table_scan_operator.current_record(record); - if (rc != RC::SUCCESS) { + Tuple * tuple = table_scan_operator.current_tuple(); + if (nullptr == tuple) { + rc = RC::INTERNAL; LOG_WARN("failed to get current record. rc=%s", strrc(rc)); break; } - record_to_string(ss, record); + tuple_to_string(ss, *tuple); ss << std::endl; } diff --git a/src/observer/sql/executor/execution_node.cpp b/src/observer/sql/executor/execution_node.cpp.bak similarity index 100% rename from src/observer/sql/executor/execution_node.cpp rename to src/observer/sql/executor/execution_node.cpp.bak diff --git a/src/observer/sql/executor/operator.h b/src/observer/sql/executor/operator.h index a5beca7..11dd64a 100644 --- a/src/observer/sql/executor/operator.h +++ b/src/observer/sql/executor/operator.h @@ -16,6 +16,7 @@ See the Mulan PSL v2 for more details. */ #include #include "rc.h" +#include "sql/executor/tuple.h" class Record; @@ -31,7 +32,7 @@ public: virtual RC next() = 0; virtual RC close() = 0; - virtual RC current_record(Record &record) = 0; + virtual Tuple * current_tuple() = 0; void add_child(Operator *oper) { children_.push_back(oper); diff --git a/src/observer/sql/executor/predicate_operator.cpp b/src/observer/sql/executor/predicate_operator.cpp index d13537d..efbb20a 100644 --- a/src/observer/sql/executor/predicate_operator.cpp +++ b/src/observer/sql/executor/predicate_operator.cpp @@ -34,14 +34,16 @@ RC PredicateOperator::next() { RC rc = RC::SUCCESS; Operator *oper = children_[0]; - Record record; + while (RC::SUCCESS == (rc = oper->next())) { - rc = oper->current_record(record); - if (rc != RC::SUCCESS) { + Tuple *tuple = oper->current_tuple(); + if (nullptr == tuple) { + rc = RC::INTERNAL; + LOG_WARN("failed to get tuple from operator"); break; } - if (do_predicate(record)) { + if (do_predicate(static_cast(*tuple))) { return rc; } } @@ -54,15 +56,15 @@ RC PredicateOperator::close() return RC::SUCCESS; } -RC PredicateOperator::current_record(Record &record) +Tuple * PredicateOperator::current_tuple() { - return children_[0]->current_record(record); + return children_[0]->current_tuple(); } -void get_cell(const Record &record, const FilterItem &filter_item, Field &cell) +void get_cell(const RowTuple &tuple, const FilterItem &filter_item, TupleCell &cell) { if (filter_item.is_attr()) { - cell.set_data(record.data() + filter_item.field().field()->offset()); + cell.set_data(tuple.record().data() + filter_item.field().field()->offset()); cell.set_type(filter_item.field().field()->type()); } else { cell.set_data((char *)filter_item.value().data); @@ -70,7 +72,7 @@ void get_cell(const Record &record, const FilterItem &filter_item, Field &cell) } } -bool PredicateOperator::do_predicate(Record &record) +bool PredicateOperator::do_predicate(RowTuple &tuple) { if (filter_stmt_ == nullptr) { return true; @@ -80,10 +82,10 @@ bool PredicateOperator::do_predicate(Record &record) const FilterItem & left = filter_unit.left(); const FilterItem & right = filter_unit.right(); CompOp comp = filter_unit.comp(); - Field left_cell; - Field right_cell; - get_cell(record, left, left_cell); - get_cell(record, right, right_cell); + TupleCell left_cell; + TupleCell right_cell; + get_cell(tuple, left, left_cell); + get_cell(tuple, right, right_cell); const int compare = left_cell.compare(right_cell); switch (comp) { diff --git a/src/observer/sql/executor/predicate_operator.h b/src/observer/sql/executor/predicate_operator.h index c8c3d62..551848f 100644 --- a/src/observer/sql/executor/predicate_operator.h +++ b/src/observer/sql/executor/predicate_operator.h @@ -30,9 +30,9 @@ public: RC next() override; RC close() override; - RC current_record(Record &record) override; + Tuple * current_tuple() override; private: - bool do_predicate(Record &record); + bool do_predicate(RowTuple &tuple); private: FilterStmt *filter_stmt_ = nullptr; }; diff --git a/src/observer/sql/executor/table_scan_operator.cpp b/src/observer/sql/executor/table_scan_operator.cpp index 5845381..3d407b7 100644 --- a/src/observer/sql/executor/table_scan_operator.cpp +++ b/src/observer/sql/executor/table_scan_operator.cpp @@ -18,7 +18,12 @@ See the Mulan PSL v2 for more details. */ RC TableScanOperator::open() { - return table_->get_record_scanner(record_scanner_); + RC rc = table_->get_record_scanner(record_scanner_); + if (rc == RC::SUCCESS) { + tuple_.set_table(table_); + tuple_.set_schema(table_->table_meta().field_metas()); + } + return rc; } RC TableScanOperator::next() @@ -37,8 +42,8 @@ RC TableScanOperator::close() return record_scanner_.close_scan(); } -RC TableScanOperator::current_record(Record &record) +Tuple * TableScanOperator::current_tuple() { - record = current_record_; // TODO should check status - return RC::SUCCESS; + tuple_.set_record(¤t_record_); + return &tuple_; } diff --git a/src/observer/sql/executor/table_scan_operator.h b/src/observer/sql/executor/table_scan_operator.h index b834267..88eb300 100644 --- a/src/observer/sql/executor/table_scan_operator.h +++ b/src/observer/sql/executor/table_scan_operator.h @@ -35,9 +35,10 @@ public: RC next() override; RC close() override; - RC current_record(Record &record) override; + Tuple * current_tuple() override; private: Table *table_ = nullptr; RecordFileScanner record_scanner_; Record current_record_; + RowTuple tuple_; }; diff --git a/src/observer/sql/executor/tuple.cpp b/src/observer/sql/executor/tuple.cpp.bak similarity index 100% rename from src/observer/sql/executor/tuple.cpp rename to src/observer/sql/executor/tuple.cpp.bak diff --git a/src/observer/sql/executor/tuple.h b/src/observer/sql/executor/tuple.h index 2a21b8b..8ad6f88 100644 --- a/src/observer/sql/executor/tuple.h +++ b/src/observer/sql/executor/tuple.h @@ -12,166 +12,107 @@ See the Mulan PSL v2 for more details. */ // Created by Meiyi & Wangyunlai on 2021/5/14. // -#ifndef __OBSERVER_SQL_EXECUTOR_TUPLE_H_ -#define __OBSERVER_SQL_EXECUTOR_TUPLE_H_ +#pragma once #include #include +#include "common/log/log.h" #include "sql/parser/parse.h" #include "sql/executor/value.h" +#include "storage/common/field.h" +#include "storage/common/record.h" class Table; -class Tuple { +class TupleCellSpec +{ + +}; + +class Tuple +{ public: Tuple() = default; + virtual ~Tuple() = default; - Tuple(const Tuple &other); - - ~Tuple(); + virtual int cell_num() const = 0; + virtual RC cell_at(int index, TupleCell &cell) const = 0; - Tuple(Tuple &&other) noexcept; - Tuple &operator=(Tuple &&other) noexcept; - - void add(TupleValue *value); - void add(const std::shared_ptr &other); - void add(int value); - void add(float value); - void add(const char *s, int len); + //virtual RC cell_spec_at(int index, TupleCellSpec &spec) const; +}; - const std::vector> &values() const +class RowTuple : public Tuple +{ +public: + RowTuple() = default; + virtual ~RowTuple() = default; + + void set_record(Record *record) { - return values_; + this->record_ = record; } - int size() const + void set_table(Table *table) { - return values_.size(); + this->table_ = table; } - const TupleValue &get(int index) const + void set_schema(const std::vector *fields) { - return *values_[index]; + this->fields_ = fields; } - const std::shared_ptr &get_pointer(int index) const + int cell_num() const override { - return values_[index]; + return fields_->size(); } -private: - std::vector> values_; -}; - -class TupleField { -public: - TupleField(AttrType type, const char *table_name, const char *field_name) - : type_(type), table_name_(table_name), field_name_(field_name) - {} - - AttrType type() const - { - return type_; - } - - const char *table_name() const - { - return table_name_.c_str(); - } - const char *field_name() const - { - return field_name_.c_str(); - } - - std::string to_string() const; - -private: - AttrType type_; - std::string table_name_; - std::string field_name_; -}; - -class TupleSchema { -public: - TupleSchema() = default; - ~TupleSchema() = default; - - void add(AttrType type, const char *table_name, const char *field_name); - void add_if_not_exists(AttrType type, const char *table_name, const char *field_name); - // void merge(const TupleSchema &other); - void append(const TupleSchema &other); - - const std::vector &fields() const + RC cell_at(int index, TupleCell &cell) const override { - return fields_; + if (index < 0 || index >= fields_->size()) { + LOG_WARN("invalid argument. index=%d", index); + return RC::INVALID_ARGUMENT; + } + + const FieldMeta &field_meta = (*fields_)[index]; + cell.set_table(table_); + cell.set_type(field_meta.type()); + cell.set_data(this->record_->data() + field_meta.offset()); + return RC::SUCCESS; } - const TupleField &field(int index) const + RC cell_spec_at(int index, TupleCellSpec &spec) const { - return fields_[index]; + if (index < 0 || index >= fields_->size()) { + LOG_WARN("invalid argument. index=%d", index); + return RC::INVALID_ARGUMENT; + } + const FieldMeta &field_meta = (*fields_)[index]; + // TODO + return RC::SUCCESS; } - int index_of_field(const char *table_name, const char *field_name) const; - void clear() + Record &record() { - fields_.clear(); + return *record_; } - void print(std::ostream &os) const; - -public: - static void from_table(const Table *table, TupleSchema &schema); - -private: - std::vector fields_; -}; - -class TupleSet { -public: - TupleSet() = default; - TupleSet(TupleSet &&other); - explicit TupleSet(const TupleSchema &schema) : schema_(schema) - {} - TupleSet &operator=(TupleSet &&other); - - ~TupleSet() = default; - - void set_schema(const TupleSchema &schema); - - const TupleSchema &get_schema() const; - - void add(Tuple &&tuple); - - void clear(); - - bool is_empty() const; - int size() const; - const Tuple &get(int index) const; - const std::vector &tuples() const; - - void print(std::ostream &os) const; - -public: - const TupleSchema &schema() const + const Record &record() const { - return schema_; + return *record_; } - private: - std::vector tuples_; - TupleSchema schema_; + Record *record_ = nullptr; + Table *table_ = nullptr; + const std::vector *fields_ = nullptr; }; -class TupleRecordConverter { +/* +class CompositeTuple : public Tuple +{ public: - TupleRecordConverter(Table *table, TupleSet &tuple_set); - - void add_record(const char *record); - private: - Table *table_; - TupleSet &tuple_set_; + std::vector tuples_; }; - -#endif //__OBSERVER_SQL_EXECUTOR_TUPLE_H_ +*/ diff --git a/src/observer/sql/stmt/filter_stmt.h b/src/observer/sql/stmt/filter_stmt.h index 72b09c3..37dad28 100644 --- a/src/observer/sql/stmt/filter_stmt.h +++ b/src/observer/sql/stmt/filter_stmt.h @@ -79,7 +79,7 @@ public: } private: - bool is_attr_ = false; // is an attribute or a value + bool is_attr_ = false; // is an attribute or a value, or maybe an expression in future FilterField field_; Value value_; }; diff --git a/src/observer/storage/common/field.cpp b/src/observer/storage/common/field.cpp index 82c7217..8f66292 100644 --- a/src/observer/storage/common/field.cpp +++ b/src/observer/storage/common/field.cpp @@ -2,7 +2,7 @@ #include "common/log/log.h" #include "util/comparator.h" -void Field::to_string(std::ostream &os) const +void TupleCell::to_string(std::ostream &os) const { switch (attr_type_) { case INTS: { @@ -25,7 +25,7 @@ void Field::to_string(std::ostream &os) const } } -int Field::compare(const Field &other) const +int TupleCell::compare(const TupleCell &other) const { if (this->attr_type_ == other.attr_type_) { switch (this->attr_type_) { diff --git a/src/observer/storage/common/field.h b/src/observer/storage/common/field.h index c9e7038..317dae7 100644 --- a/src/observer/storage/common/field.h +++ b/src/observer/storage/common/field.h @@ -4,14 +4,14 @@ #include "storage/common/table.h" #include "storage/common/field_meta.h" -class Field // TODO rename to Cell +class TupleCell // TODO rename to Cell { public: - Field() = default; + TupleCell() = default; - Field(FieldMeta *meta, char *data) : Field(nullptr, meta, data) + TupleCell(FieldMeta *meta, char *data) : TupleCell(nullptr, meta, data) {} - Field(Table *table, FieldMeta *meta, char *data) + TupleCell(Table *table, FieldMeta *meta, char *data) : table_(table), attr_type_(meta->type()), data_(data) {} @@ -22,7 +22,7 @@ public: void to_string(std::ostream &os) const; - int compare(const Field &other) const; + int compare(const TupleCell &other) const; private: Table *table_ = nullptr; diff --git a/src/observer/storage/common/record.cpp b/src/observer/storage/common/record.cpp index 2292885..40ee000 100644 --- a/src/observer/storage/common/record.cpp +++ b/src/observer/storage/common/record.cpp @@ -3,19 +3,6 @@ #include "common/log/log.h" #include "rc.h" -RC Record::field_at(int index, Field &field) const -{ - if (index < 0 || index >= fields_->size()) { - LOG_WARN("invalid argument. index=%d", index); - return RC::INVALID_ARGUMENT; - } - - const FieldMeta &field_meta = (*fields_)[index]; - field.set_type(field_meta.type()); - field.set_data(this->data_ + field_meta.offset()); - return RC::SUCCESS; -} - RC Record::set_field_value(const Value &value, int index) { // TODO -- GitLab