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 | 281 | static FunctionPtr create() { return std::make_shared<FunctionHexVariadic>(); }_ZN5doris19FunctionHexVariadicINS_13HexStringImplEE6createEv Line | Count | Source | 58 | 210 | static FunctionPtr create() { return std::make_shared<FunctionHexVariadic>(); } |
_ZN5doris19FunctionHexVariadicINS_10HexIntImplEE6createEv Line | Count | Source | 58 | 62 | static FunctionPtr create() { return std::make_shared<FunctionHexVariadic>(); } |
_ZN5doris19FunctionHexVariadicINS_10HexHLLImplEE6createEv Line | Count | Source | 58 | 9 | 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 | 254 | size_t get_number_of_arguments() const override { return 1; }_ZNK5doris19FunctionHexVariadicINS_13HexStringImplEE23get_number_of_argumentsEv Line | Count | Source | 62 | 201 | size_t get_number_of_arguments() const override { return 1; } |
_ZNK5doris19FunctionHexVariadicINS_10HexIntImplEE23get_number_of_argumentsEv Line | Count | Source | 62 | 53 | size_t get_number_of_arguments() const override { return 1; } |
Unexecuted instantiation: _ZNK5doris19FunctionHexVariadicINS_10HexHLLImplEE23get_number_of_argumentsEv |
63 | | |
64 | 254 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
65 | 254 | return std::make_shared<DataTypeString>(); |
66 | 254 | } _ZNK5doris19FunctionHexVariadicINS_13HexStringImplEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE Line | Count | Source | 64 | 201 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | 65 | 201 | return std::make_shared<DataTypeString>(); | 66 | 201 | } |
_ZNK5doris19FunctionHexVariadicINS_10HexIntImplEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE Line | Count | Source | 64 | 53 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | 65 | 53 | return std::make_shared<DataTypeString>(); | 66 | 53 | } |
Unexecuted instantiation: _ZNK5doris19FunctionHexVariadicINS_10HexHLLImplEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE |
67 | | |
68 | 24 | DataTypes get_variadic_argument_types_impl() const override { |
69 | 24 | return Impl::get_variadic_argument_types(); |
70 | 24 | } _ZNK5doris19FunctionHexVariadicINS_13HexStringImplEE32get_variadic_argument_types_implEv Line | Count | Source | 68 | 8 | DataTypes get_variadic_argument_types_impl() const override { | 69 | 8 | return Impl::get_variadic_argument_types(); | 70 | 8 | } |
_ZNK5doris19FunctionHexVariadicINS_10HexIntImplEE32get_variadic_argument_types_implEv Line | Count | Source | 68 | 8 | DataTypes get_variadic_argument_types_impl() const override { | 69 | 8 | return Impl::get_variadic_argument_types(); | 70 | 8 | } |
_ZNK5doris19FunctionHexVariadicINS_10HexHLLImplEE32get_variadic_argument_types_implEv Line | Count | Source | 68 | 8 | DataTypes get_variadic_argument_types_impl() const override { | 69 | 8 | return Impl::get_variadic_argument_types(); | 70 | 8 | } |
|
71 | | |
72 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
73 | 301 | uint32_t result, size_t input_rows_count) const override { |
74 | 301 | ColumnPtr& argument_column = block.get_by_position(arguments[0]).column; |
75 | | |
76 | 301 | auto result_data_column = ColumnString::create(); |
77 | 301 | auto& result_data = result_data_column->get_chars(); |
78 | 301 | auto& result_offset = result_data_column->get_offsets(); |
79 | | |
80 | 301 | RETURN_IF_ERROR( |
81 | 301 | Impl::vector(argument_column, input_rows_count, result_data, result_offset)); |
82 | 301 | block.replace_by_position(result, std::move(result_data_column)); |
83 | 301 | return Status::OK(); |
84 | 301 | } _ZNK5doris19FunctionHexVariadicINS_13HexStringImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm Line | Count | Source | 73 | 226 | uint32_t result, size_t input_rows_count) const override { | 74 | 226 | ColumnPtr& argument_column = block.get_by_position(arguments[0]).column; | 75 | | | 76 | 226 | auto result_data_column = ColumnString::create(); | 77 | 226 | auto& result_data = result_data_column->get_chars(); | 78 | 226 | auto& result_offset = result_data_column->get_offsets(); | 79 | | | 80 | 226 | RETURN_IF_ERROR( | 81 | 226 | Impl::vector(argument_column, input_rows_count, result_data, result_offset)); | 82 | 226 | block.replace_by_position(result, std::move(result_data_column)); | 83 | 226 | return Status::OK(); | 84 | 226 | } |
_ZNK5doris19FunctionHexVariadicINS_10HexIntImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm Line | Count | Source | 73 | 75 | uint32_t result, size_t input_rows_count) const override { | 74 | 75 | ColumnPtr& argument_column = block.get_by_position(arguments[0]).column; | 75 | | | 76 | 75 | auto result_data_column = ColumnString::create(); | 77 | 75 | auto& result_data = result_data_column->get_chars(); | 78 | 75 | auto& result_offset = result_data_column->get_offsets(); | 79 | | | 80 | 75 | RETURN_IF_ERROR( | 81 | 75 | Impl::vector(argument_column, input_rows_count, result_data, result_offset)); | 82 | 75 | block.replace_by_position(result, std::move(result_data_column)); | 83 | 75 | return Status::OK(); | 84 | 75 | } |
Unexecuted instantiation: _ZNK5doris19FunctionHexVariadicINS_10HexHLLImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm |
85 | | }; |
86 | | |
87 | | struct HexStringImpl { |
88 | 8 | 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 | 226 | ColumnString::Chars& dst_data, ColumnString::Offsets& dst_offsets) { |
92 | 226 | const auto* str_col = check_and_get_column<ColumnString>(argument_column.get()); |
93 | 226 | const auto& data = str_col->get_chars(); |
94 | 226 | const auto& offsets = str_col->get_offsets(); |
95 | 226 | dst_offsets.resize(input_rows_count); |
96 | 226 | dst_data.resize(data.size() * 2); |
97 | | |
98 | 226 | size_t offset = 0; |
99 | 226 | auto* dst_data_ptr = dst_data.data(); |
100 | 1.36k | for (int i = 0; i < input_rows_count; ++i) { |
101 | 1.13k | const auto* source = reinterpret_cast<const unsigned char*>(&data[offsets[i - 1]]); |
102 | 1.13k | size_t srclen = offsets[i] - offsets[i - 1]; |
103 | 1.13k | string_hex::hex_encode(source, srclen, dst_data_ptr, offset); |
104 | 1.13k | dst_offsets[i] = cast_set<uint32_t>(offset); |
105 | 1.13k | } |
106 | 226 | return Status::OK(); |
107 | 226 | } |
108 | | }; |
109 | | |
110 | | struct HexIntImpl { |
111 | 8 | static DataTypes get_variadic_argument_types() { return {std::make_shared<DataTypeInt64>()}; } |
112 | | |
113 | 292 | static std::string_view hex(uint64_t num, char* ans) { |
114 | 292 | static constexpr auto hex_table = "0123456789ABCDEF"; |
115 | | // uint64_t max value 0xFFFFFFFFFFFFFFFF , 16 'F' |
116 | 292 | if (num == 0) { |
117 | 114 | return {hex_table, 1}; |
118 | 114 | } |
119 | | |
120 | 178 | int i = 0; |
121 | 1.37k | while (num) { |
122 | 1.19k | ans[i++] = hex_table[num & 15]; |
123 | 1.19k | num = num >> 4; |
124 | 1.19k | } |
125 | 178 | ans[i] = '\0'; |
126 | | |
127 | | // reverse |
128 | 802 | for (int k = 0, j = i - 1; k <= j && k <= 16; k++, j--) { |
129 | 624 | char tmp = ans[j]; |
130 | 624 | ans[j] = ans[k]; |
131 | 624 | ans[k] = tmp; |
132 | 624 | } |
133 | | |
134 | 178 | return {ans, static_cast<size_t>(i)}; |
135 | 292 | } |
136 | | |
137 | | static Status vector(ColumnPtr argument_column, size_t input_rows_count, |
138 | 75 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offsets) { |
139 | 75 | const auto* str_col = check_and_get_column<ColumnInt64>(argument_column.get()); |
140 | 75 | const auto& data = str_col->get_data(); |
141 | | |
142 | 75 | res_offsets.resize(input_rows_count); |
143 | 75 | char ans[17]; |
144 | 367 | for (size_t i = 0; i < input_rows_count; ++i) { |
145 | 292 | StringOP::push_value_string(hex(data[i], ans), i, res_data, res_offsets); |
146 | 292 | } |
147 | 75 | return Status::OK(); |
148 | 75 | } |
149 | | }; |
150 | | |
151 | | struct HexHLLImpl { |
152 | 8 | 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 | 8 | void register_function_hex_variadic(SimpleFunctionFactory& factory) { |
181 | 8 | factory.register_function<FunctionHexVariadic<HexStringImpl>>(); |
182 | 8 | factory.register_function<FunctionHexVariadic<HexIntImpl>>(); |
183 | 8 | factory.register_function<FunctionHexVariadic<HexHLLImpl>>(); |
184 | 8 | } |
185 | | } // namespace doris |