aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/AggregateFunctions/AggregateFunctionCramersV.cpp
blob: 07b691141bce91970b1f39ced15a615270df5b49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <AggregateFunctions/CrossTab.h>
#include <AggregateFunctions/FactoryHelpers.h>
#include <memory>
#include <cmath>


namespace DB
{

namespace
{

struct CramersVData : CrossTabData
{
    static const char * getName()
    {
        return "cramersV";
    }

    Float64 getResult() const
    {
        if (count < 2)
            return std::numeric_limits<Float64>::quiet_NaN();

        UInt64 q = std::min(count_a.size(), count_b.size());
        return sqrt(getPhiSquared() / (q - 1));
    }
};

}

void registerAggregateFunctionCramersV(AggregateFunctionFactory & factory)
{
    factory.registerFunction(CramersVData::getName(),
        [](const std::string & name, const DataTypes & argument_types, const Array & parameters, const Settings *)
        {
            assertBinary(name, argument_types);
            assertNoParameters(name, parameters);
            return std::make_shared<AggregateFunctionCrossTab<CramersVData>>(argument_types);
        });
}

}