@@ -709,7 +709,7 @@ pub fn register(registry: &mut FunctionRegistry) {
709
709
calc_domain : Box :: new ( move |ctx, d| {
710
710
convert_to_decimal_domain ( ctx, d[ 0 ] . clone ( ) , decimal_type)
711
711
. map ( |d| FunctionDomain :: Domain ( Domain :: Decimal ( d) ) )
712
- . unwrap_or ( FunctionDomain :: Full )
712
+ . unwrap_or ( FunctionDomain :: MayThrow )
713
713
} ) ,
714
714
eval : Box :: new ( move |args, ctx| {
715
715
convert_to_decimal ( & args[ 0 ] , ctx, & from_type, decimal_type)
@@ -994,6 +994,7 @@ fn convert_to_decimal_domain(
994
994
} ;
995
995
let dest_size = dest_type. size ( ) ;
996
996
let res = convert_to_decimal ( & value. as_ref ( ) , & mut ctx, & from_type, dest_type) ;
997
+
997
998
if ctx. errors . is_some ( ) {
998
999
return None ;
999
1000
}
@@ -1313,8 +1314,9 @@ fn decimal_256_to_128(
1313
1314
. enumerate ( )
1314
1315
. map ( |( row, x) | {
1315
1316
let x = x * i128:: one ( ) ;
1317
+
1316
1318
match x. checked_div ( factor) {
1317
- Some ( x ) if x <= max && x >= min => * x . low ( ) ,
1319
+ Some ( y ) if ( y <= max && y >= min) && ! ( y == 0 && x > 0 ) => * y . low ( ) ,
1318
1320
_ => {
1319
1321
ctx. set_error ( row, concat ! ( "Decimal overflow at line : " , line!( ) ) ) ;
1320
1322
i128:: one ( )
@@ -1344,7 +1346,28 @@ macro_rules! m_decimal_to_decimal {
1344
1346
} else {
1345
1347
let values: Vec <_> = if $from_size. scale > $dest_size. scale {
1346
1348
let factor = <$dest_type_name>:: e( ( $from_size. scale - $dest_size. scale) as u32 ) ;
1347
- $buffer. iter( ) . map( |x| x / factor) . collect( )
1349
+ let max = <$dest_type_name>:: max_for_precision( $dest_size. precision) ;
1350
+ let min = <$dest_type_name>:: min_for_precision( $dest_size. precision) ;
1351
+ $buffer
1352
+ . iter( )
1353
+ . enumerate( )
1354
+ . map( |( row, x) | {
1355
+ let x = x * <$dest_type_name>:: one( ) ;
1356
+
1357
+ match x. checked_div( factor) {
1358
+ Some ( y) if y <= max && y >= min && !( y == 0 && x > 0 ) => {
1359
+ y as $dest_type_name
1360
+ }
1361
+ _ => {
1362
+ $ctx. set_error(
1363
+ row,
1364
+ concat!( "Decimal overflow at line : " , line!( ) ) ,
1365
+ ) ;
1366
+ <$dest_type_name>:: one( )
1367
+ }
1368
+ }
1369
+ } )
1370
+ . collect( )
1348
1371
} else {
1349
1372
let factor = <$dest_type_name>:: e( ( $dest_size. scale - $from_size. scale) as u32 ) ;
1350
1373
let max = <$dest_type_name>:: max_for_precision( $dest_size. precision) ;
0 commit comments