ClickHouse/src/Functions/isNotNull.cpp

74 lines
2.2 KiB
C++
Raw Normal View History

2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/FunctionFactory.h>
#include <DataTypes/DataTypesNumber.h>
#include <Core/ColumnNumbers.h>
#include <Columns/ColumnNullable.h>
#include <Common/assert_cast.h>
namespace DB
{
2020-09-07 18:00:37 +00:00
namespace
{
/// Implements the function isNotNull which returns true if a value
/// is not null, false otherwise.
class FunctionIsNotNull : public IFunction
{
public:
static constexpr auto name = "isNotNull";
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr)
{
return std::make_shared<FunctionIsNotNull>();
}
std::string getName() const override
{
return name;
}
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
2019-10-02 17:51:00 +00:00
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
DataTypePtr getReturnTypeImpl(const DataTypes &) const override
{
return std::make_shared<DataTypeUInt8>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
2020-10-19 13:42:14 +00:00
const ColumnWithTypeAndName & elem = arguments[0];
2020-04-22 08:31:10 +00:00
if (const auto * nullable = checkAndGetColumn<ColumnNullable>(*elem.column))
{
/// Return the negated null map.
auto res_column = ColumnUInt8::create(input_rows_count);
2019-06-26 17:20:33 +00:00
const auto & src_data = nullable->getNullMapData();
auto & res_data = assert_cast<ColumnUInt8 &>(*res_column).getData();
for (size_t i = 0; i < input_rows_count; ++i)
res_data[i] = !src_data[i];
2020-10-19 13:42:14 +00:00
return res_column;
}
else
{
/// Since no element is nullable, return a constant one.
2020-10-19 13:42:14 +00:00
return DataTypeUInt8().createColumnConst(elem.column->size(), 1u);
}
}
};
2020-09-07 18:00:37 +00:00
}
void registerFunctionIsNotNull(FunctionFactory & factory)
{
factory.registerFunction<FunctionIsNotNull>();
}
}