Skip to content

Commit

Permalink
implement sub for string
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKaul committed Oct 25, 2024
1 parent 023d547 commit e050686
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion iceberg-rust-spec/src/spec/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
any::Any,
collections::{btree_map::Keys, BTreeMap, HashMap},
fmt,
hash::Hash,
hash::{DefaultHasher, Hash, Hasher},
io::Cursor,
ops::{Add, Sub},
slice::Iter,
Expand Down Expand Up @@ -882,6 +882,9 @@ impl TrySub for Value {
(Value::TimestampTZ(own), Value::TimestampTZ(other)) => {
Ok(Value::TimestampTZ(own - other))
}
(Value::String(own), Value::String(other)) => {
Ok(Value::LongInt(sub_string(&own, &other) as i64))
}
(x, y) => Err(Error::Type(
x.datatype().to_string(),
y.datatype().to_string(),
Expand All @@ -890,6 +893,31 @@ impl TrySub for Value {
}
}

fn sub_string(left: &str, right: &str) -> u64 {
if let Some(distance) = left
.chars()
.zip(right.chars())
.take(256)
.skip_while(|(l, r)| l == r)
.try_fold(0, |acc, (l, r)| {
if let (Some(l), Some(r)) = (l.to_digit(36), r.to_digit(36)) {
Some(acc + (l - r).pow(2))
} else {
None
}
})
{
distance as u64
} else {
let mut hasher = DefaultHasher::new();
hasher.write(left.as_bytes());
let left = hasher.finish();
hasher.write(right.as_bytes());
let right = hasher.finish();
left - right
}
}

#[cfg(test)]
mod tests {

Expand Down

0 comments on commit e050686

Please sign in to comment.