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