Skip to content

Commit

Permalink
refactor type system (#439)
Browse files Browse the repository at this point in the history
### What problem were solved in this pull request?
refactor type system, types are implemented based on `DataType`, which
facilitates extension of the types.

---------

Co-authored-by: wangyunlai <[email protected]>
  • Loading branch information
nautaa and hnwyllmm authored Aug 21, 2024
1 parent 97bc169 commit ebe460e
Show file tree
Hide file tree
Showing 52 changed files with 1,180 additions and 612 deletions.
15 changes: 15 additions & 0 deletions deps/common/lang/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* Copyright (c) 2021 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. */

#pragma once

#include <array>

using std::array;
6 changes: 3 additions & 3 deletions deps/common/lang/comparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ int compare_string(void *arg1, int arg1_max_length, void *arg2, int arg2_max_len
int maxlen = min(arg1_max_length, arg2_max_length);
int result = strncmp(s1, s2, maxlen);
if (0 != result) {
return result;
return result < 0 ? -1 : 1;
}

if (arg1_max_length > maxlen) {
return s1[maxlen] - 0;
return 1;
}

if (arg2_max_length > maxlen) {
return 0 - s2[maxlen];
return -1;
}
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions deps/common/lang/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ See the Mulan PSL v2 for more details. */
#include <cxxabi.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>

#include <cstdlib>
#include <string>
Expand Down
5 changes: 3 additions & 2 deletions src/observer/common/rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ See the Mulan PSL v2 for more details. */
#define DEFINE_RCS \
DEFINE_RC(SUCCESS) \
DEFINE_RC(INVALID_ARGUMENT) \
DEFINE_RC(UNIMPLENMENT) \
DEFINE_RC(UNIMPLEMENTED) \
DEFINE_RC(SQL_SYNTAX) \
DEFINE_RC(INTERNAL) \
DEFINE_RC(NOMEM) \
Expand Down Expand Up @@ -75,7 +75,8 @@ See the Mulan PSL v2 for more details. */
DEFINE_RC(VARIABLE_NOT_VALID) \
DEFINE_RC(LOGBUF_FULL) \
DEFINE_RC(LOG_FILE_FULL) \
DEFINE_RC(LOG_ENTRY_INVALID)
DEFINE_RC(LOG_ENTRY_INVALID) \
DEFINE_RC(UNSUPPORTED)

enum class RC
{
Expand Down
33 changes: 33 additions & 0 deletions src/observer/common/type/attr_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Copyright (c) 2021 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. */


#include "common/lang/string.h"
#include "common/type/attr_type.h"

const char *ATTR_TYPE_NAME[] = {"undefined", "chars", "ints", "floats", "booleans"};

const char *attr_type_to_string(AttrType type)
{
if (type >= AttrType::UNDEFINED && type < AttrType::MAXTYPE) {
return ATTR_TYPE_NAME[static_cast<int>(type)];
}
return "unknown";
}

AttrType attr_type_from_string(const char *s)
{
for (unsigned int i = 0; i < sizeof(ATTR_TYPE_NAME) / sizeof(ATTR_TYPE_NAME[0]); i++) {
if (0 == strcasecmp(ATTR_TYPE_NAME[i], s)) {
return (AttrType)i;
}
}
return AttrType::UNDEFINED;
}
28 changes: 28 additions & 0 deletions src/observer/common/type/attr_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright (c) 2021 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. */

#pragma once

/**
* @brief 属性的类型
* @details AttrType 枚举列出了属性的各种数据类型。
*/
enum class AttrType
{
UNDEFINED,
CHARS, ///< 字符串类型
INTS, ///< 整数类型(4字节)
FLOATS, ///< 浮点数类型(4字节)
BOOLEANS, ///< boolean类型,当前不是由parser解析出来的,是程序内部使用的
MAXTYPE, ///< 请在 UNDEFINED 与 MAXTYPE 之间增加新类型
};

const char *attr_type_to_string(AttrType type);
AttrType attr_type_from_string(const char *s);
51 changes: 51 additions & 0 deletions src/observer/common/type/char_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Copyright (c) 2021 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. */

#include "common/lang/comparator.h"
#include "common/log/log.h"
#include "common/type/char_type.h"
#include "common/value.h"

int CharType::compare(const Value &left, const Value &right) const
{
ASSERT(left.attr_type() == AttrType::CHARS && right.attr_type() == AttrType::CHARS, "invalid type");
return common::compare_string(
(void *)left.value_.pointer_value_, left.length_, (void *)right.value_.pointer_value_, right.length_);
}

RC CharType::set_value_from_str(Value &val, const string &data) const
{
val.set_string(data.c_str());
return RC::SUCCESS;
}

RC CharType::cast_to(const Value &val, AttrType type, Value &result) const
{
switch (type) {
default: return RC::UNIMPLEMENTED;
}
return RC::SUCCESS;
}

int CharType::cast_cost(AttrType type)
{
if (type == AttrType::CHARS) {
return 0;
}
return INT32_MAX;
}

RC CharType::to_string(const Value &val, string &result) const
{
stringstream ss;
ss << val.value_.pointer_value_;
result = ss.str();
return RC::SUCCESS;
}
36 changes: 36 additions & 0 deletions src/observer/common/type/char_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright (c) 2021 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. */

#pragma once

#include "common/rc.h"
#include "common/type/data_type.h"

/**
* @brief 固定长度的字符串类型
* @ingroup DataType
*/
class CharType : public DataType
{
public:
CharType() : DataType(AttrType::CHARS) {}

virtual ~CharType() = default;

int compare(const Value &left, const Value &right) const override;

RC cast_to(const Value &val, AttrType type, Value &result) const override;

RC set_value_from_str(Value &val, const string &data) const override;

int cast_cost(AttrType type) override;

RC to_string(const Value &val, string &result) const override;
};
22 changes: 22 additions & 0 deletions src/observer/common/type/data_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Copyright (c) 2021 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. */

#include "common/type/char_type.h"
#include "common/type/float_type.h"
#include "common/type/integer_type.h"
#include "common/type/data_type.h"

array<unique_ptr<DataType>, static_cast<int>(AttrType::MAXTYPE)> DataType::type_instances_ = {
make_unique<DataType>(AttrType::UNDEFINED),
make_unique<CharType>(),
make_unique<IntegerType>(),
make_unique<FloatType>(),
make_unique<DataType>(AttrType::BOOLEANS),
};
102 changes: 102 additions & 0 deletions src/observer/common/type/data_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* Copyright (c) 2021 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. */

#pragma once

#include "common/lang/array.h"
#include "common/lang/memory.h"
#include "common/lang/string.h"
#include "common/rc.h"
#include "common/type/attr_type.h"

class Value;

/**
* @brief 定义了数据类型相关的操作,比如比较运算、算术运算等
* @defgroup 数据类型
* @details 数据类型定义的算术运算中,比如 add、subtract 等,将按照当前数据类型设置最终结果值的类型。
* 参与运算的参数类型不一定相同,不同的类型进行运算是否能够支持需要参考各个类型的实现。
*/
class DataType
{
public:
explicit DataType(AttrType attr_type) : attr_type_(attr_type) {}

virtual ~DataType() = default;

inline static DataType *type_instance(AttrType attr_type)
{
return type_instances_.at(static_cast<int>(attr_type)).get();
}

inline AttrType get_attr_type() const { return attr_type_; }

/**
* @return
* -1 表示 left < right
* 0 表示 left = right
* 1 表示 left > right
* INT32_MAX 表示未实现的比较
*/
virtual int compare(const Value &left, const Value &right) const { return INT32_MAX; }

/**
* @brief 计算 left + right,并将结果保存到 result 中
*/
virtual RC add(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; }

/**
* @brief 计算 left - right,并将结果保存到 result 中
*/
virtual RC subtract(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; }

/**
* @brief 计算 left * right,并将结果保存到 result 中
*/
virtual RC multiply(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; }

/**
* @brief 计算 left / right,并将结果保存到 result 中
*/
virtual RC divide(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; }

/**
* @brief 计算 -val,并将结果保存到 result 中
*/
virtual RC negative(const Value &val, Value &result) const { return RC::UNSUPPORTED; }

/**
* @brief 将 val 转换为 type 类型,并将结果保存到 result 中
*/
virtual RC cast_to(const Value &val, AttrType type, Value &result) const { return RC::UNSUPPORTED; }

/**
* @brief 将 val 转换为 string,并将结果保存到 result 中
*/
virtual RC to_string(const Value &val, string &result) const { return RC::UNSUPPORTED; }

/**
* @brief 计算从 type 到 attr_type 的隐式转换的 cost,如果无法转换,返回 INT32_MAX
*/
virtual int cast_cost(AttrType type)
{
if (type == attr_type_) {
return 0;
}
return INT32_MAX;
}

virtual RC set_value_from_str(Value &val, const string &data) const { return RC::UNSUPPORTED; }

protected:
AttrType attr_type_;

static array<unique_ptr<DataType>, static_cast<int>(AttrType::MAXTYPE)> type_instances_;
};
Loading

0 comments on commit ebe460e

Please sign in to comment.