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
24 changes: 12 additions & 12 deletions crates/iceberg/src/expr/visitors/strict_projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2761,7 +2761,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 < 100) AND (pcol2 < 100)) AND (pcol3 < 10000)".to_string()
"((pcol1 < 100) AND (pcol2 < 100)) AND (pcol3 < 100.00)".to_string()
);

// test less or eq
Expand All @@ -2780,7 +2780,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 < 100) AND (pcol2 < 100)) AND (pcol3 < 10000)".to_string()
"((pcol1 < 100) AND (pcol2 < 100)) AND (pcol3 < 100.00)".to_string()
);

// test greater
Expand All @@ -2799,7 +2799,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 > 100) AND (pcol2 > 100)) AND (pcol3 > 10000)".to_string()
"((pcol1 > 100) AND (pcol2 > 100)) AND (pcol3 > 100.00)".to_string()
);

// test greater or eq
Expand All @@ -2818,7 +2818,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 > 90) AND (pcol2 > 90)) AND (pcol3 > 9990)".to_string()
"((pcol1 > 90) AND (pcol2 > 90)) AND (pcol3 > 99.90)".to_string()
);

// test not eq
Expand All @@ -2837,7 +2837,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 != 100) AND (pcol2 != 100)) AND (pcol3 != 10000)".to_string()
"((pcol1 != 100) AND (pcol2 != 100)) AND (pcol3 != 100.00)".to_string()
);

// test not in
Expand Down Expand Up @@ -2880,7 +2880,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 NOT IN (100, 90)) AND (pcol2 NOT IN (100, 90))) AND (pcol3 NOT IN (10000, 10100, 9900))"
"((pcol1 NOT IN (100, 90)) AND (pcol2 NOT IN (100, 90))) AND (pcol3 NOT IN (100.00, 99.00, 101.00))"
.to_string()
);

Expand Down Expand Up @@ -2984,7 +2984,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 < 90) AND (pcol2 < 90)) AND (pcol3 < 9990)".to_string()
"((pcol1 < 90) AND (pcol2 < 90)) AND (pcol3 < 99.90)".to_string()
);

// test less or eq
Expand All @@ -3003,7 +3003,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 < 100) AND (pcol2 < 100)) AND (pcol3 < 10000)".to_string()
"((pcol1 < 100) AND (pcol2 < 100)) AND (pcol3 < 100.00)".to_string()
);

// test greater
Expand All @@ -3022,7 +3022,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 > 90) AND (pcol2 > 90)) AND (pcol3 > 9990)".to_string()
"((pcol1 > 90) AND (pcol2 > 90)) AND (pcol3 > 99.90)".to_string()
);

// test greater or eq
Expand All @@ -3041,7 +3041,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 > 90) AND (pcol2 > 90)) AND (pcol3 > 9990)".to_string()
"((pcol1 > 90) AND (pcol2 > 90)) AND (pcol3 > 99.90)".to_string()
);

// test not eq
Expand All @@ -3060,7 +3060,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 != 90) AND (pcol2 != 90)) AND (pcol3 != 9990)".to_string()
"((pcol1 != 90) AND (pcol2 != 90)) AND (pcol3 != 99.90)".to_string()
);

