From 0e25e4c43caba8e5f0d712ddd4616159665513a5 Mon Sep 17 00:00:00 2001 From: fuzhe1989 Date: Fri, 5 Nov 2021 17:41:59 +0800 Subject: [PATCH 1/4] update --- dbms/src/Functions/FunctionsNull.cpp | 2 +- dbms/src/Functions/tests/gtest_coalesce.cpp | 34 +++++++++++++++++++ .../expr/coalesce_pushdown.test | 16 +++++++-- 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 dbms/src/Functions/tests/gtest_coalesce.cpp diff --git a/dbms/src/Functions/FunctionsNull.cpp b/dbms/src/Functions/FunctionsNull.cpp index 6adcc68ae0d..171d24b5670 100644 --- a/dbms/src/Functions/FunctionsNull.cpp +++ b/dbms/src/Functions/FunctionsNull.cpp @@ -220,7 +220,7 @@ void FunctionCoalesce::executeImpl(Block & block, const ColumnNumbers & argument ColumnPtr res = std::move(temp_block.getByPosition(result).column); /// if last argument is not nullable, result should be also not nullable - if (!block.getByPosition(multi_if_args.back()).column->isColumnNullable() && res->isColumnNullable()) + if (!block.getByPosition(filtered_args.back()).type->isNullable() && res->isColumnNullable()) res = static_cast(*res).getNestedColumnPtr(); block.getByPosition(result).column = std::move(res); diff --git a/dbms/src/Functions/tests/gtest_coalesce.cpp b/dbms/src/Functions/tests/gtest_coalesce.cpp new file mode 100644 index 00000000000..5d338e65c1f --- /dev/null +++ b/dbms/src/Functions/tests/gtest_coalesce.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +#include +#include + +namespace DB::tests +{ +class CoalesceTest : public DB::tests::FunctionTest +{ +}; + +TEST_F(CoalesceTest, testOnlyNull) +try +{ + const String & func_name = "coalesce"; + + ASSERT_COLUMN_EQ( + createColumn>({"a"}), + executeFunction( + func_name, + createColumn>({"a"}), + createConstColumn>(1, std::nullopt))); + + ASSERT_COLUMN_EQ( + createColumn>({"a"}), + executeFunction( + func_name, + createColumn>({"a"}), + createOnlyNullColumn(1))); +} +CATCH +} // namespace DB::tests diff --git a/tests/fullstack-test/expr/coalesce_pushdown.test b/tests/fullstack-test/expr/coalesce_pushdown.test index 9b89f5e76d8..e10c281a847 100644 --- a/tests/fullstack-test/expr/coalesce_pushdown.test +++ b/tests/fullstack-test/expr/coalesce_pushdown.test @@ -5,9 +5,11 @@ mysql> INSERT INTO test.test_tb(id,char_,enum_,longtext_,mediumtext_,set_,text_, mysql> analyze table test.test_tb mysql> alter table test.test_tb set tiflash replica 1 -func> wait_table test test_tb - - +mysql> drop table if exists test.fix_3388 +mysql> create table if not exists test.fix_3388(a varchar(10)) +mysql> alter table test.fix_3388 set tiflash replica 1 +mysql> insert into test.fix_3388 values ("a") +func> wait_table test test_tb fix_3388 # start checking mysql> select /*+ read_from_storage(tiflash[test.test_tb]) */ id from test.test_tb where char_ = coalesce(null, char_); @@ -335,3 +337,11 @@ mysql> select /*+ read_from_storage(tiflash[test.test_tb]) */ id from test.test_ | 2 | +----+ +# fix 3388 +mysql> set tidb_enforce_mpp=1; select count(*) from test.fix_3388 where coalesce(a, null) = "a" ++----------+ +| count(*) | ++----------+ +| 1 | ++----------+ + From 70c3bc3fd2e5ea47f705f3d84b0548e8962ecef9 Mon Sep 17 00:00:00 2001 From: Fu Zhe Date: Fri, 5 Nov 2021 19:29:09 +0800 Subject: [PATCH 2/4] Update coalesce_pushdown.test --- tests/fullstack-test/expr/coalesce_pushdown.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fullstack-test/expr/coalesce_pushdown.test b/tests/fullstack-test/expr/coalesce_pushdown.test index e10c281a847..698de84e350 100644 --- a/tests/fullstack-test/expr/coalesce_pushdown.test +++ b/tests/fullstack-test/expr/coalesce_pushdown.test @@ -8,7 +8,7 @@ mysql> alter table test.test_tb set tiflash replica 1 mysql> drop table if exists test.fix_3388 mysql> create table if not exists test.fix_3388(a varchar(10)) mysql> alter table test.fix_3388 set tiflash replica 1 -mysql> insert into test.fix_3388 values ("a") +mysql> insert into test.fix_3388 values ('a') func> wait_table test test_tb fix_3388 # start checking From f222b4f442cd9cff4c0c9b5fe0d63911515e71bb Mon Sep 17 00:00:00 2001 From: Fu Zhe Date: Fri, 5 Nov 2021 21:58:16 +0800 Subject: [PATCH 3/4] Delete gtest_coalesce.cpp --- dbms/src/Functions/tests/gtest_coalesce.cpp | 34 --------------------- 1 file changed, 34 deletions(-) delete mode 100644 dbms/src/Functions/tests/gtest_coalesce.cpp diff --git a/dbms/src/Functions/tests/gtest_coalesce.cpp b/dbms/src/Functions/tests/gtest_coalesce.cpp deleted file mode 100644 index 5d338e65c1f..00000000000 --- a/dbms/src/Functions/tests/gtest_coalesce.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include - -#include -#include - -namespace DB::tests -{ -class CoalesceTest : public DB::tests::FunctionTest -{ -}; - -TEST_F(CoalesceTest, testOnlyNull) -try -{ - const String & func_name = "coalesce"; - - ASSERT_COLUMN_EQ( - createColumn>({"a"}), - executeFunction( - func_name, - createColumn>({"a"}), - createConstColumn>(1, std::nullopt))); - - ASSERT_COLUMN_EQ( - createColumn>({"a"}), - executeFunction( - func_name, - createColumn>({"a"}), - createOnlyNullColumn(1))); -} -CATCH -} // namespace DB::tests From 7f71033eb33be8aa84afc98a48bbfb56e9fe7e86 Mon Sep 17 00:00:00 2001 From: xufei Date: Thu, 14 Apr 2022 18:50:22 +0800 Subject: [PATCH 4/4] format code Signed-off-by: xufei --- dbms/src/Functions/FunctionsNull.cpp | 122 ++++++--------------------- 1 file changed, 28 insertions(+), 94 deletions(-) diff --git a/dbms/src/Functions/FunctionsNull.cpp b/dbms/src/Functions/FunctionsNull.cpp index 171d24b5670..62b9a63317e 100644 --- a/dbms/src/Functions/FunctionsNull.cpp +++ b/dbms/src/Functions/FunctionsNull.cpp @@ -1,12 +1,12 @@ -#include -#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include -#include +#include +#include namespace DB @@ -25,20 +25,11 @@ void registerFunctionsNull(FunctionFactory & factory) /// Implementation of isNull. -FunctionPtr FunctionIsNull::create(const Context &) -{ - return std::make_shared(); -} +FunctionPtr FunctionIsNull::create(const Context &) { return std::make_shared(); } -std::string FunctionIsNull::getName() const -{ - return name; -} +std::string FunctionIsNull::getName() const { return name; } -DataTypePtr FunctionIsNull::getReturnTypeImpl(const DataTypes &) const -{ - return std::make_shared(); -} +DataTypePtr FunctionIsNull::getReturnTypeImpl(const DataTypes &) const { return std::make_shared(); } void FunctionIsNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) { @@ -58,37 +49,16 @@ void FunctionIsNull::executeImpl(Block & block, const ColumnNumbers & arguments, /// Implementation of isNotNull. -FunctionPtr FunctionIsNotNull::create(const Context &) -{ - return std::make_shared(); -} +FunctionPtr FunctionIsNotNull::create(const Context &) { return std::make_shared(); } -std::string FunctionIsNotNull::getName() const -{ - return name; -} +std::string FunctionIsNotNull::getName() const { return name; } -DataTypePtr FunctionIsNotNull::getReturnTypeImpl(const DataTypes &) const -{ - return std::make_shared(); -} +DataTypePtr FunctionIsNotNull::getReturnTypeImpl(const DataTypes &) const { return std::make_shared(); } void FunctionIsNotNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) { - Block temp_block - { - block.getByPosition(arguments[0]), - { - nullptr, - std::make_shared(), - "" - }, - { - nullptr, - std::make_shared(), - "" - } - }; + Block temp_block{block.getByPosition(arguments[0]), {nullptr, std::make_shared(), ""}, + {nullptr, std::make_shared(), ""}}; FunctionIsNull{}.execute(temp_block, {0}, 1); FunctionNot{}.execute(temp_block, {1}, 2); @@ -98,15 +68,9 @@ void FunctionIsNotNull::executeImpl(Block & block, const ColumnNumbers & argumen /// Implementation of coalesce. -FunctionPtr FunctionCoalesce::create(const Context & context) -{ - return std::make_shared(context); -} +FunctionPtr FunctionCoalesce::create(const Context & context) { return std::make_shared(context); } -std::string FunctionCoalesce::getName() const -{ - return name; -} +std::string FunctionCoalesce::getName() const { return name; } DataTypePtr FunctionCoalesce::getReturnTypeImpl(const DataTypes & arguments) const { @@ -228,15 +192,9 @@ void FunctionCoalesce::executeImpl(Block & block, const ColumnNumbers & argument /// Implementation of ifNull. -FunctionPtr FunctionIfNull::create(const Context &) -{ - return std::make_shared(); -} +FunctionPtr FunctionIfNull::create(const Context &) { return std::make_shared(); } -std::string FunctionIfNull::getName() const -{ - return name; -} +std::string FunctionIfNull::getName() const { return name; } DataTypePtr FunctionIfNull::getReturnTypeImpl(const DataTypes & arguments) const { @@ -284,15 +242,9 @@ void FunctionIfNull::executeImpl(Block & block, const ColumnNumbers & arguments, /// Implementation of nullIf. -FunctionPtr FunctionNullIf::create(const Context & ) -{ - return std::make_shared(); -} +FunctionPtr FunctionNullIf::create(const Context &) { return std::make_shared(); } -std::string FunctionNullIf::getName() const -{ - return name; -} +std::string FunctionNullIf::getName() const { return name; } DataTypePtr FunctionNullIf::getReturnTypeImpl(const DataTypes & arguments) const { @@ -328,20 +280,11 @@ void FunctionNullIf::executeImpl(Block & block, const ColumnNumbers & arguments, /// Implementation of assumeNotNull. -FunctionPtr FunctionAssumeNotNull::create(const Context &) -{ - return std::make_shared(); -} +FunctionPtr FunctionAssumeNotNull::create(const Context &) { return std::make_shared(); } -std::string FunctionAssumeNotNull::getName() const -{ - return name; -} +std::string FunctionAssumeNotNull::getName() const { return name; } -DataTypePtr FunctionAssumeNotNull::getReturnTypeImpl(const DataTypes & arguments) const -{ - return removeNullable(arguments[0]); -} +DataTypePtr FunctionAssumeNotNull::getReturnTypeImpl(const DataTypes & arguments) const { return removeNullable(arguments[0]); } void FunctionAssumeNotNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) { @@ -359,24 +302,15 @@ void FunctionAssumeNotNull::executeImpl(Block & block, const ColumnNumbers & arg /// Implementation of toNullable. -FunctionPtr FunctionToNullable::create(const Context &) -{ - return std::make_shared(); -} +FunctionPtr FunctionToNullable::create(const Context &) { return std::make_shared(); } -std::string FunctionToNullable::getName() const -{ - return name; -} +std::string FunctionToNullable::getName() const { return name; } -DataTypePtr FunctionToNullable::getReturnTypeImpl(const DataTypes & arguments) const -{ - return makeNullable(arguments[0]); -} +DataTypePtr FunctionToNullable::getReturnTypeImpl(const DataTypes & arguments) const { return makeNullable(arguments[0]); } void FunctionToNullable::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) { block.getByPosition(result).column = makeNullable(block.getByPosition(arguments[0]).column); } -} +} // namespace DB