From 45f3ca1bba14f1d192f9ab0094a9f63e9acfe792 Mon Sep 17 00:00:00 2001 From: Andy Lok Date: Thu, 9 Mar 2023 01:20:49 +0800 Subject: [PATCH 1/2] fix(expr): fix common_super_type() for decimal --- src/query/expression/src/type_check.rs | 33 +++++++++++++------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/query/expression/src/type_check.rs b/src/query/expression/src/type_check.rs index 5aa464f2fb9f6..f2bff48ad3661 100755 --- a/src/query/expression/src/type_check.rs +++ b/src/query/expression/src/type_check.rs @@ -516,11 +516,10 @@ pub fn can_auto_cast_to( .zip(dest_tys) .all(|(src_ty, dest_ty)| can_auto_cast_to(src_ty, dest_ty, auto_cast_rules)) } - (DataType::Number(n), DataType::Decimal(_)) if !n.is_float() => true, (DataType::String, DataType::Decimal(_)) => true, (DataType::Decimal(x), DataType::Decimal(y)) => x.precision() <= y.precision(), - (DataType::Decimal(_), DataType::Number(NumberDataType::Float32)) => true, - (DataType::Decimal(_), DataType::Number(NumberDataType::Float64)) => true, + (DataType::Number(n), DataType::Decimal(_)) if !n.is_float() => true, + (DataType::Decimal(_), DataType::Number(n)) if n.is_float() => true, _ => false, } } @@ -559,24 +558,24 @@ pub fn common_super_type( .collect::>>()?; Some(DataType::Tuple(tys)) } - (DataType::Number(_), DataType::Decimal(ty)) - | (DataType::Decimal(ty), DataType::Number(_)) => { - let max_precision = ty.max_precision(); - let scale = ty.scale(); - - DecimalDataType::from_size(DecimalSize { - precision: max_precision, - scale, - }) - .ok() - .map(DataType::Decimal) - } - + (DataType::String, decimal_ty @ DataType::Decimal(_)) + | (decimal_ty @ DataType::Decimal(_), DataType::String) => Some(decimal_ty), (DataType::Decimal(a), DataType::Decimal(b)) => { let ty = DecimalDataType::binary_result_type(&a, &b, false, false, true).ok(); ty.map(DataType::Decimal) } - + (DataType::Number(num_ty), decimal_ty @ DataType::Decimal(_)) + | (decimal_ty @ DataType::Decimal(_), DataType::Number(num_ty)) + if !num_ty.is_float() => + { + Some(decimal_ty) + } + (DataType::Number(num_ty), DataType::Decimal(_)) + | (DataType::Decimal(_), DataType::Number(num_ty)) + if num_ty.is_float() => + { + Some(DataType::Number(num_ty)) + } (ty1, ty2) => { let ty1_can_cast_to = auto_cast_rules .iter() From b7ca3e6eda7d7ad1dc8a2769517f2361b364c08f Mon Sep 17 00:00:00 2001 From: Andy Lok Date: Tue, 14 Mar 2023 14:30:59 +0800 Subject: [PATCH 2/2] fix --- src/query/expression/src/type_check.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/query/expression/src/type_check.rs b/src/query/expression/src/type_check.rs index 512b59983fb6e..d68734165730b 100755 --- a/src/query/expression/src/type_check.rs +++ b/src/query/expression/src/type_check.rs @@ -596,11 +596,18 @@ pub fn common_super_type( let ty = DecimalDataType::binary_result_type(&a, &b, false, false, true).ok(); ty.map(DataType::Decimal) } - (DataType::Number(num_ty), decimal_ty @ DataType::Decimal(_)) - | (decimal_ty @ DataType::Decimal(_), DataType::Number(num_ty)) + (DataType::Number(num_ty), DataType::Decimal(decimal_ty)) + | (DataType::Decimal(decimal_ty), DataType::Number(num_ty)) if !num_ty.is_float() => { - Some(decimal_ty) + let max_precision = decimal_ty.max_precision(); + let scale = decimal_ty.scale(); + DecimalDataType::from_size(DecimalSize { + precision: max_precision, + scale, + }) + .ok() + .map(DataType::Decimal) } (DataType::Number(num_ty), DataType::Decimal(_)) | (DataType::Decimal(_), DataType::Number(num_ty))