Skip to content

Commit 01bba92

Browse files
authored
branch-4.1: [Fix](variance) Fix sample variance/stddev NaN res for single value#63605 (#63912)
cherry-pick: #63605
1 parent 55e0619 commit 01bba92

12 files changed

Lines changed: 1211 additions & 1138 deletions

File tree

be/src/exprs/aggregate/aggregate_function_stddev.h

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121
#include <cmath>
2222
#include <cstddef>
2323
#include <cstdint>
24+
#include <limits>
2425
#include <memory>
25-
#include <type_traits>
2626

2727
#include "core/assert_cast.h"
2828
#include "core/column/column.h"
29-
#include "core/column/column_nullable.h"
30-
#include "core/data_type/data_type_decimal.h"
3129
#include "core/data_type/data_type_number.h"
3230
#include "core/types.h"
3331
#include "exprs/aggregate/aggregate_function.h"
@@ -38,8 +36,6 @@ class Arena;
3836
class BufferReadable;
3937
class BufferWritable;
4038
template <PrimitiveType T>
41-
class ColumnDecimal;
42-
template <PrimitiveType T>
4339
class ColumnVector;
4440

4541
template <PrimitiveType T, bool is_stddev>
@@ -72,7 +68,7 @@ struct BaseData {
7268
// In MySQL, this will directly result in an error due to exceeding the double range.
7369
// For performance reasons, we are uniformly changing it to nan
7470
if (std::isinf(val)) {
75-
return std::nan("");
71+
return std::numeric_limits<double>::quiet_NaN();
7672
}
7773
return val;
7874
};
@@ -126,14 +122,9 @@ struct BaseData {
126122

127123
template <PrimitiveType T, typename Name, bool is_stddev>
128124
struct PopData : BaseData<T, is_stddev>, Name {
129-
using ColVecResult = std::conditional_t<is_decimal(T), ColumnDecimal128V2, ColumnFloat64>;
130125
void insert_result_into(IColumn& to) const {
131-
auto& col = assert_cast<ColVecResult&>(to);
132-
if constexpr (is_decimal(T)) {
133-
col.get_data().push_back(this->get_pop_result().value());
134-
} else {
135-
col.get_data().push_back(this->get_pop_result());
136-
}
126+
auto& col = assert_cast<ColumnFloat64&>(to);
127+
col.get_data().push_back(this->get_pop_result());
137128
}
138129

139130
static DataTypePtr get_return_type() { return std::make_shared<DataTypeFloat64>(); }
@@ -145,17 +136,12 @@ struct PopData : BaseData<T, is_stddev>, Name {
145136

146137
template <PrimitiveType T, typename Name, bool is_stddev>
147138
struct SampData : BaseData<T, is_stddev>, Name {
148-
using ColVecResult = std::conditional_t<is_decimal(T), ColumnDecimal128V2, ColumnFloat64>;
149139
void insert_result_into(IColumn& to) const {
150-
auto& col = assert_cast<ColVecResult&>(to);
140+
auto& col = assert_cast<ColumnFloat64&>(to);
151141
if (this->count == 1 || this->count == 0) {
152-
col.insert_default();
142+
col.get_data().push_back(std::numeric_limits<double>::quiet_NaN());
153143
} else {
154-
if constexpr (is_decimal(T)) {
155-
col.get_data().push_back(this->get_samp_result().value());
156-
} else {
157-
col.get_data().push_back(this->get_samp_result());
158-
}
144+
col.get_data().push_back(this->get_samp_result());
159145
}
160146
}
161147

0 commit comments

Comments
 (0)