-
Notifications
You must be signed in to change notification settings - Fork 409
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
[flash 411] use compatible new datetime/ date #224
Changes from all commits
f7713ff
91857bf
db7cba9
fba675a
38f8677
ff28222
0b20720
27f07f3
381dd03
1362341
1c74493
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#pragma once | ||
|
||
#include <Core/Field.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
struct MyTimeBase | ||
{ | ||
|
||
enum MyTimeType : UInt8 | ||
{ | ||
TypeDate = 0, | ||
TypeDateTime, | ||
TypeTimeStamp, | ||
TypeDuration | ||
}; | ||
|
||
UInt16 year; // year <= 9999 | ||
UInt8 month; // month <= 12 | ||
UInt8 day; // day <= 31 | ||
// When it's type is Time, HH:MM:SS may be 839:59:59 to -839:59:59, so use int16 to avoid overflow | ||
Int16 hour; | ||
UInt8 minute; | ||
UInt8 second; | ||
UInt32 micro_second; // ms second <= 999999 | ||
|
||
UInt8 fsp; | ||
|
||
MyTimeBase() = default; | ||
MyTimeBase(UInt64 packed, UInt8 fsp_ = 0); | ||
MyTimeBase(UInt16 year_, UInt8 month_, UInt8 day_, UInt16 hour_, UInt8 minute_, UInt8 second_, UInt32 micro_second_, UInt8 fsp_); | ||
|
||
UInt64 toPackedUInt() const; | ||
|
||
// DateFormat returns a textual representation of the time value formatted | ||
// according to layout | ||
// See http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format | ||
String dateFormat(const String & layout) const; | ||
|
||
protected: | ||
void convertDateFormat(char c, String & result) const; | ||
}; | ||
|
||
struct MyDateTime : public MyTimeBase | ||
{ | ||
MyDateTime(UInt64 packed, UInt8 fsp) : MyTimeBase(packed, fsp) {} | ||
|
||
MyDateTime(UInt16 year_, UInt8 month_, UInt8 day_, UInt16 hour_, UInt8 minute_, UInt8 second_, UInt32 micro_second_, UInt8 fsp_) | ||
: MyTimeBase(year_, month_, day_, hour_, minute_, second_, micro_second_, fsp_) | ||
{} | ||
|
||
String toString() const; | ||
}; | ||
|
||
struct MyDate : public MyTimeBase | ||
{ | ||
MyDate(UInt64 packed) : MyTimeBase(packed) {} | ||
|
||
MyDate(UInt16 year_, UInt8 month_, UInt8 day_) : MyTimeBase(year_, month_, day_, 0, 0, 0, 0, 0) {} | ||
|
||
String toString() const { return dateFormat("%Y-%m-%d"); } | ||
}; | ||
|
||
Field parseMyDateTime(const String & str); | ||
|
||
} // namespace DB |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,6 +133,10 @@ enum class TypeIndex | |
Function, | ||
AggregateFunction, | ||
LowCardinality, | ||
MyDate, | ||
MyDateTime, | ||
MyTimeStamp, | ||
MyTime | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MyTime is mapping TypeDuration in MyTimeType enum? Better to use the same suffix, MyTime => MyDuration or TypeDuration => TypeTime There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to keep consistent with MySQL Grammar |
||
}; | ||
|
||
template <typename T> struct TypeId; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
#include <IO/ReadHelpers.h> | ||
#include <IO/WriteHelpers.h> | ||
|
||
#include <Columns/ColumnsNumber.h> | ||
#include <DataTypes/DataTypeFactory.h> | ||
#include <DataTypes/DataTypeMyDate.h> | ||
|
||
|
||
namespace DB | ||
{ | ||
|
||
void DataTypeMyDate::serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr) const | ||
{ | ||
writeMyDateText(static_cast<const ColumnUInt64 &>(column).getData()[row_num], ostr); | ||
} | ||
|
||
static void deserializeText(IColumn & column, ReadBuffer & istr) | ||
{ | ||
UInt64 x = 0; | ||
readMyDateText(x, istr); | ||
static_cast<ColumnUInt64 &>(column).getData().push_back(x); | ||
} | ||
|
||
void DataTypeMyDate::serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr) const | ||
{ | ||
serializeText(column, row_num, ostr); | ||
} | ||
|
||
void DataTypeMyDate::deserializeTextEscaped(IColumn & column, ReadBuffer & istr) const { deserializeText(column, istr); } | ||
|
||
void DataTypeMyDate::serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr) const | ||
{ | ||
writeChar('\'', ostr); | ||
serializeText(column, row_num, ostr); | ||
writeChar('\'', ostr); | ||
} | ||
|
||
void DataTypeMyDate::deserializeTextQuoted(IColumn & column, ReadBuffer & istr) const | ||
{ | ||
UInt64 x = 0; | ||
assertChar('\'', istr); | ||
readMyDateText(x, istr); | ||
assertChar('\'', istr); | ||
static_cast<ColumnUInt64 &>(column).getData().push_back(x); /// It's important to do this at the end - for exception safety. | ||
} | ||
|
||
void DataTypeMyDate::serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettingsJSON &) const | ||
{ | ||
writeChar('"', ostr); | ||
serializeText(column, row_num, ostr); | ||
writeChar('"', ostr); | ||
} | ||
|
||
void DataTypeMyDate::deserializeTextJSON(IColumn & column, ReadBuffer & istr) const | ||
{ | ||
UInt64 x = 0; | ||
assertChar('"', istr); | ||
readMyDateText(x, istr); | ||
assertChar('"', istr); | ||
static_cast<ColumnUInt64 &>(column).getData().push_back(x); | ||
} | ||
|
||
void DataTypeMyDate::serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr) const | ||
{ | ||
writeChar('"', ostr); | ||
serializeText(column, row_num, ostr); | ||
writeChar('"', ostr); | ||
} | ||
|
||
void DataTypeMyDate::deserializeTextCSV(IColumn & column, ReadBuffer & istr, const char /*delimiter*/) const | ||
{ | ||
UInt64 value = 0; | ||
readCSV(value, istr); | ||
static_cast<ColumnUInt64 &>(column).getData().push_back(value); | ||
} | ||
|
||
bool DataTypeMyDate::equals(const IDataType & rhs) const { return typeid(rhs) == typeid(*this); } | ||
|
||
|
||
void registerDataTypeMyDate(DataTypeFactory & factory) | ||
{ | ||
factory.registerSimpleDataType( | ||
"MyDate", [] { return DataTypePtr(std::make_shared<DataTypeMyDate>()); }, DataTypeFactory::CaseInsensitive); | ||
} | ||
|
||
} // namespace DB |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <DataTypes/DataTypeMyTimeBase.h> | ||
|
||
|
||
namespace DB | ||
{ | ||
|
||
class DataTypeMyDate final : public DataTypeMyTimeBase | ||
{ | ||
public: | ||
const char * getFamilyName() const override { return "MyDate"; } | ||
|
||
TypeIndex getTypeId() const override { return TypeIndex::MyDate; } | ||
|
||
void serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr) const override; | ||
void serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr) const override; | ||
void deserializeTextEscaped(IColumn & column, ReadBuffer & istr) const override; | ||
void serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr) const override; | ||
void deserializeTextQuoted(IColumn & column, ReadBuffer & istr) const override; | ||
void serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettingsJSON &) const override; | ||
void deserializeTextJSON(IColumn & column, ReadBuffer & istr) const override; | ||
void serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr) const override; | ||
void deserializeTextCSV(IColumn & column, ReadBuffer & istr, const char delimiter) const override; | ||
|
||
bool canBeUsedAsVersion() const override { return true; } | ||
bool isDateOrDateTime() const override { return true; } | ||
bool canBeInsideNullable() const override { return true; } | ||
|
||
bool equals(const IDataType & rhs) const override; | ||
}; | ||
|
||
} // namespace DB |
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.
MyDate
inherits fromMyTimeBase
may introduce too much overhead. Can we use a lightweight class?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.
TiDB use uint64 to store date type, we can optimize it in the future.