2021-04-10 23:33:54 +00:00
|
|
|
#include <Columns/ColumnLowCardinality.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
|
|
|
#include <Core/ColumnNumbers.h>
|
2018-09-08 01:24:09 +00:00
|
|
|
#include <DataTypes/DataTypeNothing.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2018-09-08 01:24:09 +00:00
|
|
|
#include <DataTypes/getLeastSupertype.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-09-08 01:24:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2018-09-08 01:24:09 +00:00
|
|
|
|
|
|
|
/// Implements the function coalesce which takes a set of arguments and
|
|
|
|
/// returns the value of the leftmost non-null argument. If no such value is
|
|
|
|
/// found, coalesce() returns NULL.
|
|
|
|
class FunctionCoalesce : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "coalesce";
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
2018-09-08 01:24:09 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionCoalesce>(context);
|
|
|
|
}
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
explicit FunctionCoalesce(ContextPtr context_) : context(context_) {}
|
2018-09-08 01:24:09 +00:00
|
|
|
|
|
|
|
std::string getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
bool isVariadic() const override { return true; }
|
2021-05-17 13:06:11 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(ColumnsWithTypeAndName & /*arguments*/) const override { return false; }
|
2018-09-08 01:24:09 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
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)
|
|
|
|
args.push_back(i);
|
|
|
|
return args;
|
|
|
|
}
|
2018-09-08 01:24:09 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
/// Skip all NULL arguments. If any argument is non-Nullable, skip all next arguments.
|
|
|
|
DataTypes filtered_args;
|
|
|
|
filtered_args.reserve(arguments.size());
|
|
|
|
for (const auto & arg : arguments)
|
|
|
|
{
|
|
|
|
if (arg->onlyNull())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
filtered_args.push_back(arg);
|
|
|
|
|
|
|
|
if (!arg->isNullable())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypes new_args;
|
|
|
|
for (size_t i = 0; i < filtered_args.size(); ++i)
|
|
|
|
{
|
|
|
|
bool is_last = i + 1 == filtered_args.size();
|
|
|
|
|
|
|
|
if (is_last)
|
|
|
|
new_args.push_back(filtered_args[i]);
|
|
|
|
else
|
|
|
|
new_args.push_back(removeNullable(filtered_args[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_args.empty())
|
|
|
|
return std::make_shared<DataTypeNullable>(std::make_shared<DataTypeNothing>());
|
|
|
|
if (new_args.size() == 1)
|
|
|
|
return new_args.front();
|
|
|
|
|
|
|
|
auto res = getLeastSupertype(new_args);
|
|
|
|
|
|
|
|
/// if last argument is not nullable, result should be also not nullable
|
|
|
|
if (!new_args.back()->isNullable() && res->isNullable())
|
|
|
|
res = removeNullable(res);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2018-09-08 01:24:09 +00:00
|
|
|
{
|
|
|
|
/// coalesce(arg0, arg1, ..., argN) is essentially
|
|
|
|
/// multiIf(isNotNull(arg0), assumeNotNull(arg0), isNotNull(arg1), assumeNotNull(arg1), ..., argN)
|
|
|
|
/// with constant NULL arguments removed.
|
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
ColumnsWithTypeAndName filtered_args;
|
2018-09-08 01:24:09 +00:00
|
|
|
filtered_args.reserve(arguments.size());
|
|
|
|
for (const auto & arg : arguments)
|
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
const auto & type = arg.type;
|
2018-09-08 01:24:09 +00:00
|
|
|
|
|
|
|
if (type->onlyNull())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
filtered_args.push_back(arg);
|
|
|
|
|
|
|
|
if (!type->isNullable())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto is_not_null = FunctionFactory::instance().get("isNotNull", context);
|
|
|
|
auto assume_not_null = FunctionFactory::instance().get("assumeNotNull", context);
|
|
|
|
auto multi_if = FunctionFactory::instance().get("multiIf", context);
|
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
ColumnsWithTypeAndName multi_if_args;
|
|
|
|
ColumnsWithTypeAndName tmp_args(1);
|
2018-09-08 01:24:09 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < filtered_args.size(); ++i)
|
|
|
|
{
|
|
|
|
bool is_last = i + 1 == filtered_args.size();
|
|
|
|
|
|
|
|
if (is_last)
|
|
|
|
{
|
|
|
|
multi_if_args.push_back(filtered_args[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
tmp_args[0] = filtered_args[i];
|
|
|
|
auto & cond = multi_if_args.emplace_back(ColumnWithTypeAndName{nullptr, std::make_shared<DataTypeUInt8>(), ""});
|
|
|
|
cond.column = is_not_null->build(tmp_args)->execute(tmp_args, cond.type, input_rows_count);
|
2018-09-08 01:24:09 +00:00
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
tmp_args[0] = filtered_args[i];
|
|
|
|
auto & val = multi_if_args.emplace_back(ColumnWithTypeAndName{nullptr, removeNullable(filtered_args[i].type), ""});
|
|
|
|
val.column = assume_not_null->build(tmp_args)->execute(tmp_args, val.type, input_rows_count);
|
2018-09-08 01:24:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If all arguments appeared to be NULL.
|
|
|
|
if (multi_if_args.empty())
|
2020-10-17 16:48:53 +00:00
|
|
|
return result_type->createColumnConstWithDefaultValue(input_rows_count);
|
2018-09-08 01:24:09 +00:00
|
|
|
|
|
|
|
if (multi_if_args.size() == 1)
|
2020-10-17 16:48:53 +00:00
|
|
|
return multi_if_args.front().column;
|
2018-09-08 01:24:09 +00:00
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
ColumnPtr res = multi_if->build(multi_if_args)->execute(multi_if_args, result_type, input_rows_count);
|
2018-09-08 01:24:09 +00:00
|
|
|
|
|
|
|
/// if last argument is not nullable, result should be also not nullable
|
2020-10-17 16:48:53 +00:00
|
|
|
if (!multi_if_args.back().column->isNullable() && res->isNullable())
|
2019-06-27 18:50:20 +00:00
|
|
|
{
|
2020-04-22 08:31:10 +00:00
|
|
|
if (const auto * column_lc = checkAndGetColumn<ColumnLowCardinality>(*res))
|
2019-07-01 11:44:19 +00:00
|
|
|
res = checkAndGetColumn<ColumnNullable>(*column_lc->convertToFullColumn())->getNestedColumnPtr();
|
2020-04-22 08:31:10 +00:00
|
|
|
else if (const auto * column_const = checkAndGetColumn<ColumnConst>(*res))
|
2019-06-27 18:50:20 +00:00
|
|
|
res = checkAndGetColumn<ColumnNullable>(column_const->getDataColumn())->getNestedColumnPtr();
|
2019-07-01 11:44:19 +00:00
|
|
|
else
|
|
|
|
res = checkAndGetColumn<ColumnNullable>(*res)->getNestedColumnPtr();
|
2019-06-27 18:50:20 +00:00
|
|
|
}
|
2018-09-08 01:24:09 +00:00
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
return res;
|
2018-09-08 01:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-06-01 12:20:52 +00:00
|
|
|
ContextPtr context;
|
2018-09-08 01:24:09 +00:00
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2018-09-08 01:24:09 +00:00
|
|
|
|
|
|
|
void registerFunctionCoalesce(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionCoalesce>(FunctionFactory::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|