2018-12-02 02:47:47 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionIfBase.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Interpreters/castColumn.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2018-12-02 02:47:47 +00:00
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
|
|
|
#include <DataTypes/getLeastSupertype.h>
|
2021-04-22 15:14:58 +00:00
|
|
|
#include <Columns/MaskOperations.h>
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2018-12-02 02:47:47 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
/// Function multiIf, which generalizes the function if.
|
|
|
|
///
|
|
|
|
/// Syntax: multiIf(cond_1, then_1, ..., cond_N, then_N, else)
|
|
|
|
/// where N >= 1.
|
|
|
|
///
|
|
|
|
/// For all 1 <= i <= N, "cond_i" has type UInt8.
|
2019-03-23 01:27:08 +00:00
|
|
|
/// Types of all the branches "then_i" and "else" have a common type.
|
2018-12-02 02:47:47 +00:00
|
|
|
///
|
|
|
|
/// Additionally the arguments, conditions or branches, support nullable types
|
|
|
|
/// and the NULL value, with a NULL condition treated as false.
|
2021-05-07 08:43:06 +00:00
|
|
|
class FunctionMultiIf final : public FunctionIfBase
|
2018-12-02 02:47:47 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "multiIf";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionMultiIf>(); }
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
bool isVariadic() const override { return true; }
|
2021-04-20 15:55:33 +00:00
|
|
|
bool isShortCircuit() const override { return true; }
|
2018-12-02 02:47:47 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
2020-06-28 21:14:38 +00:00
|
|
|
|
2019-10-02 17:51:00 +00:00
|
|
|
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t number_of_arguments) const override
|
|
|
|
{
|
|
|
|
ColumnNumbers args;
|
|
|
|
for (size_t i = 0; i + 1 < number_of_arguments; i += 2)
|
|
|
|
args.push_back(i);
|
|
|
|
return args;
|
|
|
|
}
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & args) const override
|
|
|
|
{
|
|
|
|
/// Arguments are the following: cond1, then1, cond2, then2, ... condN, thenN, else.
|
|
|
|
|
|
|
|
auto for_conditions = [&args](auto && f)
|
|
|
|
{
|
|
|
|
size_t conditions_end = args.size() - 1;
|
|
|
|
for (size_t i = 0; i < conditions_end; i += 2)
|
|
|
|
f(args[i]);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto for_branches = [&args](auto && f)
|
|
|
|
{
|
|
|
|
size_t branches_end = args.size();
|
|
|
|
for (size_t i = 1; i < branches_end; i += 2)
|
|
|
|
f(args[i]);
|
|
|
|
f(args.back());
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!(args.size() >= 3 && args.size() % 2 == 1))
|
|
|
|
throw Exception{"Invalid number of arguments for function " + getName(),
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
|
|
|
|
|
|
|
for_conditions([&](const DataTypePtr & arg)
|
|
|
|
{
|
|
|
|
const IDataType * nested_type;
|
|
|
|
if (arg->isNullable())
|
|
|
|
{
|
|
|
|
if (arg->onlyNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const DataTypeNullable & nullable_type = static_cast<const DataTypeNullable &>(*arg);
|
|
|
|
nested_type = nullable_type.getNestedType().get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nested_type = arg.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WhichDataType(nested_type).isUInt8())
|
|
|
|
throw Exception{"Illegal type " + arg->getName() + " of argument (condition) "
|
|
|
|
"of function " + getName() + ". Must be UInt8.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
});
|
|
|
|
|
|
|
|
DataTypes types_of_branches;
|
|
|
|
types_of_branches.reserve(args.size() / 2 + 1);
|
|
|
|
|
|
|
|
for_branches([&](const DataTypePtr & arg)
|
|
|
|
{
|
|
|
|
types_of_branches.emplace_back(arg);
|
|
|
|
});
|
|
|
|
|
2019-05-10 10:42:00 +00:00
|
|
|
return getLeastSupertype(types_of_branches);
|
2018-12-02 02:47:47 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 15:14:58 +00:00
|
|
|
void executeShortCircuitArguments(ColumnsWithTypeAndName & arguments) const override
|
2018-12-02 02:47:47 +00:00
|
|
|
{
|
2021-04-22 15:14:58 +00:00
|
|
|
executeColumnIfNeeded(arguments[0]);
|
2021-04-22 20:05:50 +00:00
|
|
|
IColumn::Filter current_mask;
|
|
|
|
IColumn::Filter mask_disjunctions = IColumn::Filter(arguments[0].column->size(), 0);
|
|
|
|
|
2021-04-20 15:55:33 +00:00
|
|
|
Field default_value = 0;
|
|
|
|
size_t i = 1;
|
2021-04-22 15:14:58 +00:00
|
|
|
while (i < arguments.size())
|
2021-04-20 15:55:33 +00:00
|
|
|
{
|
2021-04-22 20:05:50 +00:00
|
|
|
getMaskFromColumn(arguments[i - 1].column, current_mask);
|
|
|
|
disjunctionMasks(mask_disjunctions, current_mask);
|
2021-04-22 15:14:58 +00:00
|
|
|
if (isColumnFunction(*arguments[i].column))
|
2021-04-22 20:05:50 +00:00
|
|
|
maskedExecute(arguments[i], current_mask);
|
2021-04-20 15:55:33 +00:00
|
|
|
|
2021-04-22 15:14:58 +00:00
|
|
|
++i;
|
2021-04-22 20:05:50 +00:00
|
|
|
|
2021-04-22 15:14:58 +00:00
|
|
|
if (isColumnFunction(*arguments[i].column))
|
2021-04-22 20:05:50 +00:00
|
|
|
maskedExecute(arguments[i], mask_disjunctions, true, &default_value);
|
|
|
|
|
2021-04-20 15:55:33 +00:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 15:14:58 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & args, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2021-04-20 15:55:33 +00:00
|
|
|
{
|
2018-12-02 02:47:47 +00:00
|
|
|
/** We will gather values from columns in branches to result column,
|
|
|
|
* depending on values of conditions.
|
|
|
|
*/
|
|
|
|
struct Instruction
|
|
|
|
{
|
|
|
|
const IColumn * condition = nullptr;
|
|
|
|
const IColumn * source = nullptr;
|
|
|
|
|
|
|
|
bool condition_always_true = false;
|
|
|
|
bool condition_is_nullable = false;
|
|
|
|
bool source_is_constant = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Instruction> instructions;
|
|
|
|
instructions.reserve(args.size() / 2 + 1);
|
|
|
|
|
|
|
|
Columns converted_columns_holder;
|
|
|
|
converted_columns_holder.reserve(instructions.size());
|
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
const DataTypePtr & return_type = result_type;
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < args.size(); i += 2)
|
|
|
|
{
|
|
|
|
Instruction instruction;
|
|
|
|
size_t source_idx = i + 1;
|
|
|
|
|
2021-01-24 21:35:49 +00:00
|
|
|
bool last_else_branch = source_idx == args.size();
|
|
|
|
|
|
|
|
if (last_else_branch)
|
2018-12-02 02:47:47 +00:00
|
|
|
{
|
|
|
|
/// The last, "else" branch can be treated as a branch with always true condition "else if (true)".
|
|
|
|
--source_idx;
|
|
|
|
instruction.condition_always_true = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
const ColumnWithTypeAndName & cond_col = args[i];
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
/// We skip branches that are always false.
|
|
|
|
/// If we encounter a branch that is always true, we can finish.
|
|
|
|
|
|
|
|
if (cond_col.column->onlyNull())
|
|
|
|
continue;
|
|
|
|
|
2021-01-24 21:35:49 +00:00
|
|
|
if (const auto * column_const = checkAndGetColumn<ColumnConst>(*cond_col.column))
|
2018-12-02 02:47:47 +00:00
|
|
|
{
|
2021-01-24 21:35:49 +00:00
|
|
|
Field value = column_const->getField();
|
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
if (value.isNull())
|
|
|
|
continue;
|
|
|
|
if (value.get<UInt64>() == 0)
|
|
|
|
continue;
|
2021-01-24 21:35:49 +00:00
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
instruction.condition_always_true = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-01 11:44:19 +00:00
|
|
|
if (isColumnNullable(*cond_col.column))
|
2018-12-02 02:47:47 +00:00
|
|
|
instruction.condition_is_nullable = true;
|
|
|
|
|
|
|
|
instruction.condition = cond_col.column.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
const ColumnWithTypeAndName & source_col = args[source_idx];
|
2018-12-02 02:47:47 +00:00
|
|
|
if (source_col.type->equals(*return_type))
|
|
|
|
{
|
|
|
|
instruction.source = source_col.column.get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Cast all columns to result type.
|
2020-04-14 21:05:45 +00:00
|
|
|
converted_columns_holder.emplace_back(castColumn(source_col, return_type));
|
2018-12-02 02:47:47 +00:00
|
|
|
instruction.source = converted_columns_holder.back().get();
|
|
|
|
}
|
|
|
|
|
2019-06-27 19:28:52 +00:00
|
|
|
if (instruction.source && isColumnConst(*instruction.source))
|
2018-12-02 02:47:47 +00:00
|
|
|
instruction.source_is_constant = true;
|
|
|
|
|
|
|
|
instructions.emplace_back(std::move(instruction));
|
|
|
|
|
|
|
|
if (instructions.back().condition_always_true)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableColumnPtr res = return_type->createColumn();
|
|
|
|
|
2021-01-24 21:35:49 +00:00
|
|
|
/// Special case if first instruction condition is always true and source is constant
|
2021-01-25 08:08:10 +00:00
|
|
|
if (instructions.size() == 1 && instructions.front().source_is_constant
|
2021-01-24 21:35:49 +00:00
|
|
|
&& instructions.front().condition_always_true)
|
|
|
|
{
|
|
|
|
auto & instruction = instructions.front();
|
|
|
|
res->insertFrom(assert_cast<const ColumnConst &>(*instruction.source).getDataColumn(), 0);
|
|
|
|
return ColumnConst::create(std::move(res), instruction.source->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t rows = input_rows_count;
|
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
for (size_t i = 0; i < rows; ++i)
|
|
|
|
{
|
|
|
|
for (const auto & instruction : instructions)
|
|
|
|
{
|
|
|
|
bool insert = false;
|
|
|
|
|
|
|
|
if (instruction.condition_always_true)
|
|
|
|
insert = true;
|
|
|
|
else if (!instruction.condition_is_nullable)
|
2019-08-21 02:28:04 +00:00
|
|
|
insert = assert_cast<const ColumnUInt8 &>(*instruction.condition).getData()[i];
|
2018-12-02 02:47:47 +00:00
|
|
|
else
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const ColumnNullable & condition_nullable = assert_cast<const ColumnNullable &>(*instruction.condition);
|
|
|
|
const ColumnUInt8 & condition_nested = assert_cast<const ColumnUInt8 &>(condition_nullable.getNestedColumn());
|
2018-12-02 02:47:47 +00:00
|
|
|
const NullMap & condition_null_map = condition_nullable.getNullMapData();
|
|
|
|
|
|
|
|
insert = !condition_null_map[i] && condition_nested.getData()[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (insert)
|
|
|
|
{
|
|
|
|
if (!instruction.source_is_constant)
|
|
|
|
res->insertFrom(*instruction.source, i);
|
|
|
|
else
|
2019-08-21 02:28:04 +00:00
|
|
|
res->insertFrom(assert_cast<const ColumnConst &>(*instruction.source).getDataColumn(), 0);
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
return res;
|
2018-12-02 02:47:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
void registerFunctionMultiIf(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionMultiIf>();
|
|
|
|
|
|
|
|
/// These are obsolete function names.
|
|
|
|
factory.registerFunction<FunctionMultiIf>("caseWithoutExpr");
|
|
|
|
factory.registerFunction<FunctionMultiIf>("caseWithoutExpression");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|