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
33 changes: 24 additions & 9 deletions crates/iceberg/src/spec/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::str::FromStr;
use fnv::FnvHashSet;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use super::values::decimal_utils::decimal_from_i128_with_scale;
use super::{Datum, PrimitiveLiteral};
use crate::ErrorKind;
use crate::error::{Error, Result};
Expand Down Expand Up @@ -660,8 +659,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 { precision, scale }, PrimitiveLiteral::Int128(v)) => {
Some(Datum::decimal_from_mantissa(v - 1, *precision, *scale)?)
}
(PrimitiveType::Date, PrimitiveLiteral::Int(v)) => Some(Datum::date(v - 1)),
(PrimitiveType::Timestamp, PrimitiveLiteral::Long(v)) => {
Expand All @@ -681,8 +680,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 { precision, scale }, PrimitiveLiteral::Int128(v)) => {
Some(Datum::decimal_from_mantissa(v + 1, *precision, *scale)?)
}
(PrimitiveType::Date, PrimitiveLiteral::Int(v)) => Some(Datum::date(v + 1)),
(PrimitiveType::Timestamp, PrimitiveLiteral::Long(v)) => {
Expand Down Expand Up @@ -825,8 +824,8 @@ 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 { precision, scale }, PrimitiveLiteral::Int128(v)) => {
Datum::decimal_from_mantissa(v + 1, *precision, *scale)
}
(PrimitiveType::Date, PrimitiveLiteral::Int(v)) => Ok(Datum::date(v + 1)),
(PrimitiveType::Timestamp, PrimitiveLiteral::Long(v)) => {
Expand Down Expand Up @@ -863,8 +862,8 @@ 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 { precision, scale }, PrimitiveLiteral::Int128(v)) => {
Datum::decimal_from_mantissa(v - 1, *precision, *scale)
}
(PrimitiveType::Date, PrimitiveLiteral::Int(v)) => Ok(Datum::date(v - 1)),
(PrimitiveType::Timestamp, PrimitiveLiteral::Long(v)) => {
Expand Down Expand Up @@ -1119,4 +1118,20 @@ mod tests {
check_boundary(PredicateOperator::GreaterThanOrEq, datum.clone(), datum);
}
}

#[test]
fn test_adjust_boundary_preserves_decimal_type() {
let datum = Datum::decimal_from_mantissa(10_000, 9, 2).unwrap();

check_boundary(
PredicateOperator::LessThan,
datum.clone(),
Datum::decimal_from_mantissa(9_999, 9, 2).unwrap(),
);
check_boundary(
PredicateOperator::GreaterThan,
datum,
Datum::decimal_from_mantissa(10_001, 9, 2).unwrap(),
);
}
}
6 changes: 5 additions & 1 deletion crates/iceberg/src/spec/values/datum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,11 @@ impl Datum {
})
}

fn decimal_from_mantissa(mantissa: i128, precision: u32, scale: u32) -> Result<Self> {
pub(crate) fn decimal_from_mantissa(
mantissa: i128,
precision: u32,
scale: u32,
) -> Result<Self> {
let r#type = Type::decimal(precision, scale)?;
if decimal_precision(mantissa) > precision {
let value = decimal_from_i128_with_scale(mantissa, scale);
Expand Down
50 changes: 35 additions & 15 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 @@ -163,11 +162,21 @@ impl TransformFunction for Truncate {
Datum::long(Self::truncate_i64(*v, width))
})),
PrimitiveLiteral::Int128(v) => Ok(Some({
let PrimitiveType::Decimal { precision, scale } = input.data_type() else {
return Err(Error::new(
crate::ErrorKind::DataInvalid,
format!(
"Expected decimal type for decimal literal, got: {:?}",
input.data_type()
),
));
};
let width = self.width as i128;
Datum::decimal(decimal_from_i128_with_scale(
Datum::decimal_from_mantissa(
Self::truncate_decimal_i128(*v, width),
0,
))?
*precision,
*scale,
)?
})),
PrimitiveLiteral::String(v) => Ok(Some({
let len = self.width as usize;
Expand Down Expand Up @@ -343,6 +352,17 @@ mod test {
Ok(())
}

#[test]
fn test_truncate_decimal_literal_preserves_type() -> Result<()> {
let input = Datum::decimal_with_precision(decimal_new(9_999, 2), 9)?;
let transformed = super::Truncate::new(10).transform_literal(&input)?.unwrap();

assert_eq!(transformed.data_type(), input.data_type());
assert_eq!(transformed.to_string(), "99.90");

Ok(())
}

#[test]
fn test_projection_truncate_upper_bound_decimal() -> Result<()> {
let prev = "98.99";
Expand All @@ -364,28 +384,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 +419,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 +454,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 +489,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
Loading