fixing nan values for functions

This commit is contained in:
myrrc 2020-10-30 21:08:33 +03:00
parent 7d6f48263e
commit 06ae95a10a
5 changed files with 1839 additions and 1840 deletions

View File

@ -23,7 +23,7 @@ avgWeighted(x)
**Returned value** **Returned value**
- `0` if the supplied parameter is empty. - `NaN` if the supplied parameter is empty.
- Mean otherwise. - Mean otherwise.
**Return type** is always [Float64](../../../sql-reference/data-types/float.md). **Return type** is always [Float64](../../../sql-reference/data-types/float.md).
@ -57,6 +57,6 @@ Result:
``` text ``` text
┌─avg(x)─┐ ┌─avg(x)─┐
0 nan
└────────┘ └────────┘
``` ```

View File

@ -11,8 +11,7 @@
namespace DB namespace DB
{ {
/// @tparam BothZeroMeansNaN If false, the pair 0 / 0 = 0, nan otherwise. template <class Denominator>
template <class Denominator, bool BothZeroMeansNaN = true>
struct RationalFraction struct RationalFraction
{ {
Float64 numerator{0}; Float64 numerator{0};
@ -20,7 +19,7 @@ struct RationalFraction
Float64 NO_SANITIZE_UNDEFINED result() const Float64 NO_SANITIZE_UNDEFINED result() const
{ {
if constexpr (BothZeroMeansNaN && std::numeric_limits<Float64>::is_iec559) if constexpr (std::numeric_limits<Float64>::is_iec559)
return static_cast<Float64>(numerator) / denominator; /// allow division by zero return static_cast<Float64>(numerator) / denominator; /// allow division by zero
if (denominator == static_cast<Denominator>(0)) if (denominator == static_cast<Denominator>(0))
@ -43,12 +42,12 @@ struct RationalFraction
* @tparam Derived When deriving from this class, use the child class name as in CRTP, e.g. * @tparam Derived When deriving from this class, use the child class name as in CRTP, e.g.
* class Self : Agg<char, bool, bool, Self>. * class Self : Agg<char, bool, bool, Self>.
*/ */
template <class Denominator, bool BothZeroMeansNaN, class Derived> template <class Denominator, class Derived>
class AggregateFunctionAvgBase : public class AggregateFunctionAvgBase : public
IAggregateFunctionDataHelper<RationalFraction<Denominator, BothZeroMeansNaN>, Derived> IAggregateFunctionDataHelper<RationalFraction<Denominator>, Derived>
{ {
public: public:
using Fraction = RationalFraction<Denominator, BothZeroMeansNaN>; using Fraction = RationalFraction<Denominator>;
using Base = IAggregateFunctionDataHelper<Fraction, Derived>; using Base = IAggregateFunctionDataHelper<Fraction, Derived>;
explicit AggregateFunctionAvgBase(const DataTypes & argument_types_): Base(argument_types_, {}) {} explicit AggregateFunctionAvgBase(const DataTypes & argument_types_): Base(argument_types_, {}) {}
@ -87,10 +86,10 @@ public:
} }
}; };
class AggregateFunctionAvg final : public AggregateFunctionAvgBase<UInt64, false, AggregateFunctionAvg> class AggregateFunctionAvg final : public AggregateFunctionAvgBase<UInt64, AggregateFunctionAvg>
{ {
public: public:
using AggregateFunctionAvgBase<UInt64, false, AggregateFunctionAvg>::AggregateFunctionAvgBase; using AggregateFunctionAvgBase<UInt64, AggregateFunctionAvg>::AggregateFunctionAvgBase;
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const final void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const final
{ {

View File

@ -5,10 +5,10 @@
namespace DB namespace DB
{ {
class AggregateFunctionAvgWeighted final : public AggregateFunctionAvgBase<Float64, true, AggregateFunctionAvgWeighted> class AggregateFunctionAvgWeighted final : public AggregateFunctionAvgBase<Float64, AggregateFunctionAvgWeighted>
{ {
public: public:
using AggregateFunctionAvgBase<Float64, true, AggregateFunctionAvgWeighted>::AggregateFunctionAvgBase; using AggregateFunctionAvgBase<Float64, AggregateFunctionAvgWeighted>::AggregateFunctionAvgBase;
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override
{ {

View File

@ -1,2 +1,2 @@
0 nan
499.5 499.5