diff --git a/dbms/src/Functions/FunctionsNull.cpp b/dbms/src/Functions/FunctionsNull.cpp index 84b13497a75..61f7bb6ec2f 100644 --- a/dbms/src/Functions/FunctionsNull.cpp +++ b/dbms/src/Functions/FunctionsNull.cpp @@ -213,7 +213,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..698de84e350 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 | ++----------+ +