tuple.h 5.1 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11
/* 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. */

//
羽飞's avatar
羽飞 已提交
12
// Created by Wangyunlai on 2021/5/14.
羽飞's avatar
羽飞 已提交
13 14
//

羽飞's avatar
羽飞 已提交
15
#pragma once
羽飞's avatar
羽飞 已提交
16 17 18 19

#include <memory>
#include <vector>

羽飞's avatar
羽飞 已提交
20
#include "common/log/log.h"
羽飞's avatar
羽飞 已提交
21
#include "sql/parser/parse.h"
羽飞's avatar
羽飞 已提交
22 23
#include "sql/expr/tuple_cell.h"
#include "sql/expr/expression.h"
羽飞's avatar
羽飞 已提交
24
#include "storage/common/record.h"
羽飞's avatar
羽飞 已提交
25 26 27

class Table;

羽飞's avatar
羽飞 已提交
28 29
class TupleCellSpec
{
羽飞's avatar
羽飞 已提交
30 31
public:
  TupleCellSpec() = default;
羽飞's avatar
羽飞 已提交
32
  TupleCellSpec(Expression *expr) : expression_(expr)
羽飞's avatar
羽飞 已提交
33 34
  {}

羽飞's avatar
羽飞 已提交
35
  ~TupleCellSpec()
羽飞's avatar
羽飞 已提交
36
  {
羽飞's avatar
羽飞 已提交
37 38 39 40
    if (expression_) {
      delete expression_;
      expression_ = nullptr;
    }
羽飞's avatar
羽飞 已提交
41 42 43 44 45 46 47 48 49 50 51
  }

  void set_alias(const char *alias)
  {
    this->alias_ = alias;
  }
  const char *alias() const
  {
    return alias_;
  }

羽飞's avatar
羽飞 已提交
52
  Expression *expression() const
羽飞's avatar
羽飞 已提交
53
  {
羽飞's avatar
羽飞 已提交
54
    return expression_;
羽飞's avatar
羽飞 已提交
55 56 57 58
  }

private:
  const char *alias_ = nullptr;
羽飞's avatar
羽飞 已提交
59
  Expression *expression_ = nullptr;
羽飞's avatar
羽飞 已提交
60 61 62 63
};

class Tuple
{
羽飞's avatar
羽飞 已提交
64 65
public:
  Tuple() = default;
羽飞's avatar
羽飞 已提交
66
  virtual ~Tuple() = default;
羽飞's avatar
羽飞 已提交
67

羽飞's avatar
羽飞 已提交
68 69
  virtual int cell_num() const = 0; 
  virtual RC  cell_at(int index, TupleCell &cell) const = 0;
羽飞's avatar
羽飞 已提交
70
  virtual RC  find_cell(const Field &field, TupleCell &cell) const = 0;
羽飞's avatar
羽飞 已提交
71

羽飞's avatar
羽飞 已提交
72
  virtual RC  cell_spec_at(int index, const TupleCellSpec *&spec) const = 0;
羽飞's avatar
羽飞 已提交
73
};
羽飞's avatar
羽飞 已提交
74

羽飞's avatar
羽飞 已提交
75 76 77 78
class RowTuple : public Tuple
{
public:
  RowTuple() = default;
羽飞's avatar
羽飞 已提交
79 80 81 82 83 84 85
  virtual ~RowTuple()
  {
    for (TupleCellSpec *spec : speces_) {
      delete spec;
    }
    speces_.clear();
  }
羽飞's avatar
羽飞 已提交
86 87
  
