提交 263a6e3e 编写于 作者: 羽飞's avatar 羽飞

operator & tuple

上级 3d84e93f
...@@ -32,15 +32,16 @@ RC DeleteOperator::open() ...@@ -32,15 +32,16 @@ RC DeleteOperator::open()
return rc; return rc;
} }
Record record;
Table *table = delete_stmt_->table(); Table *table = delete_stmt_->table();
while (RC::SUCCESS == (rc = child->next())) { while (RC::SUCCESS == (rc = child->next())) {
rc = child->current_record(record); Tuple *tuple = child->current_tuple();
if (rc != RC::SUCCESS) { if (nullptr == tuple) {
LOG_WARN("failed to get current record: %s", strrc(rc)); LOG_WARN("failed to get current record: %s", strrc(rc));
return rc; return rc;
} }
RowTuple *row_tuple = static_cast<RowTuple *>(tuple);
Record &record = row_tuple->record();
rc = table->delete_record(nullptr, &record); rc = table->delete_record(nullptr, &record);
if (rc != RC::SUCCESS) { if (rc != RC::SUCCESS) {
LOG_WARN("failed to delete record: %s", strrc(rc)); LOG_WARN("failed to delete record: %s", strrc(rc));
......
...@@ -32,8 +32,8 @@ public: ...@@ -32,8 +32,8 @@ public:
RC next() override; RC next() override;
RC close() override; RC close() override;
RC current_record(Record &record) override { Tuple * current_tuple() override {
return RC::GENERIC_ERROR; return nullptr;
} }
private: private:
DeleteStmt *delete_stmt_ = nullptr; DeleteStmt *delete_stmt_ = nullptr;
......
...@@ -25,7 +25,6 @@ See the Mulan PSL v2 for more details. */ ...@@ -25,7 +25,6 @@ See the Mulan PSL v2 for more details. */
#include "event/storage_event.h" #include "event/storage_event.h"
#include "event/sql_event.h" #include "event/sql_event.h"
#include "event/session_event.h" #include "event/session_event.h"
#include "sql/executor/execution_node.h"
#include "sql/executor/tuple.h" #include "sql/executor/tuple.h"
#include "sql/executor/table_scan_operator.h" #include "sql/executor/table_scan_operator.h"
#include "sql/executor/predicate_operator.h" #include "sql/executor/predicate_operator.h"
...@@ -43,8 +42,8 @@ See the Mulan PSL v2 for more details. */ ...@@ -43,8 +42,8 @@ See the Mulan PSL v2 for more details. */
using namespace common; using namespace common;
RC create_selection_executor( //RC create_selection_executor(
Trx *trx, const Selects &selects, const char *db, const char *table_name, SelectExeNode &select_node); // Trx *trx, const Selects &selects, const char *db, const char *table_name, SelectExeNode &select_node);
//! Constructor //! Constructor
ExecuteStage::ExecuteStage(const char *tag) : Stage(tag) ExecuteStage::ExecuteStage(const char *tag) : Stage(tag)
...@@ -217,15 +216,15 @@ void end_trx_if_need(Session *session, Trx *trx, bool all_right) ...@@ -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; RC rc = RC::SUCCESS;
bool first_field = true; bool first_field = true;
for (int i = 0; i < record.field_amount(); i++) { for (int i = 0; i < tuple.cell_num(); i++) {
rc = record.field_at(i, field); rc = tuple.cell_at(i, cell);
if (rc != RC::SUCCESS) { 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; break;
} }
...@@ -234,7 +233,7 @@ void record_to_string(std::ostream &os, const Record &record) ...@@ -234,7 +233,7 @@ void record_to_string(std::ostream &os, const Record &record)
} else { } else {
first_field = false; first_field = false;
} }
field.to_string(os); cell.to_string(os);
} }
} }
RC ExecuteStage::do_select(SQLStageEvent *sql_event) RC ExecuteStage::do_select(SQLStageEvent *sql_event)
...@@ -257,17 +256,17 @@ RC ExecuteStage::do_select(SQLStageEvent *sql_event) ...@@ -257,17 +256,17 @@ RC ExecuteStage::do_select(SQLStageEvent *sql_event)
} }
std::stringstream ss; std::stringstream ss;
Record record;
while ((rc = table_scan_operator.next()) == RC::SUCCESS) { while ((rc = table_scan_operator.next()) == RC::SUCCESS) {
// get current record // get current record
// write to response // write to response
rc = table_scan_operator.current_record(record); Tuple * tuple = table_scan_operator.current_tuple();
if (rc != RC::SUCCESS) { if (nullptr == tuple) {
rc = RC::INTERNAL;
LOG_WARN("failed to get current record. rc=%s", strrc(rc)); LOG_WARN("failed to get current record. rc=%s", strrc(rc));
break; break;
} }
record_to_string(ss, record); tuple_to_string(ss, *tuple);
ss << std::endl; ss << std::endl;
} }
......
...@@ -16,6 +16,7 @@ See the Mulan PSL v2 for more details. */ ...@@ -16,6 +16,7 @@ See the Mulan PSL v2 for more details. */
#include <vector> #include <vector>
#include "rc.h" #include "rc.h"
#include "sql/executor/tuple.h"
class Record; class Record;
...@@ -31,7 +32,7 @@ public: ...@@ -31,7 +32,7 @@ public:
virtual RC next() = 0; virtual RC next() = 0;
virtual RC close() = 0; virtual RC close() = 0;
virtual RC current_record(Record &record) = 0; virtual Tuple * current_tuple() = 0;
void add_child(Operator *oper) { void add_child(Operator *oper) {
children_.push_back(oper); children_.push_back(oper);
......
...@@ -34,14 +34,16 @@ RC PredicateOperator::next() ...@@ -34,14 +34,16 @@ RC PredicateOperator::next()
{ {
RC rc = RC::SUCCESS; RC rc = RC::SUCCESS;
Operator *oper = children_[0]; Operator *oper = children_[0];
Record record;
while (RC::SUCCESS == (rc = oper->next())) { while (RC::SUCCESS == (rc = oper->next())) {
rc = oper->current_record(record); Tuple *tuple = oper->current_tuple();
if (rc != RC::SUCCESS) { if (nullptr == tuple) {
rc = RC::INTERNAL;
LOG_WARN("failed to get tuple from operator");
break; break;
} }
if (do_predicate(record)) { if (do_predicate(static_cast<RowTuple &>(*tuple))) {
return rc; return rc;
} }
} }
...@@ -54,15 +56,15 @@ RC PredicateOperator::close() ...@@ -54,15 +56,15 @@ RC PredicateOperator::close()
return RC::SUCCESS; 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()) { 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()); cell.set_type(filter_item.field().field()->type());
} else { } else {
cell.set_data((char *)filter_item.value().data); cell.set_data((char *)filter_item.value().data);
...@@ -70,7 +72,7 @@ void get_cell(const Record &record, const FilterItem &filter_item, Field &cell) ...@@ -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) { if (filter_stmt_ == nullptr) {
return true; return true;
...@@ -80,10 +82,10 @@ bool PredicateOperator::do_predicate(Record &record) ...@@ -80,10 +82,10 @@ bool PredicateOperator::do_predicate(Record &record)
const FilterItem & left = filter_unit.left(); const FilterItem & left = filter_unit.left();
const FilterItem & right = filter_unit.right(); const FilterItem & right = filter_unit.right();
CompOp comp = filter_unit.comp(); CompOp comp = filter_unit.comp();
Field left_cell; TupleCell left_cell;
Field right_cell; TupleCell right_cell;
get_cell(record, left, left_cell); get_cell(tuple, left, left_cell);
get_cell(record, right, right_cell); get_cell(tuple, right, right_cell);
const int compare = left_cell.compare(right_cell); const int compare = left_cell.compare(right_cell);
switch (comp) { switch (comp) {
......
...@@ -30,9 +30,9 @@ public: ...@@ -30,9 +30,9 @@ public:
RC next() override; RC next() override;
RC close() override; RC close() override;
RC current_record(Record &record) override; Tuple * current_tuple() override;
private: private:
bool do_predicate(Record &record); bool do_predicate(RowTuple &tuple);
private: private:
FilterStmt *filter_stmt_ = nullptr; FilterStmt *filter_stmt_ = nullptr;
}; };
...@@ -18,7 +18,12 @@ See the Mulan PSL v2 for more details. */ ...@@ -18,7 +18,12 @@ See the Mulan PSL v2 for more details. */
RC TableScanOperator::open() 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() RC TableScanOperator::next()
...@@ -37,8 +42,8 @@ RC TableScanOperator::close() ...@@ -37,8 +42,8 @@ RC TableScanOperator::close()
return record_scanner_.close_scan(); return record_scanner_.close_scan();
} }
RC TableScanOperator::current_record(Record &record) Tuple * TableScanOperator::current_tuple()
{ {
record = current_record_; // TODO should check status tuple_.set_record(&current_record_);
return RC::SUCCESS; return &tuple_;
} }
...@@ -35,9 +35,10 @@ public: ...@@ -35,9 +35,10 @@ public:
RC next() override; RC next() override;
RC close() override; RC close() override;
RC current_record(Record &record) override; Tuple * current_tuple() override;
private: private:
Table *table_ = nullptr; Table *table_ = nullptr;
RecordFileScanner record_scanner_; RecordFileScanner record_scanner_;
Record current_record_; Record current_record_;
RowTuple tuple_;
}; };
...@@ -12,166 +12,107 @@ See the Mulan PSL v2 for more details. */ ...@@ -12,166 +12,107 @@ See the Mulan PSL v2 for more details. */
// Created by Meiyi & Wangyunlai on 2021/5/14. // Created by Meiyi & Wangyunlai on 2021/5/14.
// //
#ifndef __OBSERVER_SQL_EXECUTOR_TUPLE_H_ #pragma once
#define __OBSERVER_SQL_EXECUTOR_TUPLE_H_
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "common/log/log.h"
#include "sql/parser/parse.h" #include "sql/parser/parse.h"
#include "sql/executor/value.h" #include "sql/executor/value.h"
#include "storage/common/field.h"
#include "storage/common/record.h"
class Table; class Table;
class Tuple { class TupleCellSpec
{
};
class Tuple
{
public: public:
Tuple() = default; Tuple() = default;
virtual ~Tuple() = default;
Tuple(const Tuple &other); virtual int cell_num() const = 0;
virtual RC cell_at(int index, TupleCell &cell) const = 0;
~Tuple();
Tuple(Tuple &&other) noexcept; //virtual RC cell_spec_at(int index, TupleCellSpec &spec) const;
Tuple &operator=(Tuple &&other) noexcept; };
void add(TupleValue *value);
void add(const std::shared_ptr<TupleValue> &other);
void add(int value);
void add(float value);
void add(const char *s, int len);
const std::vector<std::shared_ptr<TupleValue>> &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<FieldMeta> *fields)
{ {
return *values_[index]; this->fields_ = fields;
} }
const std::shared_ptr<TupleValue> &get_pointer(int index) const int cell_num() const override
{ {
return values_[index]; return fields_->size();
} }
private: RC cell_at(int index, TupleCell &cell) const override
std::vector<std::shared_ptr<TupleValue>> 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<TupleField> &fields() const
{ {
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; Record &record()
void clear()
{ {
fields_.clear(); return *record_;
} }
void print(std::ostream &os) const; const Record &record() const
public:
static void from_table(const Table *table, TupleSchema &schema);
private:
std::vector<TupleField> 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<Tuple> &tuples() const;
void print(std::ostream &os) const;
public:
const TupleSchema &schema() const
{ {
return schema_; return *record_;
} }
private: private:
std::vector<Tuple> tuples_; Record *record_ = nullptr;
TupleSchema schema_; Table *table_ = nullptr;
const std::vector<FieldMeta> *fields_ = nullptr;
}; };
class TupleRecordConverter { /*
class CompositeTuple : public Tuple
{
public: public:
TupleRecordConverter(Table *table, TupleSet &tuple_set);
void add_record(const char *record);
private: private:
Table *table_; std::vector<Tuple *> tuples_;
TupleSet &tuple_set_;
}; };
*/
#endif //__OBSERVER_SQL_EXECUTOR_TUPLE_H_
...@@ -79,7 +79,7 @@ public: ...@@ -79,7 +79,7 @@ public:
} }
private: 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_; FilterField field_;
Value value_; Value value_;
}; };
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include "common/log/log.h" #include "common/log/log.h"
#include "util/comparator.h" #include "util/comparator.h"
void Field::to_string(std::ostream &os) const void TupleCell::to_string(std::ostream &os) const
{ {
switch (attr_type_) { switch (attr_type_) {
case INTS: { case INTS: {
...@@ -25,7 +25,7 @@ void Field::to_string(std::ostream &os) const ...@@ -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_) { if (this->attr_type_ == other.attr_type_) {
switch (this->attr_type_) { switch (this->attr_type_) {
......
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
#include "storage/common/table.h" #include "storage/common/table.h"
#include "storage/common/field_meta.h" #include "storage/common/field_meta.h"
class Field // TODO rename to Cell class TupleCell // TODO rename to Cell
{ {
public: 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) : table_(table), attr_type_(meta->type()), data_(data)
{} {}
...@@ -22,7 +22,7 @@ public: ...@@ -22,7 +22,7 @@ public:
void to_string(std::ostream &os) const; void to_string(std::ostream &os) const;
int compare(const Field &other) const; int compare(const TupleCell &other) const;
private: private:
Table *table_ = nullptr; Table *table_ = nullptr;
......
...@@ -3,19 +3,6 @@ ...@@ -3,19 +3,6 @@
#include "common/log/log.h" #include "common/log/log.h"
#include "rc.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) RC Record::set_field_value(const Value &value, int index)
{ {
// TODO // TODO
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册