expression.cpp 4.9 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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 2022/07/05.
//

羽飞's avatar
羽飞 已提交
15
#include "sql/expr/expression.h"
羽飞's avatar
羽飞 已提交
16 17 18 19
#include "sql/expr/tuple.h"

RC FieldExpr::get_value(const Tuple &tuple, TupleCell &cell) const
{
羽飞's avatar
羽飞 已提交
20
  return tuple.find_cell(TupleCellSpec(table_name(), field_name()), cell);
羽飞's avatar
羽飞 已提交
21 22
}

L
Longda Feng 已提交
23
RC ValueExpr::get_value(const Tuple &tuple, TupleCell &cell) const
羽飞's avatar
羽飞 已提交
24
{
羽飞's avatar
羽飞 已提交
25
  cell = tuple_cell_;
W
wangyunlai.wyl 已提交
26
  return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
27
}
羽飞's avatar
羽飞 已提交
28 29 30 31 32 33 34

/////////////////////////////////////////////////////////////////////////////////
CastExpr::CastExpr(std::unique_ptr<Expression> child, AttrType cast_type)
    : child_(std::move(child)), cast_type_(cast_type)
{}

CastExpr::~CastExpr()
L
Longda Feng 已提交
35
{}
羽飞's avatar
羽飞 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

RC CastExpr::get_value(const Tuple &tuple, TupleCell &cell) const
{
  RC rc = child_->get_value(tuple, cell);
  if (rc != RC::SUCCESS) {
    return rc;
  }

  if (child_->value_type() == this->value_type()) {
    return rc;
  }

  switch (cast_type_) {
    case BOOLEANS: {
      bool val = cell.get_boolean();
      cell.set_boolean(val);
    } break;
    default: {
      rc = RC::INTERNAL;
      LOG_WARN("unsupported convert from type %d to %d", child_->value_type(), cast_type_);
    }
  }
  return rc;
}

////////////////////////////////////////////////////////////////////////////////

ComparisonExpr::ComparisonExpr(CompOp comp, std::unique_ptr<Expression> left, std::unique_ptr<Expression> right)
    : comp_(comp), left_(std::move(left)), right_(std::move(right))
{}

ComparisonExpr::~ComparisonExpr()
L
Longda Feng 已提交
68
{}
羽飞's avatar
羽飞 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

RC ComparisonExpr::compare_tuple_cell(const TupleCell &left, const TupleCell &right, bool &value) const
{
  RC rc = RC::SUCCESS;
  int cmp_result = left.compare(right);
  value = false;
  switch (comp_) {
    case EQUAL_TO: {
      value = (0 == cmp_result);
    } break;
    case LESS_EQUAL: {
      value = (cmp_result <= 0);
    } break;
    case NOT_EQUAL: {
      value = (cmp_result != 0);
    } break;
    case LESS_THAN: {
      value = (cmp_result < 0);
    } break;
    case GREAT_EQUAL: {
      value = (cmp_result >= 0);
    } break;
    case GREAT_THAN: {
      value = (cmp_result > 0);
    } break;
    default: {
      LOG_WARN("unsupported comparison. %d", comp_);
      rc = RC::GENERIC_ERROR;
    } break;
  }
L
Longda Feng 已提交
99

羽飞's avatar
羽飞 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  return rc;
}

RC ComparisonExpr::try_get_value(TupleCell &cell) const
{
  if (left_->type() == ExprType::VALUE && right_->type() == ExprType::VALUE) {
    ValueExpr *left_value_expr = static_cast<ValueExpr *>(left_.get());
    ValueExpr *right_value_expr = static_cast<ValueExpr *>(right_.get());
    const TupleCell &left_cell = left_value_expr->get_tuple_cell();
    const TupleCell &right_cell = right_value_expr->get_tuple_cell();

    bool value = false;
    RC rc = compare_tuple_cell(left_cell, right_cell, value);
    if (rc != RC::SUCCESS) {
      LOG_WARN("failed to compare tuple cells. rc=%s", strrc(rc));
    } else {
      cell.set_boolean(value);
    }
    return rc;
  }

  return RC::INVALID_ARGUMENT;
}

RC ComparisonExpr::get_value(const Tuple &tuple, TupleCell &cell) const
{
  TupleCell left_cell;
  TupleCell right_cell;
L
Longda Feng 已提交
128

羽飞's avatar
羽飞 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  RC rc = left_->get_value(tuple, left_cell);
  if (rc != RC::SUCCESS) {
    LOG_WARN("failed to get value of left expression. rc=%s", strrc(rc));
    return rc;
  }
  rc = right_->get_value(tuple, right_cell);
  if (rc != RC::SUCCESS) {
    LOG_WARN("failed to get value of right expression. rc=%s", strrc(rc));
    return rc;
  }

  bool value = false;
  rc = compare_tuple_cell(left_cell, right_cell, value);
  if (rc == RC::SUCCESS) {
    cell.set_boolean(value);
  }
  return rc;
}

////////////////////////////////////////////////////////////////////////////////
ConjunctionExpr::ConjunctionExpr(Type type, std::vector<std::unique_ptr<Expression>> &children)
    : conjunction_type_(type), children_(std::move(children))
L
Longda Feng 已提交
151
{}
羽飞's avatar
羽飞 已提交
152 153 154 155 156 157 158 159

RC ConjunctionExpr::get_value(const Tuple &tuple, TupleCell &cell) const
{
  RC rc = RC::SUCCESS;
  if (children_.empty()) {
    cell.set_boolean(true);
    return rc;
  }
L
Longda Feng 已提交
160

羽飞's avatar
羽飞 已提交
161 162 163 164 165 166 167 168
  TupleCell tmp_cell;
  for (const std::unique_ptr<Expression> &expr : children_) {
    rc = expr->get_value(tuple, tmp_cell);
    if (rc != RC::SUCCESS) {
      LOG_WARN("failed to get value by child expression. rc=%s", strrc(rc));
      return rc;
    }
    bool value = tmp_cell.get_boolean();
L
Longda Feng 已提交
169
    if ((conjunction_type_ == Type::AND && !value) || (conjunction_type_ == Type::OR && value)) {
羽飞's avatar
羽飞 已提交
170 171 172 173 174 175 176 177 178
      cell.set_boolean(value);
      return rc;
    }
  }

  bool default_value = (conjunction_type_ == Type::AND);
  cell.set_boolean(default_value);
  return rc;
}