Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/observer/common/type/attr_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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", "vectors", "booleans"};
const char *ATTR_TYPE_NAME[] = {"undefined", "chars", "ints", "floats", "vectors", "booleans", "dates"};

const char *attr_type_to_string(AttrType type)
{
Expand Down Expand Up @@ -40,4 +40,4 @@ bool is_numerical_type(AttrType type)
bool is_string_type(AttrType type)
{
return (type == AttrType::CHARS);
}
}
1 change: 1 addition & 0 deletions src/observer/common/type/attr_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum class AttrType
FLOATS, ///< 浮点数类型(4字节)
VECTORS, ///< 向量类型
BOOLEANS, ///< boolean类型,当前不是由parser解析出来的,是程序内部使用的
DATES, ///< 日期类型(4字节,按YYYYMMDD编码)
MAXTYPE, ///< 请在 UNDEFINED 与 MAXTYPE 之间增加新类型
};

Expand Down
6 changes: 5 additions & 1 deletion src/observer/common/type/char_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ RC CharType::set_value_from_str(Value &val, const string &data) const
RC CharType::cast_to(const Value &val, AttrType type, Value &result) const
{
switch (type) {
case AttrType::DATES:
return DataType::type_instance(AttrType::DATES)->set_value_from_str(result, val.get_string());
default: return RC::UNIMPLEMENTED;
}
return RC::SUCCESS;
Expand All @@ -38,6 +40,8 @@ int CharType::cast_cost(AttrType type)
{
if (type == AttrType::CHARS) {
return 0;
} else if (type == AttrType::DATES) {
return 1;
}
return INT32_MAX;
}
Expand All @@ -48,4 +52,4 @@ RC CharType::to_string(const Value &val, string &result) const
ss << val.value_.pointer_value_;
result = ss.str();
return RC::SUCCESS;
}
}
4 changes: 3 additions & 1 deletion src/observer/common/type/data_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

#include "common/type/char_type.h"
#include "common/type/date_type.h"
#include "common/type/float_type.h"
#include "common/type/integer_type.h"
#include "common/type/data_type.h"
Expand All @@ -24,4 +25,5 @@ array<unique_ptr<DataType>, static_cast<int>(AttrType::MAXTYPE)> DataType::type_
make_unique<FloatType>(),
make_unique<VectorType>(),
make_unique<DataType>(AttrType::BOOLEANS),
};
make_unique<DateType>(),
};
97 changes: 97 additions & 0 deletions src/observer/common/type/date_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* 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/date_type.h"

#include <cstdio>
#include <algorithm>
#include <cctype>

#include "common/lang/comparator.h"
#include "common/value.h"
#include "storage/common/column.h"

