Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions datafusion/sqllogictest/src/engines/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ pub(crate) fn f16_to_str(value: f16) -> String {
} else if value == f16::NEG_INFINITY {
"-Infinity".to_string()
} else {
big_decimal_to_str(BigDecimal::from_str(&value.to_string()).unwrap(), None)
big_decimal_to_str(
BigDecimal::from_str(&value.to_string()).unwrap(),
None,
true,
)
}
}

Expand All @@ -64,7 +68,11 @@ pub(crate) fn f32_to_str(value: f32) -> String {
} else if value == f32::NEG_INFINITY {
"-Infinity".to_string()
} else {
big_decimal_to_str(BigDecimal::from_str(&value.to_string()).unwrap(), None)
big_decimal_to_str(
BigDecimal::from_str(&value.to_string()).unwrap(),
None,
true,
)
}
}

Expand All @@ -78,7 +86,11 @@ pub(crate) fn f64_to_str(value: f64) -> String {
} else if value == f64::NEG_INFINITY {
"-Infinity".to_string()
} else {
big_decimal_to_str(BigDecimal::from_str(&value.to_string()).unwrap(), None)
big_decimal_to_str(
BigDecimal::from_str(&value.to_string()).unwrap(),
None,
true,
)
}
}

Expand All @@ -92,7 +104,11 @@ pub(crate) fn spark_f64_to_str(value: f64) -> String {
} else if value == f64::NEG_INFINITY {
"-Infinity".to_string()
} else {
big_decimal_to_str(BigDecimal::from_str(&value.to_string()).unwrap(), Some(15))
big_decimal_to_str(
BigDecimal::from_str(&value.to_string()).unwrap(),
Some(15),
true,
)
}
}

Expand All @@ -105,23 +121,35 @@ pub(crate) fn arrow_decimal_to_str<T: DecimalType>(
big_decimal_to_str(
BigDecimal::from_str(&value).unwrap(),
Some(i64::from(scale)),
false,
)
}

#[cfg(feature = "postgres")]
pub(crate) fn decimal_to_str(value: BigDecimal) -> String {
big_decimal_to_str(value, None)
big_decimal_to_str(value, None, false)
}

