Skip to content

Commit

Permalink
Fix that coalesce mistakenly removed nullable from the result column (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Nov 8, 2021
1 parent 209d49b commit d7566fe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dbms/src/Functions/FunctionsNull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const ColumnNullable &>(*res).getNestedColumnPtr();

block.getByPosition(result).column = std::move(res);
Expand Down
34 changes: 34 additions & 0 deletions dbms/src/Functions/tests/gtest_coalesce.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <Interpreters/Context.h>
#include <TestUtils/FunctionTestUtils.h>
#include <TestUtils/TiFlashTestBasic.h>

#include <string>
#include <vector>

namespace DB::tests
{
class CoalesceTest : public DB::tests::FunctionTest
{
};

TEST_F(CoalesceTest, testOnlyNull)
try
{
const String & func_name = "coalesce";

ASSERT_COLUMN_EQ(
createColumn<Nullable<String>>({"a"}),
executeFunction(
func_name,
createColumn<Nullable<String>>({"a"}),
createConstColumn<Nullable<String>>(1, std::nullopt)));

ASSERT_COLUMN_EQ(
createColumn<Nullable<String>>({"a"}),
executeFunction(
func_name,
createColumn<Nullable<String>>({"a"}),
createOnlyNullColumn(1)));
}
CATCH
} // namespace DB::tests
16 changes: 13 additions & 3 deletions tests/fullstack-test/expr/coalesce_pushdown.test
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down Expand Up @@ -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 |
+----------+

0 comments on commit d7566fe

Please sign in to comment.