diff --git a/src/AggregateFunctions/AggregateFunctionDeltaSum.h b/src/AggregateFunctions/AggregateFunctionDeltaSum.h index 99406618da5..33bc2ea99d8 100644 --- a/src/AggregateFunctions/AggregateFunctionDeltaSum.h +++ b/src/AggregateFunctions/AggregateFunctionDeltaSum.h @@ -79,7 +79,7 @@ public: place_data->sum += rhs_data->sum + (rhs_data->first - place_data->last); place_data->last = rhs_data->last; } - else if ((rhs_data->last < place_data->first && rhs_data->seen_last && place_data->seen_first)) + else if ((rhs_data->first < place_data->last && rhs_data->seen_last && place_data->seen_first)) { // In the opposite scenario, the lhs comes after the rhs, e.g. [4, 6] [1, 2]. Since we // assume the input interval states are sorted by time, we assume this is a counter @@ -87,9 +87,9 @@ public: // rhs last value. place_data->sum += rhs_data->sum; - place_data->first = rhs_data->first; + place_data->last = rhs_data->last; } - else if (rhs_data->seen_first) + else if (rhs_data->seen_first && !place_data->seen_first) { // If we're here then the lhs is an empty state and the rhs does have some state, so // we'll just take that state. diff --git a/tests/queries/0_stateless/01700_deltasum.reference b/tests/queries/0_stateless/01700_deltasum.reference index be5b176c627..6be953e2b2d 100644 --- a/tests/queries/0_stateless/01700_deltasum.reference +++ b/tests/queries/0_stateless/01700_deltasum.reference @@ -7,3 +7,4 @@ 2 2.25 6.5 +7 diff --git a/tests/queries/0_stateless/01700_deltasum.sql b/tests/queries/0_stateless/01700_deltasum.sql index 93edb2e477d..83d5e0439d2 100644 --- a/tests/queries/0_stateless/01700_deltasum.sql +++ b/tests/queries/0_stateless/01700_deltasum.sql @@ -7,3 +7,4 @@ select deltaSumMerge(rows) from (select deltaSumState(arrayJoin([0, 1])) as rows select deltaSumMerge(rows) from (select deltaSumState(arrayJoin([4, 5])) as rows union all select deltaSumState(arrayJoin([0, 1])) as rows); select deltaSum(arrayJoin([2.25, 3, 4.5])); select deltaSumMerge(rows) from (select deltaSumState(arrayJoin([0.1, 0.3, 0.5])) as rows union all select deltaSumState(arrayJoin([4.1, 5.1, 6.6])) as rows); +select deltaSumMerge(rows) from (select deltaSumState(arrayJoin([3, 5])) as rows union all select deltaSumState(arrayJoin([1, 2])) as rows union all select deltaSumState(arrayJoin([4, 6])) as rows);