Skip to content

Commit c7485db

Browse files
authored
feat: to_decimal support more types (#19195)
* feat: to_decimal support more types * chore: add more test
1 parent 82a05cf commit c7485db

File tree

5 files changed

+503
-10
lines changed

5 files changed

+503
-10
lines changed

src/query/functions/src/scalars/decimal/src/cast.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ pub fn register_to_decimal(registry: &mut FunctionRegistry) {
9494
],
9595
false,
9696
),
97+
(3, false, false) => (
98+
vec![
99+
from_type.clone(),
100+
DataType::Number(NumberDataType::Int64),
101+
DataType::Number(NumberDataType::Int64),
102+
],
103+
false,
104+
),
97105
(4, true, true) => (
98106
vec![
99107
DataType::String,

src/query/functions/tests/it/scalars/cast.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn test_cast() {
4040
test_cast_between_number_and_string(file, is_try);
4141
test_cast_between_boolean_and_string(file, is_try);
4242
test_cast_between_string_and_decimal(file, is_try);
43+
test_to_decimal_with_precision_args(file, is_try);
4344
test_cast_between_number_and_boolean(file, is_try);
4445
test_cast_between_date_and_timestamp(file, is_try);
4546
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) {
758759
);
759760
}
760761

762+
fn test_to_decimal_with_precision_args(file: &mut impl Write, is_try: bool) {
763+
let func_name = if is_try {
764+
"try_to_decimal"
765+
} else {
766+
"to_decimal"
767+
};
768+
769+
// Constant numeric inputs with explicit precision/scale arguments.
770+
run_ast(file, format!("{func_name}(10, 3)(123.456)"), &[]);
771+
run_ast(file, format!("{func_name}(10, 3)(-123.456)"), &[]);
772+
run_ast(file, format!("{func_name}(12, 1)(-9876.5)"), &[]);
773+
run_ast(file, format!("{func_name}(6, 2)(5)"), &[]);
774+
run_ast(file, format!("{func_name}(6, 2)(-5)"), &[]);
775+
run_ast(file, format!("{func_name}(6, 2)(true)"), &[]);
776+
run_ast(file, format!("{func_name}(10, 2)(123.45::FLOAT32)"), &[]);
777+
run_ast(file, format!("{func_name}(10, 2)(-123.45::FLOAT32)"), &[]);
778+
run_ast(
779+
file,
780+
format!("{func_name}(12, 4)(123.456::DECIMAL(9,3))"),
781+
&[],
782+
);
783+
run_ast(
784+
file,
785+
format!("{func_name}(12, 4)(-123.456::DECIMAL(9,3))"),
786+
&[],
787+
);
788+
789+
// Column based conversions for floats, integers, and decimals.
790+
run_ast(file, format!("{func_name}(12, 4)(a)"), &[(
791+
"a",
792+
Float64Type::from_data(vec![0.1, -2.3456, 987.6543]),
793+
)]);
794+
run_ast(file, format!("{func_name}(8, 0)(b)"), &[(
795+
"b",
796+
Int32Type::from_data(vec![0, 1, -2, 123_456]),
797+
)]);
798+
run_ast(file, format!("{func_name}(9, 3)(c)"), &[(
799+
"c",
800+
Float32Type::from_data(vec![0.25f32, -1.125, 123.5]),
801+
)]);
802+
run_ast(file, format!("{func_name}(15, 3)(d)"), &[(
803+
"d",
804+
Decimal128Type::from_data_with_size(
805+
[123_450i128, -9_999, 1_000_000, -76_543],
806+
Some(DecimalSize::new_unchecked(10, 2)),
807+
),
808+
)]);
809+
}
810+
761811
#[test]
762812
fn test_decimal_to_decimal() {
763813
let mut mint = Mint::new("tests/it/scalars/testdata");

0 commit comments

Comments
 (0)