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();
|
2019-05-13 18:58:15 +00:00
|
|
|
required_names[node.name].addInclusion(alias);
|
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-05-13 18:58:15 +00:00
|
|
|
size_t ColumnNamesContext::nameInclusion(const String & name) const
|
|
|
|
{
|
|
|
|
auto it = required_names.find(name);
|
|
|
|
if (it != required_names.end())
|
|
|
|
return it->second.appears;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-15 15:47:18 +00:00
|
|
|
NameSet ColumnNamesContext::requiredColumns() 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);
|
|
|
|
}
|
|
|
|
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 << "'";
|
2019-05-13 18:58:15 +00:00
|
|
|
for (auto & alias : pr.second.aliases)
|
2019-02-15 15:34:30 +00:00
|
|
|
os << "/'" << alias << "'";
|
2020-04-12 02:05:30 +00:00
|
|
|
os << ", ";
|
2019-02-15 15:34:30 +00:00
|
|
|
}
|
2020-04-12 02:06:58 +00:00
|
|
|
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)
|
2020-04-12 02:05:30 +00:00
|
|
|
os << "'" << *alias << "'/'" << *name << "', ";
|
2018-12-26 14:43:25 +00:00
|
|
|
else if (alias)
|
2020-04-12 02:05:30 +00:00
|
|
|
os << "'" << *alias << "', ";
|
2018-12-26 14:43:25 +00:00
|
|
|
else if (name)
|
2020-04-12 02:05:30 +00:00
|
|
|
os << "'" << *name << "', ";
|
2018-12-26 14:43:25 +00:00
|
|
|
}
|
|
|
|
os << "table_aliases: ";
|
|
|
|
for (const auto & x : cols.table_aliases)
|
2020-04-12 02:05:30 +00:00
|
|
|
os << "'" << x << "', ";
|
2019-02-15 15:34:30 +00:00
|
|
|
os << "complex_aliases: ";
|
|
|
|
for (const auto & x : cols.complex_aliases)
|
2020-04-12 02:05:30 +00:00
|
|
|
os << "'" << x << "', ";
|
2018-12-26 14:43:25 +00:00
|
|
|
os << "masked_columns: ";
|
|
|
|
for (const auto & x : cols.masked_columns)
|
2020-04-12 02:05:30 +00:00
|
|
|
os << "'" << x << "', ";
|
2018-12-26 14:43:25 +00:00
|
|
|
os << "array_join_columns: ";
|
|
|
|
for (const auto & x : cols.array_join_columns)
|
2020-04-12 02:05:30 +00:00
|
|
|
os << "'" << x << "', ";
|
2018-12-26 14:43:25 +00:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|