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

Port DateLUT and LocalDate from ClickHouse #2143

Merged
merged 9 commits into from
Jun 12, 2021
Merged
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 dbms/src/Common/MyTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ Field parseMyDateTime(const String & str, int8_t fsp)
{
// Since we only use DateLUTImpl as parameter placeholder of AddSecondsImpl::execute
// and it's costly to construct a DateLUTImpl, a shared static instance is enough.
static const DateLUTImpl lut = DateLUT::instance("UTC");
static const DateLUTImpl & lut = DateLUT::instance("UTC");

Int32 year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, delta_hour = 0, delta_minute = 0;

Expand Down Expand Up @@ -806,7 +806,7 @@ void convertTimeZone(UInt64 from_time, UInt64 & to_time, const DateLUTImpl & tim
}
MyDateTime from_my_time(from_time);
time_t epoch = getEpochSecond(from_my_time, time_zone_from);
if (unlikely(epoch + time_zone_to.getOffsetAtStartEpoch() + SECONDS_PER_DAY < 0))
if (unlikely(epoch + time_zone_to.getOffsetAtStartOfEpoch() + SECONDS_PER_DAY < 0))
throw Exception("Unsupported timestamp value , TiFlash only support timestamp after 1970-01-01 00:00:00 UTC)");
MyDateTime to_my_time(time_zone_to.toYear(epoch), time_zone_to.toMonth(epoch), time_zone_to.toDayOfMonth(epoch),
time_zone_to.toHour(epoch), time_zone_to.toMinute(epoch), time_zone_to.toSecond(epoch), from_my_time.micro_second);
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Common/MyTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ inline time_t getEpochSecond(const MyDateTime & my_time, const DateLUTImpl & tim
{
/// - 3600 * 24 + my_time.hour * 3600 + my_time.minute * 60 + my_time.second is UTC based, need to adjust
/// the epoch according to the input time_zone
return -3600 * 24 + my_time.hour * 3600 + my_time.minute * 60 + my_time.second - time_zone.getOffsetAtStartEpoch();
return -3600 * 24 + my_time.hour * 3600 + my_time.minute * 60 + my_time.second - time_zone.getOffsetAtStartOfEpoch();
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions dbms/src/DataTypes/DataTypeDate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace DB

void DataTypeDate::serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr) const
{
writeDateText(DayNum_t(static_cast<const ColumnUInt16 &>(column).getData()[row_num]), ostr);
writeDateText(DayNum(static_cast<const ColumnUInt16 &>(column).getData()[row_num]), ostr);
}

static void deserializeText(IColumn & column, ReadBuffer & istr)
{
DayNum_t x;
DayNum x;
readDateText(x, istr);
static_cast<ColumnUInt16 &>(column).getData().push_back(x);
}
Expand All @@ -40,7 +40,7 @@ void DataTypeDate::serializeTextQuoted(const IColumn & column, size_t row_num, W

void DataTypeDate::deserializeTextQuoted(IColumn & column, ReadBuffer & istr) const
{
DayNum_t x;
DayNum x;
assertChar('\'', istr);
readDateText(x, istr);
assertChar('\'', istr);
Expand All @@ -56,7 +56,7 @@ void DataTypeDate::serializeTextJSON(const IColumn & column, size_t row_num, Wri

void DataTypeDate::deserializeTextJSON(IColumn & column, ReadBuffer & istr) const
{
DayNum_t x;
DayNum x;
assertChar('"', istr);
readDateText(x, istr);
assertChar('"', istr);
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Dictionaries/ClickHouseDictionarySource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ std::string ClickHouseDictionarySource::getUpdateFieldAndDate()
auto tmp_time = update_time;
update_time = std::chrono::system_clock::now();
time_t hr_time = std::chrono::system_clock::to_time_t(tmp_time) - 1;
std::string str_time = std::to_string(LocalDateTime(hr_time));
std::string str_time = LocalDateTime(hr_time).toString();
return query_builder.composeUpdateQuery(update_field, str_time);
}
else
Expand Down
12 changes: 6 additions & 6 deletions dbms/src/Functions/FunctionsComparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,20 @@ inline int memcmp16(const void * a, const void * b)

inline time_t dateToDateTime(UInt32 date_data)
{
DayNum_t day_num(date_data);
DayNum day_num(date_data);
LocalDate local_date(day_num);
// todo use timezone info
return DateLUT::instance().makeDateTime(local_date.year(), local_date.month(), local_date.day(), 0, 0, 0);
}

inline std::tuple<DayNum_t, bool> dateTimeToDate(time_t time_data)
inline std::tuple<DayNum, bool> dateTimeToDate(time_t time_data)
{
// todo use timezone info
auto & date_lut = DateLUT::instance();
auto truncated = date_lut.toHour(time_data) != 0 || date_lut.toMinute(time_data) != 0 || date_lut.toSecond(time_data) != 0;
auto values = date_lut.getValues(time_data);
auto day_num = date_lut.makeDayNum(values.year, values.month, values.day_of_month);
return std::make_tuple(day_num, truncated);
return std::make_tuple(static_cast<DayNum>(day_num), truncated);
}


Expand Down Expand Up @@ -191,7 +191,7 @@ struct DateDateTimeComparisonImpl
// date vector with datetime constant
// first check if datetime constant can be convert to date constant
bool truncated;
DayNum_t date_num;
DayNum date_num;
std::tie(date_num, truncated) = dateTimeToDate((time_t) b);
if (!truncated)
{
Expand Down Expand Up @@ -230,7 +230,7 @@ struct DateDateTimeComparisonImpl
{
// datetime constant with date vector
bool truncated;
DayNum_t date_num;
DayNum date_num;
std::tie(date_num, truncated) = dateTimeToDate((time_t) a);
if (!truncated)
{
Expand Down Expand Up @@ -1126,7 +1126,7 @@ class FunctionComparison : public IFunction

if (is_date)
{
DayNum_t date;
DayNum date;
ReadBufferFromMemory in(string_value.data, string_value.size);
readDateText(date, in);
if (!in.eof())
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Functions/FunctionsConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class FunctionTiDBUnixTimeStamp : public IFunction

bool getUnixTimeStampHelper(UInt64 packed, UInt64 & ret)
{
static const auto lut_utc = DateLUT::instance("UTC");
static const auto & lut_utc = DateLUT::instance("UTC");

if (timezone_.is_name_based)
convertTimeZone(packed, ret, *timezone_.timezone, lut_utc);
Expand Down
8 changes: 4 additions & 4 deletions dbms/src/Functions/FunctionsConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ struct ToDateTimeImpl

static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.fromDayNum(DayNum_t(d));
return time_zone.fromDayNum(DayNum(d));
}
};

Expand Down Expand Up @@ -408,7 +408,7 @@ struct FormatImpl<DataTypeDate>
{
static void execute(const DataTypeDate::FieldType x, WriteBuffer & wb, const DataTypeDate *, const DateLUTImpl *)
{
writeDateText(DayNum_t(x), wb);
writeDateText(DayNum(x), wb);
}
};

Expand Down Expand Up @@ -566,7 +566,7 @@ template <typename DataType> void parseImpl(typename DataType::FieldType & x, Re

template <> inline void parseImpl<DataTypeDate>(DataTypeDate::FieldType & x, ReadBuffer & rb, const DateLUTImpl *)
{
DayNum_t tmp(0);
DayNum tmp(0);
readDateText(tmp, rb);
x = tmp;
}
Expand Down Expand Up @@ -1408,7 +1408,7 @@ class FunctionFromUnixTime : public IFunction
}
else
{
if (unlikely(integer_part + datelut->getOffsetAtStartEpoch() + SECONDS_PER_DAY < 0))
if (unlikely(integer_part + datelut->getOffsetAtStartOfEpoch() + SECONDS_PER_DAY < 0))
throw Exception("Unsupported timestamp value , TiFlash only support timestamp after 1970-01-01 00:00:00 UTC)");
}
MyDateTime result(datelut->toYear(integer_part), datelut->toMonth(integer_part), datelut->toDayOfMonth(integer_part),
Expand Down
50 changes: 25 additions & 25 deletions dbms/src/Functions/FunctionsDateTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ struct ToMondayImpl
}
static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toFirstDayNumOfWeek(DayNum_t(d));
return time_zone.toFirstDayNumOfWeek(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toMonday", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -224,7 +224,7 @@ struct ToStartOfMonthImpl
}
static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toFirstDayNumOfMonth(DayNum_t(d));
return time_zone.toFirstDayNumOfMonth(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toStartOfMonday", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -243,7 +243,7 @@ struct ToStartOfQuarterImpl
}
static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toFirstDayNumOfQuarter(DayNum_t(d));
return time_zone.toFirstDayNumOfQuarter(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toStartOfQuarter", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -262,7 +262,7 @@ struct ToStartOfYearImpl
}
static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toFirstDayNumOfYear(DayNum_t(d));
return time_zone.toFirstDayNumOfYear(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toStartOfYear", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand Down Expand Up @@ -380,7 +380,7 @@ struct ToYearImpl
}
static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toYear(DayNum_t(d));
return time_zone.toYear(DayNum(d));
}
static inline UInt16 execute(UInt64 packed, const DateLUTImpl &) { return UInt16((packed >> 46) / 13); }

Expand All @@ -397,7 +397,7 @@ struct ToQuarterImpl
}
static inline UInt8 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toQuarter(DayNum_t(d));
return time_zone.toQuarter(DayNum(d));
}
static inline UInt8 execute(UInt64 packed, const DateLUTImpl &) { return ((/* Month */ (packed >> 46) % 13) + 2) / 3; }

Expand All @@ -414,7 +414,7 @@ struct ToMonthImpl
}
static inline UInt8 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toMonth(DayNum_t(d));
return time_zone.toMonth(DayNum(d));
}
// tidb date related type, ignore time_zone info
static inline UInt8 execute(UInt64 t, const DateLUTImpl &) { return (UInt8)((t >> 46u) % 13); }
Expand All @@ -432,7 +432,7 @@ struct ToDayOfMonthImpl
}
static inline UInt8 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toDayOfMonth(DayNum_t(d));
return time_zone.toDayOfMonth(DayNum(d));
}
static inline UInt8 execute(UInt64 t, const DateLUTImpl & ) {
return (UInt8)((t >> 41) & 31);
Expand All @@ -451,7 +451,7 @@ struct ToDayOfWeekImpl
}
static inline UInt8 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toDayOfWeek(DayNum_t(d));
return time_zone.toDayOfWeek(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toDayOfWeek", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand Down Expand Up @@ -528,7 +528,7 @@ struct ToRelativeYearNumImpl
}
static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toYear(DayNum_t(d));
return time_zone.toYear(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toRelativeYearNum", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -547,7 +547,7 @@ struct ToRelativeQuarterNumImpl
}
static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toRelativeQuarterNum(DayNum_t(d));
return time_zone.toRelativeQuarterNum(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toRelativeQuarterNum", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -566,7 +566,7 @@ struct ToRelativeMonthNumImpl
}
static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toRelativeMonthNum(DayNum_t(d));
return time_zone.toRelativeMonthNum(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toRelativeMonthNum", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -585,7 +585,7 @@ struct ToRelativeWeekNumImpl
}
static inline UInt16 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toRelativeWeekNum(DayNum_t(d));
return time_zone.toRelativeWeekNum(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toRelativeWeekNum", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -604,7 +604,7 @@ struct ToRelativeDayNumImpl
}
static inline UInt16 execute(UInt16 d, const DateLUTImpl &)
{
return static_cast<DayNum_t>(d);
return static_cast<DayNum>(d);
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toRelativeDayNum", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -624,7 +624,7 @@ struct ToRelativeHourNumImpl
}
static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toRelativeHourNum(DayNum_t(d));
return time_zone.toRelativeHourNum(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toRelativeHourNum", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -643,7 +643,7 @@ struct ToRelativeMinuteNumImpl
}
static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toRelativeMinuteNum(DayNum_t(d));
return time_zone.toRelativeMinuteNum(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toRelativeMinuteNum", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -662,7 +662,7 @@ struct ToRelativeSecondNumImpl
}
static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.fromDayNum(DayNum_t(d));
return time_zone.fromDayNum(DayNum(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toRelativeSecondNum", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -681,7 +681,7 @@ struct ToYYYYMMImpl
}
static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toNumYYYYMM(static_cast<DayNum_t>(d));
return time_zone.toNumYYYYMM(static_cast<DayNum>(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toYYYYMM", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -700,7 +700,7 @@ struct ToYYYYMMDDImpl
}
static inline UInt32 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toNumYYYYMMDD(static_cast<DayNum_t>(d));
return time_zone.toNumYYYYMMDD(static_cast<DayNum>(d));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toYYYYMMDD", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand All @@ -719,7 +719,7 @@ struct ToYYYYMMDDhhmmssImpl
}
static inline UInt64 execute(UInt16 d, const DateLUTImpl & time_zone)
{
return time_zone.toNumYYYYMMDDhhmmss(time_zone.toDate(static_cast<DayNum_t>(d)));
return time_zone.toNumYYYYMMDDhhmmss(time_zone.toDate(static_cast<DayNum>(d)));
}
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) {
throw Exception("Illegal type MyTime of argument for function toYYYYMMDDhhmmss", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
Expand Down Expand Up @@ -921,7 +921,7 @@ struct AddSecondsImpl

static inline UInt32 execute(UInt16 d, Int64 delta, const DateLUTImpl & time_zone)
{
return time_zone.fromDayNum(DayNum_t(d)) + delta;
return time_zone.fromDayNum(DayNum(d)) + delta;
}

static inline UInt64 execute(UInt64 t, Int64 delta, const DateLUTImpl &)
Expand Down Expand Up @@ -969,7 +969,7 @@ struct AddMinutesImpl

static inline UInt32 execute(UInt16 d, Int64 delta, const DateLUTImpl & time_zone)
{
return time_zone.fromDayNum(DayNum_t(d)) + delta * 60;
return time_zone.fromDayNum(DayNum(d)) + delta * 60;
}
static inline UInt64 execute(UInt64 t, Int64 delta, const DateLUTImpl & time_zone)
{
Expand All @@ -988,7 +988,7 @@ struct AddHoursImpl

static inline UInt32 execute(UInt16 d, Int64 delta, const DateLUTImpl & time_zone)
{
return time_zone.fromDayNum(DayNum_t(d)) + delta * 3600;
return time_zone.fromDayNum(DayNum(d)) + delta * 3600;
}

static inline UInt64 execute(UInt64 t, Int64 delta, const DateLUTImpl & time_zone)
Expand Down Expand Up @@ -1054,7 +1054,7 @@ struct AddMonthsImpl

static inline UInt16 execute(UInt16 d, Int64 delta, const DateLUTImpl & time_zone)
{
return time_zone.addMonths(DayNum_t(d), delta);
return time_zone.addMonths(DayNum(d), delta);
}

static inline UInt64 execute(UInt64 t, Int64 delta, const DateLUTImpl &)
Expand All @@ -1080,7 +1080,7 @@ struct AddYearsImpl

static inline UInt16 execute(UInt16 d, Int64 delta, const DateLUTImpl & time_zone)
{
return time_zone.addYears(DayNum_t(d), delta);
return time_zone.addYears(DayNum(d), delta);
}

static inline UInt64 execute(UInt64 t, Int64 delta, const DateLUTImpl & time_zone)
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Functions/FunctionsTiDBConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ struct TiDBConvertToTime
// Overflow
if (micro_second >= std::pow(10, to_fsp))
{
static const auto lut = DateLUT::instance("UTC");
static const auto & lut = DateLUT::instance("UTC");
datetime.micro_second = 0;
packed_uint = datetime.toPackedUInt();
packed_uint = AddSecondsImpl::execute(packed_uint, 1, lut);
Expand Down
Loading