mJOIN: correct names for left table columns

This commit is contained in:
chertus 2019-02-21 15:45:31 +03:00
parent b2fa415858
commit 10b473bb24
2 changed files with 14 additions and 7 deletions

View File

@ -35,19 +35,21 @@ struct ColumnAliasesMatcher
struct Data struct Data
{ {
const std::vector<DatabaseAndTableWithAlias> tables; const std::vector<DatabaseAndTableWithAlias> tables;
bool public_names;
AsteriskSemantic::RevertedAliases rev_aliases; AsteriskSemantic::RevertedAliases rev_aliases;
std::unordered_map<String, String> aliases; std::unordered_map<String, String> aliases;
std::vector<ASTIdentifier *> compound_identifiers; std::vector<std::pair<ASTIdentifier *, bool>> compound_identifiers;
Data(std::vector<DatabaseAndTableWithAlias> && tables_) Data(std::vector<DatabaseAndTableWithAlias> && tables_)
: tables(tables_) : tables(tables_)
, public_names(false)
{} {}
void replaceIdentifiersWithAliases() void replaceIdentifiersWithAliases()
{ {
String hide_prefix = "--"; /// @note restriction: user should not use alises like `--table.column` String hide_prefix = "--"; /// @note restriction: user should not use alises like `--table.column`
for (auto * identifier : compound_identifiers) for (auto & [identifier, is_public] : compound_identifiers)
{ {
auto it = rev_aliases.find(identifier->name); auto it = rev_aliases.find(identifier->name);
if (it == rev_aliases.end()) if (it == rev_aliases.end())
@ -61,7 +63,8 @@ struct ColumnAliasesMatcher
rev_aliases[long_name].push_back(alias); rev_aliases[long_name].push_back(alias);
identifier->setShortName(alias); identifier->setShortName(alias);
//identifier->setAlias(long_name); if (is_public)
identifier->setAlias(long_name);
} }
} }
else else
@ -130,7 +133,7 @@ struct ColumnAliasesMatcher
} }
} }
else else
data.compound_identifiers.push_back(&node); data.compound_identifiers.emplace_back(&node, data.public_names);
} }
}; };
@ -236,7 +239,11 @@ void JoinToSubqueryTransformMatcher::visit(ASTSelectQuery & select, ASTPtr &, Da
ColumnAliasesVisitor::Data aliases_data(getDatabaseAndTables(select, "")); ColumnAliasesVisitor::Data aliases_data(getDatabaseAndTables(select, ""));
if (select.select_expression_list) if (select.select_expression_list)
{
aliases_data.public_names = true;
ColumnAliasesVisitor(aliases_data).visit(select.select_expression_list); ColumnAliasesVisitor(aliases_data).visit(select.select_expression_list);
aliases_data.public_names = false;
}
if (select.where_expression) if (select.where_expression)
ColumnAliasesVisitor(aliases_data).visit(select.where_expression); ColumnAliasesVisitor(aliases_data).visit(select.where_expression);
if (select.prewhere_expression) if (select.prewhere_expression)

View File

@ -5,9 +5,9 @@
0 0 0 0 0 0
10 100 1000 10 100 1000
20 200 2000 20 200 2000
┌─--t1.a─┬─--t2.a─┬─--t2.b─┬─--t3.b─┬─--t3.c─┬─a─┬─b─┬─c─┐ ┌─t1.a─┬─t2.a─┬─t2.b─┬─t3.b─┬─t3.c─┬─a─┬─b─┬─c─┐
0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │
└────────────────┴──────────────┴────────┴───┴───┴───┘ └──────┴──────┴──────┴──────┴──────┴───┴───┴───┘
0 0 0 0 0 0 0 0
6 6 60 60 6 6 60 60
12 12 120 120 12 12 120 120