diff --git a/src/query/functions/src/scalars/decimal/src/cast.rs b/src/query/functions/src/scalars/decimal/src/cast.rs index dd37b51fb76db..43058a364c709 100644 --- a/src/query/functions/src/scalars/decimal/src/cast.rs +++ b/src/query/functions/src/scalars/decimal/src/cast.rs @@ -94,6 +94,14 @@ pub fn register_to_decimal(registry: &mut FunctionRegistry) { ], false, ), + (3, false, false) => ( + vec![ + from_type.clone(), + DataType::Number(NumberDataType::Int64), + DataType::Number(NumberDataType::Int64), + ], + false, + ), (4, true, true) => ( vec![ DataType::String, diff --git a/src/query/functions/tests/it/scalars/cast.rs b/src/query/functions/tests/it/scalars/cast.rs index 75cd99a8c0dee..cc56f91e3fddc 100644 --- a/src/query/functions/tests/it/scalars/cast.rs +++ b/src/query/functions/tests/it/scalars/cast.rs @@ -40,6 +40,7 @@ fn test_cast() { test_cast_between_number_and_string(file, is_try); test_cast_between_boolean_and_string(file, is_try); test_cast_between_string_and_decimal(file, is_try); + test_to_decimal_with_precision_args(file, is_try); test_cast_between_number_and_boolean(file, is_try); test_cast_between_date_and_timestamp(file, is_try); test_cast_between_string_and_timestamp(file, is_try); @@ -758,6 +759,55 @@ fn test_cast_between_binary_and_string(file: &mut impl Write, is_try: bool) { ); } +fn test_to_decimal_with_precision_args(file: &mut impl Write, is_try: bool) { + let func_name = if is_try { + "try_to_decimal" + } else { + "to_decimal" + }; + + // Constant numeric inputs with explicit precision/scale arguments. + run_ast(file, format!("{func_name}(10, 3)(123.456)"), &[]); + run_ast(file, format!("{func_name}(10, 3)(-123.456)"), &[]); + run_ast(file, format!("{func_name}(12, 1)(-9876.5)"), &[]); + run_ast(file, format!("{func_name}(6, 2)(5)"), &[]); + run_ast(file, format!("{func_name}(6, 2)(-5)"), &[]); + run_ast(file, format!("{func_name}(6, 2)(true)"), &[]); + run_ast(file, format!("{func_name}(10, 2)(123.45::FLOAT32)"), &[]); + run_ast(file, format!("{func_name}(10, 2)(-123.45::FLOAT32)"), &[]); + run_ast( + file, + format!("{func_name}(12, 4)(123.456::DECIMAL(9,3))"), + &[], + ); + run_ast( + file, + format!("{func_name}(12, 4)(-123.456::DECIMAL(9,3))"), + &[], + ); + + // Column based conversions for floats, integers, and decimals. + run_ast(file, format!("{func_name}(12, 4)(a)"), &[( + "a", + Float64Type::from_data(vec![0.1, -2.3456, 987.6543]), + )]); + run_ast(file, format!("{func_name}(8, 0)(b)"), &[( + "b", + Int32Type::from_data(vec![0, 1, -2, 123_456]), + )]); + run_ast(file, format!("{func_name}(9, 3)(c)"), &[( + "c", + Float32Type::from_data(vec![0.25f32, -1.125, 123.5]), + )]); + run_ast(file, format!("{func_name}(15, 3)(d)"), &[( + "d", + Decimal128Type::from_data_with_size( + [123_450i128, -9_999, 1_000_000, -76_543], + Some(DecimalSize::new_unchecked(10, 2)), + ), + )]); +} + #[test] fn test_decimal_to_decimal() { let mut mint = Mint::new("tests/it/scalars/testdata"); diff --git a/src/query/functions/tests/it/scalars/testdata/cast.txt b/src/query/functions/tests/it/scalars/testdata/cast.txt index 336b86bd5c5c0..cc24de52e785c 100644 --- a/src/query/functions/tests/it/scalars/testdata/cast.txt +++ b/src/query/functions/tests/it/scalars/testdata/cast.txt @@ -1077,6 +1077,186 @@ output domain : {0..=0} output : 0 +ast : to_decimal(10, 3)(123.456) +raw expr : to_decimal(10, 3)(123.456) +checked expr : to_decimal(10, 3)(123.456_d64(6,3)) +optimized expr : 123.456_d64(10,3) +output type : Decimal(10, 3) +output domain : {123.456..=123.456} +output : 123.456 + + +ast : to_decimal(10, 3)(-123.456) +raw expr : to_decimal(10, 3)(-123.456) +checked expr : to_decimal(10, 3)(-123.456_d64(6,3)) +optimized expr : -123.456_d64(10,3) +output type : Decimal(10, 3) +output domain : {-123.456..=-123.456} +output : -123.456 + + +ast : to_decimal(12, 1)(-9876.5) +raw expr : to_decimal(12, 1)(-9876.5) +checked expr : to_decimal(12, 1)(-9876.5_d64(5,1)) +optimized expr : -9876.5_d64(12,1) +output type : Decimal(12, 1) +output domain : {-9876.5..=-9876.5} +output : -9876.5 + + +ast : to_decimal(6, 2)(5) +raw expr : to_decimal(6, 2)(5) +checked expr : to_decimal(6, 2)(5_u8) +optimized expr : 5.00_d64(6,2) +output type : Decimal(6, 2) +output domain : {5.00..=5.00} +output : 5.00 + + +ast : to_decimal(6, 2)(-5) +raw expr : to_decimal(6, 2)(-5) +checked expr : to_decimal(6, 2)(-5_i8) +optimized expr : -5.00_d64(6,2) +output type : Decimal(6, 2) +output domain : {-5.00..=-5.00} +output : -5.00 + + +ast : to_decimal(6, 2)(true) +raw expr : to_decimal(6, 2)(true) +checked expr : to_decimal(6, 2)(true) +optimized expr : 1.00_d64(6,2) +output type : Decimal(6, 2) +output domain : {1.00..=1.00} +output : 1.00 + + +ast : to_decimal(10, 2)(123.45::FLOAT32) +raw expr : to_decimal(10, 2)(CAST(123.45 AS Float32)) +checked expr : to_decimal(10, 2)(CAST(123.45_d64(5,2) AS Float32)) +optimized expr : 123.44_d64(10,2) +output type : Decimal(10, 2) +output domain : {123.44..=123.44} +output : 123.44 + + +ast : to_decimal(10, 2)(-123.45::FLOAT32) +raw expr : to_decimal(10, 2)(minus(CAST(123.45 AS Float32))) +checked expr : to_decimal(10, 2)(minus(CAST(123.45_d64(5,2) AS Float32))) +optimized expr : -123.44_d64(10,2) +output type : Decimal(10, 2) +output domain : {-123.44..=-123.44} +output : -123.44 + + +ast : to_decimal(12, 4)(123.456::DECIMAL(9,3)) +raw expr : to_decimal(12, 4)(CAST(123.456 AS Decimal(9, 3))) +checked expr : to_decimal(12, 4)(CAST(123.456_d64(6,3) AS Decimal(9, 3))) +optimized expr : 123.4560_d64(12,4) +output type : Decimal(12, 4) +output domain : {123.4560..=123.4560} +output : 123.4560 + + +ast : to_decimal(12, 4)(-123.456::DECIMAL(9,3)) +raw expr : to_decimal(12, 4)(minus(CAST(123.456 AS Decimal(9, 3)))) +checked expr : to_decimal(12, 4)(minus(CAST(123.456_d64(6,3) AS Decimal(9, 3)))) +optimized expr : -123.4560_d64(12,4) +output type : Decimal(12, 4) +output domain : {-123.4560..=-123.4560} +output : -123.4560 + + +ast : to_decimal(12, 4)(a) +raw expr : to_decimal(12, 4)(a::Float64) +checked expr : to_decimal(12, 4)(a) +evaluation: ++--------+----------------------+----------------------+ +| | a | Output | ++--------+----------------------+----------------------+ +| Type | Float64 | Decimal(12, 4) | +| Domain | {-2.3456..=987.6543} | {-2.3456..=987.6543} | +| Row 0 | 0.1 | 0.1000 | +| Row 1 | -2.3456 | -2.3456 | +| Row 2 | 987.6543 | 987.6543 | ++--------+----------------------+----------------------+ +evaluation (internal): ++--------+-------------------------------------------+ +| Column | Data | ++--------+-------------------------------------------+ +| a | Column(Float64([0.1, -2.3456, 987.6543])) | +| Output | Decimal64([0.1000, -2.3456, 987.6543]) | ++--------+-------------------------------------------+ + + +ast : to_decimal(8, 0)(b) +raw expr : to_decimal(8, 0)(b::Int32) +checked expr : to_decimal(8, 0)(b) +evaluation: ++--------+---------------+---------------+ +| | b | Output | ++--------+---------------+---------------+ +| Type | Int32 | Decimal(8, 0) | +| Domain | {-2..=123456} | {-2..=123456} | +| Row 0 | 0 | 0 | +| Row 1 | 1 | 1 | +| Row 2 | -2 | -2 | +| Row 3 | 123456 | 123456 | ++--------+---------------+---------------+ +evaluation (internal): ++--------+-----------------------------------+ +| Column | Data | ++--------+-----------------------------------+ +| b | Column(Int32([0, 1, -2, 123456])) | +| Output | Decimal64([0, 1, -2, 123456]) | ++--------+-----------------------------------+ + + +ast : to_decimal(9, 3)(c) +raw expr : to_decimal(9, 3)(c::Float32) +checked expr : to_decimal(9, 3)(c) +evaluation: ++--------+------------------+--------------------+ +| | c | Output | ++--------+------------------+--------------------+ +| Type | Float32 | Decimal(9, 3) | +| Domain | {-1.125..=123.5} | {-1.125..=123.500} | +| Row 0 | 0.25 | 0.250 | +| Row 1 | -1.125 | -1.125 | +| Row 2 | 123.5 | 123.500 | ++--------+------------------+--------------------+ +evaluation (internal): ++--------+----------------------------------------+ +| Column | Data | ++--------+----------------------------------------+ +| c | Column(Float32([0.25, -1.125, 123.5])) | +| Output | Decimal64([0.250, -1.125, 123.500]) | ++--------+----------------------------------------+ + + +ast : to_decimal(15, 3)(d) +raw expr : to_decimal(15, 3)(d::Decimal(10, 2)) +checked expr : to_decimal(15, 3)(d) +evaluation: ++--------+----------------------+------------------------+ +| | d | Output | ++--------+----------------------+------------------------+ +| Type | Decimal(10, 2) | Decimal(15, 3) | +| Domain | {-765.43..=10000.00} | {-765.430..=10000.000} | +| Row 0 | 1234.50 | 1234.500 | +| Row 1 | -99.99 | -99.990 | +| Row 2 | 10000.00 | 10000.000 | +| Row 3 | -765.43 | -765.430 | ++--------+----------------------+------------------------+ +evaluation (internal): ++--------+----------------------------------------------------------+ +| Column | Data | ++--------+----------------------------------------------------------+ +| d | Column(Decimal128([1234.50, -99.99, 10000.00, -765.43])) | +| Output | Decimal64([1234.500, -99.990, 10000.000, -765.430]) | ++--------+----------------------------------------------------------+ + + ast : CAST(0 AS BOOLEAN) raw expr : CAST(0 AS Boolean) checked expr : CAST(0_u8 AS Boolean) @@ -3295,6 +3475,186 @@ output domain : {0..=0} output : 0 +ast : try_to_decimal(10, 3)(123.456) +raw expr : try_to_decimal(10, 3)(123.456) +checked expr : try_to_decimal(10, 3)(123.456_d64(6,3)) +optimized expr : 123.456_d64(10,3) +output type : Decimal(10, 3) NULL +output domain : {123.456..=123.456} +output : 123.456 + + +ast : try_to_decimal(10, 3)(-123.456) +raw expr : try_to_decimal(10, 3)(-123.456) +checked expr : try_to_decimal(10, 3)(-123.456_d64(6,3)) +optimized expr : -123.456_d64(10,3) +output type : Decimal(10, 3) NULL +output domain : {-123.456..=-123.456} +output : -123.456 + + +ast : try_to_decimal(12, 1)(-9876.5) +raw expr : try_to_decimal(12, 1)(-9876.5) +checked expr : try_to_decimal(12, 1)(-9876.5_d64(5,1)) +optimized expr : -9876.5_d64(12,1) +output type : Decimal(12, 1) NULL +output domain : {-9876.5..=-9876.5} +output : -9876.5 + + +ast : try_to_decimal(6, 2)(5) +raw expr : try_to_decimal(6, 2)(5) +checked expr : try_to_decimal(6, 2)(5_u8) +optimized expr : 5.00_d64(6,2) +output type : Decimal(6, 2) NULL +output domain : {5.00..=5.00} +output : 5.00 + + +ast : try_to_decimal(6, 2)(-5) +raw expr : try_to_decimal(6, 2)(-5) +checked expr : try_to_decimal(6, 2)(-5_i8) +optimized expr : -5.00_d64(6,2) +output type : Decimal(6, 2) NULL +output domain : {-5.00..=-5.00} +output : -5.00 + + +ast : try_to_decimal(6, 2)(true) +raw expr : try_to_decimal(6, 2)(true) +checked expr : try_to_decimal(6, 2)(true) +optimized expr : 1.00_d64(6,2) +output type : Decimal(6, 2) NULL +output domain : {1.00..=1.00} +output : 1.00 + + +ast : try_to_decimal(10, 2)(123.45::FLOAT32) +raw expr : try_to_decimal(10, 2)(CAST(123.45 AS Float32)) +checked expr : try_to_decimal(10, 2)(CAST(123.45_d64(5,2) AS Float32)) +optimized expr : 123.44_d64(10,2) +output type : Decimal(10, 2) NULL +output domain : {123.44..=123.44} +output : 123.44 + + +ast : try_to_decimal(10, 2)(-123.45::FLOAT32) +raw expr : try_to_decimal(10, 2)(minus(CAST(123.45 AS Float32))) +checked expr : try_to_decimal(10, 2)(minus(CAST(123.45_d64(5,2) AS Float32))) +optimized expr : -123.44_d64(10,2) +output type : Decimal(10, 2) NULL +output domain : {-123.44..=-123.44} +output : -123.44 + + +ast : try_to_decimal(12, 4)(123.456::DECIMAL(9,3)) +raw expr : try_to_decimal(12, 4)(CAST(123.456 AS Decimal(9, 3))) +checked expr : try_to_decimal(12, 4)(CAST(123.456_d64(6,3) AS Decimal(9, 3))) +optimized expr : 123.4560_d64(12,4) +output type : Decimal(12, 4) NULL +output domain : {123.4560..=123.4560} +output : 123.4560 + + +ast : try_to_decimal(12, 4)(-123.456::DECIMAL(9,3)) +raw expr : try_to_decimal(12, 4)(minus(CAST(123.456 AS Decimal(9, 3)))) +checked expr : try_to_decimal(12, 4)(minus(CAST(123.456_d64(6,3) AS Decimal(9, 3)))) +optimized expr : -123.4560_d64(12,4) +output type : Decimal(12, 4) NULL +output domain : {-123.4560..=-123.4560} +output : -123.4560 + + +ast : try_to_decimal(12, 4)(a) +raw expr : try_to_decimal(12, 4)(a::Float64) +checked expr : try_to_decimal(12, 4)(a) +evaluation: ++--------+----------------------+----------------------+ +| | a | Output | ++--------+----------------------+----------------------+ +| Type | Float64 | Decimal(12, 4) NULL | +| Domain | {-2.3456..=987.6543} | {-2.3456..=987.6543} | +| Row 0 | 0.1 | 0.1000 | +| Row 1 | -2.3456 | -2.3456 | +| Row 2 | 987.6543 | 987.6543 | ++--------+----------------------+----------------------+ +evaluation (internal): ++--------+-------------------------------------------------------------------------------------------+ +| Column | Data | ++--------+-------------------------------------------------------------------------------------------+ +| a | Column(Float64([0.1, -2.3456, 987.6543])) | +| Output | NullableColumn { column: Decimal64([0.1000, -2.3456, 987.6543]), validity: [0b_____111] } | ++--------+-------------------------------------------------------------------------------------------+ + + +ast : try_to_decimal(8, 0)(b) +raw expr : try_to_decimal(8, 0)(b::Int32) +checked expr : try_to_decimal(8, 0)(b) +evaluation: ++--------+---------------+--------------------+ +| | b | Output | ++--------+---------------+--------------------+ +| Type | Int32 | Decimal(8, 0) NULL | +| Domain | {-2..=123456} | {-2..=123456} | +| Row 0 | 0 | 0 | +| Row 1 | 1 | 1 | +| Row 2 | -2 | -2 | +| Row 3 | 123456 | 123456 | ++--------+---------------+--------------------+ +evaluation (internal): ++--------+----------------------------------------------------------------------------------+ +| Column | Data | ++--------+----------------------------------------------------------------------------------+ +| b | Column(Int32([0, 1, -2, 123456])) | +| Output | NullableColumn { column: Decimal64([0, 1, -2, 123456]), validity: [0b____1111] } | ++--------+----------------------------------------------------------------------------------+ + + +ast : try_to_decimal(9, 3)(c) +raw expr : try_to_decimal(9, 3)(c::Float32) +checked expr : try_to_decimal(9, 3)(c) +evaluation: ++--------+------------------+--------------------+ +| | c | Output | ++--------+------------------+--------------------+ +| Type | Float32 | Decimal(9, 3) NULL | +| Domain | {-1.125..=123.5} | {-1.125..=123.500} | +| Row 0 | 0.25 | 0.250 | +| Row 1 | -1.125 | -1.125 | +| Row 2 | 123.5 | 123.500 | ++--------+------------------+--------------------+ +evaluation (internal): ++--------+----------------------------------------------------------------------------------------+ +| Column | Data | ++--------+----------------------------------------------------------------------------------------+ +| c | Column(Float32([0.25, -1.125, 123.5])) | +| Output | NullableColumn { column: Decimal64([0.250, -1.125, 123.500]), validity: [0b_____111] } | ++--------+----------------------------------------------------------------------------------------+ + + +ast : try_to_decimal(15, 3)(d) +raw expr : try_to_decimal(15, 3)(d::Decimal(10, 2)) +checked expr : try_to_decimal(15, 3)(d) +evaluation: ++--------+----------------------+------------------------+ +| | d | Output | ++--------+----------------------+------------------------+ +| Type | Decimal(10, 2) | Decimal(15, 3) NULL | +| Domain | {-765.43..=10000.00} | {-765.430..=10000.000} | +| Row 0 | 1234.50 | 1234.500 | +| Row 1 | -99.99 | -99.990 | +| Row 2 | 10000.00 | 10000.000 | +| Row 3 | -765.43 | -765.430 | ++--------+----------------------+------------------------+ +evaluation (internal): ++--------+--------------------------------------------------------------------------------------------------------+ +| Column | Data | ++--------+--------------------------------------------------------------------------------------------------------+ +| d | Column(Decimal128([1234.50, -99.99, 10000.00, -765.43])) | +| Output | NullableColumn { column: Decimal64([1234.500, -99.990, 10000.000, -765.430]), validity: [0b____1111] } | ++--------+--------------------------------------------------------------------------------------------------------+ + + ast : TRY_CAST(0 AS BOOLEAN) raw expr : TRY_CAST(0 AS Boolean) checked expr : TRY_CAST(0_u8 AS Boolean NULL) diff --git a/src/query/functions/tests/it/scalars/testdata/decimal_to_decimal_cast.txt b/src/query/functions/tests/it/scalars/testdata/decimal_to_decimal_cast.txt index c2a0c7e4fad21..c6e5d32278f2d 100644 --- a/src/query/functions/tests/it/scalars/testdata/decimal_to_decimal_cast.txt +++ b/src/query/functions/tests/it/scalars/testdata/decimal_to_decimal_cast.txt @@ -67,7 +67,7 @@ error: --> SQL:1:1 | 1 | CAST(123.45::DECIMAL(5,2) AS DECIMAL(5,4)) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 955 while evaluating function `to_decimal(5, 4)(123.45)` in expr `CAST(123.45 AS Decimal(5, 4))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 963 while evaluating function `to_decimal(5, 4)(123.45)` in expr `CAST(123.45 AS Decimal(5, 4))` @@ -75,7 +75,7 @@ error: --> SQL:1:1 | 1 | CAST(123::DECIMAL(3,0) AS DECIMAL(3,2)) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 955 while evaluating function `to_decimal(3, 2)(123)` in expr `CAST(CAST(123 AS Decimal(3, 0)) AS Decimal(3, 2))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 963 while evaluating function `to_decimal(3, 2)(123)` in expr `CAST(CAST(123 AS Decimal(3, 0)) AS Decimal(3, 2))` @@ -449,7 +449,7 @@ error: --> SQL:1:1 | 1 | CAST(12345.67::DECIMAL(7,2) AS DECIMAL(5,2)) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 955 while evaluating function `to_decimal(5, 2)(12345.67)` in expr `CAST(12345.67 AS Decimal(5, 2))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 963 while evaluating function `to_decimal(5, 2)(12345.67)` in expr `CAST(12345.67 AS Decimal(5, 2))` @@ -457,7 +457,7 @@ error: --> SQL:1:1 | 1 | CAST(999.99::DECIMAL(5,2) AS DECIMAL(4,2)) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 955 while evaluating function `to_decimal(4, 2)(999.99)` in expr `CAST(999.99 AS Decimal(4, 2))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 963 while evaluating function `to_decimal(4, 2)(999.99)` in expr `CAST(999.99 AS Decimal(4, 2))` @@ -465,7 +465,7 @@ error: --> SQL:1:1 | 1 | CAST(99.9::DECIMAL(3,1) AS DECIMAL(2,1)) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 955 while evaluating function `to_decimal(2, 1)(99.9)` in expr `CAST(99.9 AS Decimal(2, 1))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 963 while evaluating function `to_decimal(2, 1)(99.9)` in expr `CAST(99.9 AS Decimal(2, 1))` @@ -482,7 +482,7 @@ error: --> SQL:1:1 | 1 | CAST(-123.45::DECIMAL(5,2) AS DECIMAL(4,2)) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 955 while evaluating function `to_decimal(4, 2)(-123.45)` in expr `CAST(- 123.45 AS Decimal(4, 2))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 963 while evaluating function `to_decimal(4, 2)(-123.45)` in expr `CAST(- 123.45 AS Decimal(4, 2))` @@ -490,7 +490,7 @@ error: --> SQL:1:1 | 1 | CAST(99.99::DECIMAL(4,2) AS DECIMAL(3,2)) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 955 while evaluating function `to_decimal(3, 2)(99.99)` in expr `CAST(99.99 AS Decimal(3, 2))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 963 while evaluating function `to_decimal(3, 2)(99.99)` in expr `CAST(99.99 AS Decimal(3, 2))` @@ -498,7 +498,7 @@ error: --> SQL:1:1 | 1 | CAST(9.99::DECIMAL(3,2) AS DECIMAL(2,2)) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 955 while evaluating function `to_decimal(2, 2)(9.99)` in expr `CAST(9.99 AS Decimal(2, 2))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 963 while evaluating function `to_decimal(2, 2)(9.99)` in expr `CAST(9.99 AS Decimal(2, 2))` @@ -524,7 +524,7 @@ error: --> SQL:1:1 | 1 | CAST(12345678901234567890.123456789::DECIMAL(38,9) AS DECIMAL(18,4)) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 854 while evaluating function `to_decimal(18, 4)(12345678901234567890.123456789)` in expr `CAST(CAST(12345678901234567890.123456789 AS Decimal(38, 9)) AS Decimal(18, 4))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 862 while evaluating function `to_decimal(18, 4)(12345678901234567890.123456789)` in expr `CAST(CAST(12345678901234567890.123456789 AS Decimal(38, 9)) AS Decimal(18, 4))` @@ -650,7 +650,7 @@ error: --> SQL:1:1 | 1 | CAST(a AS DECIMAL(5,1)) - | ^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 854 while evaluating function `to_decimal(5, 1)(1234567.89)` in expr `CAST(a AS Decimal(5, 1))` + | ^^^^^^^^^^^^^^^^^^^^^^^ Decimal overflow at line : 862 while evaluating function `to_decimal(5, 1)(1234567.89)` in expr `CAST(a AS Decimal(5, 1))` diff --git a/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal.test b/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal.test index c9dc7078a366c..97ca5c0ea94a7 100644 --- a/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal.test +++ b/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal.test @@ -1273,6 +1273,81 @@ SELECT to_decimal('12.3000', '99D9999'); ---- 12 +query I +SELECT to_decimal(123.4567, 10, 4); +---- +123.4567 + +query I +SELECT to_decimal(-123.4567, 10, 4); +---- +-123.4567 + +query I +SELECT to_decimal(true, 5, 2), to_decimal(false, 5, 2); +---- +1.00 0.00 + +query I +SELECT to_decimal(number, 10, 2) FROM numbers(3); +---- +0.00 +1.00 +2.00 + +query I +SELECT to_decimal(-5, 10, 2); +---- +-5.00 + +query I +SELECT to_decimal(CAST(123.45 AS FLOAT32), 10, 2); +---- +123.45 + +query I +SELECT to_decimal(to_float32(number) / 10, 8, 3) FROM numbers(3); +---- +0.000 +0.100 +0.200 + +query I +SELECT to_decimal(to_float32(number) - 3, 8, 2) FROM numbers(3); +---- +-3.00 +-2.00 +-1.00 + +query I +SELECT to_decimal(CAST(123.456 AS DECIMAL(9, 3)), 12, 4); +---- +123.4560 + +query I +SELECT to_decimal(CAST(-123.456 AS DECIMAL(9, 3)), 12, 4); +---- +-123.4560 + +query I +SELECT to_decimal(c, 15, 3) FROM (SELECT CAST(number * 100 + 5 AS DECIMAL(10, 2)) AS c FROM numbers(3)) t; +---- +5.000 +105.000 +205.000 + +query I +SELECT to_decimal(c, 15, 3) FROM (SELECT CAST(number * 100 - 5 AS DECIMAL(10, 2)) AS c FROM numbers(3)) t; +---- +-5.000 +95.000 +195.000 + +query I +SELECT try_to_decimal(123456, 6, 0); +---- +123456 + query I SELECT TO_DECIMAL('123456789', 9); ----