Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix that coalesce mistakenly removed nullable from the result column (#3394) #3400

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
+----------+