forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_type_time_v2.cpp
More file actions
256 lines (213 loc) · 9.33 KB
/
Copy pathdata_type_time_v2.cpp
File metadata and controls
256 lines (213 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "vec/data_types/data_type_time_v2.h"
#include <gen_cpp/data.pb.h>
#include <memory>
#include <ostream>
#include <string>
#include <typeinfo>
#include <utility>
#include "util/binary_cast.hpp"
#include "vec/columns/column_const.h"
#include "vec/columns/column_vector.h"
#include "vec/columns/columns_number.h"
#include "vec/common/assert_cast.h"
#include "vec/common/string_buffer.hpp"
#include "vec/core/types.h"
#include "vec/io/io_helper.h"
#include "vec/io/reader_buffer.h"
#include "vec/runtime/vdatetime_value.h"
namespace doris {
#include "common/compile_check_begin.h"
namespace vectorized {
class IColumn;
} // namespace vectorized
} // namespace doris
namespace doris::vectorized {
bool DataTypeDateV2::equals(const IDataType& rhs) const {
return typeid(rhs) == typeid(*this);
}
size_t DataTypeDateV2::number_length() const {
//2024-01-01
return 10;
}
void DataTypeDateV2::push_number(ColumnString::Chars& chars, const UInt32& num) const {
DateV2Value<DateV2ValueType> val = binary_cast<UInt32, DateV2Value<DateV2ValueType>>(num);
char buf[64];
char* pos = val.to_string(buf);
// DateTime to_string the end is /0
chars.insert(buf, pos - 1);
}
std::string DataTypeDateV2::to_string(const IColumn& column, size_t row_num) const {
auto result = check_column_const_set_readability(column, row_num);
ColumnPtr ptr = result.first;
row_num = result.second;
UInt32 int_val = assert_cast<const ColumnUInt32&>(*ptr).get_element(row_num);
DateV2Value<DateV2ValueType> val = binary_cast<UInt32, DateV2Value<DateV2ValueType>>(int_val);
char buf[64];
val.to_string(buf); // DateTime to_string the end is /0
return std::string {buf};
}
std::string DataTypeDateV2::to_string(UInt32 int_val) const {
DateV2Value<DateV2ValueType> val = binary_cast<UInt32, DateV2Value<DateV2ValueType>>(int_val);
char buf[64];
val.to_string(buf); // DateTime to_string the end is /0
return std::string {buf};
}
void DataTypeDateV2::to_string(const IColumn& column, size_t row_num, BufferWritable& ostr) const {
auto result = check_column_const_set_readability(column, row_num);
ColumnPtr ptr = result.first;
row_num = result.second;
UInt32 int_val = assert_cast<const ColumnUInt32&>(*ptr).get_element(row_num);
DateV2Value<DateV2ValueType> val = binary_cast<UInt32, DateV2Value<DateV2ValueType>>(int_val);
char buf[64];
char* pos = val.to_string(buf);
// DateTime to_string the end is /0
ostr.write(buf, pos - buf - 1);
}
Status DataTypeDateV2::from_string(ReadBuffer& rb, IColumn* column) const {
auto* column_data = static_cast<ColumnUInt32*>(column);
UInt32 val = 0;
if (!read_date_v2_text_impl<UInt32>(val, rb)) {
return Status::InvalidArgument("parse date fail, string: '{}'",
std::string(rb.position(), rb.count()).c_str());
}
column_data->insert_value(val);
return Status::OK();
}
MutableColumnPtr DataTypeDateV2::create_column() const {
return DataTypeNumberBase<UInt32>::create_column();
}
void DataTypeDateV2::cast_to_date_time(const UInt32 from, Int64& to) {
auto& to_value = (VecDateTimeValue&)to;
auto& from_value = (DateV2Value<DateV2ValueType>&)from;
to_value.create_from_date_v2(from_value, TimeType::TIME_DATETIME);
}
void DataTypeDateV2::cast_to_date(const UInt32 from, Int64& to) {
auto& to_value = (VecDateTimeValue&)(to);
auto& from_value = (DateV2Value<DateV2ValueType>&)from;
to_value.create_from_date_v2(from_value, TimeType::TIME_DATE);
}
void DataTypeDateV2::cast_to_date_time_v2(const UInt32 from, UInt64& to) {
to = ((UInt64)from) << TIME_PART_LENGTH;
}
void DataTypeDateV2::cast_from_date(const Int64 from, UInt32& to) {
auto& to_value = (DateV2Value<DateV2ValueType>&)(to);
auto from_value = binary_cast<Int64, VecDateTimeValue>(from);
to_value.unchecked_set_time(from_value.year(), from_value.month(), from_value.day(), 0, 0, 0,
0);
}
void DataTypeDateV2::cast_from_date_time(const Int64 from, UInt32& to) {
auto& to_value = (DateV2Value<DateV2ValueType>&)(to);
auto from_value = binary_cast<Int64, VecDateTimeValue>(from);
to_value.unchecked_set_time(from_value.year(), from_value.month(), from_value.day(), 0, 0, 0,
0);
}
bool DataTypeDateTimeV2::equals(const IDataType& rhs) const {
return typeid(rhs) == typeid(*this);
}
std::string DataTypeDateTimeV2::to_string(const IColumn& column, size_t row_num) const {
auto result = check_column_const_set_readability(column, row_num);
ColumnPtr ptr = result.first;
row_num = result.second;
UInt64 int_val = assert_cast<const ColumnUInt64&>(*ptr).get_element(row_num);
DateV2Value<DateTimeV2ValueType> val =
binary_cast<UInt64, DateV2Value<DateTimeV2ValueType>>(int_val);
char buf[64];
val.to_string(buf, _scale);
return buf; // DateTime to_string the end is /0
}
std::string DataTypeDateTimeV2::to_string(UInt64 int_val) const {
DateV2Value<DateTimeV2ValueType> val =
binary_cast<UInt64, DateV2Value<DateTimeV2ValueType>>(int_val);
char buf[64];
val.to_string(buf, _scale);
return buf; // DateTime to_string the end is /0
}
size_t DataTypeDateTimeV2::number_length() const {
//2024-01-01 00:00:00-000000
return 32;
}
void DataTypeDateTimeV2::push_number(ColumnString::Chars& chars, const UInt64& num) const {
DateV2Value<DateTimeV2ValueType> val =
binary_cast<UInt64, DateV2Value<DateTimeV2ValueType>>(num);
char buf[64];
char* pos = val.to_string(buf, _scale);
chars.insert(buf, pos - 1);
}
void DataTypeDateTimeV2::to_string(const IColumn& column, size_t row_num,
BufferWritable& ostr) const {
auto result = check_column_const_set_readability(column, row_num);
ColumnPtr ptr = result.first;
row_num = result.second;
UInt64 int_val = assert_cast<const ColumnUInt64&>(*ptr).get_element(row_num);
DateV2Value<DateTimeV2ValueType> val =
binary_cast<UInt64, DateV2Value<DateTimeV2ValueType>>(int_val);
char buf[64];
char* pos = val.to_string(buf, _scale);
ostr.write(buf, pos - buf - 1);
}
Status DataTypeDateTimeV2::from_string(ReadBuffer& rb, IColumn* column) const {
auto* column_data = assert_cast<ColumnUInt64*>(column);
UInt64 val = 0;
if (!read_datetime_v2_text_impl<UInt64>(val, rb, _scale)) {
return Status::InvalidArgument("parse date fail, string: '{}'",
std::string(rb.position(), rb.count()).c_str());
}
column_data->insert_value(val);
return Status::OK();
}
void DataTypeDateTimeV2::to_pb_column_meta(PColumnMeta* col_meta) const {
IDataType::to_pb_column_meta(col_meta);
col_meta->mutable_decimal_param()->set_scale(_scale);
}
MutableColumnPtr DataTypeDateTimeV2::create_column() const {
return DataTypeNumberBase<UInt64>::create_column();
}
void DataTypeDateTimeV2::cast_to_date_time(const UInt64 from, Int64& to) {
auto& to_value = (VecDateTimeValue&)to;
auto& from_value = (DateV2Value<DateTimeV2ValueType>&)from;
to_value.create_from_date_v2(from_value, TimeType::TIME_DATETIME);
}
void DataTypeDateTimeV2::cast_to_date(const UInt64 from, Int64& to) {
auto& to_value = (VecDateTimeValue&)(to);
auto& from_value = (DateV2Value<DateTimeV2ValueType>&)from;
to_value.create_from_date_v2(from_value, TimeType::TIME_DATE);
}
void DataTypeDateTimeV2::cast_from_date(const Int64 from, UInt64& to) {
auto& to_value = (DateV2Value<DateTimeV2ValueType>&)(to);
auto from_value = binary_cast<Int64, VecDateTimeValue>(from);
to_value.unchecked_set_time(from_value.year(), from_value.month(), from_value.day(),
from_value.hour(), from_value.minute(), from_value.second(), 0);
}
void DataTypeDateTimeV2::cast_from_date_time(const Int64 from, UInt64& to) {
auto& to_value = (DateV2Value<DateTimeV2ValueType>&)(to);
auto from_value = binary_cast<Int64, VecDateTimeValue>(from);
to_value.unchecked_set_time(from_value.year(), from_value.month(), from_value.day(),
from_value.hour(), from_value.minute(), from_value.second(), 0);
}
void DataTypeDateTimeV2::cast_to_date_v2(const UInt64 from, UInt32& to) {
to = from >> TIME_PART_LENGTH;
}
DataTypePtr create_datetimev2(UInt64 scale_value) {
if (scale_value > 6) {
throw doris::Exception(doris::ErrorCode::NOT_IMPLEMENTED_ERROR, "scale_value {} > 6",
scale_value);
}
return std::make_shared<DataTypeDateTimeV2>(scale_value);
}
} // namespace doris::vectorized