ClickHouse/src/Functions/runningConcurrency.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

222 lines
7.9 KiB
C++
Raw Normal View History

#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>
2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
#include <IO/WriteBufferFromString.h>
2021-10-02 07:13:14 +00:00
#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>
2021-05-15 17:33:15 +00:00
class ExecutableFunctionRunningConcurrency : public IExecutableFunction
{
public:
String getName() const override
{
return Name::name;
}
2021-05-15 17:33:15 +00:00
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;
2021-03-13 18:05:18 +00:00
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;
2021-03-13 18:05:18 +00:00
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>
2021-05-15 17:33:15 +00:00
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;
}
2021-05-15 17:33:15 +00:00
ExecutableFunctionPtr prepare(const ColumnsWithTypeAndName &) const override
{
return std::make_unique<ExecutableFunctionRunningConcurrency<Name, ArgDataType, ConcurrencyDataType>>();
}
bool isStateful() const override
{
return true;
}
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
private:
DataTypes argument_types;
DataTypePtr return_type;
};
template <typename Name, typename ConcurrencyDataType>
2021-05-15 17:33:15 +00:00
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;
2021-06-01 12:20:52 +00:00
static FunctionOverloadResolverPtr create(ContextPtr)
{
return std::make_unique<RunningConcurrencyOverloadResolver<Name, ConcurrencyDataType>>();
}
String getName() const override
{
return Name::name;
}
2021-05-15 17:33:15 +00:00
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 };
2021-05-15 17:33:15 +00:00
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;
}
2021-05-15 17:33:15 +00:00
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>>();
}
}