First portion of speedup

Do not generate DateLUT for each serialized and deserialized
date
This commit is contained in:
zvonand 2023-05-05 14:12:14 +02:00
parent 984e8fde41
commit a218f010e8
6 changed files with 33 additions and 27 deletions

View File

@ -61,17 +61,17 @@ public:
init(time);
}
LocalDate(DayNum day_num) /// NOLINT
LocalDate(DayNum day_num, const DateLUTImpl & time_zone = DateLUT::instance()) /// NOLINT
{
const auto & values = DateLUT::instance().getValues(day_num);
const auto & values = time_zone.getValues(day_num);
m_year = values.year;
m_month = values.month;
m_day = values.day_of_month;
}
explicit LocalDate(ExtendedDayNum day_num)
explicit LocalDate(ExtendedDayNum day_num, const DateLUTImpl & time_zone = DateLUT::instance())
{
const auto & values = DateLUT::instance().getValues(day_num);
const auto & values = time_zone.getValues(day_num);
m_year = values.year;
m_month = values.month;
m_day = values.day_of_month;

View File

@ -13,7 +13,7 @@ namespace DB
void SerializationDate::serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const
{
writeDateText(DayNum(assert_cast<const ColumnUInt16 &>(column).getData()[row_num]), ostr);
writeDateText(DayNum(assert_cast<const ColumnUInt16 &>(column).getData()[row_num]), ostr, time_zone);
}
void SerializationDate::deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
@ -26,7 +26,7 @@ void SerializationDate::deserializeWholeText(IColumn & column, ReadBuffer & istr
void SerializationDate::deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings &) const
{
DayNum x;
readDateText(x, istr);
readDateText(x, istr, time_zone);
assert_cast<ColumnUInt16 &>(column).getData().push_back(x);
}
@ -46,7 +46,7 @@ void SerializationDate::deserializeTextQuoted(IColumn & column, ReadBuffer & ist
{
DayNum x;
assertChar('\'', istr);
readDateText(x, istr);
readDateText(x, istr, time_zone);
assertChar('\'', istr);
assert_cast<ColumnUInt16 &>(column).getData().push_back(x); /// It's important to do this at the end - for exception safety.
}
@ -62,7 +62,7 @@ void SerializationDate::deserializeTextJSON(IColumn & column, ReadBuffer & istr,
{
DayNum x;
assertChar('"', istr);
readDateText(x, istr);
readDateText(x, istr, time_zone);
assertChar('"', istr);
assert_cast<ColumnUInt16 &>(column).getData().push_back(x);
}
@ -80,5 +80,8 @@ void SerializationDate::deserializeTextCSV(IColumn & column, ReadBuffer & istr,
readCSV(value, istr);
assert_cast<ColumnUInt16 &>(column).getData().push_back(value);
}
SerializationDate::SerializationDate(const TimezoneMixin & time_zone_) : TimezoneMixin(time_zone_)
{
}
}

View File

@ -1,13 +1,16 @@
#pragma once
#include <DataTypes/Serializations/SerializationNumber.h>
#include <DataTypes/TimezoneMixin.h>
namespace DB
{
class SerializationDate final : public SerializationNumber<UInt16>
class SerializationDate final : public SerializationNumber<UInt16>, public TimezoneMixin
{
public:
explicit SerializationDate(const TimezoneMixin & time_zone_ = TimezoneMixin());
void serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
void deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
void serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;

View File

@ -718,9 +718,9 @@ template <>
struct FormatImpl<DataTypeDate>
{
template <typename ReturnType = void>
static ReturnType execute(const DataTypeDate::FieldType x, WriteBuffer & wb, const DataTypeDate *, const DateLUTImpl *)
static ReturnType execute(const DataTypeDate::FieldType x, WriteBuffer & wb, const DataTypeDate *, const DateLUTImpl * time_zone)
{
writeDateText(DayNum(x), wb);
writeDateText(DayNum(x), wb, *time_zone);
return ReturnType(true);
}
};
@ -729,9 +729,9 @@ template <>
struct FormatImpl<DataTypeDate32>
{
template <typename ReturnType = void>
static ReturnType execute(const DataTypeDate32::FieldType x, WriteBuffer & wb, const DataTypeDate32 *, const DateLUTImpl *)
static ReturnType execute(const DataTypeDate32::FieldType x, WriteBuffer & wb, const DataTypeDate32 *, const DateLUTImpl * time_zone)
{
writeDateText(ExtendedDayNum(x), wb);
writeDateText(ExtendedDayNum(x), wb, *time_zone);
return ReturnType(true);
}
};
@ -830,8 +830,8 @@ struct ConvertImpl<FromDataType, DataTypeString, Name, ConvertDefaultBehaviorTag
const auto & type = static_cast<const FromDataType &>(*col_with_type_and_name.type);
const DateLUTImpl * time_zone = nullptr;
/// For argument of DateTime type, second argument with time zone could be specified.
if constexpr (std::is_same_v<FromDataType, DataTypeDateTime> || std::is_same_v<FromDataType, DataTypeDateTime64>)
/// For argument of Date or DateTime type, second argument with time zone could be specified.
if constexpr (std::is_same_v<FromDataType, DataTypeDateTime> || std::is_same_v<FromDataType, DataTypeDateTime64> || std::is_same_v<FromDataType, DataTypeDate>)
{
auto non_null_args = createBlockWithNestedColumns(arguments);
time_zone = &extractTimeZoneFromFunctionArguments(non_null_args, 1, 0);

View File

@ -723,7 +723,7 @@ inline void convertToDayNum(DayNum & date, ExtendedDayNum & from)
}
template <typename ReturnType = void>
inline ReturnType readDateTextImpl(DayNum & date, ReadBuffer & buf)
inline ReturnType readDateTextImpl(DayNum & date, ReadBuffer & buf, const DateLUTImpl & date_lut = DateLUT::instance())
{
static constexpr bool throw_exception = std::is_same_v<ReturnType, void>;
@ -734,13 +734,13 @@ inline ReturnType readDateTextImpl(DayNum & date, ReadBuffer & buf)
else if (!readDateTextImpl<ReturnType>(local_date, buf))
return false;
ExtendedDayNum ret = DateLUT::instance().makeDayNum(local_date.year(), local_date.month(), local_date.day());
ExtendedDayNum ret = date_lut.makeDayNum(local_date.year(), local_date.month(), local_date.day());
convertToDayNum(date,ret);
return ReturnType(true);
}
template <typename ReturnType = void>
inline ReturnType readDateTextImpl(ExtendedDayNum & date, ReadBuffer & buf)
inline ReturnType readDateTextImpl(ExtendedDayNum & date, ReadBuffer & buf, const DateLUTImpl & date_lut = DateLUT::instance())
{
static constexpr bool throw_exception = std::is_same_v<ReturnType, void>;
@ -752,7 +752,7 @@ inline ReturnType readDateTextImpl(ExtendedDayNum & date, ReadBuffer & buf)
return false;
/// When the parameter is out of rule or out of range, Date32 uses 1925-01-01 as the default value (-DateLUT::instance().getDayNumOffsetEpoch(), -16436) and Date uses 1970-01-01.
date = DateLUT::instance().makeDayNum(local_date.year(), local_date.month(), local_date.day(), -static_cast<Int32>(DateLUT::instance().getDayNumOffsetEpoch()));
date = date_lut.makeDayNum(local_date.year(), local_date.month(), local_date.day(), -static_cast<Int32>(date_lut.getDayNumOffsetEpoch()));
return ReturnType(true);
}
@ -762,14 +762,14 @@ inline void readDateText(LocalDate & date, ReadBuffer & buf)
readDateTextImpl<void>(date, buf);
}
inline void readDateText(DayNum & date, ReadBuffer & buf)
inline void readDateText(DayNum & date, ReadBuffer & buf, const DateLUTImpl & date_lut = DateLUT::instance())
{
readDateTextImpl<void>(date, buf);
readDateTextImpl<void>(date, buf, date_lut);
}
inline void readDateText(ExtendedDayNum & date, ReadBuffer & buf)
inline void readDateText(ExtendedDayNum & date, ReadBuffer & buf, const DateLUTImpl & date_lut = DateLUT::instance())
{
readDateTextImpl<void>(date, buf);
readDateTextImpl<void>(date, buf, date_lut);
}
inline bool tryReadDateText(LocalDate & date, ReadBuffer & buf)

View File

@ -694,15 +694,15 @@ inline void writeDateText(const LocalDate & date, WriteBuffer & buf)
}
template <char delimiter = '-'>
inline void writeDateText(DayNum date, WriteBuffer & buf)
inline void writeDateText(DayNum date, WriteBuffer & buf, const DateLUTImpl & time_zone = DateLUT::instance())
{
writeDateText<delimiter>(LocalDate(date), buf);
writeDateText<delimiter>(LocalDate(date, time_zone), buf);
}
template <char delimiter = '-'>
inline void writeDateText(ExtendedDayNum date, WriteBuffer & buf)
inline void writeDateText(ExtendedDayNum date, WriteBuffer & buf, const DateLUTImpl & time_zone = DateLUT::instance())
{
writeDateText<delimiter>(LocalDate(date), buf);
writeDateText<delimiter>(LocalDate(date, time_zone), buf);
}
/// In the format YYYY-MM-DD HH:MM:SS