some multiple join fixes

This commit is contained in:
chertus 2019-03-05 15:34:48 +03:00
parent a8106360bd
commit ea4d3ec661
5 changed files with 48 additions and 27 deletions

View File

@ -36,9 +36,10 @@ struct ColumnAliasesMatcher
{
const std::vector<DatabaseAndTableWithAlias> tables;
bool public_names;
AsteriskSemantic::RevertedAliases rev_aliases;
std::unordered_map<String, String> aliases;
AsteriskSemantic::RevertedAliases rev_aliases; /// long_name -> aliases
std::unordered_map<String, String> aliases; /// alias -> long_name
std::vector<std::pair<ASTIdentifier *, bool>> compound_identifiers;
std::set<String> allowed_long_names; /// original names allowed as aliases '--t.x as t.x' (select expressions only).
Data(std::vector<DatabaseAndTableWithAlias> && tables_)
: tables(tables_)
@ -51,29 +52,37 @@ struct ColumnAliasesMatcher
for (auto & [identifier, is_public] : compound_identifiers)
{
auto it = rev_aliases.find(identifier->name);
String long_name = identifier->name;
auto it = rev_aliases.find(long_name);
if (it == rev_aliases.end())
{
bool last_table = IdentifierSemantic::canReferColumnToTable(*identifier, tables.back());
if (!last_table)
{
String long_name = identifier->name;
String alias = hide_prefix + long_name;
aliases[alias] = long_name;
rev_aliases[long_name].push_back(alias);
identifier->setShortName(alias);
if (is_public)
{
identifier->setAlias(long_name);
allowed_long_names.insert(long_name);
}
}
else if (is_public)
identifier->setAlias(identifier->name); /// prevent crop long to short name
identifier->setAlias(long_name); /// prevent crop long to short name
}
else
{
if (it->second.empty())
throw Exception("No alias for '" + identifier->name + "'", ErrorCodes::LOGICAL_ERROR);
identifier->setShortName(it->second[0]);
throw Exception("No alias for '" + long_name + "'", ErrorCodes::LOGICAL_ERROR);
if (is_public && allowed_long_names.count(long_name))
; /// leave original name unchanged for correct output
else
identifier->setShortName(it->second[0]);
}
}
}
@ -131,7 +140,7 @@ struct ColumnAliasesMatcher
node.setAlias("");
}
}
else
else if (node.compound())
data.compound_identifiers.emplace_back(&node, data.public_names);
}
};

View File

@ -32,3 +32,7 @@
6 6 60 60
12 12 120 120
18 18 180 180
0 0 0 0 0 0 0
6 6 60 60 66 66 120
12 12 120 120 132 132 240
18 18 180 180 198 198 360

View File

@ -69,11 +69,11 @@ from table1 as t1
join table2 as t2 on t1.a = t2.a
join table3 as t3 on t2.b = t3.b; -- { serverError 48 }
--select t1.a as t1_a, t2.a as t2_a, t2.b as t2_b, t3.b as t3_b,
-- (t1.a + table2.b) as t1_t2_x, (table1.a + table3.b) as t1_t3_x, (t2.b + t3.b) as t2_t3_x
--from table1 as t1
--join table2 as t2 on t1_a = t2_a
--join table3 as t3 on t2_b = t3_b;
select t1.a as t1_a, t2.a as t2_a, t2.b as t2_b, t3.b as t3_b,
(t1.a + table2.b) as t1_t2_x, (table1.a + table3.b) as t1_t3_x, (t2.b + t3.b) as t2_t3_x
from table1 as t1
join table2 as t2 on t1_a = t2_a
join table3 as t3 on t2_b = t3_b;
--select (t1.a + table2.b) as t1_t2_x, (table1.a + table3.b) as t1_t3_x, (t2.b + t3.b) as t2_t3_x
--from table1 as t1

View File

@ -31,7 +31,15 @@ y.b: 0
│ 1 │ 1 │ 1 │
│ 2 │ 2 │ 2 │
└─────┴─────┴─────┘
┌─s.a─┬─s.a─┐
│ 1 │ 1 │
│ 0 │ 0 │
└─────┴─────┘
┌─s.a─┬─s.a─┬─s_b─┬─s_b─┐
│ 1 │ 1 │ 1 │ 1 │
│ 0 │ 0 │ 0 │ 0 │
└─────┴─────┴─────┴─────┘
┌─y.a─┬─y.a─┬─y_b─┬─y_b─┐
│ 1 │ 1 │ 1 │ 1 │
│ 0 │ 0 │ 0 │ 0 │
└─────┴─────┴─────┴─────┘
┌─t_a─┬─t_a─┬─s_a─┬─s_a─┬─y_a─┬─y_a─┐
│ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │
│ 2 │ 2 │ 0 │ 0 │ 0 │ 0 │
└─────┴─────┴─────┴─────┴─────┴─────┘

View File

@ -28,21 +28,21 @@ left join s on s.a = t.a
left join y on y.b = s.b format PrettyCompactNoEscapes;
select t.a, t.a, t.b as t_b from t
left join s on t.a = s.a
left join y on y.b = s.b format PrettyCompactNoEscapes;
select s.a, s.a, s.b as s_b, s.b from t
left join s on s.a = t.a
left join y on s.b = y.b format PrettyCompactNoEscapes;
select y.a, y.a, y.b as y_b, y.b from t
left join s on s.a = t.a
left join y on y.b = s.b format PrettyCompactNoEscapes;
select s.a, s.a from t
left join s on s.a = t.a
select t.a, t.a as t_a, s.a, s.a as s_a, y.a, y.a as y_a from t
left join s on t.a = s.a
left join y on y.b = s.b format PrettyCompactNoEscapes;
--select t.a, t.a, t.b as t_b, t.b from t
--left join s on s.a = t.a
--left join y on y.b = s.b format PrettyCompactNoEscapes;
--select t.a, t.a, s.b as s_b, s.b from t
--left join s on s.a = t.a
--left join y on y.b = s.b format PrettyCompactNoEscapes;
drop table t;
drop table s;
drop table y;