  void set_record(Record *record)
88
  {
羽飞's avatar
羽飞 已提交
89
    this->record_ = record;
羽飞's avatar
羽飞 已提交
90 91
  }

羽飞's avatar
羽飞 已提交
92
  void set_schema(const Table *table, const std::vector<FieldMeta> *fields)
93
  {
羽飞's avatar
羽飞 已提交
94 95 96 97 98
    table_ = table;
    this->speces_.reserve(fields->size());
    for (const FieldMeta &field : *fields) {
      speces_.push_back(new TupleCellSpec(new FieldExpr(table, &field)));
    }
羽飞's avatar
羽飞 已提交
99 100
  }

羽飞's avatar
羽飞 已提交
101
  int cell_num() const override
102
  {
羽飞's avatar
羽飞 已提交
103
    return speces_.size();
羽飞's avatar
羽飞 已提交
104 105
  }

羽飞's avatar
羽飞 已提交
106
  RC cell_at(int index, TupleCell &cell) const override
107
  {
羽飞's avatar
羽飞 已提交
108
    if (index < 0 || index >= speces_.size()) {
羽飞's avatar
羽飞 已提交
109 110 111 112
      LOG_WARN("invalid argument. index=%d", index);
      return RC::INVALID_ARGUMENT;
    }

羽飞's avatar
羽飞 已提交
113 114 115 116 117
    const TupleCellSpec *spec = speces_[index];
    FieldExpr *field_expr = (FieldExpr *)spec->expression();
    const FieldMeta *field_meta = field_expr->field().meta();
    cell.set_type(field_meta->type());
    cell.set_data(this->record_->data() + field_meta->offset());
羽飞's avatar
羽飞 已提交
118
    return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
119 120
  }

羽飞's avatar
羽飞 已提交
121
  RC find_cell(const Field &field, TupleCell &cell) const override
羽飞's avatar
羽飞 已提交
122
  {
羽飞's avatar
羽飞 已提交
123
    const char *table_name = field.table_name();
羽飞's avatar
羽飞 已提交
124 125 126 127
    if (0 != strcmp(table_name, table_->name())) {
      return RC::NOTFOUND;
    }

羽飞's avatar
羽飞 已提交
128 129 130 131 132
    const char *field_name = field.field_name();
    for (int i = 0; i < speces_.size(); ++i) {
      const FieldExpr * field_expr = (const FieldExpr *)speces_[i]->expression();
      const Field &field = field_expr->field();
      if (0 == strcmp(field_name, field.field_name())) {
羽飞's avatar
羽飞 已提交
133 134 135 136 137 138
	return cell_at(i, cell);
      }
    }
    return RC::NOTFOUND;
  }

羽飞's avatar
羽飞 已提交
139
  RC cell_spec_at(int index, const TupleCellSpec *&spec) const override
140
  {
羽飞's avatar
羽飞 已提交
141
    if (index < 0 || index >= speces_.size()) {
羽飞's avatar
羽飞 已提交
142 143 144
      LOG_WARN("invalid argument. index=%d", index);
      return RC::INVALID_ARGUMENT;
    }
羽飞's avatar
羽飞 已提交
145
    spec = speces_[index];
羽飞's avatar
羽飞 已提交
146
    return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
147 148
  }

羽飞's avatar
羽飞 已提交
149
  Record &record()
150
  {
羽飞's avatar
羽飞 已提交
151
    return *record_;
羽飞's avatar
羽飞 已提交
152 153
  }

羽飞's avatar
羽飞 已提交
154
  const Record &record() const
155
  {
羽飞's avatar
羽飞 已提交
156
    return *record_;
羽飞's avatar
羽飞 已提交
157 158
  }
private:
羽飞's avatar
羽飞 已提交
159
  Record *record_ = nullptr;
羽飞's avatar
羽飞 已提交
160
  const Table *table_ = nullptr;
羽飞's avatar
羽飞 已提交
161
  std::vector<TupleCellSpec *> speces_;
羽飞's avatar
羽飞 已提交
162 163
};

羽飞's avatar
羽飞 已提交
164 165 166
/*
class CompositeTuple : public Tuple
{
羽飞's avatar
羽飞 已提交
167
public:
羽飞's avatar
羽飞 已提交
168 169
  int cell_num() const override; 
  RC  cell_at(int index, TupleCell &cell) const = 0;
羽飞's avatar
羽飞 已提交
170
private:
羽飞's avatar
羽飞 已提交
171
  int cell_num_ = 0;
羽飞's avatar
羽飞 已提交
172
  std::vector<Tuple *> tuples_;
羽飞's avatar
羽飞 已提交
173
};
羽飞's avatar
羽飞 已提交
174
*/
羽飞's avatar
羽飞 已提交
175 176 177 178 179

class ProjectTuple : public Tuple
{
public:
  ProjectTuple() = default;
羽飞's avatar
羽飞 已提交
180 181 182 183 184 185 186
  virtual ~ProjectTuple()
  {
    for (TupleCellSpec *spec : speces_) {
      delete spec;
    }
    speces_.clear();
  }
羽飞's avatar
羽飞 已提交
187 188 189 190 191 192

  void set_tuple(Tuple *tuple)
  {
    this->tuple_ = tuple;
  }

羽飞's avatar
羽飞 已提交
193
  void add_cell_spec(TupleCellSpec *spec)
羽飞's avatar
羽飞 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  {
    speces_.push_back(spec);
  }
  int cell_num() const override
  {
    return speces_.size();
  }

  RC cell_at(int index, TupleCell &cell) const override
  {
    if (index < 0 || index >= speces_.size()) {
      return RC::GENERIC_ERROR;
    }
    if (tuple_ == nullptr) {
      return RC::GENERIC_ERROR;
    }

羽飞's avatar
羽飞 已提交
211 212
    const TupleCellSpec *spec = speces_[index];
    return spec->expression()->get_value(*tuple_, cell);
羽飞's avatar
羽飞 已提交
213 214
  }

羽飞's avatar
羽飞 已提交
215
  RC find_cell(const Field &field, TupleCell &cell) const override
羽飞's avatar
羽飞 已提交
216
  {
羽飞's avatar
羽飞 已提交
217
    return tuple_->find_cell(field, cell);
羽飞's avatar
羽飞 已提交
218
  }
羽飞's avatar
羽飞 已提交
219
  RC cell_spec_at(int index, const TupleCellSpec *&spec) const override
羽飞's avatar
羽飞 已提交
220 221 222 223 224 225 226 227
  {
    if (index < 0 || index >= speces_.size()) {
      return RC::NOTFOUND;
    }
    spec = speces_[index];
    return RC::SUCCESS;
  }
private:
羽飞's avatar
羽飞 已提交
228
  std::vector<TupleCellSpec *> speces_;
羽飞's avatar
羽飞 已提交
229 230
  Tuple *tuple_ = nullptr;
};