| 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 <string> |
| 28 | |
| 29 | #include "sql/item_func.h" |
| 30 | #include "sql/nested_join.h" |
| 31 | #include "sql/sql_lex.h" |
| 32 | #include "template_utils.h" |
| 33 | #include "thr_lock.h" |
| 34 | #include "unittest/gunit/parsertest.h" |
| 35 | #include "unittest/gunit/test_utils.h" |
| 36 | |
| 37 | namespace table_factor_syntax_unittest { |
| 38 | |
| 39 | using my_testing::Mock_error_handler; |
| 40 | using my_testing::Server_initializer; |
| 41 | |
| 42 | class TableFactorSyntaxTest : public ParserTest { |
| 43 | protected: |
| 44 | void test_table_factor_syntax(const char *query, int num_terms, |
| 45 | bool expect_syntax_error) { |
| 46 | Query_block *term1 = parse(query, expect_syntax_error ? ER_PARSE_ERROR : 0); |
| 47 | EXPECT_EQ(nullptr, term1->first_inner_query_expression()); |
| 48 | EXPECT_EQ(nullptr, term1->next_select_in_list()); |
| 49 | EXPECT_EQ(1, term1->get_fields_list()->front()->val_int()); |
| 50 | |
| 51 | Query_expression *top_union = term1->master_query_expression(); |
| 52 | EXPECT_EQ(nullptr, top_union->outer_query_block()); |
| 53 | |
| 54 | if (num_terms > 1) { |
| 55 | Query_block *term2 = term1->next_query_block(); |
| 56 | ASSERT_FALSE(term2 == nullptr); |
| 57 | |
| 58 | EXPECT_EQ(nullptr, term2->first_inner_query_expression()); |
| 59 | EXPECT_EQ(term1, term2->next_select_in_list()); |
| 60 | EXPECT_EQ(2, term2->get_fields_list()->front()->val_int()); |
| 61 | |
| 62 | if (num_terms <= 2) { |
| 63 | EXPECT_EQ(nullptr, term2->next_query_block()); |
| 64 | } |
| 65 | |
| 66 | EXPECT_EQ(top_union, term2->master_query_expression()); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void test_global_limit(const char *query) { |
| 71 | Query_block *first_term = parse(query); |
| 72 | Query_expression *unit = first_term->master_query_expression(); |
| 73 | EXPECT_EQ(1U, unit->global_parameters()->order_list.elements) << query; |
| 74 | EXPECT_FALSE(unit->global_parameters()->select_limit == nullptr) << query; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | void check_query_block(Query_block *block, int select_list_item, |
| 79 | const char *tablename) { |
| 80 | ASSERT_EQ(1U, block->num_visible_fields()); |
| 81 | EXPECT_EQ(select_list_item, block->fields.front()->val_int()); |
| 82 | |
| 83 | ASSERT_EQ(1U, block->m_table_nest.size()); |
| 84 | EXPECT_STREQ(tablename, block->m_table_nest.front()->alias); |
| 85 | } |
| 86 | |
| 87 | TEST_F(TableFactorSyntaxTest, Single) { |
| 88 | Query_block *term = parse("SELECT 2 FROM (SELECT 1 FROM t1) dt;", 0); |
| 89 | EXPECT_EQ(nullptr, term->outer_query_block()); |
| 90 | Query_expression *top_union = term->master_query_expression(); |
| 91 | |
| 92 | EXPECT_EQ(term, top_union->first_query_block()); |
| 93 | EXPECT_EQ(nullptr, term->next_query_block()); |
| 94 | |
| 95 | ASSERT_EQ(1U, term->m_table_nest.size()); |
| 96 | EXPECT_STREQ("dt", term->m_table_nest.front()->alias); |
| 97 | |
| 98 | Query_expression *inner_union = term->first_inner_query_expression(); |
| 99 | |
| 100 | Query_block *inner_term = inner_union->first_query_block(); |
| 101 | |
| 102 | check_query_block(inner_term, 1, "t1"); |
| 103 | } |
| 104 | |
| 105 | TEST_F(TableFactorSyntaxTest, TablelessTableSubquery) { |
| 106 | Query_block *term = parse("SELECT 1 FROM (SELECT 2) a;", 0); |
| 107 | EXPECT_EQ(nullptr, term->outer_query_block()); |
| 108 | Query_expression *top_union = term->master_query_expression(); |
| 109 | |
| 110 | EXPECT_EQ(term, top_union->first_query_block()); |
| 111 | EXPECT_EQ(nullptr, term->next_query_block()); |
| 112 | |
| 113 | ASSERT_EQ(1U, term->m_table_nest.size()); |
| 114 | EXPECT_STREQ("a", term->m_table_nest.front()->alias); |
| 115 | |
| 116 | Query_expression *inner_union = term->first_inner_query_expression(); |
| 117 | |
| 118 | Query_block *inner_term = inner_union->first_query_block(); |
| 119 | |
| 120 | EXPECT_EQ(nullptr, inner_term->first_inner_query_expression()); |
| 121 | |
| 122 | EXPECT_NE( |
| 123 | term, |
| 124 | term->get_table_list()->derived_query_expression()->first_query_block()) |
| 125 | << "No cycle in the AST, please."; |
| 126 | } |
| 127 | |
| 128 | TEST_F(TableFactorSyntaxTest, Union) { |
| 129 | Query_block *block = parse( |
| 130 | "SELECT 1 FROM (SELECT 1 FROM t1 UNION SELECT 2 FROM t2) dt " |
| 131 | "WHERE d1.a = 1", |
| 132 | 0); |
| 133 | Query_expression *top_union = block->master_query_expression(); |
| 134 | |
| 135 | EXPECT_EQ(block, top_union->first_query_block()); |
| 136 | EXPECT_EQ(nullptr, block->next_query_block()); |
| 137 | |
| 138 | Table_ref *dt = block->get_table_list(); |
| 139 | EXPECT_EQ(dt, block->context.first_name_resolution_table); |
| 140 | |
| 141 | Item_func *top_where_cond = down_cast<Item_func *>(block->where_cond()); |
| 142 | Item_field *d1a = down_cast<Item_field *>(top_where_cond->arguments()[0]); |
| 143 | ASSERT_FALSE(d1a->context == nullptr); |
| 144 | EXPECT_EQ(dt, d1a->context->first_name_resolution_table); |
| 145 | |
| 146 | EXPECT_EQ(1U, block->m_table_nest.size()); |
| 147 | EXPECT_STREQ("dt", block->m_table_nest.front()->alias); |
| 148 | |
| 149 | Query_expression *inner_union = block->first_inner_query_expression(); |
| 150 | |
| 151 | Query_block *first_inner_block = inner_union->first_query_block(); |
| 152 | Query_block *second_inner_block = first_inner_block->next_query_block(); |
| 153 | |
| 154 | Table_ref *t1 = first_inner_block->get_table_list(); |
| 155 | Table_ref *t2 = second_inner_block->get_table_list(); |
| 156 | |
| 157 | EXPECT_EQ(t1, first_inner_block->context.first_name_resolution_table); |
| 158 | EXPECT_EQ(t2, second_inner_block->context.first_name_resolution_table); |
| 159 | |
| 160 | EXPECT_EQ(nullptr, t1->nested_join); |
| 161 | EXPECT_EQ(nullptr, t2->nested_join); |
| 162 | |
| 163 | check_query_block(first_inner_block, 1, "t1"); |
| 164 | check_query_block(second_inner_block, 2, "t2"); |
| 165 | |
| 166 | EXPECT_EQ(nullptr, block->outer_query_block()); |
| 167 | } |
| 168 | |
| 169 | TEST_F(TableFactorSyntaxTest, NestedJoin) { |
| 170 | Query_block *term = parse("SELECT * FROM (t1 JOIN t2 ON TRUE)", 0); |
| 171 | Query_expression *top_union = term->master_query_expression(); |
| 172 | |
| 173 | EXPECT_EQ(term, top_union->first_query_block()); |
| 174 | } |
| 175 | |
| 176 | TEST_F(TableFactorSyntaxTest, NestedNestedJoin) { |
| 177 | Query_block *term = |
| 178 | parse("SELECT * FROM ((t1 JOIN t2 ON TRUE) JOIN t3 ON TRUE)", 0); |
| 179 | Query_expression *top_union = term->master_query_expression(); |
| 180 | |
| 181 | EXPECT_EQ(term, top_union->first_query_block()); |
| 182 | } |
| 183 | |
| 184 | TEST_F(TableFactorSyntaxTest, NestedTableReferenceList) { |
| 185 | Query_block *term1 = |
| 186 | parse("SELECT * FROM t1 LEFT JOIN ( t2 JOIN t3 JOIN t4 ) ON t1.a = t3.a"); |
| 187 | |
| 188 | Query_block *term2 = |
| 189 | parse("SELECT * FROM t1 LEFT JOIN ( t2, t3, t4 ) ON t1.a = t3.a"); |
| 190 | |
| 191 | Query_expression *top_union = term1->master_query_expression(); |
| 192 | Query_expression *top_union2 = term2->master_query_expression(); |
| 193 | |
| 194 | EXPECT_EQ(term1, top_union->first_query_block()); |
| 195 | EXPECT_EQ(term2, top_union2->first_query_block()); |
| 196 | |
| 197 | EXPECT_EQ(4U, term1->m_table_list.elements); |
| 198 | ASSERT_EQ(4U, term2->m_table_list.elements); |
| 199 | |
| 200 | EXPECT_STREQ("t1", term1->get_table_list()->alias); |
| 201 | EXPECT_STREQ("t1", term2->get_table_list()->alias); |
| 202 | |
| 203 | EXPECT_STREQ("(nest_last_join)", term1->m_current_table_nest->front()->alias); |
| 204 | EXPECT_STREQ("(nest_last_join)", term2->m_current_table_nest->front()->alias); |
| 205 | |
| 206 | Table_ref *t2_join_t3_join_t4 = term1->m_current_table_nest->front(); |
| 207 | Table_ref *t2_join_t3_join_t4_2 = term2->m_current_table_nest->front(); |
| 208 | |
| 209 | Table_ref *t3_join_t4 = t2_join_t3_join_t4->nested_join->m_tables.front(); |
| 210 | Table_ref *t3_join_t4_2 = t2_join_t3_join_t4_2->nested_join->m_tables.front(); |
| 211 | |
| 212 | EXPECT_STREQ("(nest_last_join)", t3_join_t4->alias); |
| 213 | EXPECT_STREQ("(nest_last_join)", t3_join_t4_2->alias); |
| 214 | |
| 215 | EXPECT_STREQ("t4", t3_join_t4->nested_join->m_tables.front()->alias); |
| 216 | } |
| 217 | |
| 218 | TEST_F(TableFactorSyntaxTest, LimitAndOrder) { |
| 219 | test_global_limit("SELECT 1 AS c UNION (SELECT 1 AS c) ORDER BY c LIMIT 1"); |
| 220 | test_global_limit("(SELECT 1 AS c UNION SELECT 1 AS c) ORDER BY c LIMIT 1"); |
| 221 | test_global_limit("((SELECT 1 AS c) UNION SELECT 1 AS c) ORDER BY c LIMIT 1"); |
| 222 | test_global_limit("(SELECT 1 AS c UNION (SELECT 1 AS c)) ORDER BY c LIMIT 1"); |
| 223 | } |
| 224 | |
| 225 | } // namespace table_factor_syntax_unittest |