diff --git a/be/src/vec/data_types/data_type_decimal.h b/be/src/vec/data_types/data_type_decimal.h index 12c7975e05..5c7ad534b0 100644 --- a/be/src/vec/data_types/data_type_decimal.h +++ b/be/src/vec/data_types/data_type_decimal.h @@ -685,6 +685,11 @@ void convert_to_decimal(typename ToDataType::FieldType* dst, if constexpr (std::is_floating_point_v) { auto multiplier = ToDataType::get_scale_multiplier(to_scale); + // For decimal256, we need to use long double to avoid overflow when + // static casting the multiplier to floating type, and also to be as precise as possible; + // For other decimal types, we use double to be as precise as possible. + using DoubleType = std::conditional_t, + long double, double>; if constexpr (narrow_integral) { for (size_t i = 0; i < size; ++i) { if (!std::isfinite(src[i])) { @@ -692,8 +697,8 @@ void convert_to_decimal(typename ToDataType::FieldType* dst, ErrorCode::ARITHMETIC_OVERFLOW_ERRROR, "Decimal convert overflow. Cannot convert infinity or NaN to decimal"); } - FromFieldType tmp = src[i] * multiplier; - if (tmp <= FromFieldType(min_result) || tmp >= FromFieldType(max_result)) { + DoubleType tmp = src[i] * static_cast(multiplier.value); + if (tmp <= DoubleType(min_result.value) || tmp >= DoubleType(max_result.value)) { ToDataType to_data_type(to_precision, to_scale); throw Exception( ErrorCode::ARITHMETIC_OVERFLOW_ERRROR, @@ -703,8 +708,9 @@ void convert_to_decimal(typename ToDataType::FieldType* dst, } } for (size_t i = 0; i < size; ++i) { - dst[i].value = typename ToDataType::FieldType::NativeType( - FromFieldType(src[i] * multiplier.value + ((src[i] >= 0) ? 0.5 : -0.5))); + dst[i].value = static_cast( + static_cast(src[i] * static_cast(multiplier.value) + + ((src[i] >= 0) ? 0.5 : -0.5))); } } else { using DecimalFrom = diff --git a/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out b/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out index 3ba2863a6f..e4498b435d 100644 --- a/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out +++ b/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out @@ -51,3 +51,205 @@ 3 2.507947739348449E-7 4 1.0 +-- !cast_float_to_decimal256_const_1_1 -- +219.77717590332030098495757448650589208640 + +-- !cast_float_to_decimal256_const_1_2 -- +219.77718000000000000000000000000000000000 + +-- !cast_float_to_decimal256_const_2_1 -- +-219.77717590332030098495757448650589208640 + +-- !cast_float_to_decimal256_const_2_2 -- +-219.77718000000000000000000000000000000000 + +-- !cast_float_to_decimal256_const_3_1 -- +99999996802856929887877846998705567504.08319368450610608065324841755887360000 + +-- !cast_float_to_decimal256_const_3_2 -- +99999996802856929887877846998705567504.08319368450610608065324841755887360000 + +-- !cast_float_to_decimal256_const_4_1 -- +-99999996802856929887877846998705567504.08319368450610608065324841755887360000 + +-- !cast_float_to_decimal256_const_4_2 -- +-99999996802856929887877846998705567504.08319368450610608065324841755887360000 + +-- !cast_float_to_decimal256_const_5_1 -- +9999999848243207687231096732081.517441445022649282668861247346191519797952000 + +-- !cast_float_to_decimal256_const_5_2 -- +9999999848243207687231096732081.517441445022649282668861247346191519797952000 + +-- !cast_float_to_decimal256_const_6_1 -- +-9999999848243207687231096732081.517441445022649282668861247346191519797952000 + +-- !cast_float_to_decimal256_const_6_2 -- +-9999999848243207687231096732081.517441445022649282668861247346191519797952000 + +-- !cast_float_to_decimal256_const_7_1 -- +0.8999999761581421686088389507648326470906229208913952822970533869391577408000 + +-- !cast_float_to_decimal256_const_7_2 -- +0.9000000000000000000000000000000000000000000000000000000000000000000000000000 + +-- !cast_float_to_decimal256_const_8_1 -- +-0.8999999761581421686088389507648326470906229208913952822970533869391577408000 + +-- !cast_float_to_decimal256_const_8_2 -- +-0.9000000000000000000000000000000000000000000000000000000000000000000000000000 + +-- !cast_float_to_decimal256_const_9_1 -- +340200000055538043127555627967637430108.2770577350592535491936868262950128000 + +-- !cast_float_to_decimal256_const_9_2 -- +340200000000000000000000000000000000000 + +-- !cast_float_to_decimal256_const_10_1 -- +-340200000055538043127555627967637430108.2770577350592535491936868262950128000 + +-- !cast_float_to_decimal256_const_10_2 -- +-340200000000000000000000000000000000000 + +-- !cast_float_to_decimal256_1 -- +0 220 +1 -220 +2 99999996802856924654458622116917936128 +3 -99999996802856924654458622116917936128 +4 9999999848243207295117249609728 +5 -9999999848243207295117249609728 +6 1 +7 -1 +8 1 +9 -1 +10 340200000055538034029787422761016098815 +11 -340200000055538034029787422761016098815 + +-- !cast_float_to_decimal256_2 -- +0 219.77717590332030098495757448650589208640 +1 -219.77717590332030098495757448650589208640 +2 99999996802856929887877846998705567504.08319368450610608065324841755887360000 +3 -99999996802856929887877846998705567504.08319368450610608065324841755887360000 +4 9999999848243206484209837188033.94655282897607351708798609489962906025 +5 -9999999848243206484209837188033.94655282897607351708798609489962906025 +6 0.99999999999999997752612184630461283328 +7 -0.99999999999999997752612184630461283328 +8 0.97718000411987307697345873505444123648 +9 -0.97718000411987307697345873505444123648 + +-- !cast_float_to_decimal256_3 -- +0 219.777175903320326853463377056255521940905600575 +1 -219.777175903320326853463377056255521940905600575 +4 9999999848243207687231096732081.517441445022649282668861247346191519797952000 +5 -9999999848243207687231096732081.517441445022649282668861247346191519797952000 +6 0.999999999999999929648868823465823162872944495 +7 -0.999999999999999929648868823465823162872944495 +8 0.977180004119873082053229390165939041981944185 +9 0.977180004119873082053229390165939041981944185 + +-- !cast_float_to_decimal256_4 -- +6 0.8999999761581421686088389507648326470906229208913952822970533869391577408000 +7 -0.8999999761581421686088389507648326470906229208913952822970533869391577408000 + +-- !cast_double_to_decimal256_const_1_1 -- +219.77717999999997235813377081619481624640 + +-- !cast_double_to_decimal256_const_1_2 -- +219.77718000000000000000000000000000000000 + +-- !cast_double_to_decimal256_const_2_1 -- +-219.77717999999997235813377081619481624640 + +-- !cast_double_to_decimal256_const_2_2 -- +-219.77718000000000000000000000000000000000 + +-- !cast_double_to_decimal256_const_3_1 -- +100000000000000004689750417003264391641.02534195992280967396974317317789248000 + +-- !cast_double_to_decimal256_const_3_2 -- +100000000000000004689750417003264391641.02534195992280967396974317317789248000 + +-- !cast_double_to_decimal256_const_4_1 -- +-100000000000000004689750417003264391641.02534195992280967396974317317789248000 + +-- !cast_double_to_decimal256_const_4_2 -- +-100000000000000004689750417003264391641.02534195992280967396974317317789248000 + +-- !cast_double_to_decimal256_const_5_1 -- +9999999999999998862036997441336.163883477299650110418370898579593514004704000 + +-- !cast_double_to_decimal256_const_5_2 -- +9999999999999998862036997441336.163883477299650110418370898579593514004704000 + +-- !cast_double_to_decimal256_const_6_1 -- +-9999999999999998862036997441336.163883477299650110418370898579593514004704000 + +-- !cast_double_to_decimal256_const_6_2 -- +-9999999999999998862036997441336.163883477299650110418370898579593514004704000 + +-- !cast_double_to_decimal256_const_7_1 -- +0.9899999999999999532261225613108815109698872817420877851753935634338416320000 + +-- !cast_double_to_decimal256_const_7_2 -- +0.9900000000000000000000000000000000000000000000000000000000000000000000000000 + +-- !cast_double_to_decimal256_const_8_1 -- +-0.9899999999999999532261225613108815109698872817420877851753935634338416320000 + +-- !cast_double_to_decimal256_const_8_2 -- +-0.9900000000000000000000000000000000000000000000000000000000000000000000000000 + +-- !cast_double_to_decimal256_const_9_1 -- +9898999999999999715726652763315471902829864349140987332177312614365076736000 + +-- !cast_double_to_decimal256_const_9_2 -- +9899000000000000000000000000000000000000000000000000000000000000000000000000 + +-- !cast_double_to_decimal256_const_10_1 -- +-9898999999999999715726652763315471902829864349140987332177312614365076736000 + +-- !cast_double_to_decimal256_const_10_2 -- +-9899000000000000000000000000000000000000000000000000000000000000000000000000 + +-- !cast_double_to_decimal256_1 -- +0 220 +1 -220 +2 99999999999999997752612184630461283328 +3 -99999999999999997752612184630461283328 +4 9999999999999999635903949692894 +5 -9999999999999999635903949692894 +6 1 +7 -1 +8 1 +9 -1 +10 340199999999999981876691358478025097215 +11 -340199999999999981876691358478025097215 +12 9898999999999999715726652763315471902829864349140987332177312614365076736000 +13 9898999999999999715726652763315471902829864349140987332177312614365076736000 + +-- !cast_double_to_decimal256_2 -- +0 219.77717999999997235813377081619481624640 +1 -219.77717999999997235813377081619481624640 +2 100000000000000004689750417003264391641.02534195992280967396974317317789248000 +3 -100000000000000004689750417003264391641.02534195992280967396974317317789248000 +4 9999999999999998808068634474203.85625841180974495274405359169525688375 +5 -9999999999999998808068634474203.85625841180974495274405359169525688375 +6 0.99999999999999997752612184630461283328 +7 -0.99999999999999997752612184630461283328 +8 0.97718000000000008819311215209494821888 +9 -0.97718000000000008819311215209494821888 + +-- !cast_double_to_decimal256_3 -- +0 219.777179999999995742093914318825305628536153675 +1 -219.777179999999995742093914318825305628536153675 +4 9999999999999998862036997441336.163883477299650110418370898579593514004704000 +5 -9999999999999998862036997441336.163883477299650110418370898579593514004704000 +6 0.999999999999999929648868823465823162872944495 +7 -0.999999999999999929648868823465823162872944495 +8 0.977180000000000074669932294976532285011775865 +9 0.977180000000000074669932294976532285011775865 + +-- !cast_double_to_decimal256_4 -- +6 0.9000000000000000743465146382091850303817327685569425389956955830346767232000 +7 -0.9000000000000000743465146382091850303817327685569425389956955830346767232000 + diff --git a/regression-test/data/nereids_arith_p0/decimal.out b/regression-test/data/nereids_arith_p0/decimal.out index 7b0e2bc82b..8be58f2ff1 100644 --- a/regression-test/data/nereids_arith_p0/decimal.out +++ b/regression-test/data/nereids_arith_p0/decimal.out @@ -1538,109 +1538,109 @@ -- !sql_test_Float_DecimalV2_0 -- \N \N \N -1 24.495000000 -24.295000000 -2 34.684000000 -34.284000000 -3 49.056000000 -48.456000000 -4 69.343000000 -68.543000000 +1 24.495000001 -24.294999999 +2 34.684000003 -34.283999997 +3 49.056000012 -48.455999988 +4 69.343000006 -68.542999994 5 97.994000000 -96.994000000 -6 138.474000000 -137.274000000 -7 195.680000000 -194.280000000 -8 276.541000000 -274.941000000 -9 390.855000000 -389.055000000 +6 138.474000024 -137.273999976 +7 195.679999988 -194.280000012 +8 276.541000012 -274.940999988 +9 390.854999976 -389.055000024 10 552.479000000 -550.479000000 -11 781.008000000 -778.808000000 -12 1104.157000000 -1101.757000000 -13 24.495000000 -24.295000000 -14 34.684000000 -34.284000000 -15 49.056000000 -48.456000000 -16 69.343000000 -68.543000000 +11 781.008000024 -778.807999976 +12 1104.157000048 -1101.756999952 +13 24.495000001 -24.294999999 +14 34.684000003 -34.283999997 +15 49.056000012 -48.455999988 +16 69.343000006 -68.542999994 17 97.994000000 -96.994000000 -18 138.474000000 -137.274000000 -19 195.680000000 -194.280000000 -20 276.541000000 -274.941000000 -21 390.855000000 -389.055000000 +18 138.474000024 -137.273999976 +19 195.679999988 -194.280000012 +20 276.541000012 -274.940999988 +21 390.854999976 -389.055000024 22 552.479000000 -550.479000000 -23 781.008000000 -778.808000000 -24 1104.157000000 -1101.757000000 +23 781.008000024 -778.807999976 +24 1104.157000048 -1101.756999952 -- !sql_test_Float_DecimalV2_notn_0 -- -1 24.495000000 -24.295000000 -2 34.684000000 -34.284000000 -3 49.056000000 -48.456000000 -4 69.343000000 -68.543000000 +1 24.495000001 -24.294999999 +2 34.684000003 -34.283999997 +3 49.056000012 -48.455999988 +4 69.343000006 -68.542999994 5 97.994000000 -96.994000000 -6 138.474000000 -137.274000000 -7 195.680000000 -194.280000000 -8 276.541000000 -274.941000000 -9 390.855000000 -389.055000000 +6 138.474000024 -137.273999976 +7 195.679999988 -194.280000012 +8 276.541000012 -274.940999988 +9 390.854999976 -389.055000024 10 552.479000000 -550.479000000 -11 781.008000000 -778.808000000 -12 1104.157000000 -1101.757000000 -13 24.495000000 -24.295000000 -14 34.684000000 -34.284000000 -15 49.056000000 -48.456000000 -16 69.343000000 -68.543000000 +11 781.008000024 -778.807999976 +12 1104.157000048 -1101.756999952 +13 24.495000001 -24.294999999 +14 34.684000003 -34.283999997 +15 49.056000012 -48.455999988 +16 69.343000006 -68.542999994 17 97.994000000 -96.994000000 -18 138.474000000 -137.274000000 -19 195.680000000 -194.280000000 -20 276.541000000 -274.941000000 -21 390.855000000 -389.055000000 +18 138.474000024 -137.273999976 +19 195.679999988 -194.280000012 +20 276.541000012 -274.940999988 +21 390.854999976 -389.055000024 22 552.479000000 -550.479000000 -23 781.008000000 -778.808000000 -24 1104.157000000 -1101.757000000 +23 781.008000024 -778.807999976 +24 1104.157000048 -1101.756999952 -- !sql_test_Float_DecimalV2_1 -- \N \N \N \N -1 2.439500000 0.0040992007169549545 0.100000000 -2 6.896800000 0.005799791293940153 0.200000000 -3 14.626800000 0.0061530890951047865 0.300000000 -4 27.577200000 0.005801894404949951 0.400000000 +1 2.439500025 0.0040992007169549545 0.100000001 +2 6.896800104 0.005799791293940153 0.200000003 +3 14.626800586 0.0061530890951047865 0.300000012 +4 27.577200414 0.005801894404949951 0.400000006 5 48.747000000 0.005128520729480788 0.500000000 -6 82.724400000 0.004351799642005439 0.600000000 -7 136.486000000 0.0035901117451998723 0.700000000 -8 220.592800000 0.0029012733395502627 0.800000000 -9 350.959500000 0.002307958549468893 0.900000000 +6 82.724403309 0.004351799642005439 0.600000024 +7 136.485997661 0.0035901117451998723 0.699999988 +8 220.592803309 0.0029012733395502627 0.800000012 +9 350.959490642 0.002307958549468893 0.899999976 10 551.479000000 0.0018133056743774468 1.000000000 -11 857.898800000 0.0014104227983837297 1.100000000 -12 1323.548400000 0.0010879844342832183 1.200000000 -13 2.439500000 0.0040992007169549545 0.100000000 -14 6.896800000 0.005799791293940153 0.200000000 -15 14.626800000 0.0061530890951047865 0.300000000 -16 27.577200000 0.005801894404949951 0.400000000 +11 857.898818718 0.0014104227983837297 1.100000024 +12 1323.548452942 0.0010879844342832183 1.200000048 +13 2.439500025 0.0040992007169549545 0.100000001 +14 6.896800104 0.005799791293940153 0.200000003 +15 14.626800586 0.0061530890951047865 0.300000012 +16 27.577200414 0.005801894404949951 0.400000006 17 48.747000000 0.005128520729480788 0.500000000 -18 82.724400000 0.004351799642005439 0.600000000 -19 136.486000000 0.0035901117451998723 0.700000000 -20 220.592800000 0.0029012733395502627 0.800000000 -21 350.959500000 0.002307958549468893 0.900000000 +18 82.724403309 0.004351799642005439 0.600000024 +19 136.485997661 0.0035901117451998723 0.699999988 +20 220.592803309 0.0029012733395502627 0.800000012 +21 350.959490642 0.002307958549468893 0.899999976 22 551.479000000 0.0018133056743774468 1.000000000 -23 857.898800000 0.0014104227983837297 1.100000000 -24 1323.548400000 0.0010879844342832183 1.200000000 +23 857.898818718 0.0014104227983837297 1.100000024 +24 1323.548452942 0.0010879844342832183 1.200000048 -- !sql_test_Float_DecimalV2_notn_1 -- -1 2.439500000 0.0040992007169549545 0.100000000 -2 6.896800000 0.005799791293940153 0.200000000 -3 14.626800000 0.0061530890951047865 0.300000000 -4 27.577200000 0.005801894404949951 0.400000000 +1 2.439500025 0.0040992007169549545 0.100000001 +2 6.896800104 0.005799791293940153 0.200000003 +3 14.626800586 0.0061530890951047865 0.300000012 +4 27.577200414 0.005801894404949951 0.400000006 5 48.747000000 0.005128520729480788 0.500000000 -6 82.724400000 0.004351799642005439 0.600000000 -7 136.486000000 0.0035901117451998723 0.700000000 -8 220.592800000 0.0029012733395502627 0.800000000 -9 350.959500000 0.002307958549468893 0.900000000 +6 82.724403309 0.004351799642005439 0.600000024 +7 136.485997661 0.0035901117451998723 0.699999988 +8 220.592803309 0.0029012733395502627 0.800000012 +9 350.959490642 0.002307958549468893 0.899999976 10 551.479000000 0.0018133056743774468 1.000000000 -11 857.898800000 0.0014104227983837297 1.100000000 -12 1323.548400000 0.0010879844342832183 1.200000000 -13 2.439500000 0.0040992007169549545 0.100000000 -14 6.896800000 0.005799791293940153 0.200000000 -15 14.626800000 0.0061530890951047865 0.300000000 -16 27.577200000 0.005801894404949951 0.400000000 +11 857.898818718 0.0014104227983837297 1.100000024 +12 1323.548452942 0.0010879844342832183 1.200000048 +13 2.439500025 0.0040992007169549545 0.100000001 +14 6.896800104 0.005799791293940153 0.200000003 +15 14.626800586 0.0061530890951047865 0.300000012 +16 27.577200414 0.005801894404949951 0.400000006 17 48.747000000 0.005128520729480788 0.500000000 -18 82.724400000 0.004351799642005439 0.600000000 -19 136.486000000 0.0035901117451998723 0.700000000 -20 220.592800000 0.0029012733395502627 0.800000000 -21 350.959500000 0.002307958549468893 0.900000000 +18 82.724403309 0.004351799642005439 0.600000024 +19 136.485997661 0.0035901117451998723 0.699999988 +20 220.592803309 0.0029012733395502627 0.800000012 +21 350.959490642 0.002307958549468893 0.899999976 22 551.479000000 0.0018133056743774468 1.000000000 -23 857.898800000 0.0014104227983837297 1.100000000 -24 1323.548400000 0.0010879844342832183 1.200000000 +23 857.898818718 0.0014104227983837297 1.100000024 +24 1323.548452942 0.0010879844342832183 1.200000048 -- !sql_test_Float_DecimalV2_2 -- \N \N @@ -8852,109 +8852,109 @@ -- !sql_test_DecimalV2_Float_0 -- \N \N \N -1 24.495000000 24.295000000 -2 34.684000000 34.284000000 -3 49.056000000 48.456000000 -4 69.343000000 68.543000000 +1 24.495000001 24.294999999 +2 34.684000003 34.283999997 +3 49.056000012 48.455999988 +4 69.343000006 68.542999994 5 97.994000000 96.994000000 -6 138.474000000 137.274000000 -7 195.680000000 194.280000000 -8 276.541000000 274.941000000 -9 390.855000000 389.055000000 +6 138.474000024 137.273999976 +7 195.679999988 194.280000012 +8 276.541000012 274.940999988 +9 390.854999976 389.055000024 10 552.479000000 550.479000000 -11 781.008000000 778.808000000 -12 1104.157000000 1101.757000000 -13 24.495000000 24.295000000 -14 34.684000000 34.284000000 -15 49.056000000 48.456000000 -16 69.343000000 68.543000000 +11 781.008000024 778.807999976 +12 1104.157000048 1101.756999952 +13 24.495000001 24.294999999 +14 34.684000003 34.283999997 +15 49.056000012 48.455999988 +16 69.343000006 68.542999994 17 97.994000000 96.994000000 -18 138.474000000 137.274000000 -19 195.680000000 194.280000000 -20 276.541000000 274.941000000 -21 390.855000000 389.055000000 +18 138.474000024 137.273999976 +19 195.679999988 194.280000012 +20 276.541000012 274.940999988 +21 390.854999976 389.055000024 22 552.479000000 550.479000000 -23 781.008000000 778.808000000 -24 1104.157000000 1101.757000000 +23 781.008000024 778.807999976 +24 1104.157000048 1101.756999952 -- !sql_test_DecimalV2_Float_notn_0 -- -1 24.495000000 24.295000000 -2 34.684000000 34.284000000 -3 49.056000000 48.456000000 -4 69.343000000 68.543000000 +1 24.495000001 24.294999999 +2 34.684000003 34.283999997 +3 49.056000012 48.455999988 +4 69.343000006 68.542999994 5 97.994000000 96.994000000 -6 138.474000000 137.274000000 -7 195.680000000 194.280000000 -8 276.541000000 274.941000000 -9 390.855000000 389.055000000 +6 138.474000024 137.273999976 +7 195.679999988 194.280000012 +8 276.541000012 274.940999988 +9 390.854999976 389.055000024 10 552.479000000 550.479000000 -11 781.008000000 778.808000000 -12 1104.157000000 1101.757000000 -13 24.495000000 24.295000000 -14 34.684000000 34.284000000 -15 49.056000000 48.456000000 -16 69.343000000 68.543000000 +11 781.008000024 778.807999976 +12 1104.157000048 1101.756999952 +13 24.495000001 24.294999999 +14 34.684000003 34.283999997 +15 49.056000012 48.455999988 +16 69.343000006 68.542999994 17 97.994000000 96.994000000 -18 138.474000000 137.274000000 -19 195.680000000 194.280000000 -20 276.541000000 274.941000000 -21 390.855000000 389.055000000 +18 138.474000024 137.273999976 +19 195.679999988 194.280000012 +20 276.541000012 274.940999988 +21 390.854999976 389.055000024 22 552.479000000 550.479000000 -23 781.008000000 778.808000000 -24 1104.157000000 1101.757000000 +23 781.008000024 778.807999976 +24 1104.157000048 1101.756999952 -- !sql_test_DecimalV2_Float_1 -- \N \N \N \N -1 2.439500000 243.9499963648618 0.095000000 -2 6.896800000 172.41999743074183 0.084000000 -3 14.626800000 162.51999354203568 0.156000000 -4 27.577200000 172.35749743167315 0.143000000 +1 2.439500025 243.9499963648618 0.094999757 +2 6.896800104 172.41999743074183 0.083999484 +3 14.626800586 162.51999354203568 0.155998056 +4 27.577200414 172.35749743167315 0.142998968 5 48.747000000 194.988 0.494000000 -6 82.724400000 229.78999086896613 0.474000000 -7 136.486000000 278.5428618864138 0.380000000 -8 220.592800000 344.67624486392367 0.541000000 -9 350.959500000 433.2833448114221 0.255000000 +6 82.724403309 229.78999086896613 0.473994504 +7 136.485997661 278.5428618864138 0.380003336 +8 220.592803309 344.67624486392367 0.540995872 +9 350.959490642 433.2833448114221 0.255010392 10 551.479000000 551.479 0.479000000 -11 857.898800000 709.0072573599543 0.008000000 -12 1323.548400000 919.130796810357 0.157000000 -13 2.439500000 243.9499963648618 0.095000000 -14 6.896800000 172.41999743074183 0.084000000 -15 14.626800000 162.51999354203568 0.156000000 -16 27.577200000 172.35749743167315 0.143000000 +11 857.898818718 709.0072573599543 0.007982984 +12 1323.548452942 919.130796810357 0.156955888 +13 2.439500025 243.9499963648618 0.094999757 +14 6.896800104 172.41999743074183 0.083999484 +15 14.626800586 162.51999354203568 0.155998056 +16 27.577200414 172.35749743167315 0.142998968 17 48.747000000 194.988 0.494000000 -18 82.724400000 229.78999086896613 0.474000000 -19 136.486000000 278.5428618864138 0.380000000 -20 220.592800000 344.67624486392367 0.541000000 -21 350.959500000 433.2833448114221 0.255000000 +18 82.724403309 229.78999086896613 0.473994504 +19 136.485997661 278.5428618864138 0.380003336 +20 220.592803309 344.67624486392367 0.540995872 +21 350.959490642 433.2833448114221 0.255010392 22 551.479000000 551.479 0.479000000 -23 857.898800000 709.0072573599543 0.008000000 -24 1323.548400000 919.130796810357 0.157000000 +23 857.898818718 709.0072573599543 0.007982984 +24 1323.548452942 919.130796810357 0.156955888 -- !sql_test_DecimalV2_Float_notn_1 -- -1 2.439500000 243.9499963648618 0.095000000 -2 6.896800000 172.41999743074183 0.084000000 -3 14.626800000 162.51999354203568 0.156000000 -4 27.577200000 172.35749743167315 0.143000000 +1 2.439500025 243.9499963648618 0.094999757 +2 6.896800104 172.41999743074183 0.083999484 +3 14.626800586 162.51999354203568 0.155998056 +4 27.577200414 172.35749743167315 0.142998968 5 48.747000000 194.988 0.494000000 -6 82.724400000 229.78999086896613 0.474000000 -7 136.486000000 278.5428618864138 0.380000000 -8 220.592800000 344.67624486392367 0.541000000 -9 350.959500000 433.2833448114221 0.255000000 +6 82.724403309 229.78999086896613 0.473994504 +7 136.485997661 278.5428618864138 0.380003336 +8 220.592803309 344.67624486392367 0.540995872 +9 350.959490642 433.2833448114221 0.255010392 10 551.479000000 551.479 0.479000000 -11 857.898800000 709.0072573599543 0.008000000 -12 1323.548400000 919.130796810357 0.157000000 -13 2.439500000 243.9499963648618 0.095000000 -14 6.896800000 172.41999743074183 0.084000000 -15 14.626800000 162.51999354203568 0.156000000 -16 27.577200000 172.35749743167315 0.143000000 +11 857.898818718 709.0072573599543 0.007982984 +12 1323.548452942 919.130796810357 0.156955888 +13 2.439500025 243.9499963648618 0.094999757 +14 6.896800104 172.41999743074183 0.083999484 +15 14.626800586 162.51999354203568 0.155998056 +16 27.577200414 172.35749743167315 0.142998968 17 48.747000000 194.988 0.494000000 -18 82.724400000 229.78999086896613 0.474000000 -19 136.486000000 278.5428618864138 0.380000000 -20 220.592800000 344.67624486392367 0.541000000 -21 350.959500000 433.2833448114221 0.255000000 +18 82.724403309 229.78999086896613 0.473994504 +19 136.485997661 278.5428618864138 0.380003336 +20 220.592803309 344.67624486392367 0.540995872 +21 350.959490642 433.2833448114221 0.255010392 22 551.479000000 551.479 0.479000000 -23 857.898800000 709.0072573599543 0.008000000 -24 1323.548400000 919.130796810357 0.157000000 +23 857.898818718 709.0072573599543 0.007982984 +24 1323.548452942 919.130796810357 0.156955888 -- !sql_test_DecimalV2_Float_2 -- \N \N diff --git a/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy b/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy index 64c5d87cac..2b04f2c5ea 100644 --- a/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy +++ b/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy @@ -154,4 +154,338 @@ suite("test_decimal256_cast") { exception "Arithmetic overflow" } + // cast float to decimal256 + qt_cast_float_to_decimal256_const_1_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("219.77718" as float) as decimalv3(76, 38));""" + qt_cast_float_to_decimal256_const_1_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("219.77718" as float) as decimalv3(76, 38));""" + + qt_cast_float_to_decimal256_const_2_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-219.77718" as float) as decimalv3(76, 38));""" + qt_cast_float_to_decimal256_const_2_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-219.77718" as float) as decimalv3(76, 38));""" + + qt_cast_float_to_decimal256_const_3_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("99999999999999999999999999999999999998.99999999999999999999999999999999999999" as float) as decimalv3(76, 38));""" + qt_cast_float_to_decimal256_const_3_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("99999999999999999999999999999999999998.99999999999999999999999999999999999999" as float) as decimalv3(76, 38));""" + + qt_cast_float_to_decimal256_const_4_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-99999999999999999999999999999999999998.99999999999999999999999999999999999999" as float) as decimalv3(76, 38));""" + qt_cast_float_to_decimal256_const_4_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-99999999999999999999999999999999999998.99999999999999999999999999999999999999" as float) as decimalv3(76, 38));""" + + qt_cast_float_to_decimal256_const_5_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("9999999999999999999999999999998.999999999999999999999999999999999999999999999" as float) as decimalv3(76, 45));""" + qt_cast_float_to_decimal256_const_5_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("9999999999999999999999999999998.999999999999999999999999999999999999999999999" as float) as decimalv3(76, 45));""" + + qt_cast_float_to_decimal256_const_6_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-9999999999999999999999999999998.999999999999999999999999999999999999999999999" as float) as decimalv3(76, 45));""" + qt_cast_float_to_decimal256_const_6_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-9999999999999999999999999999998.999999999999999999999999999999999999999999999" as float) as decimalv3(76, 45));""" + + qt_cast_float_to_decimal256_const_7_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("0.8999999999999999999999999999999999999999999999999999999999999999999999999999" as float) as decimalv3(76, 76));""" + qt_cast_float_to_decimal256_const_7_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("0.8999999999999999999999999999999999999999999999999999999999999999999999999999" as float) as decimalv3(76, 76));""" + + qt_cast_float_to_decimal256_const_8_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-0.8999999999999999999999999999999999999999999999999999999999999999999999999999" as float) as decimalv3(76, 76));""" + qt_cast_float_to_decimal256_const_8_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-0.8999999999999999999999999999999999999999999999999999999999999999999999999999" as float) as decimalv3(76, 76));""" + + qt_cast_float_to_decimal256_const_9_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("3.402e38" as float) as decimalv3(76, 37));""" + qt_cast_float_to_decimal256_const_9_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("3.402e38" as float) as decimalv3(76, 37));""" + qt_cast_float_to_decimal256_const_10_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-3.402e38" as float) as decimalv3(76, 37));""" + qt_cast_float_to_decimal256_const_10_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-3.402e38" as float) as decimalv3(76, 37));""" + + sql """ + drop table if exists cast_to_float_to_decimal256; + """ + sql """ + create table cast_to_float_to_decimal256 ( + k1 int, + v1 float + ) properties ('replication_num' = '1'); + """ + sql """ + insert into cast_to_float_to_decimal256 values + (0, "219.77718"), (1, "-219.77718"), + (2, "99999999999999999999999999999999999998.99999999999999999999999999999999999999"), + (3, "-99999999999999999999999999999999999998.99999999999999999999999999999999999999"), + (4, "9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (5, "-9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (6, "0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (7, "-0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (8, "0.97718"), + (9, "-0.97718"), + (10, "3.402e38"), + (11, "-3.402e38"); + """ + qt_cast_float_to_decimal256_1 """ + select k1, cast(v1 as decimalv3(76, 0)) from cast_to_float_to_decimal256 order by k1; + """ + + sql """ + drop table if exists cast_to_float_to_decimal256; + """ + sql """ + create table cast_to_float_to_decimal256 ( + k1 int, + v1 float + ) properties ('replication_num' = '1'); + """ + sql """ + insert into cast_to_float_to_decimal256 values + (0, "219.77718"), (1, "-219.77718"), + (2, "99999999999999999999999999999999999998.99999999999999999999999999999999999999"), + (3, "-99999999999999999999999999999999999998.99999999999999999999999999999999999999"), + (4, "9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (5, "-9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (6, "0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (7, "-0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (8, "0.97718"), + (9, "-0.97718"); + """ + qt_cast_float_to_decimal256_2 """ + select k1, cast(v1 as decimalv3(76, 38)) from cast_to_float_to_decimal256 order by k1; + """ + + sql """ + drop table if exists cast_to_float_to_decimal256; + """ + sql """ + create table cast_to_float_to_decimal256 ( + k1 int, + v1 float + ) properties ('replication_num' = '1'); + """ + sql """ + insert into cast_to_float_to_decimal256 values + (0, "219.77718"), (1, "-219.77718"), + (4, "9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (5, "-9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (6, "0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (7, "-0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (8, "0.97718"), + (9, "0.97718"); + """ + qt_cast_float_to_decimal256_3 """ + select k1, cast(v1 as decimalv3(76, 45)) from cast_to_float_to_decimal256 order by k1; + """ + + sql """ + drop table if exists cast_to_float_to_decimal256; + """ + sql """ + create table cast_to_float_to_decimal256 ( + k1 int, + v1 float + ) properties ('replication_num' = '1'); + """ + sql """ + insert into cast_to_float_to_decimal256 values + (6, "0.8999999999999999999999999999999999999999999999999999999999999999999999999999"), + (7, "-0.8999999999999999999999999999999999999999999999999999999999999999999999999999"); + """ + qt_cast_float_to_decimal256_4 """ + select k1, cast(v1 as decimalv3(76, 76)) from cast_to_float_to_decimal256 order by k1; + """ + + // cast double to decimal256 + qt_cast_double_to_decimal256_const_1_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("219.77718" as double) as decimalv3(76, 38));""" + qt_cast_double_to_decimal256_const_1_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("219.77718" as double) as decimalv3(76, 38));""" + + qt_cast_double_to_decimal256_const_2_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-219.77718" as double) as decimalv3(76, 38));""" + qt_cast_double_to_decimal256_const_2_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-219.77718" as double) as decimalv3(76, 38));""" + + qt_cast_double_to_decimal256_const_3_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("99999999999999999999999999999999999998.99999999999999999999999999999999999999" as double) as decimalv3(76, 38));""" + qt_cast_double_to_decimal256_const_3_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("99999999999999999999999999999999999998.99999999999999999999999999999999999999" as double) as decimalv3(76, 38));""" + + qt_cast_double_to_decimal256_const_4_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-99999999999999999999999999999999999998.99999999999999999999999999999999999999" as double) as decimalv3(76, 38));""" + qt_cast_double_to_decimal256_const_4_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-99999999999999999999999999999999999998.99999999999999999999999999999999999999" as double) as decimalv3(76, 38));""" + + qt_cast_double_to_decimal256_const_5_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("9999999999999999999999999999998.999999999999999999999999999999999999999999999" as double) as decimalv3(76, 45));""" + qt_cast_double_to_decimal256_const_5_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("9999999999999999999999999999998.999999999999999999999999999999999999999999999" as double) as decimalv3(76, 45));""" + + qt_cast_double_to_decimal256_const_6_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-9999999999999999999999999999998.999999999999999999999999999999999999999999999" as double) as decimalv3(76, 45));""" + qt_cast_double_to_decimal256_const_6_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-9999999999999999999999999999998.999999999999999999999999999999999999999999999" as double) as decimalv3(76, 45));""" + + qt_cast_double_to_decimal256_const_7_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("0.9899999999999999999999999999999999999999999999999999999999999999999999999999" as double) as decimalv3(76, 76));""" + qt_cast_double_to_decimal256_const_7_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("0.9899999999999999999999999999999999999999999999999999999999999999999999999999" as double) as decimalv3(76, 76));""" + + qt_cast_double_to_decimal256_const_8_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-0.9899999999999999999999999999999999999999999999999999999999999999999999999999" as double) as decimalv3(76, 76));""" + qt_cast_double_to_decimal256_const_8_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-0.9899999999999999999999999999999999999999999999999999999999999999999999999999" as double) as decimalv3(76, 76));""" + + qt_cast_double_to_decimal256_const_9_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("9.899e75" as double) as decimalv3(76, 0));""" + qt_cast_double_to_decimal256_const_9_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("9.899e75" as double) as decimalv3(76, 0));""" + qt_cast_double_to_decimal256_const_10_1 """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-9.899e75" as double) as decimalv3(76, 0));""" + qt_cast_double_to_decimal256_const_10_2 """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-9.899e75" as double) as decimalv3(76, 0));""" + + sql """ + drop table if exists cast_to_double_to_decimal256; + """ + sql """ + create table cast_to_double_to_decimal256 ( + k1 int, + v1 double + ) properties ('replication_num' = '1'); + """ + sql """ + insert into cast_to_double_to_decimal256 values + (0, "219.77718"), (1, "-219.77718"), + (2, "99999999999999999999999999999999999998.99999999999999999999999999999999999999"), + (3, "-99999999999999999999999999999999999998.99999999999999999999999999999999999999"), + (4, "9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (5, "-9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (6, "0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (7, "-0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (8, "0.97718"), + (9, "-0.97718"), + (10, "3.402e38"), + (11, "-3.402e38"), + (12, "9.899e75"), + (13, "9.899e75"); + """ + qt_cast_double_to_decimal256_1 """ + select k1, cast(v1 as decimalv3(76, 0)) from cast_to_double_to_decimal256 order by k1; + """ + + sql """ + drop table if exists cast_to_double_to_decimal256; + """ + sql """ + create table cast_to_double_to_decimal256 ( + k1 int, + v1 double + ) properties ('replication_num' = '1'); + """ + sql """ + insert into cast_to_double_to_decimal256 values + (0, "219.77718"), (1, "-219.77718"), + (2, "99999999999999999999999999999999999998.99999999999999999999999999999999999999"), + (3, "-99999999999999999999999999999999999998.99999999999999999999999999999999999999"), + (4, "9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (5, "-9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (6, "0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (7, "-0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (8, "0.97718"), + (9, "-0.97718"); + """ + qt_cast_double_to_decimal256_2 """ + select k1, cast(v1 as decimalv3(76, 38)) from cast_to_double_to_decimal256 order by k1; + """ + + sql """ + drop table if exists cast_to_double_to_decimal256; + """ + sql """ + create table cast_to_double_to_decimal256 ( + k1 int, + v1 double + ) properties ('replication_num' = '1'); + """ + sql """ + insert into cast_to_double_to_decimal256 values + (0, "219.77718"), (1, "-219.77718"), + (4, "9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (5, "-9999999999999999999999999999998.999999999999999999999999999999999999999999999"), + (6, "0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (7, "-0.9999999999999999999999999999999999999999999999999999999999999999999999999999"), + (8, "0.97718"), + (9, "0.97718"); + """ + qt_cast_double_to_decimal256_3 """ + select k1, cast(v1 as decimalv3(76, 45)) from cast_to_double_to_decimal256 order by k1; + """ + + sql """ + drop table if exists cast_to_double_to_decimal256; + """ + sql """ + create table cast_to_double_to_decimal256 ( + k1 int, + v1 double + ) properties ('replication_num' = '1'); + """ + sql """ + insert into cast_to_double_to_decimal256 values + (6, "0.8999999999999999999999999999999999999999999999999999999999999999999999999999"), + (7, "-0.8999999999999999999999999999999999999999999999999999999999999999999999999999"); + """ + qt_cast_double_to_decimal256_4 """ + select k1, cast(v1 as decimalv3(76, 76)) from cast_to_double_to_decimal256 order by k1; + """ + + // test cast float to decimal256 overflow + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("219.77718" as float) as decimalv3(76, 74));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("219.77718" as float) as decimalv3(76, 74));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-219.77718" as float) as decimalv3(76, 74));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-219.77718" as float) as decimalv3(76, 74));""" + exception "Arithmetic overflow" + } + + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("3.402e38" as float) as decimalv3(76, 38));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("3.402e38" as float) as decimalv3(76, 38));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-3.402e38" as float) as decimalv3(76, 38));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-3.402e38" as float) as decimalv3(76, 38));""" + exception "Arithmetic overflow" + } + + // test cast double to decimal256 overflow + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("219.77718" as double) as decimalv3(76, 74));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("219.77718" as double) as decimalv3(76, 74));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-219.77718" as double) as decimalv3(76, 74));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-219.77718" as double) as decimalv3(76, 74));""" + exception "Arithmetic overflow" + } + + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("3.402e38" as double) as decimalv3(76, 38));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("3.402e38" as double) as decimalv3(76, 38));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-3.402e38" as double) as decimalv3(76, 38));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-3.402e38" as double) as decimalv3(76, 38));""" + exception "Arithmetic overflow" + } + + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("9.899e76" as double) as decimalv3(76, 0));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("9.899e76" as double) as decimalv3(76, 0));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("-9.899e76" as double) as decimalv3(76, 0));""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("-9.899e76" as double) as decimalv3(76, 0));""" + exception "Arithmetic overflow" + } } \ No newline at end of file