Debian Codegraph @

unittest/gunit/select_lex_visitor-t.cc

raw symbols SCIP
1 /* Copyright (c) 2006, 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
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
27 #include <stddef.h>
28
29 #include "my_inttypes.h"
30 #include "sql/current_thd.h"
31 #include "sql/select_lex_visitor.h"
32 #include "sql/sql_lex.h"
33 #include "sql/sql_optimizer.h"
34 #include "unittest/gunit/parsertest.h"
35 #include "unittest/gunit/test_utils.h"
36
37 namespace select_lex_visitor_unittest {
38
39 using my_testing::Server_initializer;
40 using std::vector;
41
42 class SelectLexVisitorTest : public ParserTest {
43 protected:
44 void SetUp() override { initializer.SetUp(); }
45 void TearDown() override { initializer.TearDown(); }
46 };
47
48 /// A visitor that remembers what it has seen.
49 class Remembering_visitor : public Select_lex_visitor {
50 public:
51 vector<int> seen_items;
52 vector<const char *> field_names;
53
54 Remembering_visitor()
55 : m_saw_query_block(false), m_saw_query_block_query_expression(false) {}
56
57 bool visit_union(Query_expression *) override {
58 m_saw_query_block_query_expression = true;
59 return false;
60 }
61
62 bool visit_query_block(Query_block *) override {
63 m_saw_query_block = true;
64 return false;
65 }
66
67 bool visit_item(Item *item) override {
68 // Not possible to call val_XXX on item_field. So just store the name.
69 if (item->type() == Item::FIELD_ITEM)
70 field_names.push_back(item->full_name());
71 else
72 seen_items.push_back(item->val_int());
73 return false;
74 }
75
76 bool saw_query_block() { return m_saw_query_block; }
77 bool saw_query_block_query_expression() {
78 return m_saw_query_block_query_expression;
79 }
80
81 ~Remembering_visitor() override = default;
82
83 private:
84 bool m_saw_query_block, m_saw_query_block_query_expression;
85 };
86
87 /**
88 Google mock only works for objects allocated on the stack, and the Item
89 classes appear to have been designed to make it impossible to allocate them
90 on the stack because of the mandatory free list. But this little mix-in
91 class lets us inherit any item class and do that. See Mock_item_int below
92 how to use it.
93 */
94 template <class Item_class>
95 class Stack_allocated_item : public Item_class {
96 public:
97 Stack_allocated_item(int value_arg) : Item_class(value_arg) {
98 // Undo what Item::Item() does.
99 THD *thd = current_thd;
100 thd->set_item_list(this->next_free);
101 this->next_free = nullptr;
102 }
103 };
104
105 class Mock_item_int : public Stack_allocated_item<Item_int> {
106 public:
107 Mock_item_int() : Stack_allocated_item<Item_int>(42) {}
108 MOCK_METHOD3(walk, bool(Item_processor, enum_walk, uchar *));
109 };
110
111 TEST_F(SelectLexVisitorTest, SelectLex) {
112 using ::testing::_;
113
114 Mock_item_int where;
115 Mock_item_int having;
116 EXPECT_CALL(where, walk(_, _, _)).Times(1);
117 EXPECT_CALL(having, walk(_, _, _)).Times(1);
118
119 Query_block query_block(thd()->mem_root, &where, &having);
120
121 Query_expression unit(CTX_NONE);
122
123 LEX lex;
124 query_block.include_down(&lex, &unit);
125 unit.set_query_term(&query_block);
126 List<Item> items;
127 JOIN join(thd(), &query_block);
128 join.where_cond = &where;
129 join.having_for_explain = &having;
130
131 query_block.join = &join;
132 query_block.parent_lex = &lex;
133
134 Remembering_visitor visitor;
135 unit.accept(&visitor);
136 EXPECT_TRUE(visitor.saw_query_block());
137 EXPECT_TRUE(visitor.saw_query_block_query_expression());
138 }
139
140 TEST_F(SelectLexVisitorTest, InsertList) {
141 Query_block *query_block = parse("INSERT INTO t VALUES (1, 2, 3)", 0);
142 ASSERT_FALSE(query_block == nullptr);
143
144 Remembering_visitor visitor;
145 thd()->lex->accept(&visitor);
146 ASSERT_EQ(3U, visitor.seen_items.size());
147 EXPECT_EQ(1, visitor.seen_items[0]);
148 EXPECT_EQ(2, visitor.seen_items[1]);
149 EXPECT_EQ(3, visitor.seen_items[2]);
150 }
151
152 TEST_F(SelectLexVisitorTest, InsertList2) {
153 Query_block *query_block = parse("INSERT INTO t VALUES (1, 2), (3, 4)", 0);
154 ASSERT_FALSE(query_block == nullptr);
155
156 Remembering_visitor visitor;
157 thd()->lex->accept(&visitor);
158 ASSERT_EQ(4U, visitor.seen_items.size());
159 EXPECT_EQ(1, visitor.seen_items[0]);
160 EXPECT_EQ(2, visitor.seen_items[1]);
161 EXPECT_EQ(3, visitor.seen_items[2]);
162 EXPECT_EQ(4, visitor.seen_items[3]);
163 }
164
165 TEST_F(SelectLexVisitorTest, InsertSet) {
166 Query_block *query_block = parse("INSERT INTO t SET a=1, b=2, c=3", 0);
167 ASSERT_FALSE(query_block == nullptr);
168
169 Remembering_visitor visitor;
170 thd()->lex->accept(&visitor);
171 ASSERT_EQ(3U, visitor.seen_items.size());
172 EXPECT_EQ(1, visitor.seen_items[0]);
173 EXPECT_EQ(2, visitor.seen_items[1]);
174 EXPECT_EQ(3, visitor.seen_items[2]);
175
176 ASSERT_EQ(3U, visitor.field_names.size());
177 EXPECT_STREQ("a", visitor.field_names[0]);
178 EXPECT_STREQ("b", visitor.field_names[1]);
179 EXPECT_STREQ("c", visitor.field_names[2]);
180 }
181
182 TEST_F(SelectLexVisitorTest, ReplaceList) {
183 Query_block *query_block =
184 parse("REPLACE INTO t(a, b, c) VALUES (1,2,3), (4,5,6)", 0);
185 ASSERT_FALSE(query_block == nullptr);
186
187 Remembering_visitor visitor;
188 thd()->lex->accept(&visitor);
189 ASSERT_EQ(6U, visitor.seen_items.size());
190 EXPECT_EQ(1, visitor.seen_items[0]);
191 EXPECT_EQ(4, visitor.seen_items[3]);
192 EXPECT_EQ(6, visitor.seen_items[5]);
193
194 ASSERT_EQ(3U, visitor.field_names.size());
195 EXPECT_STREQ("a", visitor.field_names[0]);
196 EXPECT_STREQ("b", visitor.field_names[1]);
197 EXPECT_STREQ("c", visitor.field_names[2]);
198 }
199
200 TEST_F(SelectLexVisitorTest, InsertOnDuplicateKey) {
201 Query_block *query_block = parse(
202 "INSERT INTO t VALUES (1,2) ON DUPLICATE KEY UPDATE c= 44, a= 55", 0);
203 ASSERT_FALSE(query_block == nullptr);
204
205 Remembering_visitor visitor;
206 thd()->lex->accept(&visitor);
207 ASSERT_EQ(4U, visitor.seen_items.size());
208 EXPECT_EQ(1, visitor.seen_items[0]);
209 EXPECT_EQ(44, visitor.seen_items[2]);
210 EXPECT_EQ(55, visitor.seen_items[3]);
211
212 ASSERT_EQ(2U, visitor.field_names.size());
213 EXPECT_STREQ("c", visitor.field_names[0]);
214 EXPECT_STREQ("a", visitor.field_names[1]);
215 }
216
217 TEST_F(SelectLexVisitorTest, Update) {
218 Query_block *query_block = parse("UPDATE t SET a= 0, c= 25", 0);
219 ASSERT_FALSE(query_block == nullptr);
220
221 Remembering_visitor visitor;
222 thd()->lex->accept(&visitor);
223 ASSERT_EQ(2U, visitor.seen_items.size());
224 EXPECT_EQ(0, visitor.seen_items[0]);
225 EXPECT_EQ(25, visitor.seen_items[1]);
226
227 ASSERT_EQ(2U, visitor.field_names.size());
228 EXPECT_STREQ("a", visitor.field_names[0]);
229 EXPECT_STREQ("c", visitor.field_names[1]);
230 }
231 } // namespace select_lex_visitor_unittest