tuple.h 6.4 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11
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/record/record.h"
羽飞's avatar
羽飞 已提交
25 26 27

class Table;

羽飞's avatar
羽飞 已提交
28 29
class TupleSchema 
{
L
Longda Feng 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
public:
  void append_cell(const TupleCellSpec &cell)
  {
    cells_.push_back(cell);
  }
  void append_cell(const char *table, const char *field)
  {
    append_cell(TupleCellSpec(table, field));
  }
  void append_cell(const char *alias)
  {
    append_cell(TupleCellSpec(alias));
  }
  int cell_num() const
  {
    return static_cast<int>(cells_.size());
  }
  const TupleCellSpec &cell_at(int i) const
  {
    return cells_[i];
  }
羽飞's avatar
羽飞 已提交
51 52

private:
羽飞's avatar
羽飞 已提交
53
  std::vector<TupleCellSpec> cells_;
羽飞's avatar
羽飞 已提交
54 55
};

羽飞's avatar
羽飞 已提交
56 57
class Tuple 
{
羽飞's avatar
羽飞 已提交
58 59
public:
  Tuple() = default;
羽飞's avatar
羽飞 已提交
60
  virtual ~Tuple() = default;
羽飞's avatar
羽飞 已提交
61

L
Longda Feng 已提交
62 63 64
  virtual int cell_num() const = 0;
  virtual RC cell_at(int index, TupleCell &cell) const = 0;
  virtual RC find_cell(const TupleCellSpec &spec, TupleCell &cell) const = 0;
羽飞's avatar
羽飞 已提交
65
};
羽飞's avatar
羽飞 已提交
66

