tuple.h 5.4 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/record/record.h"
羽飞's avatar
羽飞 已提交
25 26 27

class Table;

羽飞's avatar
羽飞 已提交
28
class TupleSchema
羽飞's avatar
羽飞 已提交
29
{
羽飞's avatar
羽飞 已提交
30 31 32 33 34 35
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
羽飞 已提交
36 37

private:
羽飞's avatar
羽飞 已提交
38
  std::vector<TupleCellSpec> cells_;
羽飞's avatar
羽飞 已提交
39 40 41 42
};

class Tuple
{
羽飞's avatar
羽飞 已提交
43 44
public:
  Tuple() = default;
羽飞's avatar
羽飞 已提交
45
  virtual ~Tuple() = default;
羽飞's avatar
羽飞 已提交
46

羽飞's avatar
羽飞 已提交
47 48
  virtual int cell_num() const = 0; 
  virtual RC  cell_at(int index, TupleCell &cell) const = 0;
羽飞's avatar
羽飞 已提交
49
  virtual RC  find_cell(const TupleCellSpec &spec, TupleCell &cell) const = 0;
羽飞's avatar
羽飞 已提交
50
};
羽飞's avatar
羽飞 已提交
51

羽飞's avatar
羽飞 已提交
52 53 54 55
class RowTuple : public Tuple
{
public:
  RowTuple() = default;
羽飞's avatar
羽飞 已提交
56 57
  virtual ~RowTuple()
  {
羽飞's avatar
羽飞 已提交
58
    for (FieldExpr *spec : speces_) {
羽飞's avatar
羽飞 已提交
59 60 61 62
      delete spec;
    }
    speces_.clear();
  }
羽飞's avatar
羽飞 已提交
63 64
  
  void set_record(Record *record)
65
  {
羽飞's avatar
羽飞 已提交
66
    this->record_ = record;
羽飞's avatar
羽飞 已提交
67 68
  }

羽飞's avatar
羽飞 已提交
69
  void set_schema(const Table *table, const std::vector<FieldMeta> *fields)
70
  {
羽飞's avatar
羽飞 已提交
71 72 73
    table_ = table;
    this->speces_.reserve(fields->size());
    for (const FieldMeta &field : *fields) {
羽飞's avatar
羽飞 已提交
74
      speces_.push_back(new FieldExpr(table, &field));
羽飞's avatar
羽飞 已提交
75
    }
羽飞's avatar
羽飞 已提交
76 77
  }

羽飞's avatar
羽飞 已提交
78
  int cell_num() const override
79
  {
羽飞's avatar
羽飞 已提交
80
    return speces_.size();
羽飞's avatar
羽飞 已提交
81 82
  }

羽飞's avatar
羽飞 已提交
83
  RC cell_at(int index, TupleCell &cell) const override
84
  {
羽飞's avatar
羽飞 已提交
85
    if (index < 0 || index >= static_cast<int>(speces_.size())) {
羽飞's avatar
羽飞 已提交
86 87 88 89
      LOG_WARN("invalid argument. index=%d", index);
      return RC::INVALID_ARGUMENT;
    }

羽飞's avatar
羽飞 已提交
90
    FieldExpr *field_expr = speces_[index];
羽飞's avatar
羽飞 已提交
91 92 93
    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
羽飞 已提交
94
    cell.set_length(field_meta->len());
羽飞's avatar
羽飞 已提交
95
    return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
96 97
  }

羽飞's avatar
羽飞 已提交
98
  RC find_cell(const TupleCellSpec &spec, TupleCell &cell) const override
羽飞's avatar
羽飞 已提交
99
  {
羽飞's avatar
羽飞 已提交
100 101
    const char *table_name = spec.table_name();
    const char *field_name = spec.field_name();
羽飞's avatar
羽飞 已提交
102 103 104 105
    if (0 != strcmp(table_name, table_->name())) {
      return RC::NOTFOUND;
    }

羽飞's avatar
羽飞 已提交
106
    for (size_t i = 0; i < speces_.size(); ++i) {
羽飞's avatar
羽飞 已提交
107
      const FieldExpr * field_expr = speces_[i];
羽飞's avatar
羽飞 已提交
108 109
      const Field &field = field_expr->field();
      if (0 == strcmp(field_name, field.field_name())) {
羽飞's avatar
羽飞 已提交
110 111 112 113 114 115
	return cell_at(i, cell);
      }
    }
    return RC::NOTFOUND;
  }

羽飞's avatar
羽飞 已提交
116
  #if 0
羽飞's avatar
羽飞 已提交
117
  RC cell_spec_at(int index, const TupleCellSpec *&spec) const override
118
  {
羽飞's avatar
羽飞 已提交
119
    if (index < 0 || index >= static_cast<int>(speces_.size())) {
羽飞's avatar
羽飞 已提交
120 121 122
      LOG_WARN("invalid argument. index=%d", index);
      return RC::INVALID_ARGUMENT;
    }
羽飞's avatar
羽飞 已提交
123
    spec = speces_[index];
羽飞's avatar
羽飞 已提交
124
    return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
125
  }
羽飞's avatar
羽飞 已提交
126
  #endif
羽飞's avatar
羽飞 已提交
127

羽飞's avatar
羽飞 已提交
128
  Record &record()
129
  {
羽飞's avatar
羽飞 已提交
130
    return *record_;
羽飞's avatar
羽飞 已提交
131 132
  }

羽飞's avatar
羽飞 已提交
133
  const Record &record() const
134
  {
羽飞's avatar
羽飞 已提交
135
    return *record_;
羽飞's avatar
羽飞 已提交
136 137
  }
private:
羽飞's avatar
羽飞 已提交
138
  Record *record_ = nullptr;
羽飞's avatar
羽飞 已提交
139
  const Table *table_ = nullptr;
羽飞's avatar
羽飞 已提交
140
  std::vector<FieldExpr *> speces_;
羽飞's avatar
羽飞 已提交
141
};
羽飞's avatar
羽飞 已提交
142 143 144 145 146

