aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/runningConcurrency.cpp
blob: 61ee077d218a4198e061af007fcc1dd8e7ff92a3 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <Columns/ColumnVector.h>
#include <Core/callOnTypeIndex.h>
#include <DataTypes/IDataType.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeDateTime.h>
#include <DataTypes/DataTypeDateTime64.h>
#include <DataTypes/DataTypeNullable.h>
#include <Formats/FormatSettings.h>
#include <Functions/FunctionFactory.h>
#include <Functions/IFunction.h>
#include <IO/WriteBufferFromString.h>
#include <base/defines.h>
#include <set>

namespace DB
{
    namespace ErrorCodes
    {
        extern const int ILLEGAL_COLUMN;
        extern const int ILLEGAL_TYPE_OF_ARGUMENT;
        extern const int INCORRECT_DATA;
    }

    template <typename Name, typename ArgDataType, typename ConcurrencyDataType>
    class ExecutableFunctionRunningConcurrency : public IExecutableFunction
    {
    public:
        String getName() const override
        {
            return Name::name;
        }

        ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
        {
            using ColVecArg = typename ArgDataType::ColumnType;
            const ColVecArg * col_begin = checkAndGetColumn<ColVecArg>(arguments[0].column.get());
            const ColVecArg * col_end   = checkAndGetColumn<ColVecArg>(arguments[1].column.get());
            if (!col_begin || !col_end)
                throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Constant columns are not supported at the moment");
            const typename ColVecArg::Container & vec_begin = col_begin->getData();
            const typename ColVecArg::Container & vec_end   = col_end->getData();

            using ColVecConc = typename ConcurrencyDataType::ColumnType;
            using FieldType = typename ConcurrencyDataType::FieldType;
            typename ColVecConc::MutablePtr col_concurrency = ColVecConc::create(input_rows_count);
            typename ColVecConc::Container & vec_concurrency = col_concurrency->getData();

            std::multiset<typename ArgDataType::FieldType> ongoing_until;
            auto begin_serializaion = arguments[0].type->getDefaultSerialization();
            auto end_serialization = arguments[1].type->getDefaultSerialization();
            for (size_t i = 0; i < input_rows_count; ++i)
            {
                const auto begin = vec_begin[i];
                const auto end   = vec_end[i];

                if (unlikely(begin > end))
                {
                    const FormatSettings default_format{};
                    WriteBufferFromOwnString buf_begin, buf_end;
                    begin_serializaion->serializeTextQuoted(*(arguments[0].column), i, buf_begin, default_format);
                    end_serialization->serializeTextQuoted(*(arguments[1].column), i, buf_end, default_format);
                    throw Exception(ErrorCodes::INCORRECT_DATA, "Incorrect order of events: {} > {}", buf_begin.str(), buf_end.str());
                }

                ongoing_until.insert(end);

                // Erase all the elements from "ongoing_until" which
                // are less than or equal to "begin", i.e. durations
                // that have already ended. We consider "begin" to be
                // inclusive, and "end" to be exclusive.
                ongoing_until.erase(
                    ongoing_until.begin(), ongoing_until.upper_bound(begin));

                vec_concurrency[i] = static_cast<FieldType>(ongoing_until.size());
            }

            return col_concurrency;
        }

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

    template <typename Name, typename ArgDataType, typename ConcurrencyDataType>
    class FunctionBaseRunningConcurrency : public IFunctionBase
    {
    public:
        explicit FunctionBaseRunningConcurrency(DataTypes argument_types_, DataTypePtr return_type_)
            : argument_types(std::move(argument_types_))
            , return_type(std::move(return_type_)) {}

        String getName() const override
        {
            return Name::name;
        }

        const DataTypes & getArgumentTypes() const override
        {
            return argument_types;
        }

        const DataTypePtr & getResultType() const override
        {
            return return_type;
        }

        ExecutableFunctionPtr prepare(const ColumnsWithTypeAndName &) const override
        {
            return std::make_unique<ExecutableFunctionRunningConcurrency<Name, ArgDataType, ConcurrencyDataType>>();
        }

        bool isStateful() const override
        {
            return true;
        }

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

    private:
        DataTypes argument_types;
        DataTypePtr return_type;
    };

    template <typename Name, typename ConcurrencyDataType>
    class RunningConcurrencyOverloadResolver : public IFunctionOverloadResolver
    {
        template <typename T>
        struct TypeTag
        {
            using Type = T;
        };

        /// Call a polymorphic lambda with a type tag of src_type.
        template <typename F>
        void dispatchForSourceType(const IDataType & src_type, F && f) const
        {
            WhichDataType which(src_type);

            switch (which.idx)
            {
            case TypeIndex::Date:       f(TypeTag<DataTypeDate>());       break;
            case TypeIndex::DateTime:   f(TypeTag<DataTypeDateTime>());   break;
            case TypeIndex::DateTime64: f(TypeTag<DataTypeDateTime64>()); break;
            default:
                throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Arguments for function {} must be Date, DateTime, or DateTime64.", getName());
            }
        }

    public:
        static constexpr auto name = Name::name;

        static FunctionOverloadResolverPtr create(ContextPtr)
        {
            return std::make_unique<RunningConcurrencyOverloadResolver<Name, ConcurrencyDataType>>();
        }

        String getName() const override
        {
            return Name::name;
        }

        FunctionBasePtr buildImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & return_type) const override
        {
            // The type of the second argument must match with that of the first one.
            if (unlikely(!arguments[1].type->equals(*(arguments[0].type))))
            {
                throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Function {} must be called with two arguments having the same type.", getName());
            }

            DataTypes argument_types = { arguments[0].type, arguments[1].type };
            FunctionBasePtr base;
            dispatchForSourceType(*(arguments[0].type), [&](auto arg_type_tag) // Throws when the type is inappropriate.
            {
                using Tag = decltype(arg_type_tag);
                using ArgDataType = typename Tag::Type;

                base = std::make_unique<FunctionBaseRunningConcurrency<Name, ArgDataType, ConcurrencyDataType>>(argument_types, return_type);
            });

            return base;
        }

        DataTypePtr getReturnTypeImpl(const DataTypes &) const override
        {
            return std::make_shared<ConcurrencyDataType>();
        }

        size_t getNumberOfArguments() const override
        {
            return 2;
        }

        bool isInjective(const ColumnsWithTypeAndName &) const override
        {
            return false;
        }

        bool isStateful() const override
        {
            return true;
        }

        bool useDefaultImplementationForNulls() const override
        {
            return false;
        }
    };

    struct NameRunningConcurrency
    {
        static constexpr auto name = "runningConcurrency";
    };

    REGISTER_FUNCTION(RunningConcurrency)
    {
        factory.registerFunction<RunningConcurrencyOverloadResolver<NameRunningConcurrency, DataTypeUInt32>>();
    }
}