2019-01-25 15:42:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-10-18 16:16:57 +00:00
|
|
|
#include <optional>
|
|
|
|
|
2019-01-25 15:42:24 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
struct IdentifierSemanticImpl
|
|
|
|
{
|
2019-02-13 15:18:02 +00:00
|
|
|
bool special = false; /// for now it's 'not a column': tables, subselects and some special stuff like FORMAT
|
|
|
|
bool can_be_alias = true; /// if it's a cropped name it could not be an alias
|
2019-12-24 18:51:37 +00:00
|
|
|
bool covered = false; /// real (compound) name is hidden by an alias (short name)
|
2019-10-18 16:16:57 +00:00
|
|
|
std::optional<size_t> membership; /// table position in join
|
2019-01-25 15:42:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Static calss to manipulate IdentifierSemanticImpl via ASTIdentifier
|
|
|
|
struct IdentifierSemantic
|
|
|
|
{
|
2019-10-17 21:08:28 +00:00
|
|
|
enum class ColumnMatch
|
|
|
|
{
|
|
|
|
NoMatch,
|
2020-03-08 11:07:05 +00:00
|
|
|
ColumnName, /// column qualified with column names list
|
2019-10-18 16:16:57 +00:00
|
|
|
AliasedTableName, /// column qualified with table name (but table has an alias so its priority is lower than TableName)
|
2019-10-17 21:08:28 +00:00
|
|
|
TableName, /// column qualified with table name
|
2019-10-18 16:16:57 +00:00
|
|
|
DbAndTable, /// column qualified with database and table name
|
2019-10-17 21:08:28 +00:00
|
|
|
TableAlias, /// column qualified with table alias
|
|
|
|
Ambiguous,
|
|
|
|
};
|
|
|
|
|
2019-01-25 15:42:24 +00:00
|
|
|
/// @returns name for column identifiers
|
|
|
|
static std::optional<String> getColumnName(const ASTIdentifier & node);
|
|
|
|
static std::optional<String> getColumnName(const ASTPtr & ast);
|
|
|
|
|
|
|
|
/// @returns name for 'not a column' identifiers
|
|
|
|
static std::optional<String> getTableName(const ASTIdentifier & node);
|
|
|
|
static std::optional<String> getTableName(const ASTPtr & ast);
|
|
|
|
static std::pair<String, String> extractDatabaseAndTable(const ASTIdentifier & identifier);
|
2019-11-13 16:49:29 +00:00
|
|
|
static std::optional<String> extractNestedName(const ASTIdentifier & identifier, const String & table_name);
|
2019-01-25 15:42:24 +00:00
|
|
|
|
2019-10-17 21:08:28 +00:00
|
|
|
static ColumnMatch canReferColumnToTable(const ASTIdentifier & identifier, const DatabaseAndTableWithAlias & db_and_table);
|
2020-03-08 11:07:05 +00:00
|
|
|
static ColumnMatch canReferColumnToTable(const ASTIdentifier & identifier, const TableWithColumnNames & db_and_table);
|
|
|
|
static ColumnMatch canReferColumnToTable(const ASTIdentifier & identifier, const TableWithColumnNamesAndTypes & db_and_table);
|
|
|
|
|
2019-10-18 16:16:57 +00:00
|
|
|
static void setColumnShortName(ASTIdentifier & identifier, const DatabaseAndTableWithAlias & db_and_table);
|
2019-02-20 12:12:36 +00:00
|
|
|
static void setColumnLongName(ASTIdentifier & identifier, const DatabaseAndTableWithAlias & db_and_table);
|
2019-02-11 19:14:57 +00:00
|
|
|
static bool canBeAlias(const ASTIdentifier & identifier);
|
2020-03-09 00:08:02 +00:00
|
|
|
static void setMembership(ASTIdentifier &, size_t table_pos);
|
2019-12-24 18:51:37 +00:00
|
|
|
static void coverName(ASTIdentifier &, const String & alias);
|
|
|
|
static std::optional<ASTIdentifier> uncover(const ASTIdentifier & identifier);
|
2019-10-18 16:16:57 +00:00
|
|
|
static std::optional<size_t> getMembership(const ASTIdentifier & identifier);
|
2020-03-08 11:07:05 +00:00
|
|
|
static std::optional<size_t> chooseTable(const ASTIdentifier &, const std::vector<DatabaseAndTableWithAlias> & tables,
|
|
|
|
bool allow_ambiguous = false);
|
|
|
|
static std::optional<size_t> chooseTable(const ASTIdentifier &, const std::vector<TableWithColumnNames> & tables,
|
|
|
|
bool allow_ambiguous = false);
|
|
|
|
static std::optional<size_t> chooseTable(const ASTIdentifier &, const std::vector<TableWithColumnNamesAndTypes> & tables,
|
|
|
|
bool allow_ambiguous = false);
|
2019-01-25 15:42:24 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static bool doesIdentifierBelongTo(const ASTIdentifier & identifier, const String & database, const String & table);
|
|
|
|
static bool doesIdentifierBelongTo(const ASTIdentifier & identifier, const String & table);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|