-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathlevenshtein.rs
More file actions
339 lines (307 loc) · 12.1 KB
/
levenshtein.rs
File metadata and controls
339 lines (307 loc) · 12.1 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
// 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.
//! Levenshtein distance expression implementation.
//!
//! Computes the Levenshtein edit distance between two strings,
//! matching Apache Spark's `levenshtein(str1, str2)` semantics.
use arrow::array::{as_string_array, Array, ArrayRef, Int32Array};
use datafusion::common::{DataFusionError, Result};
use datafusion::physical_plan::ColumnarValue;
use std::sync::Arc;
/// Computes the Levenshtein edit distance between two UTF-8 strings.
///
/// This uses the standard dynamic programming algorithm with O(min(m,n)) space.
fn levenshtein_distance(s: &str, t: &str) -> i32 {
let s_chars: Vec<char> = s.chars().collect();
let t_chars: Vec<char> = t.chars().collect();
let m = s_chars.len();
let n = t_chars.len();
// Optimization: if one string is empty, distance is the length of the other
if m == 0 {
return n as i32;
}
if n == 0 {
return m as i32;
}
// Use the shorter string for the "column" to minimize space usage
let (s_chars, t_chars, m, n) = if m > n {
(t_chars, s_chars, n, m)
} else {
(s_chars, t_chars, m, n)
};
// Previous and current row of distances
let mut prev = vec![0i32; m + 1];
let mut curr = vec![0i32; m + 1];
// Initialize base case: distance from empty string
for (i, val) in prev.iter_mut().enumerate() {
*val = i as i32;
}
for j in 1..=n {
curr[0] = j as i32;
for i in 1..=m {
let cost = if s_chars[i - 1] == t_chars[j - 1] {
0
} else {
1
};
curr[i] = (prev[i] + 1) // deletion
.min(curr[i - 1] + 1) // insertion
.min(prev[i - 1] + cost); // substitution
}
std::mem::swap(&mut prev, &mut curr);
}
prev[m]
}
/// Spark-compatible levenshtein scalar function.
///
/// Accepts two or three arguments:
/// - `levenshtein(str1, str2)` → edit distance
/// - `levenshtein(str1, str2, threshold)` → edit distance if <= threshold, else -1
///
/// The threshold argument can be either a scalar or a column (array).
/// NULL inputs produce NULL outputs. NULL threshold produces NULL output for that row.
pub fn spark_levenshtein(args: &[ColumnarValue]) -> Result<ColumnarValue> {
if args.len() < 2 || args.len() > 3 {
return Err(DataFusionError::Internal(format!(
"levenshtein requires 2 or 3 arguments, got {}",
args.len()
)));
}
// Determine array length from any array argument
let len = args
.iter()
.find_map(|arg| match arg {
ColumnarValue::Array(a) => Some(a.len()),
_ => None,
})
.unwrap_or(1);
let left = args[0].clone().into_array(len)?;
let right = args[1].clone().into_array(len)?;
let left_arr = as_string_array(&left);
let right_arr = as_string_array(&right);
// Handle the optional threshold argument (scalar or array)
if args.len() == 3 {
let threshold_array = args[2].clone().into_array(len)?;
let threshold_arr = threshold_array
.as_any()
.downcast_ref::<Int32Array>()
.ok_or_else(|| {
DataFusionError::Internal("levenshtein threshold must be Int32".to_string())
})?;
let result: Int32Array = left_arr
.iter()
.zip(right_arr.iter())
.enumerate()
.map(|(i, (l, r))| {
// If threshold is NULL for this row, result is NULL
if threshold_arr.is_null(i) {
return None;
}
match (l, r) {
(Some(l), Some(r)) => {
let dist = levenshtein_distance(l, r);
let t = threshold_arr.value(i);
if dist > t {
Some(-1)
} else {
Some(dist)
}
}
_ => None, // NULL propagation
}
})
.collect();
Ok(ColumnarValue::Array(Arc::new(result) as ArrayRef))
} else {
// No threshold: just compute distance
let result: Int32Array = left_arr
.iter()
.zip(right_arr.iter())
.map(|(l, r)| match (l, r) {
(Some(l), Some(r)) => Some(levenshtein_distance(l, r)),
_ => None, // NULL propagation
})
.collect();
Ok(ColumnarValue::Array(Arc::new(result) as ArrayRef))
}
}
#[cfg(test)]
mod tests {
use super::*;
use arrow::array::StringArray;
use datafusion::common::ScalarValue;
#[test]
fn test_levenshtein_basic() {
assert_eq!(levenshtein_distance("", ""), 0);
assert_eq!(levenshtein_distance("abc", ""), 3);
assert_eq!(levenshtein_distance("", "abc"), 3);
assert_eq!(levenshtein_distance("abc", "abc"), 0);
assert_eq!(levenshtein_distance("kitten", "sitting"), 3);
assert_eq!(levenshtein_distance("frog", "fog"), 1);
}
#[test]
fn test_levenshtein_unicode() {
// Spark counts character-level (not byte-level) edit distance
assert_eq!(levenshtein_distance("你好", "你坏"), 1);
assert_eq!(levenshtein_distance("abc", "äbc"), 1);
}
#[test]
fn test_spark_levenshtein_nulls() {
let left = ColumnarValue::Array(Arc::new(StringArray::from(vec![
Some("abc"),
None,
Some("hello"),
])));
let right = ColumnarValue::Array(Arc::new(StringArray::from(vec![
Some("adc"),
Some("test"),
None,
])));
let result = spark_levenshtein(&[left, right]).unwrap();
match result {
ColumnarValue::Array(arr) => {
let int_arr = arr.as_any().downcast_ref::<Int32Array>().unwrap();
assert_eq!(int_arr.value(0), 1); // abc -> adc = 1
assert!(int_arr.is_null(1)); // NULL -> test = NULL
assert!(int_arr.is_null(2)); // hello -> NULL = NULL
}
_ => panic!("Expected array result"),
}
}
#[test]
fn test_spark_levenshtein_with_threshold() {
let left = ColumnarValue::Array(Arc::new(StringArray::from(vec![
Some("kitten"),
Some("abc"),
Some("frog"),
])));
let right = ColumnarValue::Array(Arc::new(StringArray::from(vec![
Some("sitting"),
Some("adc"),
Some("fog"),
])));
let threshold = ColumnarValue::Scalar(ScalarValue::Int32(Some(2)));
let result = spark_levenshtein(&[left, right, threshold]).unwrap();
match result {
ColumnarValue::Array(arr) => {
let int_arr = arr.as_any().downcast_ref::<Int32Array>().unwrap();
assert_eq!(int_arr.value(0), -1); // kitten->sitting=3 > 2, return -1
assert_eq!(int_arr.value(1), 1); // abc->adc=1 <= 2, return 1
assert_eq!(int_arr.value(2), 1); // frog->fog=1 <= 2, return 1
}
_ => panic!("Expected array result"),
}
}
#[test]
fn test_spark_levenshtein_null_threshold() {
let left = ColumnarValue::Array(Arc::new(StringArray::from(vec![Some("abc")])));
let right = ColumnarValue::Array(Arc::new(StringArray::from(vec![Some("adc")])));
let threshold = ColumnarValue::Scalar(ScalarValue::Int32(None));
let result = spark_levenshtein(&[left, right, threshold]).unwrap();
match result {
ColumnarValue::Array(arr) => {
let int_arr = arr.as_any().downcast_ref::<Int32Array>().unwrap();
assert_eq!(int_arr.len(), 1);
assert!(int_arr.is_null(0)); // NULL threshold -> NULL result
}
_ => panic!("Expected array result with NULL for NULL threshold"),
}
}
#[test]
fn test_spark_levenshtein_threshold_as_array() {
// threshold is a column (array) with per-row values
let left = ColumnarValue::Array(Arc::new(StringArray::from(vec![
Some("kitten"),
Some("frog"),
Some("abc"),
Some("hello"),
])));
let right = ColumnarValue::Array(Arc::new(StringArray::from(vec![
Some("sitting"),
Some("fog"),
Some("abc"),
Some("world"),
])));
// Per-row thresholds: 2, 5, 0, 3
let threshold = ColumnarValue::Array(Arc::new(Int32Array::from(vec![
Some(2),
Some(5),
Some(0),
Some(3),
])));
let result = spark_levenshtein(&[left, right, threshold]).unwrap();
match result {
ColumnarValue::Array(arr) => {
let int_arr = arr.as_any().downcast_ref::<Int32Array>().unwrap();
assert_eq!(int_arr.value(0), -1); // kitten->sitting=3 > 2, return -1
assert_eq!(int_arr.value(1), 1); // frog->fog=1 <= 5, return 1
assert_eq!(int_arr.value(2), 0); // abc->abc=0 <= 0, return 0
assert_eq!(int_arr.value(3), -1); // hello->world=4 > 3, return -1
}
_ => panic!("Expected array result"),
}
}
#[test]
fn test_spark_levenshtein_threshold_array_with_nulls() {
// threshold array where some values are NULL
let left = ColumnarValue::Array(Arc::new(StringArray::from(vec![
Some("abc"),
Some("hello"),
Some("frog"),
])));
let right = ColumnarValue::Array(Arc::new(StringArray::from(vec![
Some("adc"),
Some("world"),
Some("fog"),
])));
let threshold = ColumnarValue::Array(Arc::new(Int32Array::from(vec![
Some(2),
None, // NULL threshold for this row
Some(0),
])));
let result = spark_levenshtein(&[left, right, threshold]).unwrap();
match result {
ColumnarValue::Array(arr) => {
let int_arr = arr.as_any().downcast_ref::<Int32Array>().unwrap();
assert_eq!(int_arr.value(0), 1); // abc->adc=1 <= 2, return 1
assert!(int_arr.is_null(1)); // NULL threshold -> NULL
assert_eq!(int_arr.value(2), -1); // frog->fog=1 > 0, return -1
}
_ => panic!("Expected array result"),
}
}
#[test]
fn test_spark_levenshtein_threshold_negative() {
// Negative threshold means distance always exceeds threshold → return -1
let left =
ColumnarValue::Array(Arc::new(StringArray::from(vec![Some("abc"), Some("abc")])));
let right =
ColumnarValue::Array(Arc::new(StringArray::from(vec![Some("abc"), Some("adc")])));
let threshold = ColumnarValue::Array(Arc::new(Int32Array::from(vec![Some(-1), Some(-5)])));
let result = spark_levenshtein(&[left, right, threshold]).unwrap();
match result {
ColumnarValue::Array(arr) => {
let int_arr = arr.as_any().downcast_ref::<Int32Array>().unwrap();
// dist=0 > -1 is true, so return -1
assert_eq!(int_arr.value(0), -1);
// dist=1 > -5 is true, so return -1
assert_eq!(int_arr.value(1), -1);
}
_ => panic!("Expected array result"),
}
}
}