2018-12-02 02:47:47 +00:00
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/getLeastSupertype.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2018-12-07 03:20:27 +00:00
|
|
|
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
|
2018-12-02 02:47:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
/// Implements the CASE construction when it is
|
|
|
|
/// provided an expression. Users should not call this function.
|
|
|
|
class FunctionCaseWithExpression : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "caseWithExpression";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context_) { return std::make_shared<FunctionCaseWithExpression>(context_); }
|
2018-12-02 02:47:47 +00:00
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
explicit FunctionCaseWithExpression(ContextPtr context_) : context(context_) {}
|
2018-12-02 02:47:47 +00:00
|
|
|
bool isVariadic() const override { return true; }
|
2023-03-31 15:17:02 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return false; }
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
bool useDefaultImplementationForNothing() const override { return false; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2018-12-02 02:47:47 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & args) const override
|
|
|
|
{
|
2020-03-09 03:38:43 +00:00
|
|
|
if (args.empty())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION, "Function {} expects at least 1 arguments", getName());
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
/// See the comments in executeImpl() to understand why we actually have to
|
|
|
|
/// get the return type of a transform function.
|
|
|
|
|
|
|
|
/// Get the types of the arrays that we pass to the transform function.
|
|
|
|
DataTypes dst_array_types;
|
|
|
|
|
|
|
|
for (size_t i = 2; i < args.size() - 1; i += 2)
|
|
|
|
dst_array_types.push_back(args[i]);
|
|
|
|
|
2022-03-24 11:23:18 +00:00
|
|
|
// Type of the ELSE branch
|
|
|
|
dst_array_types.push_back(args.back());
|
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
return getLeastSupertype(dst_array_types);
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & args, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2018-12-02 02:47:47 +00:00
|
|
|
{
|
2020-03-09 03:38:43 +00:00
|
|
|
if (args.empty())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION, "Function {} expects at least 1 argument", getName());
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
/// In the following code, we turn the construction:
|
|
|
|
/// CASE expr WHEN val[0] THEN branch[0] ... WHEN val[N-1] then branch[N-1] ELSE branchN
|
|
|
|
/// into the construction transform(expr, src, dest, branchN)
|
|
|
|
/// where:
|
|
|
|
/// src = [val[0], val[1], ..., val[N-1]]
|
|
|
|
/// dst = [branch[0], ..., branch[N-1]]
|
|
|
|
/// then we perform it.
|
|
|
|
|
|
|
|
/// Create the arrays required by the transform function.
|
|
|
|
ColumnsWithTypeAndName src_array_elems;
|
|
|
|
DataTypes src_array_types;
|
|
|
|
|
|
|
|
ColumnsWithTypeAndName dst_array_elems;
|
|
|
|
DataTypes dst_array_types;
|
|
|
|
|
|
|
|
for (size_t i = 1; i < (args.size() - 1); ++i)
|
|
|
|
{
|
|
|
|
if (i % 2)
|
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
src_array_elems.push_back(args[i]);
|
|
|
|
src_array_types.push_back(args[i].type);
|
2018-12-02 02:47:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
dst_array_elems.push_back(args[i]);
|
|
|
|
dst_array_types.push_back(args[i].type);
|
2018-12-02 02:47:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr src_array_type = std::make_shared<DataTypeArray>(getLeastSupertype(src_array_types));
|
|
|
|
DataTypePtr dst_array_type = std::make_shared<DataTypeArray>(getLeastSupertype(dst_array_types));
|
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
ColumnWithTypeAndName src_array_col{nullptr, src_array_type, ""};
|
|
|
|
ColumnWithTypeAndName dst_array_col{nullptr, dst_array_type, ""};
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
auto fun_array = FunctionFactory::instance().get("array", context);
|
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
src_array_col.column = fun_array->build(src_array_elems)->execute(src_array_elems, src_array_type, input_rows_count);
|
|
|
|
dst_array_col.column = fun_array->build(dst_array_elems)->execute(dst_array_elems, dst_array_type, input_rows_count);
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
/// Execute transform.
|
2020-10-17 16:48:53 +00:00
|
|
|
ColumnsWithTypeAndName transform_args{args.front(), src_array_col, dst_array_col, args.back()};
|
|
|
|
return FunctionFactory::instance().get("transform", context)->build(transform_args)
|
|
|
|
->execute(transform_args, result_type, input_rows_count);
|
2018-12-02 02:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-06-01 12:20:52 +00:00
|
|
|
ContextPtr context;
|
2018-12-02 02:47:47 +00:00
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(CaseWithExpression)
|
2018-12-02 02:47:47 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionCaseWithExpression>();
|
|
|
|
|
|
|
|
/// These are obsolete function names.
|
|
|
|
factory.registerFunction<FunctionCaseWithExpression>("caseWithExpr");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|