Skip to content
Open
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
11 changes: 6 additions & 5 deletions crates/iceberg/src/spec/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl Transform {
self.truncate_number_strict(name, expr, &func)
} else if expr.op() == PredicateOperator::StartsWith {
let len = match expr.literal().literal() {
PrimitiveLiteral::String(s) => s.len(),
PrimitiveLiteral::String(s) => s.chars().count(),
PrimitiveLiteral::Binary(b) => b.len(),
_ => {
return Err(invalid_data!(
Expand All @@ -521,7 +521,7 @@ impl Transform {
}
} else if expr.op() == PredicateOperator::NotStartsWith {
let len = match expr.literal().literal() {
PrimitiveLiteral::String(s) => s.len(),
PrimitiveLiteral::String(s) => s.chars().count(),
PrimitiveLiteral::Binary(b) => b.len(),
_ => {
return Err(invalid_data!(
Expand Down Expand Up @@ -848,7 +848,7 @@ impl Transform {
PredicateOperator::StartsWith => match datum.literal() {
PrimitiveLiteral::String(s) => {
if let Some(w) = width
&& s.len() == w as usize
&& s.chars().count() == w as usize
{
return Some(PredicateOperator::Eq);
};
Expand All @@ -860,12 +860,13 @@ impl Transform {
PrimitiveLiteral::String(s) => {
if let Some(w) = width {
let w = w as usize;
let len = s.chars().count();

if s.len() == w {
if len == w {
return Some(PredicateOperator::NotEq);
}

if s.len() < w {
if len < w {
return Some(*op);
}

Expand Down
56 changes: 56 additions & 0 deletions crates/iceberg/src/transform/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,62 @@ mod test {
Ok(())
}

#[test]
fn test_projection_truncate_string_compares_width_in_code_points() -> Result<()> {
// "Év" is two code points but three UTF-8 bytes, so it is shorter than
// truncate[3] and the prefix must not collapse to an equality predicate.
let fixture = TestProjectionFixture::new(
Transform::Truncate(3),
"city",
NestedField::required(1, "city", Primitive(PrimitiveType::String)),
);

fixture.assert_projection(
&fixture.binary_predicate(PredicateOperator::StartsWith, Datum::string("Év")),
Some(r#"city STARTS WITH "Év""#),
)?;

fixture.assert_projection(
&fixture.binary_predicate(PredicateOperator::NotStartsWith, Datum::string("Év")),
Some(r#"city NOT STARTS WITH "Év""#),
)?;

fixture.assert_projection(
&fixture.binary_predicate(PredicateOperator::StartsWith, Datum::string("Évr")),
Some(r#"city = "Évr""#),
)?;

Ok(())
}

#[test]
fn test_strict_projection_truncate_string_compares_width_in_code_points() -> Result<()> {
let transform = Transform::Truncate(3);
let fixture = TestProjectionFixture::new(
transform,
"city",
NestedField::required(1, "city", Primitive(PrimitiveType::String)),
);

for (prefix, expected) in [
("Év", Some(r#"city STARTS WITH "Év""#)),
("Évr", Some(r#"city = "Évr""#)),
("Évry", None),
] {
let predicate =
fixture.binary_predicate(PredicateOperator::StartsWith, Datum::string(prefix));
assert_eq!(
expected,
transform
.strict_project("city", &predicate)?
.map(|p| p.to_string())
.as_deref(),
);
}

Ok(())
}

#[test]
fn test_strict_projection_truncate_binary() -> Result<()> {
let transform = Transform::Truncate(3);
Expand Down
Loading