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
    cell.set_length(field_meta->len());
羽飞's avatar
羽飞 已提交
119
    return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
120 121
  }

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

羽飞's avatar
羽飞 已提交
129 130 131 132 133
    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
羽飞 已提交
134 135 136 137 138 139
	return cell_at(i, cell);
      }
    }
    return RC::NOTFOUND;
  }

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

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

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

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

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

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

羽飞's avatar
羽飞 已提交
194
  void add_cell_spec(TupleCellSpec *spec)
羽飞's avatar
羽飞 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  {
    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
羽飞 已提交
212 213
    const TupleCellSpec *spec = speces_[index];
    return spec->expression()->get_value(*tuple_, cell);
羽飞's avatar
羽飞 已提交
214 215
  }

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