// test not in
Expand Down Expand Up @@ -3103,7 +3103,7 @@ mod tests {
let result = strict_projection.strict_project(&predicate).unwrap();
assert_eq!(
result.to_string(),
"((pcol1 NOT IN (100, 90)) AND (pcol2 NOT IN (100, 90))) AND (pcol3 NOT IN (9890, 9990, 10090))"
"((pcol1 NOT IN (100, 90)) AND (pcol2 NOT IN (100, 90))) AND (pcol3 NOT IN (98.90, 100.90, 99.90))"
.to_string()
);

Expand Down
23 changes: 12 additions & 11 deletions crates/iceberg/src/spec/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use chrono::Datelike;
use fnv::FnvHashSet;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use super::values::decimal_utils::decimal_from_i128_with_scale;
use super::values::temporal::date;
use super::{Datum, PrimitiveLiteral};
use crate::ErrorKind;
Expand Down Expand Up @@ -779,8 +778,8 @@ impl Transform {
PredicateOperator::LessThan => match (datum.data_type(), datum.literal()) {
(PrimitiveType::Int, PrimitiveLiteral::Int(v)) => Some(Datum::int(v - 1)),
(PrimitiveType::Long, PrimitiveLiteral::Long(v)) => Some(Datum::long(v - 1)),
(PrimitiveType::Decimal { .. }, PrimitiveLiteral::Int128(v)) => {
Some(Datum::decimal(decimal_from_i128_with_scale(v - 1, 0))?)
(PrimitiveType::Decimal { .. }, PrimitiveLiteral::Int128(_)) => {
Some(Self::try_decrement_number(datum)?)
}
(PrimitiveType::Date, PrimitiveLiteral::Int(v)) => Some(Datum::date(v - 1)),
(PrimitiveType::Timestamp, PrimitiveLiteral::Long(v)) => {
Expand All @@ -800,8 +799,8 @@ impl Transform {
PredicateOperator::GreaterThan => match (datum.data_type(), datum.literal()) {
(PrimitiveType::Int, PrimitiveLiteral::Int(v)) => Some(Datum::int(v + 1)),
(PrimitiveType::Long, PrimitiveLiteral::Long(v)) => Some(Datum::long(v + 1)),
(PrimitiveType::Decimal { .. }, PrimitiveLiteral::Int128(v)) => {
Some(Datum::decimal(decimal_from_i128_with_scale(v + 1, 0))?)
(PrimitiveType::Decimal { .. }, PrimitiveLiteral::Int128(_)) => {
Some(Self::try_increment_number(datum)?)
}
(PrimitiveType::Date, PrimitiveLiteral::Int(v)) => Some(Datum::date(v + 1)),
(PrimitiveType::Timestamp, PrimitiveLiteral::Long(v)) => {
Expand Down Expand Up @@ -944,9 +943,10 @@ impl Transform {
match (datum.data_type(), datum.literal()) {
(PrimitiveType::Int, PrimitiveLiteral::Int(v)) => Ok(Datum::int(v + 1)),
(PrimitiveType::Long, PrimitiveLiteral::Long(v)) => Ok(Datum::long(v + 1)),
(PrimitiveType::Decimal { .. }, PrimitiveLiteral::Int128(v)) => {
Datum::decimal(decimal_from_i128_with_scale(v + 1, 0))
}
(PrimitiveType::Decimal { .. }, PrimitiveLiteral::Int128(v)) => Ok(Datum::new(
datum.data_type().clone(),
PrimitiveLiteral::Int128(v + 1),
)),
(PrimitiveType::Date, PrimitiveLiteral::Int(v)) => Ok(Datum::date(v + 1)),
(PrimitiveType::Timestamp, PrimitiveLiteral::Long(v)) => {
Ok(Datum::timestamp_micros(v + 1))
Expand Down Expand Up @@ -982,9 +982,10 @@ impl Transform {
match (datum.data_type(), datum.literal()) {
(PrimitiveType::Int, PrimitiveLiteral::Int(v)) => Ok(Datum::int(v - 1)),
(PrimitiveType::Long, PrimitiveLiteral::Long(v)) => Ok(Datum::long(v - 1)),
(PrimitiveType::Decimal { .. }, PrimitiveLiteral::Int128(v)) => {
Datum::decimal(decimal_from_i128_with_scale(v - 1, 0))
}
(PrimitiveType::Decimal { .. }, PrimitiveLiteral::Int128(v)) => Ok(Datum::new(
datum.data_type().clone(),
PrimitiveLiteral::Int128(v - 1),
)),
(PrimitiveType::Date, PrimitiveLiteral::Int(v)) => Ok(Datum::date(v - 1)),
(PrimitiveType::Timestamp, PrimitiveLiteral::Long(v)) => {
Ok(Datum::timestamp_micros(v - 1))
Expand Down
117 changes: 93 additions & 24 deletions crates/iceberg/src/transform/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ use arrow_schema::DataType;

use super::TransformFunction;
use crate::Error;
use crate::spec::decimal_utils::decimal_from_i128_with_scale;
use crate::spec::{Datum, PrimitiveLiteral};
use crate::spec::{Datum, PrimitiveLiteral, PrimitiveType};

#[derive(Debug)]
pub struct Truncate {
Expand Down Expand Up @@ -164,15 +163,18 @@ impl TransformFunction for Truncate {
})),
PrimitiveLiteral::Int128(v) => Ok(Some({
let width = self.width as i128;
Datum::decimal(decimal_from_i128_with_scale(
Self::truncate_decimal_i128(*v, width),
0,
))?
Datum::new(
input.data_type().clone(),
PrimitiveLiteral::Int128(Self::truncate_decimal_i128(*v, width)),
)
})),
PrimitiveLiteral::String(v) => Ok(Some({
let len = self.width as usize;
Datum::string(Self::truncate_str(v, len).to_string())
})),
PrimitiveLiteral::Binary(v) if input.data_type() == &PrimitiveType::Binary => Ok(Some(
Datum::binary(Self::truncate_binary(v, self.width as usize).to_vec()),
)),
_ => Err(Error::new(
crate::ErrorKind::FeatureUnsupported,
format!(
Expand All @@ -199,8 +201,7 @@ mod test {
TimestampNs, Timestamptz, TimestamptzNs, Uuid,
};
use crate::spec::Type::{Primitive, Struct};
use crate::spec::decimal_utils::decimal_new;
use crate::spec::{Datum, NestedField, PrimitiveType, StructType, Transform};
use crate::spec::{Datum, NestedField, PrimitiveLiteral, PrimitiveType, StructType, Transform};
use crate::transform::TransformFunction;
use crate::transform::test::{TestProjectionFixture, TestTransformFixture};

Expand Down Expand Up @@ -364,28 +365,28 @@ mod test {

fixture.assert_projection(
&fixture.binary_predicate(PredicateOperator::LessThan, Datum::decimal_from_str(curr)?),
Some("name <= 9990"),
Some("name <= 99.90"),
)?;

fixture.assert_projection(
&fixture.binary_predicate(
PredicateOperator::LessThanOrEq,
Datum::decimal_from_str(curr)?,
),
Some("name <= 9990"),
Some("name <= 99.90"),
)?;

fixture.assert_projection(
&fixture.binary_predicate(
PredicateOperator::GreaterThanOrEq,
Datum::decimal_from_str(curr)?,
),
Some("name >= 9990"),
Some("name >= 99.90"),
)?;

fixture.assert_projection(
&fixture.binary_predicate(PredicateOperator::Eq, Datum::decimal_from_str(curr)?),
Some("name = 9990"),
Some("name = 99.90"),
)?;

fixture.assert_projection(
Expand All @@ -399,7 +400,7 @@ mod test {
Datum::decimal_from_str(curr)?,
Datum::decimal_from_str(next)?,
]),
Some("name IN (9890, 9990, 10090)"),
Some("name IN (99.90, 100.90, 98.90)"),
)?;

fixture.assert_projection(
Expand Down Expand Up @@ -434,28 +435,28 @@ mod test {

fixture.assert_projection(
&fixture.binary_predicate(PredicateOperator::LessThan, Datum::decimal_from_str(curr)?),
Some("name <= 9990"),
Some("name <= 99.90"),
)?;

fixture.assert_projection(
&fixture.binary_predicate(
PredicateOperator::LessThanOrEq,
Datum::decimal_from_str(curr)?,
),
Some("name <= 10000"),
Some("name <= 100.00"),
)?;

fixture.assert_projection(
&fixture.binary_predicate(
PredicateOperator::GreaterThanOrEq,
Datum::decimal_from_str(curr)?,
),
Some("name >= 10000"),
Some("name >= 100.00"),
)?;

fixture.assert_projection(
&fixture.binary_predicate(PredicateOperator::Eq, Datum::decimal_from_str(curr)?),
Some("name = 10000"),
Some("name = 100.00"),
)?;

fixture.assert_projection(
Expand All @@ -469,7 +470,7 @@ mod test {
Datum::decimal_from_str(curr)?,
Datum::decimal_from_str(next)?,
]),
Some("name IN (10000, 10100, 9900)"),
Some("name IN (99.00, 100.00, 101.00)"),
)?;

fixture.assert_projection(
Expand Down Expand Up @@ -800,6 +801,34 @@ mod test {
assert_eq!(super::Truncate::truncate_str(test4, 1), test4_1_expected);
}

#[test]
fn test_literal_spec_examples() -> Result<()> {
// https://iceberg.apache.org/spec/#truncate-transform-details
let cases = [
(10, Datum::int(1), Datum::int(0)),
(10, Datum::int(-1), Datum::int(-10)),
(10, Datum::long(1), Datum::long(0)),
(10, Datum::long(-1), Datum::long(-10)),
(
50,
Datum::decimal_from_str("10.65")?,
Datum::decimal_from_str("10.50")?,
),
(3, Datum::string("iceberg"), Datum::string("ice")),
(
3,
Datum::binary(vec![0x01, 0x02, 0x03, 0x04, 0x05]),
Datum::binary(vec![0x01, 0x02, 0x03]),
),
];

for (width, input, expected) in cases {
let result = super::Truncate::new(width).transform_literal(&input)?;
assert_eq!(result, Some(expected), "width: {width}, input: {input:?}");
}
Ok(())
}

#[test]
fn test_literal_int() {
let input = Datum::int(1);
Expand Down Expand Up @@ -836,12 +865,52 @@ mod test {

#[test]
fn test_decimal_literal() {
let input = Datum::decimal(decimal_new(1065, 0)).unwrap();
let res = super::Truncate::new(50)
.transform_literal(&input)
.unwrap()
.unwrap();
assert_eq!(res, Datum::decimal(decimal_new(1050, 0)).unwrap(),);
for (precision, scale, value, expected) in [
(9, 0, 1065, 1050),
(9, 2, 1065, 1050),
(9, 2, -1065, -1100),
(9, 2, 1050, 1050),
(9, 2, 0, 0),
(9, 5, 1065, 1050),
(38, 2, 1065, 1050),
] {
let data_type = Decimal { precision, scale };
let input = Datum::new(data_type.clone(), PrimitiveLiteral::Int128(value));
let res = super::Truncate::new(50)
.transform_literal(&input)
.unwrap()
.unwrap();
assert_eq!(
res,
Datum::new(data_type, PrimitiveLiteral::Int128(expected)),
"input: {input:?}"
);
}
}

#[test]
fn test_binary_literal() {
for (value, width, expected) in [
(vec![0x00, 0xff, 0x80, 0x01], 2, vec![0x00, 0xff]),
(vec![0x00, 0xff], 2, vec![0x00, 0xff]),
(vec![0x00, 0xff], 4, vec![0x00, 0xff]),
(vec![], 2, vec![]),
] {
let input = Datum::binary(value);
let res = super::Truncate::new(width)
.transform_literal(&input)
.unwrap()
.unwrap();
assert_eq!(res, Datum::binary(expected));
}
}

#[test]
fn test_fixed_literal_is_unsupported() {
let error = super::Truncate::new(2)
.transform_literal(&Datum::fixed(vec![0x00, 0xff, 0x80]))
.unwrap_err();
assert_eq!(error.kind(), crate::ErrorKind::FeatureUnsupported);
}

#[test]
Expand Down
Loading