diff --git a/dbms/src/Interpreters/JoinToSubqueryTransformVisitor.cpp b/dbms/src/Interpreters/JoinToSubqueryTransformVisitor.cpp index b7bf7c9b983..a2536521a7d 100644 --- a/dbms/src/Interpreters/JoinToSubqueryTransformVisitor.cpp +++ b/dbms/src/Interpreters/JoinToSubqueryTransformVisitor.cpp @@ -224,17 +224,15 @@ struct ColumnAliasesMatcher bool last_table = false; String long_name; - for (auto & table : data.tables) + + size_t table_pos = 0; + if (IdentifierSemantic::chooseTable(node, data.tables, table_pos)) { - if (value(IdentifierSemantic::canReferColumnToTable(node, table))) - { - if (!long_name.empty()) - throw Exception("Cannot refer column '" + node.name + "' to one table", ErrorCodes::AMBIGUOUS_COLUMN_NAME); - IdentifierSemantic::setColumnLongName(node, table); /// table_name.column_name -> table_alias.column_name - long_name = node.name; - if (&table == &data.tables.back()) - last_table = true; - } + auto & table = data.tables[table_pos]; + IdentifierSemantic::setColumnLongName(node, table); /// table_name.column_name -> table_alias.column_name + long_name = node.name; + if (&table == &data.tables.back()) + last_table = true; } if (long_name.empty()) diff --git a/dbms/tests/queries/0_stateless/01018_anbiguous_column.reference b/dbms/tests/queries/0_stateless/01018_anbiguous_column.reference index 90d1da5b891..90b24009d0f 100644 --- a/dbms/tests/queries/0_stateless/01018_anbiguous_column.reference +++ b/dbms/tests/queries/0_stateless/01018_anbiguous_column.reference @@ -4,3 +4,9 @@ 0 0 0 +┌─one.dummy─┬─A.dummy─┬─B.dummy─┐ +│ 0 │ 0 │ 0 │ +└───────────┴─────────┴─────────┘ +┌─A.dummy─┬─one.dummy─┬─two.dummy─┐ +│ 0 │ 0 │ 0 │ +└─────────┴───────────┴───────────┘ diff --git a/dbms/tests/queries/0_stateless/01018_anbiguous_column.sql b/dbms/tests/queries/0_stateless/01018_anbiguous_column.sql index 496a616da42..ab291178f87 100644 --- a/dbms/tests/queries/0_stateless/01018_anbiguous_column.sql +++ b/dbms/tests/queries/0_stateless/01018_anbiguous_column.sql @@ -13,12 +13,12 @@ SELECT dummy FROM one l JOIN one r ON l.dummy = dummy; -- { serverError 352 } SELECT dummy FROM one l JOIN one r ON one.dummy = r.dummy; -- { serverError 352 } SELECT dummy FROM one l JOIN one r ON l.dummy = one.dummy; -- { serverError 352 } --- SELECT * from one --- JOIN one A ON one.dummy = A.dummy --- JOIN one B ON one.dummy = B.dummy --- FORMAT PrettyCompact; --- --- SELECT * from one --- JOIN one A ON dummy = A.dummy --- JOIN one B ON dummy = B.dummy --- FORMAT PrettyCompact; +SELECT * from one +JOIN one A ON one.dummy = A.dummy +JOIN one B ON one.dummy = B.dummy +FORMAT PrettyCompact; + +SELECT * from one A +JOIN system.one one ON A.dummy = one.dummy +JOIN system.one two ON A.dummy = two.dummy +FORMAT PrettyCompact;