ClickHouse/src/Functions/ifNull.cpp

97 lines
3.2 KiB
C++
Raw Normal View History

#include <Functions/IFunctionImpl.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/FunctionFactory.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/getLeastSupertype.h>
#include <Core/ColumnNumbers.h>
#include <Columns/ColumnNullable.h>
namespace DB
{
2020-09-07 18:00:37 +00:00
namespace
{
/// Implements the function ifNull which takes 2 arguments and returns
/// the value of the 1st argument if it is not null. Otherwise it returns
/// the value of the 2nd argument.
class FunctionIfNull : public IFunction
{
public:
static constexpr auto name = "ifNull";
2020-03-18 03:27:32 +00:00
explicit FunctionIfNull(const Context & context_) : context(context_) {}
static FunctionPtr create(const Context & context)
{
return std::make_shared<FunctionIfNull>(context);
}
std::string getName() const override
{
return name;
}
size_t getNumberOfArguments() const override { return 2; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
2019-10-02 17:51:00 +00:00
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (arguments[0]->onlyNull())
return arguments[1];
if (!arguments[0]->isNullable())
return arguments[0];
return getLeastSupertype({removeNullable(arguments[0]), arguments[1]});
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
{
/// Always null.
2020-10-19 13:42:14 +00:00
if (arguments[0].type->onlyNull())
return arguments[1].column;
/// Could not contain nulls, so nullIf makes no sense.
2020-10-19 13:42:14 +00:00
if (!arguments[0].type->isNullable())
return arguments[0].column;
/// ifNull(col1, col2) == if(isNotNull(col1), assumeNotNull(col1), col2)
2020-10-19 13:42:14 +00:00
ColumnsWithTypeAndName columns{arguments[0]};
2020-10-19 13:42:14 +00:00
auto is_not_null = FunctionFactory::instance().get("isNotNull", context)->build(columns);
auto is_not_null_type = std::make_shared<DataTypeUInt8>();
auto is_not_null_res = is_not_null->execute(columns, is_not_null_type, input_rows_count);
2020-10-19 13:42:14 +00:00
auto assume_not_null = FunctionFactory::instance().get("assumeNotNull", context)->build(columns);
auto assume_not_null_type = removeNullable(arguments[0].type);
auto assume_nut_null_res = assume_not_null->execute(columns, assume_not_null_type, input_rows_count);
2020-10-19 13:42:14 +00:00
ColumnsWithTypeAndName if_columns
{
{is_not_null_res, is_not_null_type, ""},
{assume_nut_null_res, assume_not_null_type, ""},
arguments[1],
};
2020-10-19 13:42:14 +00:00
auto func_if = FunctionFactory::instance().get("if", context)->build(if_columns); //-V557
return func_if->execute(if_columns, result_type, input_rows_count);
}
private:
const Context & context;
};
2020-09-07 18:00:37 +00:00
}
void registerFunctionIfNull(FunctionFactory & factory)
{
factory.registerFunction<FunctionIfNull>(FunctionFactory::CaseInsensitive);
}
}