Coverage Report

Created: 2026-08-06 13:34

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