-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### 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
Showing
52 changed files
with
1,180 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_; | ||
}; |
Oops, something went wrong.