This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.1 by this push:
new 10e0c2925aa branch-4.1: [fix](be) Return NaN for avg_weighted when sum
of weights is zero #64333 (#64431)
10e0c2925aa is described below
commit 10e0c2925aabf2d449079165eae33c0845648ddc
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Mon Jun 15 14:37:40 2026 +0800
branch-4.1: [fix](be) Return NaN for avg_weighted when sum of weights is
zero #64333 (#64431)
Cherry-picked from #64333
Co-authored-by: TengJianPing <[email protected]>
---
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 75643457d96..261d068e746 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>
@@ -77,7 +78,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]