-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor type system #439
Merged
Merged
refactor type system #439
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f1bd71a
refactor type
nautaa 73c0785
refactor type
nautaa fa57564
fix
nautaa bedd725
format
nautaa 746f378
fix
nautaa 99cddf3
Merge branch 'refactor_type' of github.com:nautaa/miniob into refacto…
nautaa bd3a120
format
nautaa fccea67
add comments
nautaa 8413d65
Update data_type.h
hnwyllmm 08b6bd7
add comment
hnwyllmm d5b125b
use strcasecmp instead of strcmp
hnwyllmm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 == strcmp(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 属性的类型 | ||
* | ||
*/ | ||
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,32 @@ | ||
/* 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" | ||
|
||
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,77 @@ | ||
/* 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; | ||
|
||
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; } | ||
|
||
virtual RC add(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; } | ||
virtual RC subtract(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; } | ||
virtual RC multiply(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; } | ||
virtual RC divide(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; } | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strcasecmp?