2020-06-18 15:21:19 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
2020-11-11 22:52:07 +00:00
|
|
|
#include <AggregateFunctions/StatCommon.h>
|
2020-06-18 15:21:19 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnTuple.h>
|
|
|
|
#include <Common/assert_cast.h>
|
2020-11-11 14:36:14 +00:00
|
|
|
#include <Common/PODArray_fwd.h>
|
2020-09-15 09:55:57 +00:00
|
|
|
#include <common/types.h>
|
2020-06-18 15:21:19 +00:00
|
|
|
#include <DataTypes/DataTypesDecimal.h>
|
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
|
|
|
|
#include <Common/ArenaAllocator.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-11-11 22:52:07 +00:00
|
|
|
|
|
|
|
struct RankCorrelationData : public StatisticalSample<Float64, Float64>
|
2020-06-18 15:21:19 +00:00
|
|
|
{
|
2020-11-12 19:17:15 +00:00
|
|
|
Float64 getResult()
|
|
|
|
{
|
2020-11-11 22:52:07 +00:00
|
|
|
RanksArray ranks_x;
|
|
|
|
std::tie(ranks_x, std::ignore) = computeRanksAndTieCorrection(this->x);
|
|
|
|
|
|
|
|
RanksArray ranks_y;
|
|
|
|
std::tie(ranks_y, std::ignore) = computeRanksAndTieCorrection(this->y);
|
2020-06-18 15:21:19 +00:00
|
|
|
|
2020-11-11 22:52:07 +00:00
|
|
|
/// In our case sizes of both samples are equal.
|
|
|
|
const auto size = this->size_x;
|
2020-11-11 14:36:14 +00:00
|
|
|
|
2020-11-11 22:52:07 +00:00
|
|
|
/// Count d^2 sum
|
|
|
|
Float64 answer = 0;
|
|
|
|
for (size_t j = 0; j < size; ++j)
|
|
|
|
answer += (ranks_x[j] - ranks_y[j]) * (ranks_x[j] - ranks_y[j]);
|
2020-06-18 15:21:19 +00:00
|
|
|
|
2020-11-11 22:52:07 +00:00
|
|
|
answer *= 6;
|
|
|
|
answer /= size * (size * size - 1);
|
|
|
|
answer = 1 - answer;
|
|
|
|
return answer;
|
|
|
|
}
|
2020-06-18 15:21:19 +00:00
|
|
|
};
|
|
|
|
|
2020-09-02 12:00:54 +00:00
|
|
|
class AggregateFunctionRankCorrelation :
|
2020-11-11 22:52:07 +00:00
|
|
|
public IAggregateFunctionDataHelper<RankCorrelationData, AggregateFunctionRankCorrelation>
|
2020-06-18 15:21:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-09-02 12:00:54 +00:00
|
|
|
explicit AggregateFunctionRankCorrelation(const DataTypes & arguments)
|
2020-11-11 22:52:07 +00:00
|
|
|
:IAggregateFunctionDataHelper<RankCorrelationData, AggregateFunctionRankCorrelation> ({arguments}, {})
|
2020-09-02 12:00:54 +00:00
|
|
|
{}
|
2020-06-18 15:21:19 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
2020-09-02 12:00:54 +00:00
|
|
|
return "rankCorr";
|
2020-06-18 15:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
2020-08-24 19:49:55 +00:00
|
|
|
return std::make_shared<DataTypeNumber<Float64>>();
|
2020-06-18 15:21:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void add(AggregateDataPtr __restrict place, const IColumn ** columns, size_t row_num, Arena * arena) const override
|
2020-06-18 15:21:19 +00:00
|
|
|
{
|
2020-11-11 22:52:07 +00:00
|
|
|
Float64 new_x = columns[0]->getFloat64(row_num);
|
|
|
|
Float64 new_y = columns[1]->getFloat64(row_num);
|
|
|
|
this->data(place).addX(new_x, arena);
|
|
|
|
this->data(place).addY(new_y, arena);
|
2020-06-18 15:21:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
2020-06-18 15:21:19 +00:00
|
|
|
{
|
|
|
|
auto & a = this->data(place);
|
|
|
|
auto & b = this->data(rhs);
|
|
|
|
|
2020-11-11 22:52:07 +00:00
|
|
|
a.merge(b, arena);
|
2020-06-18 15:21:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void serialize(ConstAggregateDataPtr __restrict place, WriteBuffer & buf) const override
|
2020-06-18 15:21:19 +00:00
|
|
|
{
|
2020-11-11 22:52:07 +00:00
|
|
|
this->data(place).write(buf);
|
2020-06-18 15:21:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void deserialize(AggregateDataPtr __restrict place, ReadBuffer & buf, Arena * arena) const override
|
2020-06-18 15:21:19 +00:00
|
|
|
{
|
2020-11-11 22:52:07 +00:00
|
|
|
this->data(place).read(buf, arena);
|
2020-06-18 15:21:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void insertResultInto(AggregateDataPtr __restrict place, IColumn & to, Arena *) const override
|
2020-06-18 15:21:19 +00:00
|
|
|
{
|
2020-11-11 22:52:07 +00:00
|
|
|
auto answer = this->data(place).getResult();
|
2020-06-18 15:21:19 +00:00
|
|
|
|
2020-08-24 19:49:55 +00:00
|
|
|
auto & column = static_cast<ColumnVector<Float64> &>(to);
|
|
|
|
column.getData().push_back(answer);
|
2020-06-18 15:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2020-06-18 17:52:35 +00:00
|
|
|
};
|