class ProjectTuple : public Tuple
{
public:
  ProjectTuple() = default;
羽飞's avatar
羽飞 已提交
147 148 149 150 151 152 153
  virtual ~ProjectTuple()
  {
    for (TupleCellSpec *spec : speces_) {
      delete spec;
    }
    speces_.clear();
  }
羽飞's avatar
羽飞 已提交
154 155 156 157 158 159

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

羽飞's avatar
羽飞 已提交
160
  void add_cell_spec(TupleCellSpec *spec)
羽飞's avatar
羽飞 已提交
161 162 163 164 165 166 167 168 169 170
  {
    speces_.push_back(spec);
  }
  int cell_num() const override
  {
    return speces_.size();
  }

  RC cell_at(int index, TupleCell &cell) const override
  {
羽飞's avatar
羽飞 已提交
171
    if (index < 0 || index >= static_cast<int>(speces_.size())) {
羽飞's avatar
羽飞 已提交
172 173 174 175 176 177
      return RC::GENERIC_ERROR;
    }
    if (tuple_ == nullptr) {
      return RC::GENERIC_ERROR;
    }

羽飞's avatar
羽飞 已提交
178
    const TupleCellSpec *spec = speces_[index];
羽飞's avatar
羽飞 已提交
179
    return tuple_->find_cell(*spec, cell);
羽飞's avatar
羽飞 已提交
180 181
  }

羽飞's avatar
羽飞 已提交
182
  RC find_cell(const TupleCellSpec &spec, TupleCell &cell) const override
羽飞's avatar
羽飞 已提交
183
  {
羽飞's avatar
羽飞 已提交
184
    return tuple_->find_cell(spec, cell);
羽飞's avatar
羽飞 已提交
185
  }
羽飞's avatar
羽飞 已提交
186 187

  #if 0
羽飞's avatar
羽飞 已提交
188
  RC cell_spec_at(int index, const TupleCellSpec *&spec) const override
羽飞's avatar
羽飞 已提交
189
  {
羽飞's avatar
羽飞 已提交
190
    if (index < 0 || index >= static_cast<int>(speces_.size())) {
羽飞's avatar
羽飞 已提交
191 192 193 194 195
      return RC::NOTFOUND;
    }
    spec = speces_[index];
    return RC::SUCCESS;
  }
羽飞's avatar
羽飞 已提交
196
  #endif
羽飞's avatar
羽飞 已提交
197
private:
羽飞's avatar
羽飞 已提交
198
  std::vector<TupleCellSpec *> speces_;
羽飞's avatar
羽飞 已提交
199 200
  Tuple *tuple_ = nullptr;
};
羽飞's avatar
羽飞 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

class ValueListTuple : public Tuple
{
public: 
  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());
  }

  virtual RC  cell_at(int index, TupleCell &cell) const override
  {
    if (index < 0 || index >= cell_num()) {
      return RC::NOTFOUND;
    }

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

  virtual RC  find_cell(const TupleCellSpec &spec, TupleCell &cell) const override
  {
    return RC::INTERNAL;
  }

private:
  std::vector<TupleCell> cells_;
};