be/src/exprs/function/function_string_digest.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 <cstddef> |
19 | | #include <cstring> |
20 | | #include <string_view> |
21 | | #include <type_traits> |
22 | | #include <vector> |
23 | | |
24 | | #include "common/status.h" |
25 | | #include "core/assert_cast.h" |
26 | | #include "core/block/block.h" |
27 | | #include "core/block/column_numbers.h" |
28 | | #include "core/column/column_string.h" |
29 | | #include "core/column/column_varbinary.h" |
30 | | #include "core/column/column_vector.h" |
31 | | #include "core/data_type/data_type_string.h" |
32 | | #include "core/string_ref.h" |
33 | | #include "exec/common/stringop_substring.h" |
34 | | #include "exprs/function/function.h" |
35 | | #include "exprs/function/simple_function_factory.h" |
36 | | #include "exprs/function_context.h" |
37 | | #include "util/md5.h" |
38 | | #include "util/sha.h" |
39 | | #include "util/sm3.h" |
40 | | |
41 | | namespace doris { |
42 | | #include "common/compile_check_avoid_begin.h" |
43 | | |
44 | | struct SM3Sum { |
45 | | static constexpr auto name = "sm3sum"; |
46 | | using ObjectData = SM3Digest; |
47 | | }; |
48 | | |
49 | | struct MD5Sum { |
50 | | static constexpr auto name = "md5sum"; |
51 | | using ObjectData = Md5Digest; |
52 | | }; |
53 | | |
54 | | template <typename Impl> |
55 | | class FunctionStringDigestMulti : public IFunction { |
56 | | public: |
57 | | static constexpr auto name = Impl::name; |
58 | 313 | static FunctionPtr create() { return std::make_shared<FunctionStringDigestMulti>(); }_ZN5doris25FunctionStringDigestMultiINS_6SM3SumEE6createEv Line | Count | Source | 58 | 104 | static FunctionPtr create() { return std::make_shared<FunctionStringDigestMulti>(); } |
_ZN5doris25FunctionStringDigestMultiINS_6MD5SumEE6createEv Line | Count | Source | 58 | 209 | static FunctionPtr create() { return std::make_shared<FunctionStringDigestMulti>(); } |
|
59 | 0 | String get_name() const override { return name; }Unexecuted instantiation: _ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE8get_nameB5cxx11Ev Unexecuted instantiation: _ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE8get_nameB5cxx11Ev |
60 | 0 | size_t get_number_of_arguments() const override { return 0; }Unexecuted instantiation: _ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE23get_number_of_argumentsEv Unexecuted instantiation: _ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE23get_number_of_argumentsEv |
61 | 311 | bool is_variadic() const override { return true; }_ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE11is_variadicEv Line | Count | Source | 61 | 103 | bool is_variadic() const override { return true; } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE11is_variadicEv Line | Count | Source | 61 | 208 | bool is_variadic() const override { return true; } |
|
62 | | |
63 | 309 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
64 | 309 | return std::make_shared<DataTypeString>(); |
65 | 309 | } _ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE Line | Count | Source | 63 | 102 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | 64 | 102 | return std::make_shared<DataTypeString>(); | 65 | 102 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE Line | Count | Source | 63 | 207 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | 64 | 207 | return std::make_shared<DataTypeString>(); | 65 | 207 | } |
|
66 | | |
67 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
68 | 265 | uint32_t result, size_t input_rows_count) const override { |
69 | 265 | DCHECK_GE(arguments.size(), 1); |
70 | | |
71 | 265 | auto res = ColumnString::create(); |
72 | 265 | auto& res_data = res->get_chars(); |
73 | 265 | auto& res_offset = res->get_offsets(); |
74 | 265 | res_offset.resize(input_rows_count); |
75 | | |
76 | 265 | std::vector<ColumnPtr> argument_columns(arguments.size()); |
77 | 265 | std::vector<uint8_t> is_const(arguments.size(), 0); |
78 | 809 | for (size_t i = 0; i < arguments.size(); ++i) { |
79 | 544 | std::tie(argument_columns[i], is_const[i]) = |
80 | 544 | unpack_if_const(block.get_by_position(arguments[i]).column); |
81 | 544 | } |
82 | | |
83 | 265 | if (check_and_get_column<ColumnString>(argument_columns[0].get())) { |
84 | 138 | vector_execute<ColumnString>(block, input_rows_count, argument_columns, is_const, |
85 | 138 | res_data, res_offset); |
86 | 138 | } else if (check_and_get_column<ColumnVarbinary>(argument_columns[0].get())) { |
87 | 127 | vector_execute<ColumnVarbinary>(block, input_rows_count, argument_columns, is_const, |
88 | 127 | res_data, res_offset); |
89 | 127 | } else { |
90 | 0 | return Status::RuntimeError("Illegal column {} of argument of function {}", |
91 | 0 | argument_columns[0]->get_name(), get_name()); |
92 | 0 | } |
93 | | |
94 | 265 | block.replace_by_position(result, std::move(res)); |
95 | 265 | return Status::OK(); |
96 | 265 | } _ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm Line | Count | Source | 68 | 80 | uint32_t result, size_t input_rows_count) const override { | 69 | 80 | DCHECK_GE(arguments.size(), 1); | 70 | | | 71 | 80 | auto res = ColumnString::create(); | 72 | 80 | auto& res_data = res->get_chars(); | 73 | 80 | auto& res_offset = res->get_offsets(); | 74 | 80 | res_offset.resize(input_rows_count); | 75 | | | 76 | 80 | std::vector<ColumnPtr> argument_columns(arguments.size()); | 77 | 80 | std::vector<uint8_t> is_const(arguments.size(), 0); | 78 | 268 | for (size_t i = 0; i < arguments.size(); ++i) { | 79 | 188 | std::tie(argument_columns[i], is_const[i]) = | 80 | 188 | unpack_if_const(block.get_by_position(arguments[i]).column); | 81 | 188 | } | 82 | | | 83 | 80 | if (check_and_get_column<ColumnString>(argument_columns[0].get())) { | 84 | 40 | vector_execute<ColumnString>(block, input_rows_count, argument_columns, is_const, | 85 | 40 | res_data, res_offset); | 86 | 40 | } else if (check_and_get_column<ColumnVarbinary>(argument_columns[0].get())) { | 87 | 40 | vector_execute<ColumnVarbinary>(block, input_rows_count, argument_columns, is_const, | 88 | 40 | res_data, res_offset); | 89 | 40 | } else { | 90 | 0 | return Status::RuntimeError("Illegal column {} of argument of function {}", | 91 | 0 | argument_columns[0]->get_name(), get_name()); | 92 | 0 | } | 93 | | | 94 | 80 | block.replace_by_position(result, std::move(res)); | 95 | 80 | return Status::OK(); | 96 | 80 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm Line | Count | Source | 68 | 185 | uint32_t result, size_t input_rows_count) const override { | 69 | 185 | DCHECK_GE(arguments.size(), 1); | 70 | | | 71 | 185 | auto res = ColumnString::create(); | 72 | 185 | auto& res_data = res->get_chars(); | 73 | 185 | auto& res_offset = res->get_offsets(); | 74 | 185 | res_offset.resize(input_rows_count); | 75 | | | 76 | 185 | std::vector<ColumnPtr> argument_columns(arguments.size()); | 77 | 185 | std::vector<uint8_t> is_const(arguments.size(), 0); | 78 | 541 | for (size_t i = 0; i < arguments.size(); ++i) { | 79 | 356 | std::tie(argument_columns[i], is_const[i]) = | 80 | 356 | unpack_if_const(block.get_by_position(arguments[i]).column); | 81 | 356 | } | 82 | | | 83 | 185 | if (check_and_get_column<ColumnString>(argument_columns[0].get())) { | 84 | 98 | vector_execute<ColumnString>(block, input_rows_count, argument_columns, is_const, | 85 | 98 | res_data, res_offset); | 86 | 98 | } else if (check_and_get_column<ColumnVarbinary>(argument_columns[0].get())) { | 87 | 87 | vector_execute<ColumnVarbinary>(block, input_rows_count, argument_columns, is_const, | 88 | 87 | res_data, res_offset); | 89 | 87 | } else { | 90 | 0 | return Status::RuntimeError("Illegal column {} of argument of function {}", | 91 | 0 | argument_columns[0]->get_name(), get_name()); | 92 | 0 | } | 93 | | | 94 | 185 | block.replace_by_position(result, std::move(res)); | 95 | 185 | return Status::OK(); | 96 | 185 | } |
|
97 | | |
98 | | private: |
99 | | template <typename ColumnType> |
100 | | void vector_execute(Block& block, size_t input_rows_count, |
101 | | const std::vector<ColumnPtr>& argument_columns, |
102 | | const std::vector<uint8_t>& is_const, ColumnString::Chars& res_data, |
103 | 265 | ColumnString::Offsets& res_offset) const { |
104 | 265 | if constexpr (std::is_same_v<Impl, MD5Sum>) { |
105 | 185 | if (argument_columns.size() == 1) { |
106 | 86 | const auto* col = assert_cast<const ColumnType*>(argument_columns[0].get()); |
107 | 86 | vector_execute_single_md5(col, input_rows_count, is_const[0], res_data, res_offset); |
108 | 86 | return; |
109 | 86 | } |
110 | 185 | } |
111 | | |
112 | 99 | using ObjectData = typename Impl::ObjectData; |
113 | 487 | for (size_t i = 0; i < input_rows_count; ++i) { |
114 | 222 | ObjectData digest; |
115 | 768 | for (size_t j = 0; j < argument_columns.size(); ++j) { |
116 | 546 | const auto* col = assert_cast<const ColumnType*>(argument_columns[j].get()); |
117 | 546 | StringRef data_ref = col->get_data_at(is_const[j] ? 0 : i); |
118 | 546 | if (data_ref.size < 1) { |
119 | 166 | continue; |
120 | 166 | } |
121 | 380 | digest.update(data_ref.data, data_ref.size); |
122 | 380 | } |
123 | 222 | digest.digest(); |
124 | 222 | StringOP::push_value_string(std::string_view(digest.hex().c_str(), digest.hex().size()), |
125 | 222 | i, res_data, res_offset); |
126 | 222 | } |
127 | 265 | } _ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE14vector_executeINS_9ColumnStrIjEEEEvRNS_5BlockEmRKSt6vectorINS_3COWINS_7IColumnEE13immutable_ptrISA_EESaISD_EERKS8_IhSaIhEERNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNSM_IjLm4096ESP_Lm16ELm15EEE Line | Count | Source | 103 | 40 | ColumnString::Offsets& res_offset) const { | 104 | | if constexpr (std::is_same_v<Impl, MD5Sum>) { | 105 | | if (argument_columns.size() == 1) { | 106 | | const auto* col = assert_cast<const ColumnType*>(argument_columns[0].get()); | 107 | | vector_execute_single_md5(col, input_rows_count, is_const[0], res_data, res_offset); | 108 | | return; | 109 | | } | 110 | | } | 111 | | | 112 | 40 | using ObjectData = typename Impl::ObjectData; | 113 | 93 | for (size_t i = 0; i < input_rows_count; ++i) { | 114 | 53 | ObjectData digest; | 115 | 169 | for (size_t j = 0; j < argument_columns.size(); ++j) { | 116 | 116 | const auto* col = assert_cast<const ColumnType*>(argument_columns[j].get()); | 117 | 116 | StringRef data_ref = col->get_data_at(is_const[j] ? 0 : i); | 118 | 116 | if (data_ref.size < 1) { | 119 | 30 | continue; | 120 | 30 | } | 121 | 86 | digest.update(data_ref.data, data_ref.size); | 122 | 86 | } | 123 | 53 | digest.digest(); | 124 | 53 | StringOP::push_value_string(std::string_view(digest.hex().c_str(), digest.hex().size()), | 125 | 53 | i, res_data, res_offset); | 126 | 53 | } | 127 | 40 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE14vector_executeINS_15ColumnVarbinaryEEEvRNS_5BlockEmRKSt6vectorINS_3COWINS_7IColumnEE13immutable_ptrIS9_EESaISC_EERKS7_IhSaIhEERNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNSL_IjLm4096ESO_Lm16ELm15EEE Line | Count | Source | 103 | 40 | ColumnString::Offsets& res_offset) const { | 104 | | if constexpr (std::is_same_v<Impl, MD5Sum>) { | 105 | | if (argument_columns.size() == 1) { | 106 | | const auto* col = assert_cast<const ColumnType*>(argument_columns[0].get()); | 107 | | vector_execute_single_md5(col, input_rows_count, is_const[0], res_data, res_offset); | 108 | | return; | 109 | | } | 110 | | } | 111 | | | 112 | 40 | using ObjectData = typename Impl::ObjectData; | 113 | 93 | for (size_t i = 0; i < input_rows_count; ++i) { | 114 | 53 | ObjectData digest; | 115 | 169 | for (size_t j = 0; j < argument_columns.size(); ++j) { | 116 | 116 | const auto* col = assert_cast<const ColumnType*>(argument_columns[j].get()); | 117 | 116 | StringRef data_ref = col->get_data_at(is_const[j] ? 0 : i); | 118 | 116 | if (data_ref.size < 1) { | 119 | 30 | continue; | 120 | 30 | } | 121 | 86 | digest.update(data_ref.data, data_ref.size); | 122 | 86 | } | 123 | 53 | digest.digest(); | 124 | 53 | StringOP::push_value_string(std::string_view(digest.hex().c_str(), digest.hex().size()), | 125 | 53 | i, res_data, res_offset); | 126 | 53 | } | 127 | 40 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE14vector_executeINS_9ColumnStrIjEEEEvRNS_5BlockEmRKSt6vectorINS_3COWINS_7IColumnEE13immutable_ptrISA_EESaISD_EERKS8_IhSaIhEERNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNSM_IjLm4096ESP_Lm16ELm15EEE Line | Count | Source | 103 | 98 | ColumnString::Offsets& res_offset) const { | 104 | 98 | if constexpr (std::is_same_v<Impl, MD5Sum>) { | 105 | 98 | if (argument_columns.size() == 1) { | 106 | 52 | const auto* col = assert_cast<const ColumnType*>(argument_columns[0].get()); | 107 | 52 | vector_execute_single_md5(col, input_rows_count, is_const[0], res_data, res_offset); | 108 | 52 | return; | 109 | 52 | } | 110 | 98 | } | 111 | | | 112 | 46 | using ObjectData = typename Impl::ObjectData; | 113 | 152 | for (size_t i = 0; i < input_rows_count; ++i) { | 114 | 54 | ObjectData digest; | 115 | 203 | for (size_t j = 0; j < argument_columns.size(); ++j) { | 116 | 149 | const auto* col = assert_cast<const ColumnType*>(argument_columns[j].get()); | 117 | 149 | StringRef data_ref = col->get_data_at(is_const[j] ? 0 : i); | 118 | 149 | if (data_ref.size < 1) { | 119 | 51 | continue; | 120 | 51 | } | 121 | 98 | digest.update(data_ref.data, data_ref.size); | 122 | 98 | } | 123 | 54 | digest.digest(); | 124 | 54 | StringOP::push_value_string(std::string_view(digest.hex().c_str(), digest.hex().size()), | 125 | 54 | i, res_data, res_offset); | 126 | 54 | } | 127 | 98 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE14vector_executeINS_15ColumnVarbinaryEEEvRNS_5BlockEmRKSt6vectorINS_3COWINS_7IColumnEE13immutable_ptrIS9_EESaISC_EERKS7_IhSaIhEERNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNSL_IjLm4096ESO_Lm16ELm15EEE Line | Count | Source | 103 | 87 | ColumnString::Offsets& res_offset) const { | 104 | 87 | if constexpr (std::is_same_v<Impl, MD5Sum>) { | 105 | 87 | if (argument_columns.size() == 1) { | 106 | 34 | const auto* col = assert_cast<const ColumnType*>(argument_columns[0].get()); | 107 | 34 | vector_execute_single_md5(col, input_rows_count, is_const[0], res_data, res_offset); | 108 | 34 | return; | 109 | 34 | } | 110 | 87 | } | 111 | | | 112 | 53 | using ObjectData = typename Impl::ObjectData; | 113 | 149 | for (size_t i = 0; i < input_rows_count; ++i) { | 114 | 62 | ObjectData digest; | 115 | 227 | for (size_t j = 0; j < argument_columns.size(); ++j) { | 116 | 165 | const auto* col = assert_cast<const ColumnType*>(argument_columns[j].get()); | 117 | 165 | StringRef data_ref = col->get_data_at(is_const[j] ? 0 : i); | 118 | 165 | if (data_ref.size < 1) { | 119 | 55 | continue; | 120 | 55 | } | 121 | 110 | digest.update(data_ref.data, data_ref.size); | 122 | 110 | } | 123 | 62 | digest.digest(); | 124 | 62 | StringOP::push_value_string(std::string_view(digest.hex().c_str(), digest.hex().size()), | 125 | 62 | i, res_data, res_offset); | 126 | 62 | } | 127 | 87 | } |
|
128 | | |
129 | | template <typename ColumnType> |
130 | | void vector_execute_single_md5(const ColumnType* col, size_t input_rows_count, bool is_const, |
131 | | ColumnString::Chars& res_data, |
132 | 86 | ColumnString::Offsets& res_offset) const { |
133 | 86 | ColumnString::check_chars_length(input_rows_count * MD5_HEX_LENGTH, input_rows_count); |
134 | 86 | res_data.resize(input_rows_count * MD5_HEX_LENGTH); |
135 | 248 | for (size_t i = 0; i < input_rows_count; ++i) { |
136 | 162 | res_offset[i] = (i + 1) * MD5_HEX_LENGTH; |
137 | 162 | } |
138 | 86 | if (input_rows_count == 0) { |
139 | 0 | return; |
140 | 0 | } |
141 | | |
142 | 86 | if (is_const) { |
143 | 0 | StringRef data_ref = col->get_data_at(0); |
144 | 0 | const unsigned char* input = reinterpret_cast<const unsigned char*>(data_ref.data); |
145 | 0 | size_t length = data_ref.size; |
146 | 0 | char digest[MD5_HEX_LENGTH]; |
147 | 0 | md5_hex_batch(&input, &length, digest, 1); |
148 | 0 | for (size_t i = 0; i < input_rows_count; ++i) { |
149 | 0 | std::memcpy(res_data.data() + i * MD5_HEX_LENGTH, digest, MD5_HEX_LENGTH); |
150 | 0 | } |
151 | 0 | return; |
152 | 0 | } |
153 | | |
154 | 86 | std::vector<const unsigned char*> inputs(input_rows_count); |
155 | 86 | std::vector<size_t> lengths(input_rows_count); |
156 | 248 | for (size_t i = 0; i < input_rows_count; ++i) { |
157 | 162 | StringRef data_ref = col->get_data_at(i); |
158 | 162 | inputs[i] = reinterpret_cast<const unsigned char*>(data_ref.data); |
159 | 162 | lengths[i] = data_ref.size; |
160 | 162 | } |
161 | 86 | md5_hex_batch(inputs.data(), lengths.data(), reinterpret_cast<char*>(res_data.data()), |
162 | 86 | input_rows_count); |
163 | 86 | } _ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE25vector_execute_single_md5INS_9ColumnStrIjEEEEvPKT_mbRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS9_IjLm4096ESC_Lm16ELm15EEE Line | Count | Source | 132 | 52 | ColumnString::Offsets& res_offset) const { | 133 | 52 | ColumnString::check_chars_length(input_rows_count * MD5_HEX_LENGTH, input_rows_count); | 134 | 52 | res_data.resize(input_rows_count * MD5_HEX_LENGTH); | 135 | 151 | for (size_t i = 0; i < input_rows_count; ++i) { | 136 | 99 | res_offset[i] = (i + 1) * MD5_HEX_LENGTH; | 137 | 99 | } | 138 | 52 | if (input_rows_count == 0) { | 139 | 0 | return; | 140 | 0 | } | 141 | | | 142 | 52 | if (is_const) { | 143 | 0 | StringRef data_ref = col->get_data_at(0); | 144 | 0 | const unsigned char* input = reinterpret_cast<const unsigned char*>(data_ref.data); | 145 | 0 | size_t length = data_ref.size; | 146 | 0 | char digest[MD5_HEX_LENGTH]; | 147 | 0 | md5_hex_batch(&input, &length, digest, 1); | 148 | 0 | for (size_t i = 0; i < input_rows_count; ++i) { | 149 | 0 | std::memcpy(res_data.data() + i * MD5_HEX_LENGTH, digest, MD5_HEX_LENGTH); | 150 | 0 | } | 151 | 0 | return; | 152 | 0 | } | 153 | | | 154 | 52 | std::vector<const unsigned char*> inputs(input_rows_count); | 155 | 52 | std::vector<size_t> lengths(input_rows_count); | 156 | 151 | for (size_t i = 0; i < input_rows_count; ++i) { | 157 | 99 | StringRef data_ref = col->get_data_at(i); | 158 | 99 | inputs[i] = reinterpret_cast<const unsigned char*>(data_ref.data); | 159 | 99 | lengths[i] = data_ref.size; | 160 | 99 | } | 161 | 52 | md5_hex_batch(inputs.data(), lengths.data(), reinterpret_cast<char*>(res_data.data()), | 162 | 52 | input_rows_count); | 163 | 52 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE25vector_execute_single_md5INS_15ColumnVarbinaryEEEvPKT_mbRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Line | Count | Source | 132 | 34 | ColumnString::Offsets& res_offset) const { | 133 | 34 | ColumnString::check_chars_length(input_rows_count * MD5_HEX_LENGTH, input_rows_count); | 134 | 34 | res_data.resize(input_rows_count * MD5_HEX_LENGTH); | 135 | 97 | for (size_t i = 0; i < input_rows_count; ++i) { | 136 | 63 | res_offset[i] = (i + 1) * MD5_HEX_LENGTH; | 137 | 63 | } | 138 | 34 | if (input_rows_count == 0) { | 139 | 0 | return; | 140 | 0 | } | 141 | | | 142 | 34 | if (is_const) { | 143 | 0 | StringRef data_ref = col->get_data_at(0); | 144 | 0 | const unsigned char* input = reinterpret_cast<const unsigned char*>(data_ref.data); | 145 | 0 | size_t length = data_ref.size; | 146 | 0 | char digest[MD5_HEX_LENGTH]; | 147 | 0 | md5_hex_batch(&input, &length, digest, 1); | 148 | 0 | for (size_t i = 0; i < input_rows_count; ++i) { | 149 | 0 | std::memcpy(res_data.data() + i * MD5_HEX_LENGTH, digest, MD5_HEX_LENGTH); | 150 | 0 | } | 151 | 0 | return; | 152 | 0 | } | 153 | | | 154 | 34 | std::vector<const unsigned char*> inputs(input_rows_count); | 155 | 34 | std::vector<size_t> lengths(input_rows_count); | 156 | 97 | for (size_t i = 0; i < input_rows_count; ++i) { | 157 | 63 | StringRef data_ref = col->get_data_at(i); | 158 | 63 | inputs[i] = reinterpret_cast<const unsigned char*>(data_ref.data); | 159 | 63 | lengths[i] = data_ref.size; | 160 | 63 | } | 161 | 34 | md5_hex_batch(inputs.data(), lengths.data(), reinterpret_cast<char*>(res_data.data()), | 162 | 34 | input_rows_count); | 163 | 34 | } |
|
164 | | }; |
165 | | |
166 | | class FunctionStringDigestSHA1 : public IFunction { |
167 | | public: |
168 | | static constexpr auto name = "sha1"; |
169 | 14 | static FunctionPtr create() { return std::make_shared<FunctionStringDigestSHA1>(); } |
170 | 0 | String get_name() const override { return name; } |
171 | 0 | size_t get_number_of_arguments() const override { return 1; } |
172 | 13 | bool is_variadic() const override { return true; } |
173 | | |
174 | 12 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
175 | 12 | return std::make_shared<DataTypeString>(); |
176 | 12 | } |
177 | | |
178 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
179 | 10 | uint32_t result, size_t input_rows_count) const override { |
180 | 10 | DCHECK_EQ(arguments.size(), 1); |
181 | 10 | ColumnPtr data_col = block.get_by_position(arguments[0]).column; |
182 | | |
183 | 10 | auto res_col = ColumnString::create(); |
184 | 10 | auto& res_data = res_col->get_chars(); |
185 | 10 | auto& res_offset = res_col->get_offsets(); |
186 | 10 | res_offset.resize(input_rows_count); |
187 | 10 | if (const auto* str_col = check_and_get_column<ColumnString>(data_col.get())) { |
188 | 5 | vector_execute(str_col, input_rows_count, res_data, res_offset); |
189 | 5 | } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(data_col.get())) { |
190 | 5 | vector_execute(vb_col, input_rows_count, res_data, res_offset); |
191 | 5 | } else { |
192 | 0 | return Status::RuntimeError("Illegal column {} of argument of function {}", |
193 | 0 | data_col->get_name(), get_name()); |
194 | 0 | } |
195 | | |
196 | 10 | block.replace_by_position(result, std::move(res_col)); |
197 | 10 | return Status::OK(); |
198 | 10 | } |
199 | | |
200 | | private: |
201 | | template <typename ColumnType> |
202 | | void vector_execute(const ColumnType* col, size_t input_rows_count, |
203 | 10 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { |
204 | 10 | SHA1Digest digest; |
205 | 28 | for (size_t i = 0; i < input_rows_count; ++i) { |
206 | 18 | StringRef data_ref = col->get_data_at(i); |
207 | 18 | digest.reset(data_ref.data, data_ref.size); |
208 | 18 | std::string_view ans = digest.digest(); |
209 | | |
210 | 18 | StringOP::push_value_string(ans, i, res_data, res_offset); |
211 | 18 | } |
212 | 10 | } _ZNK5doris24FunctionStringDigestSHA114vector_executeINS_9ColumnStrIjEEEEvPKT_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS7_IjLm4096ESA_Lm16ELm15EEE Line | Count | Source | 203 | 5 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { | 204 | 5 | SHA1Digest digest; | 205 | 14 | for (size_t i = 0; i < input_rows_count; ++i) { | 206 | 9 | StringRef data_ref = col->get_data_at(i); | 207 | 9 | digest.reset(data_ref.data, data_ref.size); | 208 | 9 | std::string_view ans = digest.digest(); | 209 | | | 210 | 9 | StringOP::push_value_string(ans, i, res_data, res_offset); | 211 | 9 | } | 212 | 5 | } |
_ZNK5doris24FunctionStringDigestSHA114vector_executeINS_15ColumnVarbinaryEEEvPKT_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS6_IjLm4096ES9_Lm16ELm15EEE Line | Count | Source | 203 | 5 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { | 204 | 5 | SHA1Digest digest; | 205 | 14 | for (size_t i = 0; i < input_rows_count; ++i) { | 206 | 9 | StringRef data_ref = col->get_data_at(i); | 207 | 9 | digest.reset(data_ref.data, data_ref.size); | 208 | 9 | std::string_view ans = digest.digest(); | 209 | | | 210 | 9 | StringOP::push_value_string(ans, i, res_data, res_offset); | 211 | 9 | } | 212 | 5 | } |
|
213 | | }; |
214 | | |
215 | | class FunctionStringDigestSHA2 : public IFunction { |
216 | | public: |
217 | | static constexpr auto name = "sha2"; |
218 | 2 | static FunctionPtr create() { return std::make_shared<FunctionStringDigestSHA2>(); } |
219 | 0 | String get_name() const override { return name; } |
220 | 0 | size_t get_number_of_arguments() const override { return 2; } |
221 | 1 | bool is_variadic() const override { return true; } |
222 | | |
223 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
224 | 0 | return std::make_shared<DataTypeString>(); |
225 | 0 | } |
226 | | |
227 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
228 | 0 | uint32_t result, size_t input_rows_count) const override { |
229 | 0 | DCHECK(!is_column_const(*block.get_by_position(arguments[0]).column)); |
230 | |
|
231 | 0 | ColumnPtr data_col = block.get_by_position(arguments[0]).column; |
232 | |
|
233 | 0 | [[maybe_unused]] const auto& [right_column, right_const] = |
234 | 0 | unpack_if_const(block.get_by_position(arguments[1]).column); |
235 | 0 | auto digest_length = assert_cast<const ColumnInt32*>(right_column.get())->get_data()[0]; |
236 | |
|
237 | 0 | auto res_col = ColumnString::create(); |
238 | 0 | auto& res_data = res_col->get_chars(); |
239 | 0 | auto& res_offset = res_col->get_offsets(); |
240 | 0 | res_offset.resize(input_rows_count); |
241 | |
|
242 | 0 | if (digest_length == 224) { |
243 | 0 | execute_base<SHA224Digest>(data_col, input_rows_count, res_data, res_offset); |
244 | 0 | } else if (digest_length == 256) { |
245 | 0 | execute_base<SHA256Digest>(data_col, input_rows_count, res_data, res_offset); |
246 | 0 | } else if (digest_length == 384) { |
247 | 0 | execute_base<SHA384Digest>(data_col, input_rows_count, res_data, res_offset); |
248 | 0 | } else if (digest_length == 512) { |
249 | 0 | execute_base<SHA512Digest>(data_col, input_rows_count, res_data, res_offset); |
250 | 0 | } else { |
251 | 0 | return Status::InvalidArgument( |
252 | 0 | "sha2's digest length only support 224/256/384/512 but meet {}", digest_length); |
253 | 0 | } |
254 | | |
255 | 0 | block.replace_by_position(result, std::move(res_col)); |
256 | 0 | return Status::OK(); |
257 | 0 | } |
258 | | |
259 | | private: |
260 | | template <typename T> |
261 | | void execute_base(ColumnPtr data_col, int input_rows_count, ColumnString::Chars& res_data, |
262 | 0 | ColumnString::Offsets& res_offset) const { |
263 | 0 | if (const auto* str_col = check_and_get_column<ColumnString>(data_col.get())) { |
264 | 0 | vector_execute<T>(str_col, input_rows_count, res_data, res_offset); |
265 | 0 | } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(data_col.get())) { |
266 | 0 | vector_execute<T>(vb_col, input_rows_count, res_data, res_offset); |
267 | 0 | } else { |
268 | 0 | throw Exception(ErrorCode::RUNTIME_ERROR, |
269 | 0 | "Illegal column {} of argument of function {}", data_col->get_name(), |
270 | 0 | get_name()); |
271 | 0 | } |
272 | 0 | } Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA212execute_baseINS_12SHA224DigestEEEvNS_3COWINS_7IColumnEE13immutable_ptrIS4_EEiRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA212execute_baseINS_12SHA256DigestEEEvNS_3COWINS_7IColumnEE13immutable_ptrIS4_EEiRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA212execute_baseINS_12SHA384DigestEEEvNS_3COWINS_7IColumnEE13immutable_ptrIS4_EEiRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA212execute_baseINS_12SHA512DigestEEEvNS_3COWINS_7IColumnEE13immutable_ptrIS4_EEiRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE |
273 | | |
274 | | template <typename DigestType, typename ColumnType> |
275 | | void vector_execute(const ColumnType* col, size_t input_rows_count, |
276 | 0 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { |
277 | 0 | DigestType digest; |
278 | 0 | for (size_t i = 0; i < input_rows_count; ++i) { |
279 | 0 | StringRef data_ref = col->get_data_at(i); |
280 | 0 | digest.reset(data_ref.data, data_ref.size); |
281 | 0 | std::string_view ans = digest.digest(); |
282 | |
|
283 | 0 | StringOP::push_value_string(ans, i, res_data, res_offset); |
284 | 0 | } |
285 | 0 | } Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA224DigestENS_9ColumnStrIjEEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA224DigestENS_15ColumnVarbinaryEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS7_IjLm4096ESA_Lm16ELm15EEE Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA256DigestENS_9ColumnStrIjEEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA256DigestENS_15ColumnVarbinaryEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS7_IjLm4096ESA_Lm16ELm15EEE Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA384DigestENS_9ColumnStrIjEEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA384DigestENS_15ColumnVarbinaryEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS7_IjLm4096ESA_Lm16ELm15EEE Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA512DigestENS_9ColumnStrIjEEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA512DigestENS_15ColumnVarbinaryEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS7_IjLm4096ESA_Lm16ELm15EEE |
286 | | }; |
287 | | |
288 | 1 | void register_function_string_digest(SimpleFunctionFactory& factory) { |
289 | 1 | factory.register_function<FunctionStringDigestMulti<SM3Sum>>(); |
290 | 1 | factory.register_function<FunctionStringDigestMulti<MD5Sum>>(); |
291 | 1 | factory.register_function<FunctionStringDigestSHA1>(); |
292 | 1 | factory.register_function<FunctionStringDigestSHA2>(); |
293 | | |
294 | 1 | factory.register_alias(FunctionStringDigestMulti<MD5Sum>::name, "md5"); |
295 | 1 | factory.register_alias(FunctionStringDigestMulti<SM3Sum>::name, "sm3"); |
296 | 1 | factory.register_alias(FunctionStringDigestSHA1::name, "sha"); |
297 | 1 | } |
298 | | |
299 | | #include "common/compile_check_avoid_end.h" |
300 | | } // namespace doris |