ClickHouse

Форк
0
/
finalizeAggregation.cpp 
79 строк · 2.4 Кб
1
#include <Functions/IFunction.h>
2
#include <Functions/FunctionFactory.h>
3
#include <Functions/FunctionHelpers.h>
4
#include <DataTypes/DataTypeAggregateFunction.h>
5
#include <Columns/ColumnAggregateFunction.h>
6
#include <Common/typeid_cast.h>
7

8

9
namespace DB
10
{
11
namespace ErrorCodes
12
{
13
    extern const int ILLEGAL_COLUMN;
14
    extern const int ILLEGAL_TYPE_OF_ARGUMENT;
15
}
16

17
namespace
18
{
19

20
/** finalizeAggregation(agg_state) - get the result from the aggregation state.
21
  * Takes state of aggregate function. Returns result of aggregation (finalized state).
22
  */
23
class FunctionFinalizeAggregation : public IFunction
24
{
25
public:
26
    static constexpr auto name = "finalizeAggregation";
27
    static FunctionPtr create(ContextPtr)
28
    {
29
        return std::make_shared<FunctionFinalizeAggregation>();
30
    }
31

32
    String getName() const override
33
    {
34
        return name;
35
    }
36

37
    size_t getNumberOfArguments() const override
38
    {
39
        return 1;
40
    }
41

42
    bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
43

44
    bool useDefaultImplementationForConstants() const override { return true; }
45

46
    DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
47
    {
48
        const DataTypeAggregateFunction * type = checkAndGetDataType<DataTypeAggregateFunction>(arguments[0].get());
49
        if (!type)
50
        {
51
            throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
52
                "Argument for function '{}' must have type AggregateFunction - state of aggregate function."
53
                " Got '{}' instead", getName(), arguments[0]->getName());
54
        }
55

56
        return type->getReturnType();
57
    }
58

59
    ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
60
    {
61
        auto column = arguments.at(0).column;
62
        if (!typeid_cast<const ColumnAggregateFunction *>(column.get()))
63
            throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of first argument of function {}",
64
                    arguments.at(0).column->getName(), getName());
65

66
        /// Column is copied here, because there is no guarantee that we own it.
67
        auto mut_column = IColumn::mutate(std::move(column));
68
        return ColumnAggregateFunction::convertToValues(std::move(mut_column));
69
    }
70
};
71

72
}
73

74
REGISTER_FUNCTION(FinalizeAggregation)
75
{
76
    factory.registerFunction<FunctionFinalizeAggregation>();
77
}
78

79
}
80

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.