RC DateType::parse_date(const string &text, int &encoded_date)
{
const size_t first_dash = text.find('-');
const size_t second_dash = first_dash == string::npos ? string::npos : text.find('-', first_dash + 1);
if (first_dash != 4 || second_dash == string::npos || text.find('-', second_dash + 1) != string::npos ||
second_dash - first_dash < 2 || second_dash - first_dash > 3 ||
text.size() - second_dash < 2 || text.size() - second_dash > 3) {
return RC::SCHEMA_FIELD_TYPE_MISMATCH;
}

const auto all_digits = [](const string &part) {
return !part.empty() && std::all_of(part.begin(), part.end(), [](unsigned char ch) { return std::isdigit(ch); });
};
const string year_text = text.substr(0, first_dash);
const string month_text = text.substr(first_dash + 1, second_dash - first_dash - 1);
const string day_text = text.substr(second_dash + 1);
if (!all_digits(year_text) || !all_digits(month_text) || !all_digits(day_text)) {
return RC::SCHEMA_FIELD_TYPE_MISMATCH;
}

const int year = stoi(year_text);
const int month = stoi(month_text);
const int day = stoi(day_text);
if (year < 1 || month < 1 || month > 12) {
return RC::SCHEMA_FIELD_TYPE_MISMATCH;
}

static constexpr int DAYS_PER_MONTH[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int max_day = DAYS_PER_MONTH[month - 1];
const bool leap_year = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
if (month == 2 && leap_year) {
max_day = 29;
}
if (day < 1 || day > max_day) {
return RC::SCHEMA_FIELD_TYPE_MISMATCH;
}

encoded_date = year * 10000 + month * 100 + day;
return RC::SUCCESS;
}

int DateType::compare(const Value &left, const Value &right) const
{
ASSERT(left.attr_type() == AttrType::DATES && right.attr_type() == AttrType::DATES, "invalid date type");
int left_date = left.get_int();
int right_date = right.get_int();
return common::compare_int(&left_date, &right_date);
}

int DateType::compare(const Column &left, const Column &right, int left_idx, int right_idx) const
{
ASSERT(left.attr_type() == AttrType::DATES && right.attr_type() == AttrType::DATES, "invalid date column type");
return common::compare_int(
(void *)&((int *)left.data())[left_idx], (void *)&((int *)right.data())[right_idx]);
}

RC DateType::to_string(const Value &val, string &result) const
{
const int encoded_date = val.get_int();
const int year = encoded_date / 10000;
const int month = encoded_date / 100 % 100;
const int day = encoded_date % 100;
char buffer[16];
snprintf(buffer, sizeof(buffer), "%04d-%02d-%02d", year, month, day);
result = buffer;
return RC::SUCCESS;
}

RC DateType::set_value_from_str(Value &val, const string &data) const
{
int encoded_date = 0;
RC rc = parse_date(data, encoded_date);
if (OB_SUCC(rc)) {
val.set_date(encoded_date);
}
return rc;
}
27 changes: 27 additions & 0 deletions src/observer/common/type/date_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* 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/type/data_type.h"

class DateType : public DataType
{
public:
DateType() : DataType(AttrType::DATES) {}
~DateType() override = default;

int compare(const Value &left, const Value &right) const override;
int compare(const Column &left, const Column &right, int left_idx, int right_idx) const override;
RC to_string(const Value &val, string &result) const override;
RC set_value_from_str(Value &val, const string &data) const override;

static RC parse_date(const string &text, int &encoded_date);
};
17 changes: 17 additions & 0 deletions src/observer/common/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ void Value::reset()
void Value::set_data(char *data, int length)
{
switch (attr_type_) {
case AttrType::DATES: {
value_.int_value_ = *(int *)data;
length_ = length;
} break;
case AttrType::CHARS: {
set_string(data, length);
} break;
Expand Down Expand Up @@ -149,6 +153,13 @@ void Value::set_float(float val)
value_.float_value_ = val;
length_ = sizeof(val);
}
void Value::set_date(int val)
{
reset();
attr_type_ = AttrType::DATES;
value_.int_value_ = val;
length_ = sizeof(val);
}
void Value::set_boolean(bool val)
{
reset();
Expand Down Expand Up @@ -194,6 +205,9 @@ void Value::set_empty_string(int len)
void Value::set_value(const Value &value)
{
switch (value.attr_type_) {
case AttrType::DATES: {
set_date(value.get_int());
} break;
case AttrType::INTS: {
set_int(value.get_int());
} break;
Expand Down Expand Up @@ -250,6 +264,9 @@ int Value::compare(const Value &other) const { return DataType::type_instance(th
int Value::get_int() const
{
switch (attr_type_) {
case AttrType::DATES: {
return value_.int_value_;
}
case AttrType::CHARS: {
try {
return (int)(stol(value_.pointer_value_));
Expand Down
1 change: 1 addition & 0 deletions src/observer/common/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class Value final
public:
void set_int(int val);
void set_float(float val);
void set_date(int val);
void set_string(const char *s, int len = 0);
void set_empty_string(int len);
void set_string_from_other(const Value &other);
Expand Down
6 changes: 6 additions & 0 deletions src/observer/sql/executor/command_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ See the Mulan PSL v2 for more details. */
#include "sql/executor/create_index_executor.h"
#include "sql/executor/create_table_executor.h"
#include "sql/executor/desc_table_executor.h"
#include "sql/executor/drop_table_executor.h"
#include "sql/executor/help_executor.h"
#include "sql/executor/load_data_executor.h"
#include "sql/executor/set_variable_executor.h"
Expand All @@ -43,6 +44,11 @@ RC CommandExecutor::execute(SQLStageEvent *sql_event)
rc = executor.execute(sql_event);
} break;

case StmtType::DROP_TABLE: {
DropTableExecutor executor;
rc = executor.execute(sql_event);
} break;

case StmtType::DESC_TABLE: {
DescTableExecutor executor;
rc = executor.execute(sql_event);
Expand Down
30 changes: 30 additions & 0 deletions src/observer/sql/executor/drop_table_executor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* 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 "sql/executor/drop_table_executor.h"

#include "common/log/log.h"
#include "event/session_event.h"
#include "event/sql_event.h"
#include "session/session.h"
#include "sql/stmt/drop_table_stmt.h"
#include "storage/db/db.h"

RC DropTableExecutor::execute(SQLStageEvent *sql_event)
{
Stmt *stmt = sql_event->stmt();
Session *session = sql_event->session_event()->session();
ASSERT(stmt->type() == StmtType::DROP_TABLE,
"drop table executor can not run this command: %d",
static_cast<int>(stmt->type()));

DropTableStmt *drop_table_stmt = static_cast<DropTableStmt *>(stmt);
return session->get_current_db()->drop_table(drop_table_stmt->table_name().c_str());
}
24 changes: 24 additions & 0 deletions src/observer/sql/executor/drop_table_executor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* 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/sys/rc.h"

class SQLStageEvent;

class DropTableExecutor
{
public:
DropTableExecutor() = default;
virtual ~DropTableExecutor() = default;

RC execute(SQLStageEvent *sql_event);
};
Loading
Loading