added getFloat64 to ColumnDecimal, updated tests

This commit is contained in:
myrrc 2020-10-26 16:27:58 +03:00
parent d8370116c1
commit 5b4981b466
7 changed files with 62 additions and 13 deletions

View File

@ -14,7 +14,7 @@ namespace DB
template <class Numerator, class Denominator>
struct RationalFraction
{
constexpr RationalFraction(): numerator(0), denominator(0) {}
constexpr RationalFraction(): numerator(0), denominator(0) {}
Numerator numerator;
Denominator denominator;
@ -114,8 +114,7 @@ public:
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const final
{
const auto & column = static_cast<const ColumnVector<Float64> &>(*columns[0]);
this->data(place).numerator += column.getData()[row_num];
this->data(place).numerator += columns[0]->getFloat64(row_num);
++this->data(place).denominator;
}

View File

@ -12,11 +12,8 @@ public:
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override
{
const auto & values = static_cast<const ColumnVector<Float64> &>(*columns[0]);
const auto & weights = static_cast<const ColumnVector<Float64> &>(*columns[1]);
const auto value = values.getData()[row_num];
const auto weight = weights.getData()[row_num];
const auto value = columns[0]->getFloat64(row_num);
const auto weight = columns[1]->getFloat64(row_num);
this->data(place).numerator += value * weight;
this->data(place).denominator += weight;

View File

@ -3,6 +3,7 @@
#include <cmath>
#include <Common/typeid_cast.h>
#include "Core/DecimalFunctions.h"
#include <Columns/IColumn.h>
#include <Columns/IColumnImpl.h>
#include <Columns/ColumnVectorHelper.h>
@ -182,6 +183,8 @@ public:
throw Exception("getDataAt() is not implemented for big integers", ErrorCodes::NOT_IMPLEMENTED);
}
Float64 getFloat64(size_t n) const final { return DecimalUtils::convertTo<Float64>(data[n], scale); }
StringRef serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const override;
const char * deserializeAndInsertFromArena(const char * pos) override;
void updateHashWithValue(size_t n, SipHash & hash) const override;

View File

@ -1,3 +0,0 @@
2.3333333333333335
nan
1

18
tests/queries/0_stateless/01035_avg_weighted.sh Executable file → Normal file
View File

@ -6,10 +6,12 @@ CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
${CLICKHOUSE_CLIENT} --query="SELECT avgWeighted(x, weight) FROM (SELECT t.1 AS x, t.2 AS weight FROM (SELECT arrayJoin([(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]) AS t));"
${CLICKHOUSE_CLIENT} --query="SELECT avgWeighted(x, weight) FROM (SELECT t.1 AS x, t.2 AS weight FROM (SELECT arrayJoin([(1, 0), (2, 0), (3, 0), (4, 0), (5, 0)]) AS t));"
# Decimals not tested in types' combinations as they
# 1) Require different initialization (precision + scale)
# 2) Won't be used in these functions often
types=("Int8" "Int16" "Int32" "Int64" "Int128" "Int256"
"UInt8" "UInt16" "UInt32" "UInt64" "UInt128" "UInt256"
"Float32" "Float64"
"Decimal32" "Decimal64" "Decimal128" "Decimal256")
"Float32" "Float64")
for left in "${types[@]}"
do
@ -21,5 +23,17 @@ do
done
done
# Decimal types
dtypes=("32" "64" "128" "256")
for left in "${dtypes[@]}"
do
for right in "${dtypes[@]}"
do
${CLICKHOUSE_CLIENT} --query="SELECT avgWeighted(toDecimal${left}(2, 4), toDecimal${right}(1, 4))"
done
done
echo "$(${CLICKHOUSE_CLIENT} --server_logs_file=/dev/null --query="SELECT avgWeighted(['string'], toFloat64(0))" 2>&1)" \
| grep -c 'Code: 43. DB::Exception: .* DB::Exception:.* Types .* of arguments are non-conforming as arguments for aggregate function avgWeighted'

View File

@ -0,0 +1,39 @@
#!/usr/bin/env bash
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. "$CUR_DIR"/../shell_config.sh
${CLICKHOUSE_CLIENT} --query="SELECT avgWeighted(x, weight) FROM (SELECT t.1 AS x, t.2 AS weight FROM (SELECT arrayJoin([(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]) AS t));"
${CLICKHOUSE_CLIENT} --query="SELECT avgWeighted(x, weight) FROM (SELECT t.1 AS x, t.2 AS weight FROM (SELECT arrayJoin([(1, 0), (2, 0), (3, 0), (4, 0), (5, 0)]) AS t));"
# Decimals not tested in types' combinations as they
# 1) Require different initialization (precision + scale)
# 2) Won't be used in these functions often
types=("Int8" "Int16" "Int32" "Int64" "Int128" "Int256"
"UInt8" "UInt16" "UInt32" "UInt64" "UInt128" "UInt256"
"Float32" "Float64")
for left in "${types[@]}"
do
for right in "${types[@]}"
do
${CLICKHOUSE_CLIENT} --query="SELECT avgWeighted(x, w) FROM values('x ${left}, w ${right}', (4, 1), (1, 0), (10, 2))"
${CLICKHOUSE_CLIENT} --query="SELECT avgWeighted(x, w) FROM values('x ${left}, w ${right}', (8, 1), (122, 0))"
${CLICKHOUSE_CLIENT} --query="SELECT avgWeighted(x, w) FROM values('x ${left}, w ${right}', (0, 0), (1, 0))"
done
done
# Decimal types
dtypes=("32" "64" "128" "256")
for left in "${dtypes[@]}"
do
for right in "${dtypes[@]}"
do
${CLICKHOUSE_CLIENT} --query="SELECT avgWeighted(toDecimal${left}(2, 4), toDecimal${right}(1, 4))"
done
done
echo "$(${CLICKHOUSE_CLIENT} --server_logs_file=/dev/null --query="SELECT avgWeighted(['string'], toFloat64(0))" 2>&1)" \
| grep -c 'Code: 43. DB::Exception: .* DB::Exception:.* Types .* of arguments are non-conforming as arguments for aggregate function avgWeighted'