tuple_cell.cpp 2.0 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 16 17 18
#include "sql/expr/tuple_cell.h"
#include "storage/common/field.h"
#include "common/log/log.h"
#include "util/comparator.h"
羽飞's avatar
羽飞 已提交
19
#include "util/util.h"
羽飞's avatar
羽飞 已提交
20 21 22 23 24 25 26 27

void TupleCell::to_string(std::ostream &os) const
{
  switch (attr_type_) {
  case INTS: {
    os << *(int *)data_;
  } break;
  case FLOATS: {
羽飞's avatar
羽飞 已提交
28 29
    float v = *(float *)data_;
    os << double2string(v);
羽飞's avatar
羽飞 已提交
30 31
  } break;
  case CHARS: {
羽飞's avatar
羽飞 已提交
32
    for (int i = 0; i < length_; i++) {
羽飞's avatar
羽飞 已提交
33
      if (data_[i] == '\0') {
羽飞's avatar
羽飞 已提交
34
        break;
羽飞's avatar
羽飞 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      }
      os << data_[i];
    }
  } break;
  default: {
    LOG_WARN("unsupported attr type: %d", attr_type_);
  } break;
  }
}

int TupleCell::compare(const TupleCell &other) const
{
  if (this->attr_type_ == other.attr_type_) {
    switch (this->attr_type_) {
    case INTS: return compare_int(this->data_, other.data_);
    case FLOATS: return compare_float(this->data_, other.data_);
羽飞's avatar
羽飞 已提交
51
    case CHARS: return compare_string(this->data_, this->length_, other.data_, other.length_);
羽飞's avatar
羽飞 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65
    default: {
      LOG_WARN("unsupported type: %d", this->attr_type_);
    }
    }
  } else if (this->attr_type_ == INTS && other.attr_type_ == FLOATS) {
    float this_data = *(int *)data_;
    return compare_float(&this_data, other.data_);
  } else if (this->attr_type_ == FLOATS && other.attr_type_ == INTS) {
    float other_data = *(int *)other.data_;
    return compare_float(data_, &other_data);
  }
  LOG_WARN("not supported");
  return -1; // TODO return rc?
}