tuple_cell.cpp 7.1 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 12 13 14
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 <sstream>
羽飞's avatar
羽飞 已提交
16
#include "sql/expr/tuple_cell.h"
羽飞's avatar
羽飞 已提交
17
#include "storage/field/field.h"
羽飞's avatar
羽飞 已提交
18
#include "common/log/log.h"
19 20
#include "common/lang/comparator.h"
#include "common/lang/string.h"
羽飞's avatar
羽飞 已提交
21

羽飞's avatar
羽飞 已提交
22 23 24 25 26 27 28 29 30 31
TupleCellSpec::TupleCellSpec(const char *table_name, const char *field_name, const char *alias)
{
  if (table_name) {
    table_name_ = table_name;
  }
  if (field_name) {
    field_name_ = field_name;
  }
  if (alias) {
    alias_ = alias;
羽飞's avatar
羽飞 已提交
32 33 34 35 36 37
  } else {
    if (table_name_.empty()) {
      alias_ = field_name_;
    } else {
      alias_ = table_name_ + "." + field_name_;
    }
羽飞's avatar
羽飞 已提交
38 39 40 41 42 43 44 45 46 47 48
  }
}

TupleCellSpec::TupleCellSpec(const char *alias)
{
  if (alias) {
    alias_ = alias;
  }
}
////////////////////////////////////////////////////////////////////////////////

羽飞's avatar
羽飞 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 99 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 128 129 130 131 132 133 134 135
void TupleCell::set_data(char *data, int length)
{
  switch (attr_type_) {
    case CHARS: {
      set_string(data, length);
    } break;
    case INTS: {
      num_value_.int_value_ = *(int *)data;
      length_ = length;
    } break;
    case FLOATS: {
      num_value_.float_value_ = *(float *)data;
      length_ = length;
    } break;
    case BOOLEANS: {
      num_value_.bool_value_ = *(int *)data != 0;
      length_ = length;
    } break;
    default: {
      LOG_WARN("unknown data type: %d", attr_type_);
    } break;
  }
}
void TupleCell::set_int(int val)
{
  attr_type_ = INTS;
  num_value_.int_value_ = val;
  length_ = sizeof(val);
}

void TupleCell::set_float(float val)
{
  attr_type_ = FLOATS;
  num_value_.float_value_ = val;
  length_ = sizeof(val);
}
void TupleCell::set_boolean(bool val)
{
  attr_type_ = BOOLEANS;
  num_value_.bool_value_ = val;
  length_ = sizeof(val);
}
void TupleCell::set_string(const char *s, int len /*= 0*/)
{
  attr_type_ = CHARS;
  if (len > 0) {
    len = strnlen(s, len);
    str_value_.assign(s, len);
  } else {
    str_value_.assign(s);
  }
  length_ = str_value_.length();
}

void TupleCell::set_value(const Value &value)
{
  switch (value.type) {
    case INTS: {
      set_int(value.int_value);
    } break;
    case FLOATS: {
      set_float(value.float_value);
    } break;
    case CHARS: {
      set_string(value.string_value.c_str());
    } break;
    case BOOLEANS: {
      set_boolean(value.bool_value);
    } break;
    case UNDEFINED: {
      ASSERT(false, "got an invalid value type");
    } break;
  }
}

const char *TupleCell::data() const
{
  switch (attr_type_) {
    case CHARS: {
      return str_value_.c_str();
    } break;
    default: {
      return (const char *)&num_value_;
    } break;
  }
}

羽飞's avatar
羽飞 已提交
136 137 138
void TupleCell::to_string(std::ostream &os) const
{
  switch (attr_type_) {
L
Longda Feng 已提交
139 140 141 142
    case INTS: {
      os << num_value_.int_value_;
    } break;
    case FLOATS: {
143
      os << common::double_to_str(num_value_.float_value_);
L
Longda Feng 已提交
144 145 146 147 148 149 150 151 152 153
    } break;
    case BOOLEANS: {
      os << num_value_.bool_value_;
    } break;
    case CHARS: {
      os << str_value_;
    } break;
    default: {
      LOG_WARN("unsupported attr type: %d", attr_type_);
    } break;
羽飞's avatar
羽飞 已提交
154 155 156 157 158 159 160
  }
}

