forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_type_date.cpp
More file actions
104 lines (90 loc) · 3.66 KB
/
Copy pathdata_type_date.cpp
File metadata and controls
104 lines (90 loc) · 3.66 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
// 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.
// This file is copied from
// https://github.com/ClickHouse/ClickHouse/blob/master/src/DataTypes/DataTypeDate.cpp
// and modified by Doris
#include "vec/data_types/data_type_date.h"
#include <typeinfo>
#include <utility>
#include "util/binary_cast.hpp"
#include "vec/columns/column.h"
#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/data_types/data_type.h"
#include "vec/io/io_helper.h"
#include "vec/io/reader_buffer.h"
#include "vec/runtime/vdatetime_value.h"
namespace doris::vectorized {
bool DataTypeDate::equals(const IDataType& rhs) const {
return typeid(rhs) == typeid(*this);
}
size_t DataTypeDate::number_length() const {
return 10;
}
void DataTypeDate::push_number(ColumnString::Chars& chars, const Int64& num) const {
doris::VecDateTimeValue value = binary_cast<Int64, doris::VecDateTimeValue>(num);
char buf[64];
char* pos = value.to_string(buf);
// DateTime to_string the end is /0
chars.insert(buf, pos - 1);
}
std::string DataTypeDate::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;
Int64 int_val = assert_cast<const ColumnInt64&>(*ptr).get_element(row_num);
doris::VecDateTimeValue value = binary_cast<Int64, doris::VecDateTimeValue>(int_val);
char buf[64];
value.to_string(buf);
return buf;
}
void DataTypeDate::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;
Int64 int_val = assert_cast<const ColumnInt64&>(*ptr).get_element(row_num);
doris::VecDateTimeValue value = binary_cast<Int64, doris::VecDateTimeValue>(int_val);
char buf[64];
char* pos = value.to_string(buf);
// DateTime to_string the end is /0
ostr.write(buf, pos - buf - 1);
}
Status DataTypeDate::from_string(ReadBuffer& rb, IColumn* column) const {
auto* column_data = static_cast<ColumnInt64*>(column);
Int64 val = 0;
if (!read_date_text_impl<Int64>(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();
}
void DataTypeDate::cast_to_date(Int64& x) {
auto value = binary_cast<Int64, VecDateTimeValue>(x);
value.cast_to_date();
x = binary_cast<VecDateTimeValue, Int64>(value);
}
MutableColumnPtr DataTypeDate::create_column() const {
auto col = DataTypeNumberBase<Int64>::create_column();
col->set_date_type();
return col;
}
} // namespace doris::vectorized