1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/* Copyright (c) 2012, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <gtest/gtest.h>
#include <sys/types.h>
#include "my_inttypes.h"
#include "sql/filesort.h"
#include "sql/sort_param.h"
#include "sql/sql_lex.h"
#include "sql/sql_sort.h"
#include "sql/sys_vars.h"
#include "unittest/gunit/fake_table.h"
#include "unittest/gunit/test_utils.h"
namespace make_sortkey_unittest {
using my_testing::Mock_error_handler;
using my_testing::Server_initializer;
/**
Test that sortlength() and make_sortkey() agree on what to do:
i.e. that there is no buffer underwrite/overwrite in make_sortkey()
if sortlength() has set a very small size.
We allocate a buffer, fill it with 'a's and then tell make_sortkey()
to put it's result somewhere in the middle.
The buffer should be unchanged outside of the area determined by sortlength.
*/
class MakeSortKeyTest : public ::testing::Test {
protected:
MakeSortKeyTest() {
m_sort_fields[0] = st_sort_field();
m_sort_fields[1] = st_sort_field();
m_sort_param.local_sortorder =
Bounds_checked_array<st_sort_field>(m_sort_fields, 1);
memset(m_buff, 'a', sizeof(m_buff));
m_to = Bounds_checked_array<uchar>(&m_buff[8], sizeof(m_buff) - 8);
}
void SetUp() override { initializer.SetUp(); }
void TearDown() override { initializer.TearDown(); }
THD *thd() { return initializer.thd(); }
void verify_buff(uint length) {
for (uchar *pu = m_buff; pu < m_to.array(); ++pu) {
EXPECT_EQ('a', *pu) << " position " << pu - m_buff;
}
for (uchar *pu = m_to.array() + length; pu < m_buff + 100; ++pu) {
EXPECT_EQ('a', *pu) << " position " << pu - m_buff;
}
}
Server_initializer initializer;
Sort_param m_sort_param;
st_sort_field m_sort_fields[2]; // sortlength() adds an end marker !!
uchar m_buff[100];
Bounds_checked_array<uchar> m_to;
};
TEST_F(MakeSortKeyTest, IntResult) {
thd()->variables.max_sort_length = 4U;
m_sort_fields[0].item = new Item_int(42);
const uint total_length = sortlength(thd(), m_sort_fields, 1);
EXPECT_EQ(sizeof(longlong), total_length);
EXPECT_EQ(sizeof(longlong), m_sort_fields[0].length);
EXPECT_EQ(INT_RESULT, m_sort_fields[0].result_type);
size_t longest_addon_so_far = 0; // Unused.
m_sort_param.make_sortkey(m_to, {}, &longest_addon_so_far);
SCOPED_TRACE("");
verify_buff(total_length);
}
TEST_F(MakeSortKeyTest, IntResultNull) {
thd()->variables.max_sort_length = 4U;
Item *int_item = m_sort_fields[0].item = new Item_int(42);
int_item->set_nullable(true);
int_item->null_value = true;
const uint total_length = sortlength(thd(), m_sort_fields, 1);
EXPECT_EQ(1 + sizeof(longlong), total_length);
EXPECT_EQ(sizeof(longlong), m_sort_fields[0].length);
EXPECT_EQ(INT_RESULT, m_sort_fields[0].result_type);
size_t longest_addon_so_far = 0; // Unused.
m_sort_param.make_sortkey(m_to, {}, &longest_addon_so_far);
SCOPED_TRACE("");
verify_buff(total_length);
}
TEST_F(MakeSortKeyTest, DecimalResult) {
const char dec_str[] = "1234567890.1234567890";
thd()->variables.max_sort_length = 4U;
m_sort_fields[0].item =
new Item_decimal(POS(), dec_str, strlen(dec_str), &my_charset_bin);
Parse_context pc(thd(), thd()->lex->current_query_block());
EXPECT_FALSE(m_sort_fields[0].item->itemize(&pc, &m_sort_fields[0].item));
const uint total_length = sortlength(thd(), m_sort_fields, 1);
EXPECT_EQ(10U, total_length);
EXPECT_EQ(10U, m_sort_fields[0].length);
EXPECT_EQ(DECIMAL_RESULT, m_sort_fields[0].result_type);
size_t longest_addon_so_far = 0; // Unused.
m_sort_param.make_sortkey(m_to, {}, &longest_addon_so_far);
SCOPED_TRACE("");
verify_buff(total_length);
}
TEST_F(MakeSortKeyTest, RealResult) {
const char dbl_str[] = "1234567890.1234567890";
thd()->variables.max_sort_length = 4U;
m_sort_fields[0].item = new Item_float(dbl_str, strlen(dbl_str));
const uint total_length = sortlength(thd(), m_sort_fields, 1);
EXPECT_EQ(sizeof(double), total_length);
EXPECT_EQ(sizeof(double), m_sort_fields[0].length);
EXPECT_EQ(REAL_RESULT, m_sort_fields[0].result_type);
size_t longest_addon_so_far = 0; // Unused.
m_sort_param.make_sortkey(m_to, {}, &longest_addon_so_far);
SCOPED_TRACE("");
verify_buff(total_length);
}
TEST_F(MakeSortKeyTest, AddonFields) {
m_sort_fields[0].item = new Item_int(42);
const uint total_length = sortlength(thd(), m_sort_fields, 1);
EXPECT_EQ(sizeof(longlong), total_length);
EXPECT_EQ(sizeof(longlong), m_sort_fields[0].length);
EXPECT_EQ(INT_RESULT, m_sort_fields[0].result_type);
Sort_addon_field addon_field;
float val = static_cast<float>(M_PI);
Field_float field(nullptr, 0, nullptr, '\0', Field::NONE, "", 0, false,
false);
Fake_TABLE table(&field);
table.s->db_low_byte_first = false;
field.set_field_ptr(reinterpret_cast<unsigned char *>(&val));
addon_field.field = &field;
addon_field.max_length = field.max_packed_col_length();
Addon_fields addon_fields(make_array(&addon_field, 1));
addon_fields.set_first_addon_relative_offset(0);
addon_fields.set_using_packed_addons(true);
m_sort_param.addon_fields = &addon_fields;
// Test regular packing.
size_t longest_addon_so_far = 0; // Unused.
size_t len = m_sort_param.make_sortkey(m_to, {}, &longest_addon_so_far);
EXPECT_EQ(total_length + sizeof(float) + addon_fields.first_addon_offset(),
len);
float unpacked_val;
field.unpack(reinterpret_cast<uchar *>(&unpacked_val),
m_to.array() + m_sort_fields[0].length +
addon_fields.first_addon_offset(),
/*param_data=*/0);
EXPECT_EQ(unpacked_val, val);
// Test truncation. (The actual contents don't matter in this case.)
std::unique_ptr<uchar[]> trunc_buf(new uchar[len - 4]);
size_t trunc_len = m_sort_param.make_sortkey(
make_array(trunc_buf.get(), len - 4), {}, &longest_addon_so_far);
EXPECT_GT(trunc_len, len - 4)
<< "make_sortkey() should report back that there was not enough room.";
}
} // namespace make_sortkey_unittest
|