Skip to content

Commit 0da9089

Browse files
committed
fix: exclude precision-losing integer-to-float conversions from CastExpr::check_bigger_cast (apache#23808)
1 parent 9e3c71f commit 0da9089

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

  • datafusion/physical-expr/src/expressions

‎datafusion/physical-expr/src/expressions/cast.rs‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,8 @@ impl CastExpr {
179179
| (UInt8, UInt16 | UInt32 | UInt64)
180180
| (UInt16, UInt32 | UInt64)
181181
| (UInt32, UInt64)
182-
| (
183-
Int8 | Int16 | Int32 | UInt8 | UInt16 | UInt32,
184-
Float32 | Float64
185-
)
186-
| (Int64 | UInt64, Float64)
182+
| (Int8 | Int16 | UInt8 | UInt16, Float32)
183+
| (Int8 | Int16 | Int32 | UInt8 | UInt16 | UInt32, Float64)
187184
| (Utf8, LargeUtf8)
188185
)
189186
}
@@ -1369,4 +1366,24 @@ mod proto_tests {
13691366
DataFusionError::Internal(msg) if msg.contains("call 1")
13701367
));
13711368
}
1369+
1370+
#[test]
1371+
fn test_check_bigger_cast_precision_loss() {
1372+
use DataType::*;
1373+
1374+
// Exact conversions without precision loss
1375+
assert!(CastExpr::check_bigger_cast(&Int16, &Int8));
1376+
assert!(CastExpr::check_bigger_cast(&Int64, &Int32));
1377+
assert!(CastExpr::check_bigger_cast(&Float32, &Int16));
1378+
assert!(CastExpr::check_bigger_cast(&Float32, &UInt16));
1379+
assert!(CastExpr::check_bigger_cast(&Float64, &Int32));
1380+
assert!(CastExpr::check_bigger_cast(&Float64, &UInt32));
1381+
assert!(CastExpr::check_bigger_cast(&LargeUtf8, &Utf8));
1382+
1383+
// Precision-losing int-to-float conversions should return false
1384+
assert!(!CastExpr::check_bigger_cast(&Float32, &Int32));
1385+
assert!(!CastExpr::check_bigger_cast(&Float32, &UInt32));
1386+
assert!(!CastExpr::check_bigger_cast(&Float64, &Int64));
1387+
assert!(!CastExpr::check_bigger_cast(&Float64, &UInt64));
1388+
}
13721389
}

0 commit comments

Comments
 (0)