Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions datafusion/common/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,10 +693,14 @@ pub mod datafusion_strsim {
}

/// Calculates the minimum number of insertions, deletions, and substitutions
/// required to change one sequence into the other.
fn generic_levenshtein<'a, 'b, Iter1, Iter2, Elem1, Elem2>(
/// required to change one sequence into the other, using a reusable cache buffer.
///
/// This is the generic implementation that works with any iterator types.
/// The `cache` buffer will be resized as needed and reused across calls.
fn generic_levenshtein_with_buffer<'a, 'b, Iter1, Iter2, Elem1, Elem2>(
a: &'a Iter1,
b: &'b Iter2,
cache: &mut Vec<usize>,
) -> usize
where
&'a Iter1: IntoIterator<Item = Elem1>,
Expand All @@ -709,7 +713,9 @@ pub mod datafusion_strsim {
return b_len;
}

let mut cache: Vec<usize> = (1..b_len + 1).collect();
// Resize cache to fit b_len elements
cache.clear();
cache.extend(1..=b_len);

let mut result = 0;

Expand All @@ -729,6 +735,21 @@ pub mod datafusion_strsim {
result
}

/// Calculates the minimum number of insertions, deletions, and substitutions
/// required to change one sequence into the other.
fn generic_levenshtein<'a, 'b, Iter1, Iter2, Elem1, Elem2>(
a: &'a Iter1,
b: &'b Iter2,
) -> usize
where
&'a Iter1: IntoIterator<Item = Elem1>,
&'b Iter2: IntoIterator<Item = Elem2>,
Elem1: PartialEq<Elem2>,
{
let mut cache = Vec::new();
generic_levenshtein_with_buffer(a, b, &mut cache)
}

/// Calculates the minimum number of insertions, deletions, and substitutions
/// required to change one string into the other.
///
Expand All @@ -741,6 +762,15 @@ pub mod datafusion_strsim {
generic_levenshtein(&StringWrapper(a), &StringWrapper(b))
}

/// Calculates the Levenshtein distance using a reusable cache buffer.
/// This avoids allocating a new Vec for each call, improving performance
/// when computing many distances.
///
/// The `cache` buffer will be resized as needed and reused across calls.
pub fn levenshtein_with_buffer(a: &str, b: &str, cache: &mut Vec<usize>) -> usize {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a way to unify this with generic_levenshtein to avoid duplication, something like having generic_levenshtein call this but creating a vec buffer at the callsite? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I refactored it to generic_levenshtein_with_buffer. Now generic_levenshtein calls it.

generic_levenshtein_with_buffer(&StringWrapper(a), &StringWrapper(b), cache)
}

/// Calculates the normalized Levenshtein distance between two strings.
/// The normalized distance is a value between 0.0 and 1.0, where 1.0 indicates
/// that the strings are identical and 0.0 indicates no similarity.
Expand Down
5 changes: 5 additions & 0 deletions datafusion/functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,8 @@ required-features = ["string_expressions"]
harness = false
name = "ends_with"
required-features = ["string_expressions"]

[[bench]]
harness = false
name = "levenshtein"
required-features = ["unicode_expressions"]
87 changes: 87 additions & 0 deletions datafusion/functions/benches/levenshtein.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// 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.

extern crate criterion;

use arrow::array::OffsetSizeTrait;
use arrow::datatypes::{DataType, Field};
use arrow::util::bench_util::create_string_array_with_len;
use criterion::{Criterion, SamplingMode, criterion_group, criterion_main};
use datafusion_common::DataFusionError;
use datafusion_common::config::ConfigOptions;
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs};
use datafusion_functions::string;
use std::hint::black_box;
use std::sync::Arc;
use std::time::Duration;

fn create_args<O: OffsetSizeTrait>(size: usize, str_len: usize) -> Vec<ColumnarValue> {
let string1_array = Arc::new(create_string_array_with_len::<O>(size, 0.1, str_len));
let string2_array = Arc::new(create_string_array_with_len::<O>(size, 0.1, str_len));

vec![
ColumnarValue::Array(string1_array),
ColumnarValue::Array(string2_array),
]
}

fn invoke_levenshtein_with_args(
args: Vec<ColumnarValue>,
number_rows: usize,
) -> Result<ColumnarValue, DataFusionError> {
let arg_fields = args
.iter()
.enumerate()
.map(|(idx, arg)| Field::new(format!("arg_{idx}"), arg.data_type(), true).into())
.collect::<Vec<_>>();
let config_options = Arc::new(ConfigOptions::default());

string::levenshtein().invoke_with_args(ScalarFunctionArgs {
args,
arg_fields,
number_rows,
return_field: Field::new("f", DataType::Int32, true).into(),
config_options: Arc::clone(&config_options),
})
}

fn criterion_benchmark(c: &mut Criterion) {
for size in [1024, 4096] {
let mut group = c.benchmark_group(format!("levenshtein size={size}"));
group.sampling_mode(SamplingMode::Flat);
group.sample_size(10);
group.measurement_time(Duration::from_secs(10));

for str_len in [8, 32] {
let args = create_args::<i32>(size, str_len);
group.bench_function(
format!("levenshtein_string [size={size}, str_len={str_len}]"),
|b| {
b.iter(|| {
let args_cloned = args.clone();
black_box(invoke_levenshtein_with_args(args_cloned, size))
})
},
);
}

group.finish();
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
24 changes: 21 additions & 3 deletions datafusion/functions/src/string/levenshtein.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,18 @@ fn levenshtein<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
DataType::Utf8View => {
let str1_array = as_string_view_array(&str1)?;
let str2_array = as_string_view_array(&str2)?;

// Reusable buffer to avoid allocating for each row
let mut cache = Vec::new();

let result = str1_array
.iter()
.zip(str2_array.iter())
.map(|(string1, string2)| match (string1, string2) {
(Some(string1), Some(string2)) => {
Some(datafusion_strsim::levenshtein(string1, string2) as i32)
Some(datafusion_strsim::levenshtein_with_buffer(
string1, string2, &mut cache,
) as i32)
}
_ => None,
})
Expand All @@ -166,12 +172,18 @@ fn levenshtein<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
DataType::Utf8 => {
let str1_array = as_generic_string_array::<T>(&str1)?;
let str2_array = as_generic_string_array::<T>(&str2)?;

// Reusable buffer to avoid allocating for each row
let mut cache = Vec::new();

let result = str1_array
.iter()
.zip(str2_array.iter())
.map(|(string1, string2)| match (string1, string2) {
(Some(string1), Some(string2)) => {
Some(datafusion_strsim::levenshtein(string1, string2) as i32)
Some(datafusion_strsim::levenshtein_with_buffer(
string1, string2, &mut cache,
) as i32)
}
_ => None,
})
Expand All @@ -181,12 +193,18 @@ fn levenshtein<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
DataType::LargeUtf8 => {
let str1_array = as_generic_string_array::<T>(&str1)?;
let str2_array = as_generic_string_array::<T>(&str2)?;

// Reusable buffer to avoid allocating for each row
let mut cache = Vec::new();

let result = str1_array
.iter()
.zip(str2_array.iter())
.map(|(string1, string2)| match (string1, string2) {
(Some(string1), Some(string2)) => {
Some(datafusion_strsim::levenshtein(string1, string2) as i64)
Some(datafusion_strsim::levenshtein_with_buffer(
string1, string2, &mut cache,
) as i64)
}
_ => None,
})
Expand Down