羽飞's avatar
羽飞 已提交
67 68
class RowTuple : public Tuple 
{
羽飞's avatar
羽飞 已提交
69 70
public:
  RowTuple() = default;
羽飞's avatar
羽飞 已提交
71 72
  virtual ~RowTuple()
  {
羽飞's avatar
羽飞 已提交
73
    for (FieldExpr *spec : speces_) {
羽飞's avatar
羽飞 已提交
74 75 76 77
      delete spec;
    }
    speces_.clear();
  }
L
Longda Feng 已提交
78

羽飞's avatar
羽飞 已提交
79
  void set_record(Record *record)
80
  {
羽飞's avatar
羽飞 已提交
81
    this->record_ = record;
羽飞's avatar
羽飞 已提交
82 83
  }

羽飞's avatar
羽飞 已提交
84
  void set_schema(const Table *table, const std::vector<FieldMeta> *fields)
85
  {
羽飞's avatar
羽飞 已提交
86 87 88
    table_ = table;
    this->speces_.reserve(fields->size());
    for (const FieldMeta &field : *fields) {
羽飞's avatar
羽飞 已提交
89
      speces_.push_back(new FieldExpr(table, &field));
羽飞's avatar
羽飞 已提交
90
    }
羽飞's avatar
羽飞 已提交
91 92
  }

羽飞's avatar
羽飞 已提交
93
  int cell_num() const override
94
  {
羽飞's avatar
羽飞 已提交
95
    return speces_.size();
羽飞's avatar
羽飞 已提交
96 97
  }

羽飞's avatar
羽飞 已提交
98
  RC cell_at(int index, TupleCell &cell) const override
99
  {
羽飞's avatar
羽飞 已提交
100
    if (index < 0 || index >= static_cast<int>(speces_.size())) {
羽飞's avatar
羽飞 已提交
101 102 103 104
      LOG_WARN("invalid argument. index=%d", index);
      return RC::INVALID_ARGUMENT;
    }

羽飞's avatar
羽飞 已提交
105
    FieldExpr *field_expr = speces_[index];
羽飞's avatar
羽飞 已提交
106 107
    const FieldMeta *field_meta = field_expr->field().meta();
    cell.set_type(field_meta->type());
羽飞's avatar
羽飞 已提交
108
    cell.set_data(this->record_->data() + field_meta->offset(), field_meta->len());
羽飞's avatar
羽飞 已提交
109
    return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
110 111
  }

羽飞's avatar
羽飞 已提交
112
  RC find_cell(const TupleCellSpec &spec, TupleCell &cell) const override
羽飞's avatar
羽飞 已提交
113
  {
羽飞's avatar
羽飞 已提交
114 115
    const char *table_name = spec.table_name();
    const char *field_name = spec.field_name();
羽飞's avatar
羽飞 已提交
116 117 118 119
    if (0 != strcmp(table_name, table_->name())) {
      return RC::NOTFOUND;
    }

羽飞's avatar
羽飞 已提交
120
    for (size_t i = 0; i < speces_.size(); ++i) {
L
Longda Feng 已提交
121
      const FieldExpr *field_expr = speces_[i];
羽飞's avatar
羽飞 已提交
122 123
      const Field &field = field_expr->field();
      if (0 == strcmp(field_name, field.field_name())) {
L
Longda Feng 已提交
124
        return cell_at(i, cell);
羽飞's avatar
羽飞 已提交
125 126 127 128 129
      }
    }
    return RC::NOTFOUND;
  }

L
Longda Feng 已提交
130
#if 0
羽飞's avatar
羽飞 已提交
131
  RC cell_spec_at(int index, const TupleCellSpec *&spec) const override
132
  {
羽飞's avatar
羽飞 已提交
133
    if (index < 0 || index >= static_cast<int>(speces_.size())) {
羽飞's avatar
羽飞 已提交
134 135 136
      LOG_WARN("invalid argument. index=%d", index);
      return RC::INVALID_ARGUMENT;
    }
羽飞's avatar
羽飞 已提交
137
    spec = speces_[index];
羽飞's avatar
羽飞 已提交
138
    return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
139
  }
L
Longda Feng 已提交
140
#endif
羽飞's avatar
羽飞 已提交
141

羽飞's avatar
羽飞 已提交
142
  Record &record()
143
  {
羽飞's avatar
羽飞 已提交
144
    return *record_;
羽飞's avatar
羽飞 已提交
145 146
  }

羽飞's avatar
羽飞 已提交
147
  const Record &record() const
148
  {
羽飞's avatar
羽飞 已提交
149
    return *record_;
羽飞's avatar
羽飞 已提交
150
  }
L
Longda Feng 已提交
151

羽飞's avatar
羽飞 已提交
152
private:
羽飞's avatar
羽飞 已提交
153
  Record *record_ = nullptr;
羽飞's avatar
羽飞 已提交
154
  const Table *table_ = nullptr;
羽飞's avatar
羽飞 已提交
155
  std::vector<FieldExpr *> speces_;
羽飞's avatar
羽飞 已提交
156
};
羽飞's avatar
羽飞 已提交
157

羽飞's avatar
羽飞 已提交
158 159
class ProjectTuple : public Tuple 
{
羽飞's avatar
羽飞 已提交
160 161
public:
  ProjectTuple() = default;
羽飞's avatar
羽飞 已提交
162 163 164 165 166 167 168
  virtual ~ProjectTuple()
  {
    for (TupleCellSpec *spec : speces_) {
      delete spec;
    }
    speces_.clear();
  }
羽飞's avatar
羽飞 已提交
169 170 171 172 173 174

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

羽飞's avatar
羽飞 已提交
175
  void add_cell_spec(TupleCellSpec *spec)
羽飞's avatar
羽飞 已提交
176 177 178 179 180 181 182 183 184 185
  {
    speces_.push_back(spec);
  }
  int cell_num() const override
  {
    return speces_.size();
  }

