From 33b79e09a4a52414bad974420afbc5529946adda Mon Sep 17 00:00:00 2001 From: Jigao Luo Date: Tue, 27 Sep 2022 11:09:45 +0200 Subject: [PATCH] remove IfNull duplicated implementation (#6036) close pingcap/tiflash#6037 --- dbms/src/Functions/FunctionsNull.cpp | 63 ++-------------------------- dbms/src/Functions/FunctionsNull.h | 17 -------- 2 files changed, 3 insertions(+), 77 deletions(-) diff --git a/dbms/src/Functions/FunctionsNull.cpp b/dbms/src/Functions/FunctionsNull.cpp index 27a2808a48f..0e3d1133d99 100644 --- a/dbms/src/Functions/FunctionsNull.cpp +++ b/dbms/src/Functions/FunctionsNull.cpp @@ -30,7 +30,6 @@ void registerFunctionsNull(FunctionFactory & factory) factory.registerFunction(); factory.registerFunction(); factory.registerFunction(); - factory.registerFunction(); factory.registerFunction(); factory.registerFunction(); factory.registerFunction(); @@ -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(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(0)); } } @@ -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(); -} - -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(), 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(), ""}); - size_t assume_not_null_pos = temp_block.columns(); - temp_block.insert({nullptr, removeNullable(block.getByPosition(arguments[0]).type), ""}); - - DefaultExecutable(std::make_shared()).execute(temp_block, {arguments[0]}, is_not_null_pos); - DefaultExecutable(std::make_shared()).execute(temp_block, {arguments[0]}, assume_not_null_pos); - - DefaultExecutable(std::make_shared()).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 &) @@ -363,7 +306,7 @@ void FunctionAssumeNotNull::executeImpl(Block & block, const ColumnNumbers & arg if (col->isColumnNullable()) { - const ColumnNullable & nullable_col = static_cast(*col); + const auto & nullable_col = static_cast(*col); res_col = nullable_col.getNestedColumnPtr(); } else diff --git a/dbms/src/Functions/FunctionsNull.h b/dbms/src/Functions/FunctionsNull.h index c9c9d2616bf..565d101e2ad 100644 --- a/dbms/src/Functions/FunctionsNull.h +++ b/dbms/src/Functions/FunctionsNull.h @@ -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.