2018-12-26 14:43:25 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <ostream>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <Core/Names.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Information about table and column names extracted from ASTSelectQuery block. Do not include info from subselects.
|
|
|
|
struct ColumnNamesContext
|
|
|
|
{
|
|
|
|
struct JoinedTable
|
|
|
|
{
|
2019-01-11 19:33:00 +00:00
|
|
|
const ASTTableExpression * expr = nullptr;
|
|
|
|
const ASTTableJoin * join = nullptr;
|
2018-12-26 14:43:25 +00:00
|
|
|
|
|
|
|
std::optional<String> alias() const
|
|
|
|
{
|
|
|
|
String alias;
|
2019-01-11 19:33:00 +00:00
|
|
|
if (expr)
|
|
|
|
{
|
|
|
|
if (expr->database_and_table_name)
|
|
|
|
alias = expr->database_and_table_name->tryGetAlias();
|
|
|
|
else if (expr->table_function)
|
|
|
|
alias = expr->table_function->tryGetAlias();
|
|
|
|
else if (expr->subquery)
|
|
|
|
alias = expr->subquery->tryGetAlias();
|
|
|
|
}
|
2018-12-26 14:43:25 +00:00
|
|
|
if (!alias.empty())
|
|
|
|
return alias;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<String> name() const
|
|
|
|
{
|
2019-01-11 19:33:00 +00:00
|
|
|
if (expr)
|
2019-01-14 18:15:04 +00:00
|
|
|
return getIdentifierName(expr->database_and_table_name);
|
2018-12-26 14:43:25 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<ASTTableJoin::Kind> joinKind() const
|
|
|
|
{
|
|
|
|
if (join)
|
|
|
|
return join->kind;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-15 15:34:30 +00:00
|
|
|
std::unordered_map<String, std::set<String>> required_names; /// names with aliases
|
2018-12-26 14:43:25 +00:00
|
|
|
NameSet table_aliases;
|
|
|
|
NameSet private_aliases;
|
2019-02-15 15:34:30 +00:00
|
|
|
NameSet complex_aliases;
|
2018-12-26 14:43:25 +00:00
|
|
|
NameSet masked_columns;
|
|
|
|
NameSet array_join_columns;
|
|
|
|
std::vector<JoinedTable> tables; /// ordered list of visited tables in FROM section with joins
|
|
|
|
bool has_table_join = false;
|
|
|
|
bool has_array_join = false;
|
|
|
|
|
|
|
|
bool addTableAliasIfAny(const IAST & ast);
|
2019-02-15 15:34:30 +00:00
|
|
|
bool addColumnAliasIfAny(const IAST & ast);
|
|
|
|
void addColumnIdentifier(const ASTIdentifier & node);
|
2018-12-26 14:43:25 +00:00
|
|
|
bool addArrayJoinAliasIfAny(const IAST & ast);
|
|
|
|
void addArrayJoinIdentifier(const ASTIdentifier & node);
|
|
|
|
|
2019-02-15 15:34:30 +00:00
|
|
|
/// returns set of columns and map of ther aliases (alias -> column)
|
|
|
|
NameSet requiredColumns(std::unordered_map<String, String> & alias_map) const;
|
2018-12-26 14:43:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream & operator << (std::ostream & os, const ColumnNamesContext & cols);
|
|
|
|
|
|
|
|
}
|