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

operator & tuple

上级 3d84e93f
......@@ -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<RowTuple *>(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));
......
......@@ -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;
......
......@@ -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;
}
......
......@@ -16,6 +16,7 @@ See the Mulan PSL v2 for more details. */
#include <vector>
#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);
......
......@@ -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<RowTuple &>(*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) {
......
......@@ -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;
};
......@@ -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(&current_record_);
return &tuple_;
}
......@@ -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_;
};
......@@ -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 <memory>
#include <vector>
#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<TupleValue> &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<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:
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
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<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
const Record &record() const
{
return schema_;
return *record_;
}
private:
std::vector<Tuple> tuples_;
TupleSchema schema_;
Record *record_ = nullptr;
Table *table_ = nullptr;
const std::vector<FieldMeta> *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<Tuple *> tuples_;
};
#endif //__OBSERVER_SQL_EXECUTOR_TUPLE_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_;
};
......
......@@ -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_) {
......
......@@ -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;
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册