forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_cast_ip.cpp
More file actions
78 lines (69 loc) · 3.27 KB
/
Copy pathfunction_cast_ip.cpp
File metadata and controls
78 lines (69 loc) · 3.27 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
// 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 translation unit is the ONLY place that includes cast_to_ip.h.
// All CastToImpl<CastMode, From, ToIpType> template instantiations are
// confined here, keeping them out of function_cast.cpp.
#include "core/data_type/data_type_ipv4.h"
#include "core/data_type/data_type_ipv6.h"
#include "exprs/function/cast/cast_base.h"
#include "exprs/function/cast/cast_to_ip.h"
namespace doris::CastWrapper {
template <typename IpType>
requires(std::is_same_v<IpType, DataTypeIPv4> || std::is_same_v<IpType, DataTypeIPv6>)
WrapperType create_ip_wrapper(FunctionContext* context, const DataTypePtr& from_type) {
std::shared_ptr<CastToBase> cast_to_ip;
auto make_ip_wrapper = [&](const auto& types) -> bool {
using Types = std::decay_t<decltype(types)>;
using FromDataType = typename Types::LeftType;
if constexpr (IsDataTypeNumber<FromDataType> || IsStringType<FromDataType> ||
IsIPType<FromDataType>) {
if (context->enable_strict_mode()) {
cast_to_ip = std::make_shared<
CastToImpl<CastModeType::StrictMode, FromDataType, IpType>>();
} else {
cast_to_ip = std::make_shared<
CastToImpl<CastModeType::NonStrictMode, FromDataType, IpType>>();
}
return true;
} else {
return false;
}
};
if (!call_on_index_and_data_type<void>(from_type->get_primitive_type(), make_ip_wrapper)) {
return create_unsupport_wrapper(
fmt::format("CAST AS ip not supported {}", from_type->get_name()));
}
return [cast_to_ip](FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count,
const NullMap::value_type* null_map = nullptr) {
return cast_to_ip->execute_impl(context, block, arguments, result, input_rows_count,
null_map);
};
}
WrapperType create_ip_wrapper(FunctionContext* context, const DataTypePtr& from_type,
PrimitiveType to_type) {
switch (to_type) {
case TYPE_IPV4:
return create_ip_wrapper<DataTypeIPv4>(context, from_type);
case TYPE_IPV6:
return create_ip_wrapper<DataTypeIPv6>(context, from_type);
default:
return create_unsupport_wrapper(
fmt::format("CAST AS ip: unsupported to_type {}", type_to_string(to_type)));
}
}
} // namespace doris::CastWrapper