ClickHouse/src/Functions/isZeroOrNull.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
4.7 KiB
C++
Raw Normal View History

2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
2020-07-06 02:41:28 +00:00
#include <Functions/castTypeToEither.h>
#include <Functions/FunctionFactory.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeNullable.h>
#include <Core/ColumnNumbers.h>
#include <Columns/ColumnNullable.h>
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int ILLEGAL_COLUMN;
}
2020-09-07 18:00:37 +00:00
namespace
{
2020-07-06 02:41:28 +00:00
/// Returns 1 if argument is zero or NULL.
/// It can be used to negate filter in WHERE condition.
/// "WHERE isZeroOrNull(expr)" will return exactly the same rows that "WHERE expr" will filter out.
class FunctionIsZeroOrNull : public IFunction
{
public:
static constexpr auto name = "isZeroOrNull";
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr)
2020-07-06 02:41:28 +00:00
{
return std::make_shared<FunctionIsZeroOrNull>();
}
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; }
2020-07-06 02:41:28 +00:00
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
DataTypePtr getReturnTypeImpl(const DataTypes & types) const override
{
2023-09-05 13:53:15 +00:00
if (!isNumber(removeNullable(types.at(0))) && !isNothing(removeNullable(types.at(0))))
throw Exception(
ErrorCodes::BAD_ARGUMENTS, "The argument of function {} must have simple numeric type, possibly Nullable or Null", name);
2020-07-06 02:41:28 +00:00
return std::make_shared<DataTypeUInt8>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
2020-07-06 02:41:28 +00:00
{
2023-09-05 13:53:15 +00:00
if (isNothing(removeNullable(arguments[0].type)))
return DataTypeUInt8{}.createColumnConst(input_rows_count, 1);
2020-10-19 13:42:14 +00:00
const ColumnPtr & input_column = arguments[0].column;
2020-07-06 02:41:28 +00:00
2020-10-20 13:11:57 +00:00
ColumnPtr res;
2020-07-06 02:41:28 +00:00
if (const ColumnNullable * input_column_nullable = checkAndGetColumn<ColumnNullable>(input_column.get()))
{
const NullMap & null_map = input_column_nullable->getNullMapData();
const IColumn * nested_column = &input_column_nullable->getNestedColumn();
if (!castTypeToEither<
ColumnUInt8, ColumnUInt16, ColumnUInt32, ColumnUInt64,
ColumnInt8, ColumnInt16, ColumnInt32, ColumnInt64,
ColumnFloat32, ColumnFloat64>(
nested_column, [&](const auto & column)
{
2020-10-20 13:11:57 +00:00
auto col = ColumnUInt8::create(input_rows_count);
processNullable(column.getData(), null_map, col->getData(), input_rows_count);
res = std::move(col);
return true;
2020-07-06 02:41:28 +00:00
}))
{
2023-09-05 13:53:15 +00:00
throw Exception(
ErrorCodes::ILLEGAL_COLUMN,
"The argument of function {} must have simple numeric type, possibly Nullable or Null",
name);
2020-07-06 02:41:28 +00:00
}
}
else
{
if (!castTypeToEither<
ColumnUInt8, ColumnUInt16, ColumnUInt32, ColumnUInt64,
ColumnInt8, ColumnInt16, ColumnInt32, ColumnInt64,
ColumnFloat32, ColumnFloat64>(
input_column.get(), [&](const auto & column)
{
2020-10-20 13:11:57 +00:00
auto col = ColumnUInt8::create(input_rows_count);
processNotNullable(column.getData(), col->getData(), input_rows_count);
res = std::move(col);
return true;
2020-07-06 02:41:28 +00:00
}))
{
2023-09-05 13:53:15 +00:00
throw Exception(
ErrorCodes::ILLEGAL_COLUMN,
"The argument of function {} must have simple numeric type, possibly Nullable or Null",
name);
2020-07-06 02:41:28 +00:00
}
}
2020-10-20 13:11:57 +00:00
return res;
2020-07-06 02:41:28 +00:00
}
private:
template <typename InputData>
2020-07-21 13:58:07 +00:00
void processNotNullable(const InputData & input_data, ColumnUInt8::Container & result_data, size_t input_rows_count) const
2020-07-06 02:41:28 +00:00
{
for (size_t i = 0; i < input_rows_count; ++i)
2022-09-11 02:49:43 +00:00
result_data[i] = input_data[i] == 0;
2020-07-06 02:41:28 +00:00
}
template <typename InputData>
void processNullable(const InputData & input_data, const NullMap & input_null_map,
2020-07-21 13:58:07 +00:00
ColumnUInt8::Container & result_data, size_t input_rows_count) const
2020-07-06 02:41:28 +00:00
{
for (size_t i = 0; i < input_rows_count; ++i)
2022-09-11 02:49:43 +00:00
result_data[i] = input_null_map[i] || input_data[i] == 0;
2020-07-06 02:41:28 +00:00
}
};
2020-09-07 18:00:37 +00:00
}
2020-07-06 02:41:28 +00:00
REGISTER_FUNCTION(IsZeroOrNull)
2020-07-06 02:41:28 +00:00
{
factory.registerFunction<FunctionIsZeroOrNull>();
}
}