2018-12-26 14:43:25 +00:00
|
|
|
#include <Interpreters/ColumnNamesContext.h>
|
2019-01-25 15:42:24 +00:00
|
|
|
#include <Interpreters/IdentifierSemantic.h>
|
2018-12-26 14:43:25 +00:00
|
|
|
#include <DataTypes/NestedUtils.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
bool ColumnNamesContext::addTableAliasIfAny(const IAST & ast)
|
|
|
|
{
|
|
|
|
String alias = ast.tryGetAlias();
|
|
|
|
if (alias.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
table_aliases.insert(alias);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-15 15:34:30 +00:00
|
|
|
bool ColumnNamesContext::addColumnAliasIfAny(const IAST & ast)
|
2018-12-26 14:43:25 +00:00
|
|
|
{
|
|
|
|
String alias = ast.tryGetAlias();
|
|
|
|
if (alias.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (required_names.count(alias))
|
|
|
|
masked_columns.insert(alias);
|
|
|
|
|
2019-02-15 15:34:30 +00:00
|
|
|
complex_aliases.insert(alias);
|
2018-12-26 14:43:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-15 15:34:30 +00:00
|
|
|
void ColumnNamesContext::addColumnIdentifier(const ASTIdentifier & node)
|
2018-12-26 14:43:25 +00:00
|
|
|
{
|
2019-01-25 15:42:24 +00:00
|
|
|
if (!IdentifierSemantic::getColumnName(node))
|
2018-12-26 14:43:25 +00:00
|
|
|
return;
|
|
|
|
|
2019-02-15 15:34:30 +00:00
|
|
|
/// There should be no complex cases after query normalization. Names to aliases: one-to-many.
|
|
|
|
String alias = node.tryGetAlias();
|
|
|
|
if (!alias.empty())
|
|
|
|
required_names[node.name].insert(alias);
|
2018-12-26 14:43:25 +00:00
|
|
|
|
2019-02-15 15:34:30 +00:00
|
|
|
if (!required_names.count(node.name))
|
|
|
|
required_names[node.name] = {};
|
2018-12-26 14:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ColumnNamesContext::addArrayJoinAliasIfAny(const IAST & ast)
|
|
|
|
{
|
|
|
|
String alias = ast.tryGetAlias();
|
|
|
|
if (alias.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
array_join_columns.insert(alias);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNamesContext::addArrayJoinIdentifier(const ASTIdentifier & node)
|
|
|
|
{
|
|
|
|
array_join_columns.insert(node.name);
|
|
|
|
}
|
|
|
|
|
2019-02-15 15:34:30 +00:00
|
|
|
NameSet ColumnNamesContext::requiredColumns(std::unordered_map<String, String> & alias_map) const
|
2018-12-26 14:43:25 +00:00
|
|
|
{
|
|
|
|
NameSet required;
|
2019-02-15 15:34:30 +00:00
|
|
|
for (const auto & pr : required_names)
|
2018-12-26 14:43:25 +00:00
|
|
|
{
|
2019-02-15 15:34:30 +00:00
|
|
|
const auto & name = pr.first;
|
2018-12-26 14:43:25 +00:00
|
|
|
String table_name = Nested::extractTableName(name);
|
|
|
|
|
|
|
|
/// Tech debt. There's its own logic for ARRAY JOIN columns.
|
|
|
|
if (array_join_columns.count(name) || array_join_columns.count(table_name))
|
|
|
|
continue;
|
|
|
|
|
2019-02-15 15:34:30 +00:00
|
|
|
if (!complex_aliases.count(name) || masked_columns.count(name))
|
2018-12-26 14:43:25 +00:00
|
|
|
required.insert(name);
|
2019-02-15 15:34:30 +00:00
|
|
|
|
|
|
|
/// It's possible that column is masked but alias is available
|
|
|
|
for (const String & alias : pr.second)
|
|
|
|
alias_map[alias] = name;
|
2018-12-26 14:43:25 +00:00
|
|
|
}
|
|
|
|
return required;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream & operator << (std::ostream & os, const ColumnNamesContext & cols)
|
|
|
|
{
|
|
|
|
os << "required_names: ";
|
2019-02-15 15:34:30 +00:00
|
|
|
for (const auto & pr : cols.required_names)
|
|
|
|
{
|
|
|
|
os << "'" << pr.first << "'";
|
|
|
|
for (auto & alias : pr.second)
|
|
|
|
os << "/'" << alias << "'";
|
|
|
|
}
|
|
|
|
os << " source_tables: ";
|
2018-12-26 14:43:25 +00:00
|
|
|
for (const auto & x : cols.tables)
|
|
|
|
{
|
|
|
|
auto alias = x.alias();
|
|
|
|
auto name = x.name();
|
|
|
|
if (alias && name)
|
|
|
|
os << "'" << *alias << "'/'" << *name << "' ";
|
|
|
|
else if (alias)
|
|
|
|
os << "'" << *alias << "' ";
|
|
|
|
else if (name)
|
|
|
|
os << "'" << *name << "' ";
|
|
|
|
}
|
|
|
|
os << "table_aliases: ";
|
|
|
|
for (const auto & x : cols.table_aliases)
|
|
|
|
os << "'" << x << "' ";
|
2019-02-15 15:34:30 +00:00
|
|
|
os << "complex_aliases: ";
|
|
|
|
for (const auto & x : cols.complex_aliases)
|
2018-12-26 14:43:25 +00:00
|
|
|
os << "'" << x << "' ";
|
|
|
|
os << "masked_columns: ";
|
|
|
|
for (const auto & x : cols.masked_columns)
|
|
|
|
os << "'" << x << "' ";
|
|
|
|
os << "array_join_columns: ";
|
|
|
|
for (const auto & x : cols.array_join_columns)
|
|
|
|
os << "'" << x << "' ";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|