be/src/exprs/function/function_hex.cpp
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 | | #include <stddef.h> |
19 | | #include <stdint.h> |
20 | | |
21 | | #include <algorithm> |
22 | | #include <boost/iterator/iterator_facade.hpp> |
23 | | #include <memory> |
24 | | #include <string> |
25 | | #include <string_view> |
26 | | #include <utility> |
27 | | |
28 | | #include "common/cast_set.h" |
29 | | #include "common/status.h" |
30 | | #include "core/block/block.h" |
31 | | #include "core/block/column_numbers.h" |
32 | | #include "core/block/column_with_type_and_name.h" |
33 | | #include "core/column/column.h" |
34 | | #include "core/column/column_complex.h" |
35 | | #include "core/column/column_string.h" |
36 | | #include "core/column/column_vector.h" |
37 | | #include "core/data_type/data_type.h" |
38 | | #include "core/data_type/data_type_hll.h" |
39 | | #include "core/data_type/data_type_number.h" |
40 | | #include "core/data_type/data_type_string.h" |
41 | | #include "core/types.h" |
42 | | #include "exec/common/stringop_substring.h" |
43 | | #include "exprs/aggregate/aggregate_function.h" |
44 | | #include "exprs/function/function.h" |
45 | | #include "exprs/function/simple_function_factory.h" |
46 | | #include "exprs/function/string_hex_util.h" |
47 | | |
48 | | namespace doris { |
49 | | class FunctionContext; |
50 | | } // namespace doris |
51 | | |
52 | | namespace doris { |
53 | | template <typename Impl> |
54 | | class FunctionHexVariadic : public IFunction { |
55 | | public: |
56 | | static constexpr auto name = "hex"; |
57 | | |
58 | 25 | static FunctionPtr create() { return std::make_shared<FunctionHexVariadic>(); }_ZN5doris19FunctionHexVariadicINS_13HexStringImplEE6createEv Line | Count | Source | 58 | 20 | static FunctionPtr create() { return std::make_shared<FunctionHexVariadic>(); } |
_ZN5doris19FunctionHexVariadicINS_10HexIntImplEE6createEv Line | Count | Source | 58 | 3 | static FunctionPtr create() { return std::make_shared<FunctionHexVariadic>(); } |
_ZN5doris19FunctionHexVariadicINS_10HexHLLImplEE6createEv Line | Count | Source | 58 | 2 | static FunctionPtr create() { return std::make_shared<FunctionHexVariadic>(); } |
|
59 | | |
60 | 3 | String get_name() const override { return name; }_ZNK5doris19FunctionHexVariadicINS_13HexStringImplEE8get_nameB5cxx11Ev Line | Count | Source | 60 | 1 | String get_name() const override { return name; } |
_ZNK5doris19FunctionHexVariadicINS_10HexIntImplEE8get_nameB5cxx11Ev Line | Count | Source | 60 | 1 | String get_name() const override { return name; } |
_ZNK5doris19FunctionHexVariadicINS_10HexHLLImplEE8get_nameB5cxx11Ev Line | Count | Source | 60 | 1 | String get_name() const override { return name; } |
|
61 | | |
62 | 19 | size_t get_number_of_arguments() const override { return 1; }_ZNK5doris19FunctionHexVariadicINS_13HexStringImplEE23get_number_of_argumentsEv Line | Count | Source | 62 | 18 | size_t get_number_of_arguments() const override { return 1; } |
_ZNK5doris19FunctionHexVariadicINS_10HexIntImplEE23get_number_of_argumentsEv Line | Count | Source | 62 | 1 | size_t get_number_of_arguments() const override { return 1; } |
Unexecuted instantiation: _ZNK5doris19FunctionHexVariadicINS_10HexHLLImplEE23get_number_of_argumentsEv |
63 | | |
64 | 19 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
65 | 19 | return std::make_shared<DataTypeString>(); |
66 | 19 | } _ZNK5doris19FunctionHexVariadicINS_13HexStringImplEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE Line | Count | Source | 64 | 18 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | 65 | 18 | return std::make_shared<DataTypeString>(); | 66 | 18 | } |
_ZNK5doris19FunctionHexVariadicINS_10HexIntImplEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE Line | Count | Source | 64 | 1 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | 65 | 1 | return std::make_shared<DataTypeString>(); | 66 | 1 | } |
Unexecuted instantiation: _ZNK5doris19FunctionHexVariadicINS_10HexHLLImplEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE |
67 | | |
68 | 3 | DataTypes get_variadic_argument_types_impl() const override { |
69 | 3 | return Impl::get_variadic_argument_types(); |
70 | 3 | } _ZNK5doris19FunctionHexVariadicINS_13HexStringImplEE32get_variadic_argument_types_implEv Line | Count | Source | 68 | 1 | DataTypes get_variadic_argument_types_impl() const override { | 69 | 1 | return Impl::get_variadic_argument_types(); | 70 | 1 | } |
_ZNK5doris19FunctionHexVariadicINS_10HexIntImplEE32get_variadic_argument_types_implEv Line | Count | Source | 68 | 1 | DataTypes get_variadic_argument_types_impl() const override { | 69 | 1 | return Impl::get_variadic_argument_types(); | 70 | 1 | } |
_ZNK5doris19FunctionHexVariadicINS_10HexHLLImplEE32get_variadic_argument_types_implEv Line | Count | Source | 68 | 1 | DataTypes get_variadic_argument_types_impl() const override { | 69 | 1 | return Impl::get_variadic_argument_types(); | 70 | 1 | } |
|
71 | | |
72 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
73 | 18 | uint32_t result, size_t input_rows_count) const override { |
74 | 18 | ColumnPtr& argument_column = block.get_by_position(arguments[0]).column; |
75 | | |
76 | 18 | auto result_data_column = ColumnString::create(); |
77 | 18 | auto& result_data = result_data_column->get_chars(); |
78 | 18 | auto& result_offset = result_data_column->get_offsets(); |
79 | | |
80 | 18 | RETURN_IF_ERROR( |
81 | 18 | Impl::vector(argument_column, input_rows_count, result_data, result_offset)); |
82 | 18 | block.replace_by_position(result, std::move(result_data_column)); |
83 | 18 | return Status::OK(); |
84 | 18 | } _ZNK5doris19FunctionHexVariadicINS_13HexStringImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm Line | Count | Source | 73 | 17 | uint32_t result, size_t input_rows_count) const override { | 74 | 17 | ColumnPtr& argument_column = block.get_by_position(arguments[0]).column; | 75 | | | 76 | 17 | auto result_data_column = ColumnString::create(); | 77 | 17 | auto& result_data = result_data_column->get_chars(); | 78 | 17 | auto& result_offset = result_data_column->get_offsets(); | 79 | | | 80 | 17 | RETURN_IF_ERROR( | 81 | 17 | Impl::vector(argument_column, input_rows_count, result_data, result_offset)); | 82 | 17 | block.replace_by_position(result, std::move(result_data_column)); | 83 | 17 | return Status::OK(); | 84 | 17 | } |
_ZNK5doris19FunctionHexVariadicINS_10HexIntImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm Line | Count | Source | 73 | 1 | uint32_t result, size_t input_rows_count) const override { | 74 | 1 | ColumnPtr& argument_column = block.get_by_position(arguments[0]).column; | 75 | | | 76 | 1 | auto result_data_column = ColumnString::create(); | 77 | 1 | auto& result_data = result_data_column->get_chars(); | 78 | 1 | auto& result_offset = result_data_column->get_offsets(); | 79 | | | 80 | 1 | RETURN_IF_ERROR( | 81 | 1 | Impl::vector(argument_column, input_rows_count, result_data, result_offset)); | 82 | 1 | block.replace_by_position(result, std::move(result_data_column)); | 83 | 1 | return Status::OK(); | 84 | 1 | } |
Unexecuted instantiation: _ZNK5doris19FunctionHexVariadicINS_10HexHLLImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm |
85 | | }; |
86 | | |
87 | | struct HexStringImpl { |
88 | 1 | static DataTypes get_variadic_argument_types() { return {std::make_shared<DataTypeString>()}; } |
89 | | |
90 | | static Status vector(ColumnPtr argument_column, size_t input_rows_count, |
91 | 17 | ColumnString::Chars& dst_data, ColumnString::Offsets& dst_offsets) { |
92 | 17 | const auto* str_col = check_and_get_column<ColumnString>(argument_column.get()); |
93 | 17 | const auto& data = str_col->get_chars(); |
94 | 17 | const auto& offsets = str_col->get_offsets(); |
95 | 17 | dst_offsets.resize(input_rows_count); |
96 | 17 | dst_data.resize(data.size() * 2); |
97 | | |
98 | 17 | size_t offset = 0; |
99 | 17 | auto* dst_data_ptr = dst_data.data(); |
100 | 50 | for (int i = 0; i < input_rows_count; ++i) { |
101 | 33 | const auto* source = reinterpret_cast<const unsigned char*>(&data[offsets[i - 1]]); |
102 | 33 | size_t srclen = offsets[i] - offsets[i - 1]; |
103 | 33 | string_hex::hex_encode(source, srclen, dst_data_ptr, offset); |
104 | 33 | dst_offsets[i] = cast_set<uint32_t>(offset); |
105 | 33 | } |
106 | 17 | return Status::OK(); |
107 | 17 | } |
108 | | }; |
109 | | |
110 | | struct HexIntImpl { |
111 | 1 | static DataTypes get_variadic_argument_types() { return {std::make_shared<DataTypeInt64>()}; } |
112 | | |
113 | 9 | static std::string_view hex(uint64_t num, char* ans) { |
114 | 9 | static constexpr auto hex_table = "0123456789ABCDEF"; |
115 | | // uint64_t max value 0xFFFFFFFFFFFFFFFF , 16 'F' |
116 | 9 | if (num == 0) { |
117 | 2 | return {hex_table, 1}; |
118 | 2 | } |
119 | | |
120 | 7 | int i = 0; |
121 | 79 | while (num) { |
122 | 72 | ans[i++] = hex_table[num & 15]; |
123 | 72 | num = num >> 4; |
124 | 72 | } |
125 | 7 | ans[i] = '\0'; |
126 | | |
127 | | // reverse |
128 | 44 | for (int k = 0, j = i - 1; k <= j && k <= 16; k++, j--) { |
129 | 37 | char tmp = ans[j]; |
130 | 37 | ans[j] = ans[k]; |
131 | 37 | ans[k] = tmp; |
132 | 37 | } |
133 | | |
134 | 7 | return {ans, static_cast<size_t>(i)}; |
135 | 9 | } |
136 | | |
137 | | static Status vector(ColumnPtr argument_column, size_t input_rows_count, |
138 | 1 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offsets) { |
139 | 1 | const auto* str_col = check_and_get_column<ColumnInt64>(argument_column.get()); |
140 | 1 | const auto& data = str_col->get_data(); |
141 | | |
142 | 1 | res_offsets.resize(input_rows_count); |
143 | 1 | char ans[17]; |
144 | 10 | for (size_t i = 0; i < input_rows_count; ++i) { |
145 | 9 | StringOP::push_value_string(hex(data[i], ans), i, res_data, res_offsets); |
146 | 9 | } |
147 | 1 | return Status::OK(); |
148 | 1 | } |
149 | | }; |
150 | | |
151 | | struct HexHLLImpl { |
152 | 1 | static DataTypes get_variadic_argument_types() { return {std::make_shared<DataTypeHLL>()}; } |
153 | | |
154 | | static Status vector(ColumnPtr argument_column, size_t input_rows_count, |
155 | 0 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offsets) { |
156 | 0 | const auto* str_col = check_and_get_column<ColumnHLL>(argument_column.get()); |
157 | 0 | const auto& hll_data = str_col->get_data(); |
158 | 0 | res_offsets.resize(input_rows_count); |
159 | 0 | size_t total_length = 0, offset = 0; |
160 | 0 | std::string hll_str; |
161 | 0 | unsigned char* dst_data_ptr = nullptr; |
162 | |
|
163 | 0 | for (size_t i = 0; i < input_rows_count; ++i) { |
164 | 0 | hll_str.resize(hll_data[i].max_serialized_size(), '0'); |
165 | 0 | size_t actual_size = hll_data[i].serialize((uint8_t*)hll_str.data()); |
166 | 0 | hll_str.resize(actual_size); |
167 | 0 | total_length += actual_size; |
168 | |
|
169 | 0 | res_data.resize(total_length * 2 + (i + 1)); |
170 | 0 | dst_data_ptr = res_data.data() + offset; |
171 | 0 | string_hex::hex_encode(reinterpret_cast<const unsigned char*>(hll_str.data()), |
172 | 0 | hll_str.length(), dst_data_ptr, offset); |
173 | 0 | res_offsets[i] = cast_set<uint32_t>(offset); |
174 | 0 | hll_str.clear(); |
175 | 0 | } |
176 | 0 | return Status::OK(); |
177 | 0 | } |
178 | | }; |
179 | | |
180 | 1 | void register_function_hex_variadic(SimpleFunctionFactory& factory) { |
181 | 1 | factory.register_function<FunctionHexVariadic<HexStringImpl>>(); |
182 | 1 | factory.register_function<FunctionHexVariadic<HexIntImpl>>(); |
183 | 1 | factory.register_function<FunctionHexVariadic<HexHLLImpl>>(); |
184 | 1 | } |
185 | | } // namespace doris |