This is an automated email from the ASF dual-hosted git repository.
HappenLee pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 8fffa028266 [fix](be) Return NaN for avg_weighted when sum of weights
is zero (#64333)
8fffa028266 is described below
commit 8fffa028266e19c3ee205a01f98c2c9fcc7c2f10
Author: TengJianPing <[email protected]>
AuthorDate: Thu Jun 11 14:37:42 2026 +0800
[fix](be) Return NaN for avg_weighted when sum of weights is zero (#64333)
Problem Summary: When the sum of weights passed to avg_weighted is zero
but the weighted data sum is non-zero, the function computed data_sum /
weight_sum and returned +/-Infinity (e.g. avg_weighted(f1, f2) over rows
(1, 1), (2, -1) returned -Infinity). The expected mathematical result of
a division by zero weight is undefined, so it should return NaN.
This fixes AggregateFunctionAvgWeightedData::get() to return quiet_NaN()
when weight_sum is exactly zero, instead of producing Infinity.
---
be/src/exprs/aggregate/aggregate_function_avg_weighted.h | 8 +++++++-
.../aggregate/support_type/avg_weighted/avg_weighted.out | 3 +++
.../support_type/avg_weighted/avg_weighted.groovy | 14 ++++++++++++++
3 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/be/src/exprs/aggregate/aggregate_function_avg_weighted.h
b/be/src/exprs/aggregate/aggregate_function_avg_weighted.h
index c9a4c423d65..1eb1c9cd416 100644
--- a/be/src/exprs/aggregate/aggregate_function_avg_weighted.h
+++ b/be/src/exprs/aggregate/aggregate_function_avg_weighted.h
@@ -22,6 +22,7 @@
#include <algorithm>
#include <boost/iterator/iterator_facade.hpp>
#include <cmath>
+#include <limits>
#include <memory>
#include <type_traits>
@@ -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;
diff --git
a/regression-test/data/query_p0/aggregate/support_type/avg_weighted/avg_weighted.out
b/regression-test/data/query_p0/aggregate/support_type/avg_weighted/avg_weighted.out
index f7365ee6724..dd8b4f7056b 100644
---
a/regression-test/data/query_p0/aggregate/support_type/avg_weighted/avg_weighted.out
+++
b/regression-test/data/query_p0/aggregate/support_type/avg_weighted/avg_weighted.out
@@ -2,3 +2,6 @@
-- !sql_double --
2.718281828
+-- !sql_zero_weight --
+NaN
+
diff --git
a/regression-test/suites/query_p0/aggregate/support_type/avg_weighted/avg_weighted.groovy
b/regression-test/suites/query_p0/aggregate/support_type/avg_weighted/avg_weighted.groovy
index f49adef5d8c..755adb41196 100644
---
a/regression-test/suites/query_p0/aggregate/support_type/avg_weighted/avg_weighted.groovy
+++
b/regression-test/suites/query_p0/aggregate/support_type/avg_weighted/avg_weighted.groovy
@@ -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;"""
}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]