aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/byteSize.cpp
blob: 93a3a86641ac9160792cd679372194ca52ff29a4 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <DataTypes/DataTypesNumber.h>
#include <Columns/ColumnsNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/IFunction.h>


namespace DB
{

namespace
{

/** byteSize() - get the value size in number of bytes for accounting purposes.
  */
class FunctionByteSize : public IFunction
{
public:
    static constexpr auto name = "byteSize";
    static FunctionPtr create(ContextPtr)
    {
        return std::make_shared<FunctionByteSize>();
    }

    String getName() const override { return name; }
    bool useDefaultImplementationForNulls() const override { return false; }
    bool useDefaultImplementationForNothing() const override { return false; }
    bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
    bool useDefaultImplementationForSparseColumns() const override { return false; }
    bool isVariadic() const override { return true; }
    bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
    size_t getNumberOfArguments() const override { return 0; }

    DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
    {
        return std::make_shared<DataTypeUInt64>();
    }

    ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
    {
        size_t num_args = arguments.size();

        /// If the resulting size is constant, return constant column.

        bool all_constant = true;
        UInt64 constant_size = 0;
        for (size_t arg_num = 0; arg_num < num_args; ++arg_num)
        {
            if (arguments[arg_num].type->isValueUnambiguouslyRepresentedInFixedSizeContiguousMemoryRegion())
            {
                constant_size += arguments[arg_num].type->getSizeOfValueInMemory();
            }
            else
            {
                all_constant = false;
                break;
            }
        }

        if (all_constant)
            return result_type->createColumnConst(input_rows_count, constant_size);

        auto result_col = ColumnUInt64::create(input_rows_count);
        auto & vec_res = result_col->getData();
        for (size_t arg_num = 0; arg_num < num_args; ++arg_num)
        {
            const IColumn * column = arguments[arg_num].column.get();

            if (arg_num == 0)
                for (size_t row_num = 0; row_num < input_rows_count; ++row_num)
                    vec_res[row_num] = column->byteSizeAt(row_num);
            else
                for (size_t row_num = 0; row_num < input_rows_count; ++row_num)
                    vec_res[row_num] += column->byteSizeAt(row_num);
        }

        return result_col;
    }
};

}

REGISTER_FUNCTION(ByteSize)
{
    factory.registerFunction<FunctionByteSize>();
}

}