/// Converts a `BigDecimal` to its plain string representation, optionally rounding to a specified number of decimal places.
///
/// If `round_digits` is `None`, the value is rounded to 12 decimal places by default.
///
/// If `normalize` is true, trailing 0s are trimmed.
#[expect(clippy::needless_pass_by_value)]
pub(crate) fn big_decimal_to_str(value: BigDecimal, round_digits: Option<i64>) -> String {
pub(crate) fn big_decimal_to_str(
value: BigDecimal,
round_digits: Option<i64>,
normalize: bool,
) -> String {
// Round the value to limit the number of decimal places
let value = value.round(round_digits.unwrap_or(12)).normalized();
let value = value.round(round_digits.unwrap_or(12));

// Format the value to a string
value.to_plain_string()
if normalize {
value.normalized().to_plain_string()
} else {
value.to_plain_string()
}
}

#[cfg(test)]
Expand All @@ -137,7 +165,8 @@ mod tests {
assert_eq!(
big_decimal_to_str(
BigDecimal::from_bigint(BigInt::from($integer), $scale),
$round_digits
$round_digits,
true
),
$expected
);
Expand Down
48 changes: 24 additions & 24 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -3586,7 +3586,7 @@ create table t (c1 decimal(10, 0)) as values (1), (2), (3), (4), (5), (6);
query RT
select avg(c1), arrow_typeof(avg(c1)) from t;
----
3.5 Decimal128(14, 4)
3.5000 Decimal128(14, 4)

statement ok
drop table t;
Expand All @@ -3598,7 +3598,7 @@ create table t (c1 decimal(10, 0)) as values (1), (NULL), (3), (4), (5);
query RT
select avg(c1), arrow_typeof(avg(c1)) from t;
----
3.25 Decimal128(14, 4)
3.2500 Decimal128(14, 4)

statement ok
drop table t;
Expand Down Expand Up @@ -5278,7 +5278,7 @@ INSERT INTO decimals VALUES (123.0001), (124.00);
query RR
SELECT MIN(value), MAX(value) FROM decimals;
----
123 124
123.00 124.00

statement ok
DROP TABLE decimals;
Expand Down Expand Up @@ -5347,7 +5347,7 @@ INSERT INTO decimals_error VALUES (123.00), (arrow_cast(124.001, 'Decimal128(10,
query RR
SELECT MIN(value), MAX(value) FROM decimals_error;
----
123 124
123.00 124.00

statement ok
DROP TABLE decimals_error;
Expand Down Expand Up @@ -6497,7 +6497,7 @@ NULL
query RT
select sum(c1), arrow_typeof(sum(c1)) from d_table;
----
100 Decimal128(20, 3)
100.000 Decimal128(20, 3)

# aggregate sum with decimal
statement ok
Expand All @@ -6506,7 +6506,7 @@ create table t (c decimal(35, 3)) as values (10), (null), (20);
query RT
select sum(c), arrow_typeof(sum(c)) from t;
----
30 Decimal128(38, 3)
30.000 Decimal128(38, 3)

statement ok
drop table t;
Expand Down Expand Up @@ -6585,13 +6585,13 @@ B -1000.045 Decimal128(20, 3)
query RT
select avg(c1), arrow_typeof(avg(c1)) from d_table
----
5 Decimal128(14, 7)
5.0000000 Decimal128(14, 7)

query TRT
select c2, avg(c1), arrow_typeof(avg(c1)) from d_table GROUP BY c2 ORDER BY c2
----
A 110.0045 Decimal128(14, 7)
B -100.0045 Decimal128(14, 7)
A 110.0045000 Decimal128(14, 7)
B -100.0045000 Decimal128(14, 7)

# aggregate_decimal_count_distinct
query I
Expand Down Expand Up @@ -6763,18 +6763,18 @@ INSERT INTO test_decimal_table VALUES (1, 10.10, 100.1, NULL), (1, 20.20, 200.2,
query IIRRRRIIRR rowsort
select c1, count(c2), avg(c2), sum(c2), min(c2), max(c2), count(c3), count(c4), sum(c4), avg(c4) from test_decimal_table group by c1
----
1 2 15.15 30.3 10.1 20.2 2 0 NULL NULL
2 2 15.15 30.3 10.1 20.2 2 0 NULL NULL
3 2 10.1 20.2 10.1 10.1 1 0 NULL NULL
1 2 15.150000 30.30 10.10 20.20 2 0 NULL NULL
2 2 15.150000 30.30 10.10 20.20 2 0 NULL NULL
3 2 10.100000 20.20 10.10 10.10 1 0 NULL NULL

# aggregate_decimal_with_group_by_decimal
query RIRRRRIR rowsort
select c3, count(c2), avg(c2), sum(c2), min(c2), max(c2), count(c4), sum(c4) from test_decimal_table group by c3
----
100.1 2 10.1 20.2 10.1 10.1 0 NULL
200.2 1 20.2 20.2 20.2 20.2 0 NULL
700.1 2 15.15 30.3 10.1 20.2 0 NULL
NULL 1 10.1 10.1 10.1 10.1 0 NULL
100.1 2 10.100000 20.20 10.10 10.10 0 NULL
200.2 1 20.200000 20.20 20.20 20.20 0 NULL
700.1 2 15.150000 30.30 10.10 20.20 0 NULL
NULL 1 10.100000 10.10 10.10 10.10 0 NULL

## Multiple distinct aggregates and dictionaries
statement ok
Expand Down Expand Up @@ -8941,7 +8941,7 @@ create table t_decimal (c decimal(10, 4)) as values (100.00), (125.00), (175.00)
query RT
select avg(distinct c), arrow_typeof(avg(distinct c)) from t_decimal;
----
180 Decimal128(14, 8)
180.00000000 Decimal128(14, 8)

statement ok
drop table t_decimal;
Expand All @@ -8961,7 +8961,7 @@ create table t_decimal256 (c decimal(50, 2)) as values
query RT
select avg(distinct c), arrow_typeof(avg(distinct c)) from t_decimal256;
----
180 Decimal256(54, 6)
180.000000 Decimal256(54, 6)

statement ok
drop table t_decimal256;
Expand Down Expand Up @@ -9118,7 +9118,7 @@ select
avg(d)
from distinct_avg;
----
3 Float64 37.4255 Float64 698.56005 Decimal128(14, 8) 15041.868333 Decimal256(54, 6) 4 56.52525 957.11074444 1272562.81625
3 Float64 37.4255 Float64 698.56005000 Decimal128(14, 8) 15041.868333 Decimal256(54, 6) 4 56.52525 957.11074444 1272562.816250

query RRRR rowsort
select
Expand All @@ -9129,11 +9129,11 @@ select
from distinct_avg
group by b;
----
1 4.09 4222.124 0
3 NULL 2161.1901 90251.21
5 1 100.25625 45125.105
5 100.5 -49.5781 0.333333
5 44.112 -132.12 NULL
1 4.09 4222.12400000 0.000000
3 NULL 2161.19010000 90251.210000
5 1 100.25625000 45125.105000
5 100.5 -49.57810000 0.333333
5 44.112 -132.12000000 NULL

query RRRR
select
Expand Down
6 changes: 3 additions & 3 deletions datafusion/sqllogictest/test_files/aggregate_skip_partial.slt
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,9 @@ FROM decimal_table CROSS JOIN t
GROUP BY i
ORDER BY i;
----
1 66
2 66
3 33
1 66.000
2 66.000
3 33.000

# Config reset
statement ok
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/array/array_min_max.slt
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ from (
) as dec_list
) t;
----
100 300 Decimal128(20, 4) Decimal128(20, 4)
100.0000 300.0000 Decimal128(20, 4) Decimal128(20, 4)



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,4 +688,3 @@ AS
arrow_cast(column4, 'FixedSizeList(3, Float64)') AS column4
FROM arrays_distance_table
;

2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/array_add.slt
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,4 @@ select array_add(array_add(a, b), c) from (values
----
[111.0, 222.0]
NULL
[111.0, NULL]
[111.0, NULL]
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/array_product.slt
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ select list_product(column1) from (values
) as t(column1);
----
6
NULL
NULL
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/array_subtract.slt
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,4 @@ select array_subtract(array_subtract(a, b), c) from (values
----
[89.0, 178.0]
NULL
[89.0, NULL]
[89.0, NULL]
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/arrow_typeof.slt
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ SELECT
col_d256
FROM foo;
----
100 100
100.00 100.00

statement ok
drop table foo
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/binary.slt
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,4 @@ hellohello
query T
SELECT 'hello' || arrow_cast(arrow_cast('hello', 'Binary'), 'BinaryView');
----
hellohello
hellohello
4 changes: 2 additions & 2 deletions datafusion/sqllogictest/test_files/coalesce.slt
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ select
coalesce(arrow_cast(2, 'Decimal128(7, 2)'), 0),
arrow_typeof(coalesce(arrow_cast(2, 'Decimal128(7, 2)'), 0))
----
2 Decimal128(22, 2)
2.00 Decimal128(22, 2)

query RT
select
coalesce(arrow_cast(2, 'Decimal256(7, 2)'), 0),
arrow_typeof(coalesce(arrow_cast(2, 'Decimal256(7, 2)'), 0));
----
2 Decimal256(22, 2)
2.00 Decimal256(22, 2)

# coalesce string
query TT
Expand Down
Loading
Loading