Skip to content

Commit

Permalink
remove IfNull duplicated implementation (#6036)
Browse files Browse the repository at this point in the history
close #6037
  • Loading branch information
JigaoLuo authored Sep 27, 2022
1 parent 3548744 commit 33b79e0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 77 deletions.
63 changes: 3 additions & 60 deletions dbms/src/Functions/FunctionsNull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ void registerFunctionsNull(FunctionFactory & factory)
factory.registerFunction<FunctionIsNull>();
factory.registerFunction<FunctionIsNotNull>();
factory.registerFunction<FunctionCoalesce>();
factory.registerFunction<FunctionIfNull>();
factory.registerFunction<FunctionNullIf>();
factory.registerFunction<FunctionAssumeNotNull>();
factory.registerFunction<FunctionToNullable>();
Expand Down Expand Up @@ -65,13 +64,13 @@ void FunctionIsNull::executeImpl(Block & block, const ColumnNumbers & arguments,
{
/// Since all element is null, return a one-constant column representing
/// a one-filled null map.
block.getByPosition(result).column = DataTypeUInt8().createColumnConst(elem.column->size(), UInt64(1));
block.getByPosition(result).column = DataTypeUInt8().createColumnConst(elem.column->size(), static_cast<UInt64>(1));
}
else
{
/// Since no element is nullable, return a zero-constant column representing
/// a zero-filled null map.
block.getByPosition(result).column = DataTypeUInt8().createColumnConst(elem.column->size(), UInt64(0));
block.getByPosition(result).column = DataTypeUInt8().createColumnConst(elem.column->size(), static_cast<UInt64>(0));
}
}

Expand Down Expand Up @@ -239,62 +238,6 @@ void FunctionCoalesce::executeImpl(Block & block, const ColumnNumbers & argument
block.getByPosition(result).column = std::move(res);
}

/// Implementation of ifNull.

FunctionPtr FunctionIfNull::create(const Context &)
{
return std::make_shared<FunctionIfNull>();
}

std::string FunctionIfNull::getName() const
{
return name;
}

DataTypePtr FunctionIfNull::getReturnTypeImpl(const DataTypes & arguments) const
{
if (arguments[0]->onlyNull())
return arguments[1];

if (!arguments[0]->isNullable())
return arguments[0];

return FunctionIf{}.getReturnTypeImpl({std::make_shared<DataTypeUInt8>(), removeNullable(arguments[0]), arguments[1]});
}

void FunctionIfNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) const
{
/// Always null.
if (block.getByPosition(arguments[0]).type->onlyNull())
{
block.getByPosition(result).column = block.getByPosition(arguments[1]).column;
return;
}

/// Could not contain nulls, so nullIf makes no sense.
if (!block.getByPosition(arguments[0]).type->isNullable())
{
block.getByPosition(result).column = block.getByPosition(arguments[0]).column;
return;
}

/// ifNull(col1, col2) == if(isNotNull(col1), assumeNotNull(col1), col2)

Block temp_block = block;

size_t is_not_null_pos = temp_block.columns();
temp_block.insert({nullptr, std::make_shared<DataTypeUInt8>(), ""});
size_t assume_not_null_pos = temp_block.columns();
temp_block.insert({nullptr, removeNullable(block.getByPosition(arguments[0]).type), ""});

DefaultExecutable(std::make_shared<FunctionIsNotNull>()).execute(temp_block, {arguments[0]}, is_not_null_pos);
DefaultExecutable(std::make_shared<FunctionAssumeNotNull>()).execute(temp_block, {arguments[0]}, assume_not_null_pos);

DefaultExecutable(std::make_shared<FunctionIf>()).execute(temp_block, {is_not_null_pos, assume_not_null_pos, arguments[1]}, result);

block.getByPosition(result).column = std::move(temp_block.getByPosition(result).column);
}

/// Implementation of nullIf.

FunctionPtr FunctionNullIf::create(const Context &)
Expand Down Expand Up @@ -363,7 +306,7 @@ void FunctionAssumeNotNull::executeImpl(Block & block, const ColumnNumbers & arg

if (col->isColumnNullable())
{
const ColumnNullable & nullable_col = static_cast<const ColumnNullable &>(*col);
const auto & nullable_col = static_cast<const ColumnNullable &>(*col);
res_col = nullable_col.getNestedColumnPtr();
}
else
Expand Down
17 changes: 0 additions & 17 deletions dbms/src/Functions/FunctionsNull.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,6 @@ class FunctionCoalesce : public IFunction
const Context & context;
};

/// 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";
static FunctionPtr create(const Context & context);

std::string getName() const override;
size_t getNumberOfArguments() const override { return 2; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override;
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) const override;
};

/// Implements the function nullIf which takes 2 arguments and returns
/// NULL if both arguments have the same value. Otherwise it returns the
/// value of the first argument.
Expand Down

0 comments on commit 33b79e0

Please sign in to comment.