be/src/exprs/function/cast/cast_to_ip.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include "core/column/column_nullable.h" |
21 | | #include "core/data_type/data_type_ipv4.h" |
22 | | #include "core/data_type/primitive_type.h" |
23 | | #include "exprs/function/cast/cast_base.h" |
24 | | |
25 | | namespace doris { |
26 | | #include "common/compile_check_begin.h" |
27 | | |
28 | | struct CastToIPv4 { |
29 | | static bool from_string(const StringRef& from, IPv4& to, CastParameters&); |
30 | | }; |
31 | | |
32 | 48 | inline bool CastToIPv4::from_string(const StringRef& from, IPv4& to, CastParameters&) { |
33 | 48 | return IPv4Value::from_string(to, from.data, from.size); |
34 | 48 | } |
35 | | |
36 | | struct CastToIPv6 { |
37 | | static bool from_string(const StringRef& from, IPv6& to, CastParameters&); |
38 | | static bool from_ipv4(const IPv4& from, IPv6& to, CastParameters&); |
39 | | }; |
40 | | |
41 | 51 | inline bool CastToIPv6::from_string(const StringRef& from, IPv6& to, CastParameters&) { |
42 | 51 | return IPv6Value::from_string(to, from.data, from.size); |
43 | 51 | } |
44 | | |
45 | 4 | inline bool CastToIPv6::from_ipv4(const IPv4& from, IPv6& to, CastParameters&) { |
46 | 4 | map_ipv4_to_ipv6(from, reinterpret_cast<UInt8*>(&to)); |
47 | 4 | return true; |
48 | 4 | } |
49 | | |
50 | | template <CastModeType Mode, typename IpDataType> |
51 | | requires(IsIPType<IpDataType>) |
52 | | class CastToImpl<Mode, DataTypeString, IpDataType> : public CastToBase { |
53 | | public: |
54 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
55 | | uint32_t result, size_t input_rows_count, |
56 | 16 | const NullMap::value_type* null_map = nullptr) const override { |
57 | 16 | const auto* col_from = check_and_get_column<DataTypeString::ColumnType>( |
58 | 16 | block.get_by_position(arguments[0]).column.get()); |
59 | | |
60 | 16 | auto to_type = block.get_by_position(result).type; |
61 | 16 | auto serde = remove_nullable(to_type)->get_serde(); |
62 | | |
63 | | // by default framework, to_type is already unwrapped nullable |
64 | 16 | MutableColumnPtr column_to = to_type->create_column(); |
65 | 16 | ColumnNullable::MutablePtr nullable_col_to = ColumnNullable::create( |
66 | 16 | std::move(column_to), ColumnUInt8::create(input_rows_count, 0)); |
67 | | |
68 | 16 | if constexpr (Mode == CastModeType::NonStrictMode) { |
69 | | // may write nulls to nullable_col_to |
70 | 4 | RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {})); |
71 | 12 | } else if constexpr (Mode == CastModeType::StrictMode) { |
72 | | // WON'T write nulls to nullable_col_to, just raise errors. null_map is only used to skip invalid rows |
73 | 12 | RETURN_IF_ERROR(serde->from_string_strict_mode_batch( |
74 | 12 | *col_from, nullable_col_to->get_nested_column(), {}, null_map)); |
75 | | } else { |
76 | | return Status::InternalError("Unsupported cast mode"); |
77 | | } |
78 | | |
79 | 6 | block.get_by_position(result).column = std::move(nullable_col_to); |
80 | 16 | return Status::OK(); |
81 | 16 | } _ZNK5doris10CastToImplILNS_12CastModeTypeE0ENS_14DataTypeStringENS_12DataTypeIPv4EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh Line | Count | Source | 56 | 5 | const NullMap::value_type* null_map = nullptr) const override { | 57 | 5 | const auto* col_from = check_and_get_column<DataTypeString::ColumnType>( | 58 | 5 | block.get_by_position(arguments[0]).column.get()); | 59 | | | 60 | 5 | auto to_type = block.get_by_position(result).type; | 61 | 5 | auto serde = remove_nullable(to_type)->get_serde(); | 62 | | | 63 | | // by default framework, to_type is already unwrapped nullable | 64 | 5 | MutableColumnPtr column_to = to_type->create_column(); | 65 | 5 | ColumnNullable::MutablePtr nullable_col_to = ColumnNullable::create( | 66 | 5 | std::move(column_to), ColumnUInt8::create(input_rows_count, 0)); | 67 | | | 68 | | if constexpr (Mode == CastModeType::NonStrictMode) { | 69 | | // may write nulls to nullable_col_to | 70 | | RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {})); | 71 | 5 | } else if constexpr (Mode == CastModeType::StrictMode) { | 72 | | // WON'T write nulls to nullable_col_to, just raise errors. null_map is only used to skip invalid rows | 73 | 5 | RETURN_IF_ERROR(serde->from_string_strict_mode_batch( | 74 | 5 | *col_from, nullable_col_to->get_nested_column(), {}, null_map)); | 75 | | } else { | 76 | | return Status::InternalError("Unsupported cast mode"); | 77 | | } | 78 | | | 79 | 1 | block.get_by_position(result).column = std::move(nullable_col_to); | 80 | 5 | return Status::OK(); | 81 | 5 | } |
_ZNK5doris10CastToImplILNS_12CastModeTypeE1ENS_14DataTypeStringENS_12DataTypeIPv4EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh Line | Count | Source | 56 | 2 | const NullMap::value_type* null_map = nullptr) const override { | 57 | 2 | const auto* col_from = check_and_get_column<DataTypeString::ColumnType>( | 58 | 2 | block.get_by_position(arguments[0]).column.get()); | 59 | | | 60 | 2 | auto to_type = block.get_by_position(result).type; | 61 | 2 | auto serde = remove_nullable(to_type)->get_serde(); | 62 | | | 63 | | // by default framework, to_type is already unwrapped nullable | 64 | 2 | MutableColumnPtr column_to = to_type->create_column(); | 65 | 2 | ColumnNullable::MutablePtr nullable_col_to = ColumnNullable::create( | 66 | 2 | std::move(column_to), ColumnUInt8::create(input_rows_count, 0)); | 67 | | | 68 | 2 | if constexpr (Mode == CastModeType::NonStrictMode) { | 69 | | // may write nulls to nullable_col_to | 70 | 2 | RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {})); | 71 | | } else if constexpr (Mode == CastModeType::StrictMode) { | 72 | | // WON'T write nulls to nullable_col_to, just raise errors. null_map is only used to skip invalid rows | 73 | | RETURN_IF_ERROR(serde->from_string_strict_mode_batch( | 74 | | *col_from, nullable_col_to->get_nested_column(), {}, null_map)); | 75 | | } else { | 76 | | return Status::InternalError("Unsupported cast mode"); | 77 | | } | 78 | | | 79 | 2 | block.get_by_position(result).column = std::move(nullable_col_to); | 80 | 2 | return Status::OK(); | 81 | 2 | } |
_ZNK5doris10CastToImplILNS_12CastModeTypeE0ENS_14DataTypeStringENS_12DataTypeIPv6EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh Line | Count | Source | 56 | 7 | const NullMap::value_type* null_map = nullptr) const override { | 57 | 7 | const auto* col_from = check_and_get_column<DataTypeString::ColumnType>( | 58 | 7 | block.get_by_position(arguments[0]).column.get()); | 59 | | | 60 | 7 | auto to_type = block.get_by_position(result).type; | 61 | 7 | auto serde = remove_nullable(to_type)->get_serde(); | 62 | | | 63 | | // by default framework, to_type is already unwrapped nullable | 64 | 7 | MutableColumnPtr column_to = to_type->create_column(); | 65 | 7 | ColumnNullable::MutablePtr nullable_col_to = ColumnNullable::create( | 66 | 7 | std::move(column_to), ColumnUInt8::create(input_rows_count, 0)); | 67 | | | 68 | | if constexpr (Mode == CastModeType::NonStrictMode) { | 69 | | // may write nulls to nullable_col_to | 70 | | RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {})); | 71 | 7 | } else if constexpr (Mode == CastModeType::StrictMode) { | 72 | | // WON'T write nulls to nullable_col_to, just raise errors. null_map is only used to skip invalid rows | 73 | 7 | RETURN_IF_ERROR(serde->from_string_strict_mode_batch( | 74 | 7 | *col_from, nullable_col_to->get_nested_column(), {}, null_map)); | 75 | | } else { | 76 | | return Status::InternalError("Unsupported cast mode"); | 77 | | } | 78 | | | 79 | 1 | block.get_by_position(result).column = std::move(nullable_col_to); | 80 | 7 | return Status::OK(); | 81 | 7 | } |
_ZNK5doris10CastToImplILNS_12CastModeTypeE1ENS_14DataTypeStringENS_12DataTypeIPv6EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh Line | Count | Source | 56 | 2 | const NullMap::value_type* null_map = nullptr) const override { | 57 | 2 | const auto* col_from = check_and_get_column<DataTypeString::ColumnType>( | 58 | 2 | block.get_by_position(arguments[0]).column.get()); | 59 | | | 60 | 2 | auto to_type = block.get_by_position(result).type; | 61 | 2 | auto serde = remove_nullable(to_type)->get_serde(); | 62 | | | 63 | | // by default framework, to_type is already unwrapped nullable | 64 | 2 | MutableColumnPtr column_to = to_type->create_column(); | 65 | 2 | ColumnNullable::MutablePtr nullable_col_to = ColumnNullable::create( | 66 | 2 | std::move(column_to), ColumnUInt8::create(input_rows_count, 0)); | 67 | | | 68 | 2 | if constexpr (Mode == CastModeType::NonStrictMode) { | 69 | | // may write nulls to nullable_col_to | 70 | 2 | RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {})); | 71 | | } else if constexpr (Mode == CastModeType::StrictMode) { | 72 | | // WON'T write nulls to nullable_col_to, just raise errors. null_map is only used to skip invalid rows | 73 | | RETURN_IF_ERROR(serde->from_string_strict_mode_batch( | 74 | | *col_from, nullable_col_to->get_nested_column(), {}, null_map)); | 75 | | } else { | 76 | | return Status::InternalError("Unsupported cast mode"); | 77 | | } | 78 | | | 79 | 2 | block.get_by_position(result).column = std::move(nullable_col_to); | 80 | 2 | return Status::OK(); | 81 | 2 | } |
|
82 | | }; |
83 | | |
84 | | template <CastModeType AllMode> |
85 | | class CastToImpl<AllMode, DataTypeIPv4, DataTypeIPv6> : public CastToBase { |
86 | | public: |
87 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
88 | | uint32_t result, size_t input_rows_count, |
89 | 1 | const NullMap::value_type* null_map = nullptr) const override { |
90 | 1 | const auto* col_from = check_and_get_column<DataTypeIPv4::ColumnType>( |
91 | 1 | block.get_by_position(arguments[0]).column.get()); |
92 | 1 | const auto size = col_from->size(); |
93 | 1 | auto col_to = DataTypeIPv6::ColumnType::create(size); |
94 | 1 | auto& to_data = col_to->get_data(); |
95 | 1 | const auto& from_data = col_from->get_data(); |
96 | 1 | CastParameters params; |
97 | 1 | params.is_strict = (AllMode == CastModeType::StrictMode); |
98 | | |
99 | 5 | for (size_t i = 0; i < size; ++i) { |
100 | 4 | CastToIPv6::from_ipv4(from_data[i], to_data[i], params); |
101 | 4 | } |
102 | | |
103 | 1 | block.get_by_position(result).column = std::move(col_to); |
104 | 1 | return Status::OK(); |
105 | 1 | } Unexecuted instantiation: _ZNK5doris10CastToImplILNS_12CastModeTypeE0ENS_12DataTypeIPv4ENS_12DataTypeIPv6EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh _ZNK5doris10CastToImplILNS_12CastModeTypeE1ENS_12DataTypeIPv4ENS_12DataTypeIPv6EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh Line | Count | Source | 89 | 1 | const NullMap::value_type* null_map = nullptr) const override { | 90 | 1 | const auto* col_from = check_and_get_column<DataTypeIPv4::ColumnType>( | 91 | 1 | block.get_by_position(arguments[0]).column.get()); | 92 | 1 | const auto size = col_from->size(); | 93 | 1 | auto col_to = DataTypeIPv6::ColumnType::create(size); | 94 | 1 | auto& to_data = col_to->get_data(); | 95 | 1 | const auto& from_data = col_from->get_data(); | 96 | 1 | CastParameters params; | 97 | 1 | params.is_strict = (AllMode == CastModeType::StrictMode); | 98 | | | 99 | 5 | for (size_t i = 0; i < size; ++i) { | 100 | 4 | CastToIPv6::from_ipv4(from_data[i], to_data[i], params); | 101 | 4 | } | 102 | | | 103 | 1 | block.get_by_position(result).column = std::move(col_to); | 104 | 1 | return Status::OK(); | 105 | 1 | } |
|
106 | | }; |
107 | | #include "common/compile_check_end.h" |
108 | | } // namespace doris |