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 <algorithm> |
19 | | #include <array> |
20 | | #include <limits> |
21 | | #include <string_view> |
22 | | #include <unordered_map> |
23 | | #include <vector> |
24 | | |
25 | | #include "common/status.h" |
26 | | #include "core/custom_allocator.h" |
27 | | #include "core/data_type/data_type_number.h" |
28 | | #include "core/pod_array.h" |
29 | | #include "core/string_ref.h" |
30 | | #include "exprs/function/function_totype.h" |
31 | | #include "exprs/function/simple_function_factory.h" |
32 | | #include "util/simd/vstring_function.h" |
33 | | |
34 | | namespace doris { |
35 | | |
36 | | struct NameLevenshtein { |
37 | | static constexpr auto name = "levenshtein"; |
38 | | }; |
39 | | |
40 | | struct NameDamerauLevenshteinDistance { |
41 | | static constexpr auto name = "damerau_levenshtein_distance"; |
42 | | }; |
43 | | |
44 | | // 64MB limit for the distance matrix. |
45 | | static constexpr size_t MAX_DAMERAU_LEVENSHTEIN_MATRIX_CELLS = 16 * 1024 * 1024; |
46 | | |
47 | | using Utf8Offsets = DorisVector<size_t>; |
48 | | |
49 | | static StringRef string_ref_at(const ColumnString::Chars& data, |
50 | 0 | const ColumnString::Offsets& offsets, size_t i) { |
51 | 0 | DCHECK_LT(i, offsets.size()); |
52 | 0 | const auto previous_offset = i == 0 ? 0 : offsets[i - 1]; |
53 | 0 | return StringRef(data.data() + previous_offset, offsets[i] - previous_offset) |
54 | 0 | .trim_tail_padding_zero(); |
55 | 0 | } |
56 | | |
57 | 0 | static void get_utf8_char_offsets(const StringRef& ref, Utf8Offsets& offsets) { |
58 | 0 | offsets.clear(); |
59 | 0 | offsets.reserve(ref.size); |
60 | 0 | for (size_t i = 0, char_size = 0; i < ref.size; i += char_size) { |
61 | 0 | char_size = UTF8_BYTE_LENGTH[static_cast<unsigned char>(ref.data[i])]; |
62 | 0 | offsets.push_back(i); |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | struct LevenshteinDistance { |
67 | 0 | static Status ascii(const StringRef& left, const StringRef& right, Int32& result) { |
68 | 0 | const StringRef* left_ref = &left; |
69 | 0 | const StringRef* right_ref = &right; |
70 | 0 | size_t m = left.size; |
71 | 0 | size_t n = right.size; |
72 | |
|
73 | 0 | if (n > m) { |
74 | 0 | std::swap(left_ref, right_ref); |
75 | 0 | std::swap(m, n); |
76 | 0 | } |
77 | |
|
78 | 0 | std::vector<Int32> prev(n + 1); |
79 | 0 | std::vector<Int32> curr(n + 1); |
80 | 0 | for (size_t j = 0; j <= n; ++j) { |
81 | 0 | prev[j] = static_cast<Int32>(j); |
82 | 0 | } |
83 | |
|
84 | 0 | for (size_t i = 1; i <= m; ++i) { |
85 | 0 | curr[0] = static_cast<Int32>(i); |
86 | 0 | const char left_char = left_ref->data[i - 1]; |
87 | |
|
88 | 0 | for (size_t j = 1; j <= n; ++j) { |
89 | 0 | const Int32 cost = left_char == right_ref->data[j - 1] ? 0 : 1; |
90 | 0 | const Int32 insert_cost = curr[j - 1] + 1; |
91 | 0 | const Int32 delete_cost = prev[j] + 1; |
92 | 0 | const Int32 replace_cost = prev[j - 1] + cost; |
93 | 0 | curr[j] = std::min(std::min(insert_cost, delete_cost), replace_cost); |
94 | 0 | } |
95 | 0 | std::swap(prev, curr); |
96 | 0 | } |
97 | |
|
98 | 0 | result = prev[n]; |
99 | 0 | return Status::OK(); |
100 | 0 | } |
101 | | |
102 | | static Status utf8(const StringRef& left, const Utf8Offsets& left_offsets, |
103 | 0 | const StringRef& right, const Utf8Offsets& right_offsets, Int32& result) { |
104 | 0 | const StringRef* left_ref = &left; |
105 | 0 | const StringRef* right_ref = &right; |
106 | 0 | const Utf8Offsets* left_offsets_ref = &left_offsets; |
107 | 0 | const Utf8Offsets* right_offsets_ref = &right_offsets; |
108 | 0 | if (right_offsets_ref->size() > left_offsets_ref->size()) { |
109 | 0 | std::swap(left_offsets_ref, right_offsets_ref); |
110 | 0 | std::swap(left_ref, right_ref); |
111 | 0 | } |
112 | |
|
113 | 0 | const size_t m = left_offsets_ref->size(); |
114 | 0 | const size_t n = right_offsets_ref->size(); |
115 | |
|
116 | 0 | std::vector<Int32> prev(n + 1); |
117 | 0 | std::vector<Int32> curr(n + 1); |
118 | 0 | for (size_t j = 0; j <= n; ++j) { |
119 | 0 | prev[j] = static_cast<Int32>(j); |
120 | 0 | } |
121 | |
|
122 | 0 | for (size_t i = 1; i <= m; ++i) { |
123 | 0 | curr[0] = static_cast<Int32>(i); |
124 | 0 | const size_t left_off = (*left_offsets_ref)[i - 1]; |
125 | 0 | const size_t left_next = i < m ? (*left_offsets_ref)[i] : left_ref->size; |
126 | |
|
127 | 0 | for (size_t j = 1; j <= n; ++j) { |
128 | 0 | const size_t right_off = (*right_offsets_ref)[j - 1]; |
129 | 0 | const size_t right_next = j < n ? (*right_offsets_ref)[j] : right_ref->size; |
130 | |
|
131 | 0 | const Int32 cost = |
132 | 0 | simd::VStringFunctions::utf8_char_equal(*left_ref, left_off, left_next, |
133 | 0 | *right_ref, right_off, right_next) |
134 | 0 | ? 0 |
135 | 0 | : 1; |
136 | |
|
137 | 0 | const Int32 insert_cost = curr[j - 1] + 1; |
138 | 0 | const Int32 delete_cost = prev[j] + 1; |
139 | 0 | const Int32 replace_cost = prev[j - 1] + cost; |
140 | 0 | curr[j] = std::min(std::min(insert_cost, delete_cost), replace_cost); |
141 | 0 | } |
142 | 0 | std::swap(prev, curr); |
143 | 0 | } |
144 | |
|
145 | 0 | result = prev[n]; |
146 | 0 | return Status::OK(); |
147 | 0 | } |
148 | | }; |
149 | | |
150 | | struct DamerauLevenshteinDistance { |
151 | | using SymbolId = Int32; |
152 | | using SymbolVector = DorisVector<SymbolId>; |
153 | | using SymbolMap = |
154 | | std::unordered_map<std::string_view, SymbolId, std::hash<std::string_view>, |
155 | | std::equal_to<std::string_view>, |
156 | | CustomStdAllocator<std::pair<const std::string_view, SymbolId>>>; |
157 | | |
158 | | // Do not use absl::strings_internal::CappedDamerauLevenshteinDistance here: |
159 | | // 1. It is capped: distances greater than cutoff return cutoff + 1, not exact distance. |
160 | | // 2. It returns uint8_t and is intended for short strings. |
161 | | // 3. It implements the restricted/OSA variant, not full Damerau-Levenshtein. |
162 | | // 4. It works on bytes, cannot handle UTF-8 characters. |
163 | 0 | static Status ascii(const StringRef& left, const StringRef& right, Int32& result) { |
164 | 0 | const size_t m = left.size; |
165 | 0 | const size_t n = right.size; |
166 | 0 | if (m == 0) { |
167 | 0 | result = static_cast<Int32>(n); |
168 | 0 | return Status::OK(); |
169 | 0 | } |
170 | 0 | if (n == 0) { |
171 | 0 | result = static_cast<Int32>(m); |
172 | 0 | return Status::OK(); |
173 | 0 | } |
174 | | |
175 | 0 | size_t matrix_cells = 0; |
176 | 0 | RETURN_IF_ERROR(get_matrix_cell_count(m, n, matrix_cells)); |
177 | | |
178 | 0 | const Int32 max_distance = static_cast<Int32>(m + n); |
179 | 0 | std::array<Int32, 256> last_row {}; |
180 | 0 | PaddedPODArray<Int32> dist; |
181 | 0 | dist.resize_fill(matrix_cells, 0); |
182 | 0 | auto at = [&](size_t i, size_t j) -> Int32& { return dist[i * (n + 2) + j]; }; |
183 | |
|
184 | 0 | at(0, 0) = max_distance; |
185 | 0 | for (size_t i = 0; i <= m; ++i) { |
186 | 0 | at(i + 1, 0) = max_distance; |
187 | 0 | at(i + 1, 1) = static_cast<Int32>(i); |
188 | 0 | } |
189 | 0 | for (size_t j = 0; j <= n; ++j) { |
190 | 0 | at(0, j + 1) = max_distance; |
191 | 0 | at(1, j + 1) = static_cast<Int32>(j); |
192 | 0 | } |
193 | |
|
194 | 0 | for (size_t i = 1; i <= m; ++i) { |
195 | 0 | Int32 last_match_col = 0; |
196 | 0 | const auto left_symbol = static_cast<unsigned char>(left.data[i - 1]); |
197 | 0 | for (size_t j = 1; j <= n; ++j) { |
198 | 0 | const auto right_symbol = static_cast<unsigned char>(right.data[j - 1]); |
199 | 0 | const Int32 last_match_row = last_row[right_symbol]; |
200 | 0 | const Int32 transposition_col = last_match_col; |
201 | 0 | Int32 cost = 1; |
202 | 0 | if (left_symbol == right_symbol) { |
203 | 0 | cost = 0; |
204 | 0 | last_match_col = static_cast<Int32>(j); |
205 | 0 | } |
206 | |
|
207 | 0 | const Int32 replace_cost = at(i, j) + cost; |
208 | 0 | const Int32 insert_cost = at(i + 1, j) + 1; |
209 | 0 | const Int32 delete_cost = at(i, j + 1) + 1; |
210 | 0 | const Int32 transpose_cost = at(last_match_row, transposition_col) + |
211 | 0 | static_cast<Int32>(i - last_match_row - 1) + 1 + |
212 | 0 | static_cast<Int32>(j - transposition_col - 1); |
213 | 0 | at(i + 1, j + 1) = std::min(std::min(replace_cost, insert_cost), |
214 | 0 | std::min(delete_cost, transpose_cost)); |
215 | 0 | } |
216 | 0 | last_row[left_symbol] = static_cast<Int32>(i); |
217 | 0 | } |
218 | |
|
219 | 0 | result = at(m + 1, n + 1); |
220 | 0 | return Status::OK(); |
221 | 0 | } |
222 | | |
223 | | /* |
224 | | * Keep the UTF-8 symbol mapping path. The benchmark below shows that mapping each UTF-8 |
225 | | * character to an integer symbol first is faster than comparing UTF-8 slices inside the |
226 | | * dp loop, because the hot loop only needs integer equality and indexed last-row lookup. |
227 | | * |
228 | | * -------------------------------------------------------------------------- |
229 | | * Benchmark Time CPU |
230 | | * -------------------------------------------------------------------------- |
231 | | * BM_DamerauLevenshtein_UTF8/SymbolMapped_16 1.85 us 1.82 us |
232 | | * BM_DamerauLevenshtein_UTF8/DirectCompare_16 6.48 us 6.43 us |
233 | | * BM_DamerauLevenshtein_UTF8/SymbolMapped_64 12.6 us 12.6 us |
234 | | * BM_DamerauLevenshtein_UTF8/DirectCompare_64 119 us 118 us |
235 | | * BM_DamerauLevenshtein_UTF8/SymbolMapped_128 43.4 us 43.1 us |
236 | | * BM_DamerauLevenshtein_UTF8/DirectCompare_128 478 us 475 us |
237 | | * BM_DamerauLevenshtein_UTF8/SymbolMapped_256 160 us 159 us |
238 | | * BM_DamerauLevenshtein_UTF8/DirectCompare_256 1892 us 1882 us |
239 | | */ |
240 | | static Status utf8(const StringRef& left, const Utf8Offsets& left_offsets, |
241 | 0 | const StringRef& right, const Utf8Offsets& right_offsets, Int32& result) { |
242 | 0 | SymbolVector left_symbols; |
243 | 0 | SymbolVector right_symbols; |
244 | 0 | SymbolMap symbol_ids; |
245 | 0 | symbol_ids.reserve(left_offsets.size() + right_offsets.size()); |
246 | 0 | SymbolId next_symbol = 0; |
247 | 0 | append_utf8_symbols(left, left_offsets, left_symbols, symbol_ids, next_symbol); |
248 | 0 | append_utf8_symbols(right, right_offsets, right_symbols, symbol_ids, next_symbol); |
249 | 0 | return by_symbols(left_symbols, right_symbols, next_symbol, result); |
250 | 0 | } |
251 | | |
252 | | private: |
253 | | static Status by_symbols(const SymbolVector& left, const SymbolVector& right, |
254 | 0 | size_t alphabet_size, Int32& result) { |
255 | 0 | const size_t m = left.size(); |
256 | 0 | const size_t n = right.size(); |
257 | 0 | if (m == 0) { |
258 | 0 | result = static_cast<Int32>(n); |
259 | 0 | return Status::OK(); |
260 | 0 | } |
261 | 0 | if (n == 0) { |
262 | 0 | result = static_cast<Int32>(m); |
263 | 0 | return Status::OK(); |
264 | 0 | } |
265 | | |
266 | 0 | size_t matrix_cells = 0; |
267 | 0 | RETURN_IF_ERROR(get_matrix_cell_count(m, n, matrix_cells)); |
268 | | |
269 | 0 | const Int32 max_distance = static_cast<Int32>(m + n); |
270 | 0 | DorisVector<Int32> last_row(alphabet_size, 0); |
271 | 0 | PaddedPODArray<Int32> dist; |
272 | 0 | dist.resize_fill(matrix_cells, 0); |
273 | 0 | auto at = [&](size_t i, size_t j) -> Int32& { return dist[i * (n + 2) + j]; }; |
274 | |
|
275 | 0 | at(0, 0) = max_distance; |
276 | 0 | for (size_t i = 0; i <= m; ++i) { |
277 | 0 | at(i + 1, 0) = max_distance; |
278 | 0 | at(i + 1, 1) = static_cast<Int32>(i); |
279 | 0 | } |
280 | 0 | for (size_t j = 0; j <= n; ++j) { |
281 | 0 | at(0, j + 1) = max_distance; |
282 | 0 | at(1, j + 1) = static_cast<Int32>(j); |
283 | 0 | } |
284 | |
|
285 | 0 | for (size_t i = 1; i <= m; ++i) { |
286 | 0 | Int32 last_match_col = 0; |
287 | 0 | for (size_t j = 1; j <= n; ++j) { |
288 | 0 | const Int32 last_match_row = last_row[right[j - 1]]; |
289 | 0 | const Int32 transposition_col = last_match_col; |
290 | 0 | Int32 cost = 1; |
291 | 0 | if (left[i - 1] == right[j - 1]) { |
292 | 0 | cost = 0; |
293 | 0 | last_match_col = static_cast<Int32>(j); |
294 | 0 | } |
295 | |
|
296 | 0 | const Int32 replace_cost = at(i, j) + cost; |
297 | 0 | const Int32 insert_cost = at(i + 1, j) + 1; |
298 | 0 | const Int32 delete_cost = at(i, j + 1) + 1; |
299 | 0 | const Int32 transpose_cost = at(last_match_row, transposition_col) + |
300 | 0 | static_cast<Int32>(i - last_match_row - 1) + 1 + |
301 | 0 | static_cast<Int32>(j - transposition_col - 1); |
302 | 0 | at(i + 1, j + 1) = std::min(std::min(replace_cost, insert_cost), |
303 | 0 | std::min(delete_cost, transpose_cost)); |
304 | 0 | } |
305 | 0 | last_row[left[i - 1]] = static_cast<Int32>(i); |
306 | 0 | } |
307 | |
|
308 | 0 | result = at(m + 1, n + 1); |
309 | 0 | return Status::OK(); |
310 | 0 | } |
311 | | |
312 | 0 | static Status get_matrix_cell_count(size_t m, size_t n, size_t& matrix_cells) { |
313 | 0 | const auto max_size = std::numeric_limits<size_t>::max(); |
314 | 0 | if (m > max_size - 2 || n > max_size - 2) { |
315 | 0 | return Status::InvalidArgument( |
316 | 0 | "damerau_levenshtein_distance input is too large to allocate distance matrix"); |
317 | 0 | } |
318 | | |
319 | 0 | const size_t rows = m + 2; |
320 | 0 | const size_t cols = n + 2; |
321 | 0 | if (rows > max_size / cols) { |
322 | 0 | return Status::InvalidArgument( |
323 | 0 | "damerau_levenshtein_distance distance matrix size overflows"); |
324 | 0 | } |
325 | | |
326 | 0 | matrix_cells = rows * cols; |
327 | 0 | if (matrix_cells > MAX_DAMERAU_LEVENSHTEIN_MATRIX_CELLS) { |
328 | 0 | return Status::InvalidArgument( |
329 | 0 | "damerau_levenshtein_distance distance matrix is too large: {} cells exceeds " |
330 | 0 | "limit {}", |
331 | 0 | matrix_cells, MAX_DAMERAU_LEVENSHTEIN_MATRIX_CELLS); |
332 | 0 | } |
333 | 0 | return Status::OK(); |
334 | 0 | } |
335 | | |
336 | | static void append_utf8_symbols(const StringRef& str, const Utf8Offsets& offsets, |
337 | | SymbolVector& symbols, SymbolMap& symbol_ids, |
338 | 0 | SymbolId& next_symbol) { |
339 | 0 | symbols.reserve(symbols.size() + offsets.size()); |
340 | 0 | for (size_t i = 0; i < offsets.size(); ++i) { |
341 | 0 | const size_t offset = offsets[i]; |
342 | 0 | const size_t next_offset = i + 1 < offsets.size() ? offsets[i + 1] : str.size; |
343 | 0 | const std::string_view symbol(str.data + offset, next_offset - offset); |
344 | 0 | auto [it, inserted] = symbol_ids.emplace(symbol, next_symbol); |
345 | 0 | if (inserted) { |
346 | 0 | ++next_symbol; |
347 | 0 | } |
348 | 0 | symbols.push_back(it->second); |
349 | 0 | } |
350 | 0 | } |
351 | | }; |
352 | | |
353 | | template <typename Distance> |
354 | | struct StringDistanceImplBase { |
355 | | using ResultDataType = DataTypeInt32; |
356 | | using ResultPaddedPODArray = PaddedPODArray<Int32>; |
357 | | |
358 | | static Status vector_vector(const ColumnString::Chars& ldata, |
359 | | const ColumnString::Offsets& loffsets, |
360 | | const ColumnString::Chars& rdata, |
361 | 0 | const ColumnString::Offsets& roffsets, ResultPaddedPODArray& res) { |
362 | 0 | DCHECK_EQ(loffsets.size(), roffsets.size()); |
363 | |
|
364 | 0 | const size_t size = loffsets.size(); |
365 | 0 | res.resize(size); |
366 | 0 | Utf8Offsets left_offsets; |
367 | 0 | Utf8Offsets right_offsets; |
368 | 0 | for (size_t i = 0; i < size; ++i) { |
369 | 0 | RETURN_IF_ERROR(distance(string_ref_at(ldata, loffsets, i), |
370 | 0 | string_ref_at(rdata, roffsets, i), left_offsets, right_offsets, |
371 | 0 | res[i])); |
372 | 0 | } |
373 | 0 | return Status::OK(); |
374 | 0 | } Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_19LevenshteinDistanceEE13vector_vectorERKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERKNS3_IjLm4096ES6_Lm16ELm15EEES9_SC_RNS3_IiLm4096ES6_Lm16ELm15EEE Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_26DamerauLevenshteinDistanceEE13vector_vectorERKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERKNS3_IjLm4096ES6_Lm16ELm15EEES9_SC_RNS3_IiLm4096ES6_Lm16ELm15EEE |
375 | | |
376 | | static Status vector_scalar(const ColumnString::Chars& data, |
377 | | const ColumnString::Offsets& offsets, const StringRef& constant, |
378 | 0 | ResultPaddedPODArray& res) { |
379 | 0 | return vector_const(data, offsets, constant, res); |
380 | 0 | } Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_19LevenshteinDistanceEE13vector_scalarERKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERKNS3_IjLm4096ES6_Lm16ELm15EEERKNS_9StringRefERNS3_IiLm4096ES6_Lm16ELm15EEE Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_26DamerauLevenshteinDistanceEE13vector_scalarERKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERKNS3_IjLm4096ES6_Lm16ELm15EEERKNS_9StringRefERNS3_IiLm4096ES6_Lm16ELm15EEE |
381 | | |
382 | | static Status scalar_vector(const StringRef& constant, const ColumnString::Chars& data, |
383 | 0 | const ColumnString::Offsets& offsets, ResultPaddedPODArray& res) { |
384 | 0 | return vector_const(data, offsets, constant, res); |
385 | 0 | } Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_19LevenshteinDistanceEE13scalar_vectorERKNS_9StringRefERKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERKNS6_IjLm4096ES9_Lm16ELm15EEERNS6_IiLm4096ES9_Lm16ELm15EEE Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_26DamerauLevenshteinDistanceEE13scalar_vectorERKNS_9StringRefERKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERKNS6_IjLm4096ES9_Lm16ELm15EEERNS6_IiLm4096ES9_Lm16ELm15EEE |
386 | | |
387 | | private: |
388 | | static Status vector_const(const ColumnString::Chars& data, |
389 | | const ColumnString::Offsets& offsets, const StringRef& constant, |
390 | 0 | ResultPaddedPODArray& res) { |
391 | 0 | const size_t size = offsets.size(); |
392 | 0 | res.resize(size); |
393 | 0 | const auto constant_ref = constant.trim_tail_padding_zero(); |
394 | 0 | const bool constant_ascii = simd::VStringFunctions::is_ascii(constant_ref); |
395 | 0 | Utf8Offsets constant_offsets; |
396 | 0 | get_utf8_char_offsets(constant_ref, constant_offsets); |
397 | 0 | Utf8Offsets value_offsets; |
398 | 0 | for (size_t i = 0; i < size; ++i) { |
399 | 0 | RETURN_IF_ERROR(distance_with_const_offsets(string_ref_at(data, offsets, i), |
400 | 0 | value_offsets, constant_ref, |
401 | 0 | constant_offsets, constant_ascii, res[i])); |
402 | 0 | } |
403 | 0 | return Status::OK(); |
404 | 0 | } Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_19LevenshteinDistanceEE12vector_constERKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERKNS3_IjLm4096ES6_Lm16ELm15EEERKNS_9StringRefERNS3_IiLm4096ES6_Lm16ELm15EEE Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_26DamerauLevenshteinDistanceEE12vector_constERKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERKNS3_IjLm4096ES6_Lm16ELm15EEERKNS_9StringRefERNS3_IiLm4096ES6_Lm16ELm15EEE |
405 | | |
406 | | static Status distance(const StringRef& left, const StringRef& right, Utf8Offsets& left_offsets, |
407 | 0 | Utf8Offsets& right_offsets, Int32& result) { |
408 | 0 | const bool left_ascii = simd::VStringFunctions::is_ascii(left); |
409 | 0 | const bool right_ascii = simd::VStringFunctions::is_ascii(right); |
410 | 0 | if (left_ascii && right_ascii) { |
411 | 0 | return Distance::ascii(left, right, result); |
412 | 0 | } |
413 | | |
414 | 0 | if (left.size == 0) { |
415 | 0 | result = static_cast<Int32>( |
416 | 0 | simd::VStringFunctions::get_char_len(right.data, right.size)); |
417 | 0 | return Status::OK(); |
418 | 0 | } |
419 | 0 | if (right.size == 0) { |
420 | 0 | result = static_cast<Int32>(simd::VStringFunctions::get_char_len(left.data, left.size)); |
421 | 0 | return Status::OK(); |
422 | 0 | } |
423 | | |
424 | 0 | get_utf8_char_offsets(left, left_offsets); |
425 | 0 | get_utf8_char_offsets(right, right_offsets); |
426 | 0 | return Distance::utf8(left, left_offsets, right, right_offsets, result); |
427 | 0 | } Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_19LevenshteinDistanceEE8distanceERKNS_9StringRefES5_RSt6vectorImNS_18CustomStdAllocatorImNS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEESD_Ri Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_26DamerauLevenshteinDistanceEE8distanceERKNS_9StringRefES5_RSt6vectorImNS_18CustomStdAllocatorImNS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEESD_Ri |
428 | | |
429 | | static Status distance_with_const_offsets(const StringRef& value, Utf8Offsets& value_offsets, |
430 | | const StringRef& constant, |
431 | | const Utf8Offsets& constant_offsets, |
432 | 0 | bool constant_ascii, Int32& result) { |
433 | 0 | const bool value_ascii = simd::VStringFunctions::is_ascii(value); |
434 | 0 | if (value_ascii && constant_ascii) { |
435 | 0 | return Distance::ascii(value, constant, result); |
436 | 0 | } |
437 | | |
438 | 0 | if (value.size == 0) { |
439 | 0 | result = static_cast<Int32>(constant_offsets.size()); |
440 | 0 | return Status::OK(); |
441 | 0 | } |
442 | 0 | if (constant.size == 0) { |
443 | 0 | result = value_ascii ? static_cast<Int32>(value.size) |
444 | 0 | : static_cast<Int32>(simd::VStringFunctions::get_char_len( |
445 | 0 | value.data, value.size)); |
446 | 0 | return Status::OK(); |
447 | 0 | } |
448 | | |
449 | 0 | get_utf8_char_offsets(value, value_offsets); |
450 | 0 | return Distance::utf8(value, value_offsets, constant, constant_offsets, result); |
451 | 0 | } Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_19LevenshteinDistanceEE27distance_with_const_offsetsERKNS_9StringRefERSt6vectorImNS_18CustomStdAllocatorImNS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEES5_RKSC_bRi Unexecuted instantiation: _ZN5doris22StringDistanceImplBaseINS_26DamerauLevenshteinDistanceEE27distance_with_const_offsetsERKNS_9StringRefERSt6vectorImNS_18CustomStdAllocatorImNS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEES5_RKSC_bRi |
452 | | }; |
453 | | |
454 | | template <typename LeftDataType, typename RightDataType> |
455 | | struct LevenshteinImpl : public StringDistanceImplBase<LevenshteinDistance> {}; |
456 | | |
457 | | template <typename LeftDataType, typename RightDataType> |
458 | | struct DamerauLevenshteinDistanceImpl : public StringDistanceImplBase<DamerauLevenshteinDistance> { |
459 | | }; |
460 | | |
461 | | using FunctionLevenshtein = |
462 | | FunctionBinaryToType<DataTypeString, DataTypeString, LevenshteinImpl, NameLevenshtein>; |
463 | | using FunctionDamerauLevenshteinDistance = |
464 | | FunctionBinaryToType<DataTypeString, DataTypeString, DamerauLevenshteinDistanceImpl, |
465 | | NameDamerauLevenshteinDistance>; |
466 | | |
467 | 1 | void register_function_levenshtein(SimpleFunctionFactory& factory) { |
468 | 1 | factory.register_function<FunctionLevenshtein>(); |
469 | 1 | factory.register_alias(FunctionLevenshtein::name, "levenshtein_distance"); |
470 | 1 | factory.register_alias(FunctionLevenshtein::name, "edit_distance"); |
471 | 1 | factory.register_function<FunctionDamerauLevenshteinDistance>(); |
472 | 1 | } |
473 | | |
474 | | } // namespace doris |