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
8 changes: 7 additions & 1 deletion be/src/exprs/aggregate/aggregate_function_avg_weighted.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <algorithm>
#include <boost/iterator/iterator_facade.hpp>
#include <cmath>
#include <limits>
#include <memory>
#include <type_traits>

Expand Down Expand Up @@ -76,7 +77,12 @@ struct AggregateFunctionAvgWeightedData {
weight_sum = 0.0;
}

double get() const { return data_sum / weight_sum; }
double get() const {
if (weight_sum == 0.0) {
return std::numeric_limits<double>::quiet_NaN();
}
return data_sum / weight_sum;
}

double data_sum = 0.0;
double weight_sum = 0.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
-- !sql_double --
2.718281828

-- !sql_zero_weight --
NaN

Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,18 @@ suite("avg_weighted") {
"""

qt_sql_double """select avg_weighted(col_double, weight_double) from d_table;"""

// weight sum is zero, should return NaN instead of +/-Infinity
sql """
drop table if exists test_avg_weighted_zero;
"""
sql """
create table test_avg_weighted_zero (f1 int, f2 int)
distributed BY hash(f1) buckets 1
properties("replication_num" = "1");
"""
sql """
insert into test_avg_weighted_zero values (1, 1), (2, -1);
"""
qt_sql_zero_weight """select avg_weighted(f1, f2) from test_avg_weighted_zero;"""
}
Loading