| 1 | /* Copyright (c) 2015, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <stddef.h> |
| 26 | |
| 27 | #include "sql/item_cmpfunc.h" |
| 28 | #include "unittest/gunit/test_utils.h" |
| 29 | |
| 30 | namespace item_func_case_unittest { |
| 31 | |
| 32 | using my_testing::Server_initializer; |
| 33 | |
| 34 | class ItemFuncCaseTest : public ::testing::Test { |
| 35 | protected: |
| 36 | void SetUp() override { initializer.SetUp(); } |
| 37 | |
| 38 | void TearDown() override { initializer.TearDown(); } |
| 39 | |
| 40 | THD *thd() { return initializer.thd(); } |
| 41 | |
| 42 | Server_initializer initializer; |
| 43 | }; |
| 44 | |
| 45 | /* |
| 46 | Bug#21381060 |
| 47 | A "CASE WHEN" EXPRESSION WITH NULL AND AN UNSIGNED TYPE GIVES A SIGNED RESULT |
| 48 | |
| 49 | Original test case: |
| 50 | create table MyTable (`n` tinyint unsigned not null); |
| 51 | insert into MyTable (n) values (180); |
| 52 | select (case when 1 then n else null end) as value from MyTable; |
| 53 | |
| 54 | The returned value was signed, rather than unsigned. |
| 55 | |
| 56 | This unit test verifies that the bug is fixed in 5.7 and up. |
| 57 | */ |
| 58 | TEST_F(ItemFuncCaseTest, CaseWhenElseNull) { |
| 59 | Item_int *int_one = new Item_int(1); |
| 60 | Item_int *int_n = new Item_int(180ULL); |
| 61 | mem_root_deque<Item *> list(*THR_MALLOC); |
| 62 | list.push_back(int_one); |
| 63 | list.push_back(int_n); |
| 64 | Item_func_case *item_case = |
| 65 | new Item_func_case(POS(), &list, nullptr, new Item_null()); |
| 66 | EXPECT_FALSE(item_case->fix_fields(thd(), nullptr)); |
| 67 | |
| 68 | EXPECT_FALSE(int_one->unsigned_flag); |
| 69 | EXPECT_TRUE(int_n->unsigned_flag); |
| 70 | EXPECT_EQ(180, item_case->val_int()); |
| 71 | // Result should be unsigned. |
| 72 | EXPECT_TRUE(item_case->unsigned_flag); |
| 73 | } |
| 74 | |
| 75 | } // namespace item_func_case_unittest |