1 | #pragma once |
2 | |
3 | #include <Core/Types.h> |
4 | #include <DataTypes/DataTypeDateTime.h> |
5 | #include <DataTypes/DataTypeDecimalBase.h> |
6 | |
7 | class DateLUTImpl; |
8 | |
9 | namespace DB |
10 | { |
11 | |
12 | /** DateTime64 is same as DateTime, but it stores values as Int64 and has configurable sub-second part. |
13 | * |
14 | * `scale` determines number of decimal places for sub-second part of the DateTime64. |
15 | */ |
16 | class DataTypeDateTime64 final : public DataTypeDecimalBase<DateTime64>, public TimezoneMixin |
17 | { |
18 | public: |
19 | static constexpr UInt8 default_scale = 3; |
20 | static constexpr auto family_name = "DateTime64" ; |
21 | |
22 | explicit DataTypeDateTime64(UInt32 scale_, const std::string & time_zone_name = "" ); |
23 | |
24 | // reuse timezone from other DateTime/DateTime64 |
25 | DataTypeDateTime64(UInt32 scale_, const TimezoneMixin & time_zone_info); |
26 | |
27 | const char * getFamilyName() const override { return family_name; } |
28 | std::string doGetName() const override; |
29 | TypeIndex getTypeId() const override { return TypeIndex::DateTime64; } |
30 | |
31 | void serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
32 | void deserializeText(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override; |
33 | void deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override; |
34 | void serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
35 | void deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override; |
36 | void serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
37 | void deserializeTextQuoted(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override; |
38 | void serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
39 | void deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override; |
40 | void serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; |
41 | void deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override; |
42 | void serializeProtobuf(const IColumn & column, size_t row_num, ProtobufWriter & protobuf, size_t & value_index) const override; |
43 | void deserializeProtobuf(IColumn & column, ProtobufReader & protobuf, bool allow_add_row, bool & row_added) const override; |
44 | |
45 | bool equals(const IDataType & rhs) const override; |
46 | }; |
47 | |
48 | |
49 | /** Basic wrapper for Tansform-types for DateTime64. |
50 | * |
51 | * Allows reusing existing Transform (that takes DateTime-values as UInt32) with DateTime64-values, |
52 | * by discarding fractional part and producing SAME return type as original Transform. |
53 | * |
54 | * Such Transfotm-types are commonly used in Date/DateTime manipulation functions, |
55 | * and implement static execute fucntion with following signature: |
56 | * R execute(UInt32, T, const DateLUTImpl &) |
57 | * |
58 | * Wehere R and T could be arbitrary types. |
59 | */ |
60 | template <typename Transform> |
61 | class DateTime64BasicTransformWrapper : public Transform |
62 | { |
63 | public: |
64 | using Transform::execute; |
65 | |
66 | explicit DateTime64BasicTransformWrapper(UInt32 scale_) |
67 | : scale_multiplier(DecimalUtils::scaleMultiplier<DateTime64::NativeType>(scale_)) |
68 | {} |
69 | |
70 | template <typename T> |
71 | auto execute(DateTime64 t, T v, const DateLUTImpl & time_zone) const |
72 | { |
73 | const auto components = DecimalUtils::splitWithScaleMultiplier(t, scale_multiplier); |
74 | return static_cast<const Transform *>(this)->execute( |
75 | static_cast<UInt32>(components.whole), v, time_zone); |
76 | } |
77 | |
78 | private: |
79 | UInt32 scale_multiplier = 1; |
80 | }; |
81 | |
82 | |
83 | } |
84 | |
85 | |