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

move operators to operator directory

上级 5f4ea56d
......@@ -26,10 +26,10 @@ See the Mulan PSL v2 for more details. */
#include "event/sql_event.h"
#include "event/session_event.h"
#include "sql/executor/tuple.h"
#include "sql/executor/table_scan_operator.h"
#include "sql/executor/predicate_operator.h"
#include "sql/executor/delete_operator.h"
#include "sql/executor/project_operator.h"
#include "sql/operator/table_scan_operator.h"
#include "sql/operator/predicate_operator.h"
#include "sql/operator/delete_operator.h"
#include "sql/operator/project_operator.h"
#include "sql/stmt/stmt.h"
#include "sql/stmt/select_stmt.h"
#include "sql/stmt/update_stmt.h"
......
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
//
// Created by Meiyi & Wangyunlai on 2021/5/14.
//
#include "sql/executor/execution_node.h"
#include "storage/common/table.h"
#include "common/log/log.h"
SelectExeNode::SelectExeNode() : table_(nullptr)
{}
SelectExeNode::~SelectExeNode()
{
for (DefaultConditionFilter *&filter : condition_filters_) {
delete filter;
}
condition_filters_.clear();
}
RC SelectExeNode::init(
Trx *trx, Table *table, TupleSchema &&tuple_schema, std::vector<DefaultConditionFilter *> &&condition_filters)
{
trx_ = trx;
table_ = table;
tuple_schema_ = tuple_schema;
condition_filters_ = std::move(condition_filters);
return RC::SUCCESS;
}
void record_reader(const char *data, void *context)
{
TupleRecordConverter *converter = (TupleRecordConverter *)context;
converter->add_record(data);
}
RC SelectExeNode::execute(TupleSet &tuple_set)
{
CompositeConditionFilter condition_filter;
condition_filter.init((const ConditionFilter **)condition_filters_.data(), condition_filters_.size());
tuple_set.clear();
tuple_set.set_schema(tuple_schema_);
TupleRecordConverter converter(table_, tuple_set);
return table_->scan_record(trx_, &condition_filter, -1, (void *)&converter, record_reader);
}
\ No newline at end of file
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
//
// Created by Meiyi & Wangyunlai on 2021/5/13.
//
#ifndef __OBSERVER_SQL_EXECUTOR_EXECUTION_NODE_H_
#define __OBSERVER_SQL_EXECUTOR_EXECUTION_NODE_H_
#include <vector>
#include "storage/common/condition_filter.h"
#include "sql/executor/tuple.h"
class Table;
class Trx;
class ExecutionNode {
public:
ExecutionNode() = default;
virtual ~ExecutionNode() = default;
virtual RC execute(TupleSet &tuple_set) = 0;
};
class SelectExeNode : public ExecutionNode {
public:
SelectExeNode();
virtual ~SelectExeNode();
RC init(
Trx *trx, Table *table, TupleSchema &&tuple_schema, std::vector<DefaultConditionFilter *> &&condition_filters);
RC execute(TupleSet &tuple_set) override;
private:
Trx *trx_ = nullptr;
Table *table_;
TupleSchema tuple_schema_;
std::vector<DefaultConditionFilter *> condition_filters_;
};
#endif //__OBSERVER_SQL_EXECUTOR_EXECUTION_NODE_H_
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
//
// Created by WangYunlai on 2021/6/9.
//
#include "sql/executor/predicate.h"
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
//
// Created by WangYunlai on 2021/6/10.
//
#pragma once
class Predicate
{
public:
Predicate()
{}
virtual ~Predicate() = default;
private:
};
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
//
// Created by Meiyi & Wangyunlai on 2021/5/14.
//
#include "sql/executor/tuple.h"
#include "storage/common/table.h"
#include "common/log/log.h"
Tuple::Tuple(const Tuple &other)
{
LOG_PANIC("Copy constructor of tuple is not supported");
exit(1);
}
Tuple::Tuple(Tuple &&other) noexcept : values_(std::move(other.values_))
{}
Tuple &Tuple::operator=(Tuple &&other) noexcept
{
if (&other == this) {
return *this;
}
values_.clear();
values_.swap(other.values_);
return *this;
}
Tuple::~Tuple()
{}
// add (Value && value)
void Tuple::add(TupleValue *value)
{
values_.emplace_back(value);
}
void Tuple::add(const std::shared_ptr<TupleValue> &other)
{
values_.emplace_back(other);
}
void Tuple::add(int value)
{
add(new IntValue(value));
}
void Tuple::add(float value)
{
add(new FloatValue(value));
}
void Tuple::add(const char *s, int len)
{
add(new StringValue(s, len));
}
////////////////////////////////////////////////////////////////////////////////
std::string TupleField::to_string() const
{
return std::string(table_name_) + "." + field_name_ + std::to_string(type_);
}
////////////////////////////////////////////////////////////////////////////////
void TupleSchema::from_table(const Table *table, TupleSchema &schema)
{
const char *table_name = table->name();
const TableMeta &table_meta = table->table_meta();
const int field_num = table_meta.field_num();
for (int i = 0; i < field_num; i++) {
const FieldMeta *field_meta = table_meta.field(i);
if (field_meta->visible()) {
schema.add(field_meta->type(), table_name, field_meta->name());
}
}
}
void TupleSchema::add(AttrType type, const char *table_name, const char *field_name)
{
fields_.emplace_back(type, table_name, field_name);
}
void TupleSchema::add_if_not_exists(AttrType type, const char *table_name, const char *field_name)
{
for (const auto &field : fields_) {
if (0 == strcmp(field.table_name(), table_name) && 0 == strcmp(field.field_name(), field_name)) {
return;
}
}
add(type, table_name, field_name);
}
void TupleSchema::append(const TupleSchema &other)
{
fields_.reserve(fields_.size() + other.fields_.size());
for (const auto &field : other.fields_) {
fields_.emplace_back(field);
}
}
int TupleSchema::index_of_field(const char *table_name, const char *field_name) const
{
const int size = fields_.size();
for (int i = 0; i < size; i++) {
const TupleField &field = fields_[i];
if (0 == strcmp(field.table_name(), table_name) && 0 == strcmp(field.field_name(), field_name)) {
return i;
}
}
return -1;
}
void TupleSchema::print(std::ostream &os) const
{
if (fields_.empty()) {
os << "No schema";
return;
}
// 判断有多张表还是只有一张表
std::set<std::string> table_names;
for (const auto &field : fields_) {
table_names.insert(field.table_name());
}
for (std::vector<TupleField>::const_iterator iter = fields_.begin(), end = --fields_.end(); iter != end; ++iter) {
if (table_names.size() > 1) {
os << iter->table_name() << ".";
}
os << iter->field_name() << " | ";
}
if (table_names.size() > 1) {
os << fields_.back().table_name() << ".";
}
os << fields_.back().field_name() << std::endl;
}
/////////////////////////////////////////////////////////////////////////////
TupleSet::TupleSet(TupleSet &&other) : tuples_(std::move(other.tuples_)), schema_(other.schema_)
{
other.schema_.clear();
}
TupleSet &TupleSet::operator=(TupleSet &&other)
{
if (this == &other) {
return *this;
}
schema_.clear();
schema_.append(other.schema_);
other.schema_.clear();
tuples_.clear();
tuples_.swap(other.tuples_);
return *this;
}
void TupleSet::add(Tuple &&tuple)
{
tuples_.emplace_back(std::move(tuple));
}
void TupleSet::clear()
{
tuples_.clear();
schema_.clear();
}
void TupleSet::print(std::ostream &os) const
{
if (schema_.fields().empty()) {
LOG_WARN("Got empty schema");
return;
}
schema_.print(os);
for (const Tuple &item : tuples_) {
const std::vector<std::shared_ptr<TupleValue>> &values = item.values();
for (std::vector<std::shared_ptr<TupleValue>>::const_iterator iter = values.begin(), end = --values.end();
iter != end;
++iter) {
(*iter)->to_string(os);
os << " | ";
}
values.back()->to_string(os);
os << std::endl;
}
}
void TupleSet::set_schema(const TupleSchema &schema)
{
schema_ = schema;
}
const TupleSchema &TupleSet::get_schema() const
{
return schema_;
}
bool TupleSet::is_empty() const
{
return tuples_.empty();
}
int TupleSet::size() const
{
return tuples_.size();
}
const Tuple &TupleSet::get(int index) const
{
return tuples_[index];
}
const std::vector<Tuple> &TupleSet::tuples() const
{
return tuples_;
}
/////////////////////////////////////////////////////////////////////////////
TupleRecordConverter::TupleRecordConverter(Table *table, TupleSet &tuple_set) : table_(table), tuple_set_(tuple_set)
{}
void TupleRecordConverter::add_record(const char *record)
{
const TupleSchema &schema = tuple_set_.schema();
Tuple tuple;
const TableMeta &table_meta = table_->table_meta();
for (const TupleField &field : schema.fields()) {
const FieldMeta *field_meta = table_meta.field(field.field_name());
assert(field_meta != nullptr);
switch (field_meta->type()) {
case INTS: {
int value = *(int *)(record + field_meta->offset());
tuple.add(value);
} break;
case FLOATS: {
float value = *(float *)(record + field_meta->offset());
tuple.add(value);
} break;
case CHARS: {
const char *s = record + field_meta->offset(); // 现在当做Cstring来处理
tuple.add(s, strlen(s));
} break;
default: {
LOG_PANIC("Unsupported field type. type=%d", field_meta->type());
}
}
}
tuple_set_.add(std::move(tuple));
}
......@@ -19,7 +19,6 @@ See the Mulan PSL v2 for more details. */
#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"
......
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
//
// Created by Meiyi & Wangyunlai on 2021/5/14.
//
#ifndef __OBSERVER_SQL_EXECUTOR_VALUE_H_
#define __OBSERVER_SQL_EXECUTOR_VALUE_H_
#include <string.h>
#include <string>
#include <ostream>
class TupleValue {
public:
TupleValue() = default;
virtual ~TupleValue() = default;
virtual void to_string(std::ostream &os) const = 0;
virtual int compare(const TupleValue &other) const = 0;
private:
};
class IntValue : public TupleValue {
public:
explicit IntValue(int value) : value_(value)
{}
void to_string(std::ostream &os) const override
{
os << value_;
}
int compare(const TupleValue &other) const override
{
const IntValue &int_other = (const IntValue &)other;
return value_ - int_other.value_;
}
private:
int value_;
};
class FloatValue : public TupleValue {
public:
explicit FloatValue(float value) : value_(value)
{}
void to_string(std::ostream &os) const override
{
os << value_;
}
int compare(const TupleValue &other) const override
{
const FloatValue &float_other = (const FloatValue &)other;
float result = value_ - float_other.value_;
if (result > 0) { // 浮点数没有考虑精度问题
return 1;
}
if (result < 0) {
return -1;
}
return 0;
}
private:
float value_;
};
class StringValue : public TupleValue {
public:
StringValue(const char *value, int len) : value_(value, len)
{}
explicit StringValue(const char *value) : value_(value)
{}
void to_string(std::ostream &os) const override
{
os << value_;
}
int compare(const TupleValue &other) const override
{
const StringValue &string_other = (const StringValue &)other;
return strcmp(value_.c_str(), string_other.value_.c_str());
}
private:
std::string value_;
};
#endif //__OBSERVER_SQL_EXECUTOR_VALUE_H_
......@@ -13,7 +13,7 @@ See the Mulan PSL v2 for more details. */
//
#include "common/log/log.h"
#include "sql/executor/delete_operator.h"
#include "sql/operator/delete_operator.h"
#include "storage/common/record.h"
#include "storage/common/table.h"
#include "sql/stmt/delete_stmt.h"
......
......@@ -14,7 +14,7 @@ See the Mulan PSL v2 for more details. */
#pragma once
#include "sql/executor/operator.h"
#include "sql/operator/operator.h"
#include "rc.h"
class DeleteStmt;
......
......@@ -12,7 +12,7 @@ See the Mulan PSL v2 for more details. */
// Created by WangYunlai on 2021/6/9.
//
#include "sql/executor/insert_operator.h"
#include "sql/operator/insert_operator.h"
#include "sql/stmt/insert_stmt.h"
#include "storage/common/table.h"
#include "rc.h"
......
......@@ -14,8 +14,7 @@ See the Mulan PSL v2 for more details. */
#pragma once
#include "common/seda/stage.h"
#include "sql/executor/operator.h"
#include "sql/operator/operator.h"
#include "sql/parser/parse.h"
#include "rc.h"
......
......@@ -12,7 +12,7 @@ See the Mulan PSL v2 for more details. */
// Created by WangYunlai on 2021/6/10.
//
#include "sql/executor/join_operator.h"
#include "sql/operator/join_operator.h"
RC JoinOperator::open()
{
......
......@@ -15,7 +15,7 @@ See the Mulan PSL v2 for more details. */
#pragma once
#include "sql/parser/parse.h"
#include "sql/executor/operator.h"
#include "sql/operator/operator.h"
#include "rc.h"
class JoinPredicate;
......
......@@ -13,7 +13,7 @@ See the Mulan PSL v2 for more details. */
//
#include "common/log/log.h"
#include "sql/executor/predicate_operator.h"
#include "sql/operator/predicate_operator.h"
#include "storage/common/record.h"
#include "sql/stmt/filter_stmt.h"
#include "storage/common/field.h"
......
......@@ -14,7 +14,7 @@ See the Mulan PSL v2 for more details. */
#pragma once
#include "sql/executor/operator.h"
#include "sql/operator/operator.h"
class FilterStmt;
class PredicateOperator : public Operator
......
......@@ -13,7 +13,7 @@ See the Mulan PSL v2 for more details. */
//
#include "common/log/log.h"
#include "sql/executor/project_operator.h"
#include "sql/operator/project_operator.h"
#include "storage/common/record.h"
#include "storage/common/table.h"
......
......@@ -14,7 +14,7 @@ See the Mulan PSL v2 for more details. */
#pragma once
#include "sql/executor/operator.h"
#include "sql/operator/operator.h"
#include "rc.h"
class ProjectOperator : public Operator
......
......@@ -12,7 +12,7 @@ See the Mulan PSL v2 for more details. */
// Created by WangYunlai on 2021/6/9.
//
#include "sql/executor/table_scan_operator.h"
#include "sql/operator/table_scan_operator.h"
#include "storage/common/table.h"
#include "rc.h"
......@@ -33,7 +33,6 @@ RC TableScanOperator::next()
}
RC rc = record_scanner_.next(current_record_);
current_record_.set_fields(table_->table_meta().field_metas());
return rc;
}
......
......@@ -14,13 +14,11 @@ See the Mulan PSL v2 for more details. */
#pragma once
#include "sql/executor/predicate.h"
#include "sql/executor/operator.h"
#include "sql/operator/operator.h"
#include "storage/common/record_manager.h"
#include "rc.h"
class Table;
class Predicate;
class TableScanOperator : public Operator
{
......
......@@ -2,15 +2,3 @@
#include "storage/common/field.h"
#include "common/log/log.h"
#include "rc.h"
RC Record::set_field_value(const Value &value, int index)
{
// TODO
return RC::UNIMPLENMENT;
}
RC Record::set_field_values(const Value *values, int value_num, int start_index)
{
// TODO
return RC::UNIMPLENMENT;
}
......@@ -91,22 +91,12 @@ public:
char *data() { return this->data_; }
const char *data() const { return this->data_; }
void set_fields(const std::vector<FieldMeta> *fields) { this->fields_ = fields; }
const std::vector<FieldMeta> *field_metas() const { return fields_; }
RC field_at(int index, Field &field) const;
int field_amount() const { return fields_->size();}
void set_rid(const RID &rid) { this->rid_ = rid; }
void set_rid(const PageNum page_num, const SlotNum slot_num) { this->rid_.page_num = page_num; this->rid_.slot_num = slot_num; }
RID & rid() { return rid_; }
const RID &rid() const { return rid_; };
RC set_field_value(const Value &value, int index);
RC set_field_values(const Value *values, int value_num, int start_index);
private:
const std::vector<FieldMeta> * fields_ = nullptr;
RID rid_;
// the data buffer
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册