better ambiguous column detection in multiple join rewriter

This commit is contained in:
chertus 2019-10-18 20:09:17 +03:00
parent 37f07213ee
commit c5a850240c
3 changed files with 23 additions and 19 deletions

View File

@ -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())

View File

@ -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 │
└─────────┴───────────┴───────────┘

View File

@ -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;