Skip to content
Merged
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
6 changes: 6 additions & 0 deletions datafusion/functions-aggregate/src/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ impl AggregateUDFImpl for Sum {
if lit_type == DataType::Null {
return Ok(None);
}
// `CAST(COUNT(arg) AS <lit type>)` is not valid for intervals, and for
// decimals it can overflow or change the SUM result type.
// See https://github.com/apache/datafusion/issues/25943
if !(lit_type.is_integer() || lit_type.is_floating()) {
Comment thread
1fanwang marked this conversation as resolved.
return Ok(None);
}

// Build up SUM(arg)
let mut sum_agg = agg_function.clone();
Expand Down
40 changes: 40 additions & 0 deletions datafusion/sqllogictest/test_files/aggregates_simplify.slt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,46 @@ physical_plan
02)--AggregateExec: mode=Single, gby=[], aggr=[sum(sum_simplify_t.column1), count(sum_simplify_t.column1)]
03)----DataSourceExec: partitions=1, partition_sizes=[1]

statement ok
CREATE TABLE sum_simplify_decimal_interval_t AS
SELECT
CAST(v AS DECIMAL(10,2)) AS d,
CAST(v || ' days' AS INTERVAL) AS iv
FROM generate_series(1, 12) AS g(v);

query ??
SELECT
SUM(iv + INTERVAL '1 day') AS a,
SUM(iv + INTERVAL '2 days') AS b
FROM sum_simplify_decimal_interval_t;
----
90 days 102 days

statement ok
SET datafusion.sql_parser.parse_float_as_decimal = true;

query RR
SELECT
SUM(d + 1.5) AS a,
SUM(d + 2.5) AS b
FROM sum_simplify_decimal_interval_t;
----
96.00 108.00

query RTRT
SELECT
SUM(d + 1.00001) AS a,
arrow_typeof(SUM(d + 1.00001)) AS a_type,
SUM(d + 2.00001) AS b,
arrow_typeof(SUM(d + 2.00001)) AS b_type
FROM sum_simplify_decimal_interval_t
WHERE d <= 5;
----
20.00005 Decimal128(24, 5) 25.00005 Decimal128(24, 5)

statement ok
SET datafusion.sql_parser.parse_float_as_decimal = false;

# Reordered expressions that still compute the same thing
query II
SELECT SUM(1 + column1), SUM(column1 + 2) FROM sum_simplify_t;
Expand Down
Loading