Skip to content
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 11 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 == strcmp(ATTR_TYPE_NAME[i], s)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strcasecmp?

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 属性的类型
*
*/
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;
}
32 changes: 32 additions & 0 deletions src/observer/common/type/char_type.h
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;
};
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),
};
77 changes: 77 additions & 0 deletions src/observer/common/type/data_type.h
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_;
};
Loading
Loading