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 | 341 | static FunctionPtr create() { return std::make_shared<FunctionStringDigestMulti>(); }_ZN5doris25FunctionStringDigestMultiINS_6SM3SumEE6createEv Line | Count | Source | 58 | 118 | static FunctionPtr create() { return std::make_shared<FunctionStringDigestMulti>(); } |
_ZN5doris25FunctionStringDigestMultiINS_6MD5SumEE6createEv Line | Count | Source | 58 | 223 | 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 | 327 | bool is_variadic() const override { return true; }_ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE11is_variadicEv Line | Count | Source | 61 | 111 | bool is_variadic() const override { return true; } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE11is_variadicEv Line | Count | Source | 61 | 216 | bool is_variadic() const override { return true; } |
|
62 | | |
63 | 325 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
64 | 325 | return std::make_shared<DataTypeString>(); |
65 | 325 | } _ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE Line | Count | Source | 63 | 110 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | 64 | 110 | return std::make_shared<DataTypeString>(); | 65 | 110 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE Line | Count | Source | 63 | 215 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | 64 | 215 | return std::make_shared<DataTypeString>(); | 65 | 215 | } |
|
66 | | |
67 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
68 | 281 | uint32_t result, size_t input_rows_count) const override { |
69 | 281 | DCHECK_GE(arguments.size(), 1); |
70 | | |
71 | 281 | auto res = ColumnString::create(); |
72 | 281 | auto& res_data = res->get_chars(); |
73 | 281 | auto& res_offset = res->get_offsets(); |
74 | 281 | res_offset.resize(input_rows_count); |
75 | | |
76 | 281 | std::vector<ColumnPtr> argument_columns(arguments.size()); |
77 | 281 | std::vector<uint8_t> is_const(arguments.size(), 0); |
78 | 857 | for (size_t i = 0; i < arguments.size(); ++i) { |
79 | 576 | std::tie(argument_columns[i], is_const[i]) = |
80 | 576 | unpack_if_const(block.get_by_position(arguments[i]).column); |
81 | 576 | } |
82 | | |
83 | 281 | if (check_and_get_column<ColumnString>(argument_columns[0].get())) { |
84 | 146 | vector_execute<ColumnString>(block, input_rows_count, argument_columns, is_const, |
85 | 146 | res_data, res_offset); |
86 | 146 | } else if (check_and_get_column<ColumnVarbinary>(argument_columns[0].get())) { |
87 | 135 | vector_execute<ColumnVarbinary>(block, input_rows_count, argument_columns, is_const, |
88 | 135 | res_data, res_offset); |
89 | 135 | } 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 | 281 | block.replace_by_position(result, std::move(res)); |
95 | 281 | return Status::OK(); |
96 | 281 | } _ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm Line | Count | Source | 68 | 88 | uint32_t result, size_t input_rows_count) const override { | 69 | 88 | DCHECK_GE(arguments.size(), 1); | 70 | | | 71 | 88 | auto res = ColumnString::create(); | 72 | 88 | auto& res_data = res->get_chars(); | 73 | 88 | auto& res_offset = res->get_offsets(); | 74 | 88 | res_offset.resize(input_rows_count); | 75 | | | 76 | 88 | std::vector<ColumnPtr> argument_columns(arguments.size()); | 77 | 88 | std::vector<uint8_t> is_const(arguments.size(), 0); | 78 | 292 | for (size_t i = 0; i < arguments.size(); ++i) { | 79 | 204 | std::tie(argument_columns[i], is_const[i]) = | 80 | 204 | unpack_if_const(block.get_by_position(arguments[i]).column); | 81 | 204 | } | 82 | | | 83 | 88 | if (check_and_get_column<ColumnString>(argument_columns[0].get())) { | 84 | 44 | vector_execute<ColumnString>(block, input_rows_count, argument_columns, is_const, | 85 | 44 | res_data, res_offset); | 86 | 44 | } else if (check_and_get_column<ColumnVarbinary>(argument_columns[0].get())) { | 87 | 44 | vector_execute<ColumnVarbinary>(block, input_rows_count, argument_columns, is_const, | 88 | 44 | res_data, res_offset); | 89 | 44 | } 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 | 88 | block.replace_by_position(result, std::move(res)); | 95 | 88 | return Status::OK(); | 96 | 88 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm Line | Count | Source | 68 | 193 | uint32_t result, size_t input_rows_count) const override { | 69 | 193 | DCHECK_GE(arguments.size(), 1); | 70 | | | 71 | 193 | auto res = ColumnString::create(); | 72 | 193 | auto& res_data = res->get_chars(); | 73 | 193 | auto& res_offset = res->get_offsets(); | 74 | 193 | res_offset.resize(input_rows_count); | 75 | | | 76 | 193 | std::vector<ColumnPtr> argument_columns(arguments.size()); | 77 | 193 | std::vector<uint8_t> is_const(arguments.size(), 0); | 78 | 565 | for (size_t i = 0; i < arguments.size(); ++i) { | 79 | 372 | std::tie(argument_columns[i], is_const[i]) = | 80 | 372 | unpack_if_const(block.get_by_position(arguments[i]).column); | 81 | 372 | } | 82 | | | 83 | 193 | if (check_and_get_column<ColumnString>(argument_columns[0].get())) { | 84 | 102 | vector_execute<ColumnString>(block, input_rows_count, argument_columns, is_const, | 85 | 102 | res_data, res_offset); | 86 | 102 | } else if (check_and_get_column<ColumnVarbinary>(argument_columns[0].get())) { | 87 | 91 | vector_execute<ColumnVarbinary>(block, input_rows_count, argument_columns, is_const, | 88 | 91 | res_data, res_offset); | 89 | 91 | } 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 | 193 | block.replace_by_position(result, std::move(res)); | 95 | 193 | return Status::OK(); | 96 | 193 | } |
|
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 | 281 | ColumnString::Offsets& res_offset) const { |
104 | 281 | if constexpr (std::is_same_v<Impl, MD5Sum>) { |
105 | 193 | if (argument_columns.size() == 1) { |
106 | 90 | const auto* col = assert_cast<const ColumnType*>(argument_columns[0].get()); |
107 | 90 | vector_execute_single_md5(col, input_rows_count, is_const[0], res_data, res_offset); |
108 | 90 | return; |
109 | 90 | } |
110 | 193 | } |
111 | | |
112 | 103 | using ObjectData = typename Impl::ObjectData; |
113 | 563 | for (size_t i = 0; i < input_rows_count; ++i) { |
114 | 282 | ObjectData digest; |
115 | 968 | for (size_t j = 0; j < argument_columns.size(); ++j) { |
116 | 686 | const auto* col = assert_cast<const ColumnType*>(argument_columns[j].get()); |
117 | 686 | StringRef data_ref = col->get_data_at(is_const[j] ? 0 : i); |
118 | 686 | if (data_ref.size < 1) { |
119 | 194 | continue; |
120 | 194 | } |
121 | 492 | digest.update(data_ref.data, data_ref.size); |
122 | 492 | } |
123 | 282 | digest.digest(); |
124 | 282 | StringOP::push_value_string(std::string_view(digest.hex().c_str(), digest.hex().size()), |
125 | 282 | i, res_data, res_offset); |
126 | 282 | } |
127 | 281 | } _ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE14vector_executeINS_9ColumnStrIjEEEEvRNS_5BlockEmRKSt6vectorINS_3COWINS_7IColumnEE13immutable_ptrISA_EESaISD_EERKS8_IhSaIhEERNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNSM_IjLm4096ESP_Lm16ELm15EEE Line | Count | Source | 103 | 44 | 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 | 44 | using ObjectData = typename Impl::ObjectData; | 113 | 117 | for (size_t i = 0; i < input_rows_count; ++i) { | 114 | 73 | ObjectData digest; | 115 | 229 | for (size_t j = 0; j < argument_columns.size(); ++j) { | 116 | 156 | const auto* col = assert_cast<const ColumnType*>(argument_columns[j].get()); | 117 | 156 | StringRef data_ref = col->get_data_at(is_const[j] ? 0 : i); | 118 | 156 | if (data_ref.size < 1) { | 119 | 38 | continue; | 120 | 38 | } | 121 | 118 | digest.update(data_ref.data, data_ref.size); | 122 | 118 | } | 123 | 73 | digest.digest(); | 124 | 73 | StringOP::push_value_string(std::string_view(digest.hex().c_str(), digest.hex().size()), | 125 | 73 | i, res_data, res_offset); | 126 | 73 | } | 127 | 44 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6SM3SumEE14vector_executeINS_15ColumnVarbinaryEEEvRNS_5BlockEmRKSt6vectorINS_3COWINS_7IColumnEE13immutable_ptrIS9_EESaISC_EERKS7_IhSaIhEERNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNSL_IjLm4096ESO_Lm16ELm15EEE Line | Count | Source | 103 | 44 | 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 | 44 | using ObjectData = typename Impl::ObjectData; | 113 | 117 | for (size_t i = 0; i < input_rows_count; ++i) { | 114 | 73 | ObjectData digest; | 115 | 229 | for (size_t j = 0; j < argument_columns.size(); ++j) { | 116 | 156 | const auto* col = assert_cast<const ColumnType*>(argument_columns[j].get()); | 117 | 156 | StringRef data_ref = col->get_data_at(is_const[j] ? 0 : i); | 118 | 156 | if (data_ref.size < 1) { | 119 | 38 | continue; | 120 | 38 | } | 121 | 118 | digest.update(data_ref.data, data_ref.size); | 122 | 118 | } | 123 | 73 | digest.digest(); | 124 | 73 | StringOP::push_value_string(std::string_view(digest.hex().c_str(), digest.hex().size()), | 125 | 73 | i, res_data, res_offset); | 126 | 73 | } | 127 | 44 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE14vector_executeINS_9ColumnStrIjEEEEvRNS_5BlockEmRKSt6vectorINS_3COWINS_7IColumnEE13immutable_ptrISA_EESaISD_EERKS8_IhSaIhEERNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNSM_IjLm4096ESP_Lm16ELm15EEE Line | Count | Source | 103 | 102 | ColumnString::Offsets& res_offset) const { | 104 | 102 | if constexpr (std::is_same_v<Impl, MD5Sum>) { | 105 | 102 | if (argument_columns.size() == 1) { | 106 | 54 | const auto* col = assert_cast<const ColumnType*>(argument_columns[0].get()); | 107 | 54 | vector_execute_single_md5(col, input_rows_count, is_const[0], res_data, res_offset); | 108 | 54 | return; | 109 | 54 | } | 110 | 102 | } | 111 | | | 112 | 48 | using ObjectData = typename Impl::ObjectData; | 113 | 166 | for (size_t i = 0; i < input_rows_count; ++i) { | 114 | 64 | ObjectData digest; | 115 | 243 | for (size_t j = 0; j < argument_columns.size(); ++j) { | 116 | 179 | const auto* col = assert_cast<const ColumnType*>(argument_columns[j].get()); | 117 | 179 | StringRef data_ref = col->get_data_at(is_const[j] ? 0 : i); | 118 | 179 | if (data_ref.size < 1) { | 119 | 57 | continue; | 120 | 57 | } | 121 | 122 | digest.update(data_ref.data, data_ref.size); | 122 | 122 | } | 123 | 64 | digest.digest(); | 124 | 64 | StringOP::push_value_string(std::string_view(digest.hex().c_str(), digest.hex().size()), | 125 | 64 | i, res_data, res_offset); | 126 | 64 | } | 127 | 102 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE14vector_executeINS_15ColumnVarbinaryEEEvRNS_5BlockEmRKSt6vectorINS_3COWINS_7IColumnEE13immutable_ptrIS9_EESaISC_EERKS7_IhSaIhEERNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNSL_IjLm4096ESO_Lm16ELm15EEE Line | Count | Source | 103 | 91 | ColumnString::Offsets& res_offset) const { | 104 | 91 | if constexpr (std::is_same_v<Impl, MD5Sum>) { | 105 | 91 | if (argument_columns.size() == 1) { | 106 | 36 | const auto* col = assert_cast<const ColumnType*>(argument_columns[0].get()); | 107 | 36 | vector_execute_single_md5(col, input_rows_count, is_const[0], res_data, res_offset); | 108 | 36 | return; | 109 | 36 | } | 110 | 91 | } | 111 | | | 112 | 55 | using ObjectData = typename Impl::ObjectData; | 113 | 163 | for (size_t i = 0; i < input_rows_count; ++i) { | 114 | 72 | ObjectData digest; | 115 | 267 | for (size_t j = 0; j < argument_columns.size(); ++j) { | 116 | 195 | const auto* col = assert_cast<const ColumnType*>(argument_columns[j].get()); | 117 | 195 | StringRef data_ref = col->get_data_at(is_const[j] ? 0 : i); | 118 | 195 | if (data_ref.size < 1) { | 119 | 61 | continue; | 120 | 61 | } | 121 | 134 | digest.update(data_ref.data, data_ref.size); | 122 | 134 | } | 123 | 72 | digest.digest(); | 124 | 72 | StringOP::push_value_string(std::string_view(digest.hex().c_str(), digest.hex().size()), | 125 | 72 | i, res_data, res_offset); | 126 | 72 | } | 127 | 91 | } |
|
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 | 90 | ColumnString::Offsets& res_offset) const { |
133 | 90 | ColumnString::check_chars_length(input_rows_count * MD5_HEX_LENGTH, input_rows_count); |
134 | 90 | res_data.resize(input_rows_count * MD5_HEX_LENGTH); |
135 | 272 | for (size_t i = 0; i < input_rows_count; ++i) { |
136 | 182 | res_offset[i] = (i + 1) * MD5_HEX_LENGTH; |
137 | 182 | } |
138 | 90 | if (input_rows_count == 0) { |
139 | 0 | return; |
140 | 0 | } |
141 | | |
142 | 90 | 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 | 90 | std::vector<const unsigned char*> inputs(input_rows_count); |
155 | 90 | std::vector<size_t> lengths(input_rows_count); |
156 | 272 | for (size_t i = 0; i < input_rows_count; ++i) { |
157 | 182 | StringRef data_ref = col->get_data_at(i); |
158 | 182 | inputs[i] = reinterpret_cast<const unsigned char*>(data_ref.data); |
159 | 182 | lengths[i] = data_ref.size; |
160 | 182 | } |
161 | 90 | md5_hex_batch(inputs.data(), lengths.data(), reinterpret_cast<char*>(res_data.data()), |
162 | 90 | input_rows_count); |
163 | 90 | } _ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE25vector_execute_single_md5INS_9ColumnStrIjEEEEvPKT_mbRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS9_IjLm4096ESC_Lm16ELm15EEE Line | Count | Source | 132 | 54 | ColumnString::Offsets& res_offset) const { | 133 | 54 | ColumnString::check_chars_length(input_rows_count * MD5_HEX_LENGTH, input_rows_count); | 134 | 54 | res_data.resize(input_rows_count * MD5_HEX_LENGTH); | 135 | 163 | for (size_t i = 0; i < input_rows_count; ++i) { | 136 | 109 | res_offset[i] = (i + 1) * MD5_HEX_LENGTH; | 137 | 109 | } | 138 | 54 | if (input_rows_count == 0) { | 139 | 0 | return; | 140 | 0 | } | 141 | | | 142 | 54 | 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 | 54 | std::vector<const unsigned char*> inputs(input_rows_count); | 155 | 54 | std::vector<size_t> lengths(input_rows_count); | 156 | 163 | for (size_t i = 0; i < input_rows_count; ++i) { | 157 | 109 | StringRef data_ref = col->get_data_at(i); | 158 | 109 | inputs[i] = reinterpret_cast<const unsigned char*>(data_ref.data); | 159 | 109 | lengths[i] = data_ref.size; | 160 | 109 | } | 161 | 54 | md5_hex_batch(inputs.data(), lengths.data(), reinterpret_cast<char*>(res_data.data()), | 162 | 54 | input_rows_count); | 163 | 54 | } |
_ZNK5doris25FunctionStringDigestMultiINS_6MD5SumEE25vector_execute_single_md5INS_15ColumnVarbinaryEEEvPKT_mbRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Line | Count | Source | 132 | 36 | ColumnString::Offsets& res_offset) const { | 133 | 36 | ColumnString::check_chars_length(input_rows_count * MD5_HEX_LENGTH, input_rows_count); | 134 | 36 | res_data.resize(input_rows_count * MD5_HEX_LENGTH); | 135 | 109 | for (size_t i = 0; i < input_rows_count; ++i) { | 136 | 73 | res_offset[i] = (i + 1) * MD5_HEX_LENGTH; | 137 | 73 | } | 138 | 36 | if (input_rows_count == 0) { | 139 | 0 | return; | 140 | 0 | } | 141 | | | 142 | 36 | 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 | 36 | std::vector<const unsigned char*> inputs(input_rows_count); | 155 | 36 | std::vector<size_t> lengths(input_rows_count); | 156 | 109 | for (size_t i = 0; i < input_rows_count; ++i) { | 157 | 73 | StringRef data_ref = col->get_data_at(i); | 158 | 73 | inputs[i] = reinterpret_cast<const unsigned char*>(data_ref.data); | 159 | 73 | lengths[i] = data_ref.size; | 160 | 73 | } | 161 | 36 | md5_hex_batch(inputs.data(), lengths.data(), reinterpret_cast<char*>(res_data.data()), | 162 | 36 | input_rows_count); | 163 | 36 | } |
|
164 | | }; |
165 | | |
166 | | class FunctionStringDigestSHA1 : public IFunction { |
167 | | public: |
168 | | static constexpr auto name = "sha1"; |
169 | 24 | 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 | 17 | bool is_variadic() const override { return true; } |
173 | | |
174 | 16 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
175 | 16 | return std::make_shared<DataTypeString>(); |
176 | 16 | } |
177 | | |
178 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
179 | 14 | uint32_t result, size_t input_rows_count) const override { |
180 | 14 | DCHECK_EQ(arguments.size(), 1); |
181 | 14 | ColumnPtr data_col = block.get_by_position(arguments[0]).column; |
182 | | |
183 | 14 | auto res_col = ColumnString::create(); |
184 | 14 | auto& res_data = res_col->get_chars(); |
185 | 14 | auto& res_offset = res_col->get_offsets(); |
186 | 14 | res_offset.resize(input_rows_count); |
187 | 14 | if (const auto* str_col = check_and_get_column<ColumnString>(data_col.get())) { |
188 | 7 | vector_execute(str_col, input_rows_count, res_data, res_offset); |
189 | 7 | } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(data_col.get())) { |
190 | 7 | vector_execute(vb_col, input_rows_count, res_data, res_offset); |
191 | 7 | } else { |
192 | 0 | return Status::RuntimeError("Illegal column {} of argument of function {}", |
193 | 0 | data_col->get_name(), get_name()); |
194 | 0 | } |
195 | | |
196 | 14 | block.replace_by_position(result, std::move(res_col)); |
197 | 14 | return Status::OK(); |
198 | 14 | } |
199 | | |
200 | | private: |
201 | | template <typename ColumnType> |
202 | | void vector_execute(const ColumnType* col, size_t input_rows_count, |
203 | 14 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { |
204 | 14 | SHA1Digest digest; |
205 | 52 | for (size_t i = 0; i < input_rows_count; ++i) { |
206 | 38 | StringRef data_ref = col->get_data_at(i); |
207 | 38 | digest.reset(data_ref.data, data_ref.size); |
208 | 38 | std::string_view ans = digest.digest(); |
209 | | |
210 | 38 | StringOP::push_value_string(ans, i, res_data, res_offset); |
211 | 38 | } |
212 | 14 | } _ZNK5doris24FunctionStringDigestSHA114vector_executeINS_9ColumnStrIjEEEEvPKT_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS7_IjLm4096ESA_Lm16ELm15EEE Line | Count | Source | 203 | 7 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { | 204 | 7 | SHA1Digest digest; | 205 | 26 | for (size_t i = 0; i < input_rows_count; ++i) { | 206 | 19 | StringRef data_ref = col->get_data_at(i); | 207 | 19 | digest.reset(data_ref.data, data_ref.size); | 208 | 19 | std::string_view ans = digest.digest(); | 209 | | | 210 | 19 | StringOP::push_value_string(ans, i, res_data, res_offset); | 211 | 19 | } | 212 | 7 | } |
_ZNK5doris24FunctionStringDigestSHA114vector_executeINS_15ColumnVarbinaryEEEvPKT_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS6_IjLm4096ES9_Lm16ELm15EEE Line | Count | Source | 203 | 7 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { | 204 | 7 | SHA1Digest digest; | 205 | 26 | for (size_t i = 0; i < input_rows_count; ++i) { | 206 | 19 | StringRef data_ref = col->get_data_at(i); | 207 | 19 | digest.reset(data_ref.data, data_ref.size); | 208 | 19 | std::string_view ans = digest.digest(); | 209 | | | 210 | 19 | StringOP::push_value_string(ans, i, res_data, res_offset); | 211 | 19 | } | 212 | 7 | } |
|
213 | | }; |
214 | | |
215 | | class FunctionStringDigestSHA2 : public IFunction { |
216 | | public: |
217 | | static constexpr auto name = "sha2"; |
218 | 24 | 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 | 17 | bool is_variadic() const override { return true; } |
222 | | |
223 | 16 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
224 | 16 | return std::make_shared<DataTypeString>(); |
225 | 16 | } |
226 | | |
227 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
228 | 16 | uint32_t result, size_t input_rows_count) const override { |
229 | 16 | DCHECK(!is_column_const(*block.get_by_position(arguments[0]).column)); |
230 | | |
231 | 16 | ColumnPtr data_col = block.get_by_position(arguments[0]).column; |
232 | | |
233 | 16 | [[maybe_unused]] const auto& [right_column, right_const] = |
234 | 16 | unpack_if_const(block.get_by_position(arguments[1]).column); |
235 | 16 | auto digest_length = assert_cast<const ColumnInt32*>(right_column.get())->get_data()[0]; |
236 | | |
237 | 16 | auto res_col = ColumnString::create(); |
238 | 16 | auto& res_data = res_col->get_chars(); |
239 | 16 | auto& res_offset = res_col->get_offsets(); |
240 | 16 | res_offset.resize(input_rows_count); |
241 | | |
242 | 16 | if (digest_length == 224) { |
243 | 4 | execute_base<SHA224Digest>(data_col, input_rows_count, res_data, res_offset); |
244 | 12 | } else if (digest_length == 256) { |
245 | 4 | execute_base<SHA256Digest>(data_col, input_rows_count, res_data, res_offset); |
246 | 8 | } else if (digest_length == 384) { |
247 | 4 | execute_base<SHA384Digest>(data_col, input_rows_count, res_data, res_offset); |
248 | 4 | } else if (digest_length == 512) { |
249 | 4 | execute_base<SHA512Digest>(data_col, input_rows_count, res_data, res_offset); |
250 | 4 | } 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 | 16 | block.replace_by_position(result, std::move(res_col)); |
256 | 16 | return Status::OK(); |
257 | 16 | } |
258 | | |
259 | | private: |
260 | | template <typename T> |
261 | | void execute_base(ColumnPtr data_col, int input_rows_count, ColumnString::Chars& res_data, |
262 | 16 | ColumnString::Offsets& res_offset) const { |
263 | 16 | if (const auto* str_col = check_and_get_column<ColumnString>(data_col.get())) { |
264 | 16 | vector_execute<T>(str_col, input_rows_count, res_data, res_offset); |
265 | 16 | } 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 | 16 | } _ZNK5doris24FunctionStringDigestSHA212execute_baseINS_12SHA224DigestEEEvNS_3COWINS_7IColumnEE13immutable_ptrIS4_EEiRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Line | Count | Source | 262 | 4 | ColumnString::Offsets& res_offset) const { | 263 | 4 | if (const auto* str_col = check_and_get_column<ColumnString>(data_col.get())) { | 264 | 4 | vector_execute<T>(str_col, input_rows_count, res_data, res_offset); | 265 | 4 | } 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 | 4 | } |
_ZNK5doris24FunctionStringDigestSHA212execute_baseINS_12SHA256DigestEEEvNS_3COWINS_7IColumnEE13immutable_ptrIS4_EEiRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Line | Count | Source | 262 | 4 | ColumnString::Offsets& res_offset) const { | 263 | 4 | if (const auto* str_col = check_and_get_column<ColumnString>(data_col.get())) { | 264 | 4 | vector_execute<T>(str_col, input_rows_count, res_data, res_offset); | 265 | 4 | } 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 | 4 | } |
_ZNK5doris24FunctionStringDigestSHA212execute_baseINS_12SHA384DigestEEEvNS_3COWINS_7IColumnEE13immutable_ptrIS4_EEiRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Line | Count | Source | 262 | 4 | ColumnString::Offsets& res_offset) const { | 263 | 4 | if (const auto* str_col = check_and_get_column<ColumnString>(data_col.get())) { | 264 | 4 | vector_execute<T>(str_col, input_rows_count, res_data, res_offset); | 265 | 4 | } 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 | 4 | } |
_ZNK5doris24FunctionStringDigestSHA212execute_baseINS_12SHA512DigestEEEvNS_3COWINS_7IColumnEE13immutable_ptrIS4_EEiRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Line | Count | Source | 262 | 4 | ColumnString::Offsets& res_offset) const { | 263 | 4 | if (const auto* str_col = check_and_get_column<ColumnString>(data_col.get())) { | 264 | 4 | vector_execute<T>(str_col, input_rows_count, res_data, res_offset); | 265 | 4 | } 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 | 4 | } |
|
273 | | |
274 | | template <typename DigestType, typename ColumnType> |
275 | | void vector_execute(const ColumnType* col, size_t input_rows_count, |
276 | 16 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { |
277 | 16 | DigestType digest; |
278 | 96 | for (size_t i = 0; i < input_rows_count; ++i) { |
279 | 80 | StringRef data_ref = col->get_data_at(i); |
280 | 80 | digest.reset(data_ref.data, data_ref.size); |
281 | 80 | std::string_view ans = digest.digest(); |
282 | | |
283 | 80 | StringOP::push_value_string(ans, i, res_data, res_offset); |
284 | 80 | } |
285 | 16 | } _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA224DigestENS_9ColumnStrIjEEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Line | Count | Source | 276 | 4 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { | 277 | 4 | DigestType digest; | 278 | 24 | for (size_t i = 0; i < input_rows_count; ++i) { | 279 | 20 | StringRef data_ref = col->get_data_at(i); | 280 | 20 | digest.reset(data_ref.data, data_ref.size); | 281 | 20 | std::string_view ans = digest.digest(); | 282 | | | 283 | 20 | StringOP::push_value_string(ans, i, res_data, res_offset); | 284 | 20 | } | 285 | 4 | } |
Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA224DigestENS_15ColumnVarbinaryEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS7_IjLm4096ESA_Lm16ELm15EEE _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA256DigestENS_9ColumnStrIjEEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Line | Count | Source | 276 | 4 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { | 277 | 4 | DigestType digest; | 278 | 24 | for (size_t i = 0; i < input_rows_count; ++i) { | 279 | 20 | StringRef data_ref = col->get_data_at(i); | 280 | 20 | digest.reset(data_ref.data, data_ref.size); | 281 | 20 | std::string_view ans = digest.digest(); | 282 | | | 283 | 20 | StringOP::push_value_string(ans, i, res_data, res_offset); | 284 | 20 | } | 285 | 4 | } |
Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA256DigestENS_15ColumnVarbinaryEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS7_IjLm4096ESA_Lm16ELm15EEE _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA384DigestENS_9ColumnStrIjEEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Line | Count | Source | 276 | 4 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { | 277 | 4 | DigestType digest; | 278 | 24 | for (size_t i = 0; i < input_rows_count; ++i) { | 279 | 20 | StringRef data_ref = col->get_data_at(i); | 280 | 20 | digest.reset(data_ref.data, data_ref.size); | 281 | 20 | std::string_view ans = digest.digest(); | 282 | | | 283 | 20 | StringOP::push_value_string(ans, i, res_data, res_offset); | 284 | 20 | } | 285 | 4 | } |
Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA384DigestENS_15ColumnVarbinaryEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS7_IjLm4096ESA_Lm16ELm15EEE _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA512DigestENS_9ColumnStrIjEEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS8_IjLm4096ESB_Lm16ELm15EEE Line | Count | Source | 276 | 4 | ColumnString::Chars& res_data, ColumnString::Offsets& res_offset) const { | 277 | 4 | DigestType digest; | 278 | 24 | for (size_t i = 0; i < input_rows_count; ++i) { | 279 | 20 | StringRef data_ref = col->get_data_at(i); | 280 | 20 | digest.reset(data_ref.data, data_ref.size); | 281 | 20 | std::string_view ans = digest.digest(); | 282 | | | 283 | 20 | StringOP::push_value_string(ans, i, res_data, res_offset); | 284 | 20 | } | 285 | 4 | } |
Unexecuted instantiation: _ZNK5doris24FunctionStringDigestSHA214vector_executeINS_12SHA512DigestENS_15ColumnVarbinaryEEEvPKT0_mRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNS7_IjLm4096ESA_Lm16ELm15EEE |
286 | | }; |
287 | | |
288 | 7 | void register_function_string_digest(SimpleFunctionFactory& factory) { |
289 | 7 | factory.register_function<FunctionStringDigestMulti<SM3Sum>>(); |
290 | 7 | factory.register_function<FunctionStringDigestMulti<MD5Sum>>(); |
291 | 7 | factory.register_function<FunctionStringDigestSHA1>(); |
292 | 7 | factory.register_function<FunctionStringDigestSHA2>(); |
293 | | |
294 | 7 | factory.register_alias(FunctionStringDigestMulti<MD5Sum>::name, "md5"); |
295 | 7 | factory.register_alias(FunctionStringDigestMulti<SM3Sum>::name, "sm3"); |
296 | 7 | factory.register_alias(FunctionStringDigestSHA1::name, "sha"); |
297 | 7 | } |
298 | | |
299 | | #include "common/compile_check_avoid_end.h" |
300 | | } // namespace doris |