File tree Expand file tree Collapse file tree 2 files changed +26
-10
lines changed
crates/polars-compute/src/cast
py-polars/tests/unit/datatypes Expand file tree Collapse file tree 2 files changed +26
-10
lines changed Original file line number Diff line number Diff line change @@ -229,12 +229,12 @@ pub fn integer_to_decimal<T: NativeType + AsPrimitive<i128>>(
229
229
to_precision : usize ,
230
230
to_scale : usize ,
231
231
) -> PrimitiveArray < i128 > {
232
- let multiplier = 10_i128 . pow ( to_scale as u32 ) ;
232
+ assert ! ( to_precision <= 38 ) ;
233
+ assert ! ( to_scale <= 38 ) ;
233
234
234
- let min_for_precision = 9_i128
235
- . saturating_pow ( 1 + to_precision as u32 )
236
- . saturating_neg ( ) ;
237
- let max_for_precision = 9_i128 . saturating_pow ( 1 + to_precision as u32 ) ;
235
+ let multiplier = 10_i128 . pow ( to_scale as u32 ) ;
236
+ let max_for_precision = 10_i128 . pow ( to_precision as u32 ) - 1 ;
237
+ let min_for_precision = -max_for_precision;
238
238
239
239
let values = from. iter ( ) . map ( |x| {
240
240
x. and_then ( |x| {
@@ -274,13 +274,13 @@ where
274
274
T : NativeType + Float + ToPrimitive ,
275
275
f64 : AsPrimitive < T > ,
276
276
{
277
+ assert ! ( to_precision <= 38 ) ;
278
+ assert ! ( to_scale <= 38 ) ;
279
+
277
280
// 1.2 => 12
278
281
let multiplier: T = ( 10_f64 ) . powi ( to_scale as i32 ) . as_ ( ) ;
279
-
280
- let min_for_precision = 9_i128
281
- . saturating_pow ( 1 + to_precision as u32 )
282
- . saturating_neg ( ) ;
283
- let max_for_precision = 9_i128 . saturating_pow ( 1 + to_precision as u32 ) ;
282
+ let max_for_precision = 10_i128 . pow ( to_precision as u32 ) - 1 ;
283
+ let min_for_precision = -max_for_precision;
284
284
285
285
let values = from. iter ( ) . map ( |x| {
286
286
x. and_then ( |x| {
Original file line number Diff line number Diff line change 13
13
import pytest
14
14
15
15
import polars as pl
16
+ from polars .exceptions import InvalidOperationError
16
17
from polars .testing import assert_frame_equal , assert_series_equal
17
18
18
19
@@ -758,3 +759,18 @@ def test_decimal32_decimal64_22946() -> None:
758
759
]
759
760
),
760
761
)
762
+
763
+
764
+ def test_decimal_cast_limit () -> None :
765
+ fits = pl .Series ([10 ** 38 - 1 , - (10 ** 38 - 1 )])
766
+ assert_series_equal (fits .cast (pl .Decimal (38 , 0 )).cast (pl .Int128 ), fits )
767
+
768
+ with pytest .raises (InvalidOperationError ):
769
+ fits .cast (pl .Decimal (39 , 0 ))
770
+
771
+ too_large1 = pl .Series ([10 ** 38 ])
772
+ too_large2 = pl .Series ([- (10 ** 38 )])
773
+ with pytest .raises (InvalidOperationError ):
774
+ too_large1 .cast (pl .Decimal (38 , 0 ))
775
+ with pytest .raises (InvalidOperationError ):
776
+ too_large2 .cast (pl .Decimal (38 , 0 ))
You can’t perform that action at this time.
0 commit comments