  RC cell_at(int index, TupleCell &cell) const override
  {
羽飞's avatar
羽飞 已提交
186
    if (index < 0 || index >= static_cast<int>(speces_.size())) {
羽飞's avatar
羽飞 已提交
187 188 189 190 191 192
      return RC::GENERIC_ERROR;
    }
    if (tuple_ == nullptr) {
      return RC::GENERIC_ERROR;
    }

羽飞's avatar
羽飞 已提交
193
    const TupleCellSpec *spec = speces_[index];
羽飞's avatar
羽飞 已提交
194
    return tuple_->find_cell(*spec, cell);
羽飞's avatar
羽飞 已提交
195 196
  }

羽飞's avatar
羽飞 已提交
197
  RC find_cell(const TupleCellSpec &spec, TupleCell &cell) const override
羽飞's avatar
羽飞 已提交
198
  {
羽飞's avatar
羽飞 已提交
199
    return tuple_->find_cell(spec, cell);
羽飞's avatar
羽飞 已提交
200
  }
羽飞's avatar
羽飞 已提交
201

L
Longda Feng 已提交
202
#if 0
羽飞's avatar
羽飞 已提交
203
  RC cell_spec_at(int index, const TupleCellSpec *&spec) const override
羽飞's avatar
羽飞 已提交
204
  {
羽飞's avatar
羽飞 已提交
205
    if (index < 0 || index >= static_cast<int>(speces_.size())) {
羽飞's avatar
羽飞 已提交
206 207 208 209 210
      return RC::NOTFOUND;
    }
    spec = speces_[index];
    return RC::SUCCESS;
  }
L
Longda Feng 已提交
211
#endif
羽飞's avatar
羽飞 已提交
212
private:
羽飞's avatar
羽飞 已提交
213
  std::vector<TupleCellSpec *> speces_;
羽飞's avatar
羽飞 已提交
214 215
  Tuple *tuple_ = nullptr;
};
羽飞's avatar
羽飞 已提交
216

羽飞's avatar
羽飞 已提交
217 218
class ValueListTuple : public Tuple 
{
L
Longda Feng 已提交
219
public:
羽飞's avatar
羽飞 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232
  ValueListTuple() = default;
  virtual ~ValueListTuple() = default;

  void set_cells(const std::vector<TupleCell> &cells)
  {
    cells_ = cells;
  }

  virtual int cell_num() const override
  {
    return static_cast<int>(cells_.size());
  }

L
Longda Feng 已提交
233
  virtual RC cell_at(int index, TupleCell &cell) const override
羽飞's avatar
羽飞 已提交
234 235 236 237 238 239 240 241 242
  {
    if (index < 0 || index >= cell_num()) {
      return RC::NOTFOUND;
    }

    cell = cells_[index];
    return RC::SUCCESS;
  }

L
Longda Feng 已提交
243
  virtual RC find_cell(const TupleCellSpec &spec, TupleCell &cell) const override
羽飞's avatar
羽飞 已提交
244 245 246 247 248 249 250
  {
    return RC::INTERNAL;
  }

private:
  std::vector<TupleCell> cells_;
};
羽飞's avatar
羽飞 已提交
251 252 253 254 255

/**
 * 将两个tuple合并为一个tuple
 * 在join算子中使用
 */
羽飞's avatar
羽飞 已提交
256 257
class JoinedTuple : public Tuple 
{
羽飞's avatar
羽飞 已提交
258 259 260 261
public:
  JoinedTuple() = default;
  virtual ~JoinedTuple() = default;

L
Longda Feng 已提交
262 263 264 265 266 267 268 269 270
  void set_left(Tuple *left)
  {
    left_ = left;
  }
  void set_right(Tuple *right)
  {
    right_ = right;
  }

羽飞's avatar
羽飞 已提交
271 272 273 274 275 276 277 278
  int cell_num() const override
  {
    return left_->cell_num() + right_->cell_num();
  }

  RC cell_at(int index, TupleCell &cell) const override
  {
    const int left_cell_num = left_->cell_num();
L
Longda Feng 已提交
279
    if (index > 0 && index < left_cell_num) {
羽飞's avatar
羽飞 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
      return left_->cell_at(index, cell);
    }

    if (index >= left_cell_num && index < left_cell_num + right_->cell_num()) {
      return right_->cell_at(index - left_cell_num, cell);
    }

    return RC::NOTFOUND;
  }

  RC find_cell(const TupleCellSpec &spec, TupleCell &cell) const override
  {
    RC rc = left_->find_cell(spec, cell);
    if (rc == RC::SUCCESS || rc != RC::NOTFOUND) {
      return rc;
    }

    return right_->find_cell(spec, cell);
  }
L
Longda Feng 已提交
299

羽飞's avatar
羽飞 已提交
300
private:
L
Longda Feng 已提交
301 302
  Tuple *left_ = nullptr;
  Tuple *right_ = nullptr;
羽飞's avatar
羽飞 已提交
303
};