int TupleCell::compare(const TupleCell &other) const
{
  if (this->attr_type_ == other.attr_type_) {
    switch (this->attr_type_) {
羽飞's avatar
羽飞 已提交
161
      case INTS: {
162
        return common::compare_int((void *)&this->num_value_.int_value_, (void *)&other.num_value_.int_value_);
羽飞's avatar
羽飞 已提交
163 164
      } break;
      case FLOATS: {
165
        return common::compare_float((void *)&this->num_value_.float_value_, (void *)&other.num_value_.float_value_);
羽飞's avatar
羽飞 已提交
166 167
      } break;
      case CHARS: {
168
        return common::compare_string((void *)this->str_value_.c_str(),
L
Longda Feng 已提交
169 170 171
            this->str_value_.length(),
            (void *)other.str_value_.c_str(),
            other.str_value_.length());
羽飞's avatar
羽飞 已提交
172 173
      } break;
      case BOOLEANS: {
174
        return common::compare_int((void *)&this->num_value_.bool_value_, (void *)&other.num_value_.bool_value_);
羽飞's avatar
羽飞 已提交
175 176 177 178
      }
      default: {
        LOG_WARN("unsupported type: %d", this->attr_type_);
      }
羽飞's avatar
羽飞 已提交
179 180
    }
  } else if (this->attr_type_ == INTS && other.attr_type_ == FLOATS) {
羽飞's avatar
羽飞 已提交
181
    float this_data = this->num_value_.int_value_;
182
    return common::compare_float((void *)&this_data, (void *)&other.num_value_.float_value_);
羽飞's avatar
羽飞 已提交
183
  } else if (this->attr_type_ == FLOATS && other.attr_type_ == INTS) {
羽飞's avatar
羽飞 已提交
184
    float other_data = other.num_value_.int_value_;
185
    return common::compare_float((void *)&this->num_value_.float_value_, (void *)&other_data);
羽飞's avatar
羽飞 已提交
186 187
  }
  LOG_WARN("not supported");
L
Longda Feng 已提交
188
  return -1;  // TODO return rc?
羽飞's avatar
羽飞 已提交
189
}
羽飞's avatar
羽飞 已提交
190 191 192 193 194 195 196 197 198 199 200 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

int TupleCell::get_int() const
{
  switch (attr_type_) {
    case CHARS: {
      try {
        return (int)(std::stol(str_value_));
      } catch (std::exception const &ex) {
        LOG_TRACE("failed to convert string to number. s=%s, ex=%s", str_value_.c_str(), ex.what());
        return 0;
      }
    }
    case INTS: {
      return num_value_.int_value_;
    }
    case FLOATS: {
      return (int)(num_value_.float_value_);
    }
    case BOOLEANS: {
      return (int)(num_value_.bool_value_);
    }
    default: {
      LOG_WARN("unknown data type. type=%d", attr_type_);
      return 0;
    }
  }
  return 0;
}

float TupleCell::get_float() const
{
  switch (attr_type_) {
    case CHARS: {
      try {
        return std::stof(str_value_);
      } catch (std::exception const &ex) {
        LOG_TRACE("failed to convert string to float. s=%s, ex=%s", str_value_.c_str(), ex.what());
        return 0.0;
      }
    } break;
    case INTS: {
      return float(num_value_.int_value_);
    } break;
    case FLOATS: {
      return num_value_.float_value_;
    } break;
    case BOOLEANS: {
      return float(num_value_.bool_value_);
    } break;
    default: {
      LOG_WARN("unknown data type. type=%d", attr_type_);
      return 0;
    }
  }
  return 0;
}

std::string TupleCell::get_string() const
{
  std::stringstream ss;
  to_string(ss);
  return ss.str();
}

bool TupleCell::get_boolean() const
{
  switch (attr_type_) {
    case CHARS: {
      try {
        float val = std::stof(str_value_);
        if (val >= EPSILON || val <= -EPSILON) {
          return true;
        }

        int int_val = std::stol(str_value_);
        if (int_val != 0) {
          return true;
        }

        return !str_value_.empty();
      } catch (std::exception const &ex) {
        LOG_TRACE("failed to convert string to float or integer. s=%s, ex=%s", str_value_.c_str(), ex.what());
        return !str_value_.empty();
      }
    } break;
    case INTS: {
      return num_value_.int_value_ != 0;
    } break;
    case FLOATS: {
      float val = num_value_.float_value_;
      return val >= EPSILON || val <= -EPSILON;
    } break;
    case BOOLEANS: {
      return num_value_.bool_value_;
    } break;
    default: {
      LOG_WARN("unknown data type. type=%d", attr_type_);
      return false;
    }
  }
  return false;
}