ClickHouse/src/Interpreters/DatabaseAndTableWithAlias.h

98 lines
3.0 KiB
C++
Raw Normal View History

#pragma once
2019-03-11 14:01:45 +00:00
#include <Core/Names.h>
2020-09-15 09:55:57 +00:00
#include <common/types.h>
#include <Core/NamesAndTypes.h>
2019-03-11 14:01:45 +00:00
#include <Parsers/IAST_fwd.h>
2018-09-28 15:01:13 +00:00
#include <memory>
2018-11-01 11:32:44 +00:00
#include <optional>
2020-03-12 18:04:29 +00:00
#include <Core/UUID.h>
2019-01-16 17:26:14 +00:00
namespace DB
{
class ASTSelectQuery;
class ASTIdentifier;
struct ASTTableExpression;
/// Extracts database name (and/or alias) from table expression or identifier
struct DatabaseAndTableWithAlias
{
String database;
String table;
String alias;
2020-03-12 18:04:29 +00:00
UUID uuid = UUIDHelpers::Nil;
2019-01-10 18:58:55 +00:00
DatabaseAndTableWithAlias() = default;
2021-03-24 18:36:31 +00:00
explicit DatabaseAndTableWithAlias(const ASTPtr & identifier_node, const String & current_database = "");
explicit DatabaseAndTableWithAlias(const ASTIdentifier & identifier, const String & current_database = "");
explicit DatabaseAndTableWithAlias(const ASTTableExpression & table_expression, const String & current_database = "");
/// "alias." or "table." if alias is empty
String getQualifiedNamePrefix(bool with_dot = true) const;
2019-01-16 12:08:43 +00:00
/// Check if it satisfies another db_table name. @note opterion is not symmetric.
2020-04-22 06:01:33 +00:00
bool satisfies(const DatabaseAndTableWithAlias & table, bool table_may_be_an_alias) const;
/// Exactly the same table name
bool same(const DatabaseAndTableWithAlias & db_table) const
{
2020-03-12 18:04:29 +00:00
return database == db_table.database && table == db_table.table && alias == db_table.alias && uuid == db_table.uuid;
}
};
2020-03-02 19:39:39 +00:00
struct TableWithColumnNamesAndTypes
{
DatabaseAndTableWithAlias table;
NamesAndTypesList columns;
NamesAndTypesList hidden_columns; /// Not general columns like MATERIALIZED, ALIAS, VIRTUAL. They are omitted in * and t.* results by default.
NamesAndTypesList alias_columns;
NamesAndTypesList materialized_columns;
2020-03-02 19:39:39 +00:00
TableWithColumnNamesAndTypes(const DatabaseAndTableWithAlias & table_, const NamesAndTypesList & columns_)
: table(table_)
, columns(columns_)
{
2020-04-14 14:43:09 +00:00
for (auto & col : columns)
names.insert(col.name);
}
2020-04-14 14:43:09 +00:00
bool hasColumn(const String & name) const { return names.count(name); }
2020-03-02 19:39:39 +00:00
void addHiddenColumns(const NamesAndTypesList & addition)
{
addAdditionalColumns(hidden_columns, addition);
}
void addAliasColumns(const NamesAndTypesList & addition)
{
addAdditionalColumns(alias_columns, addition);
}
void addMaterializedColumns(const NamesAndTypesList & addition)
{
2021-04-08 08:51:22 +00:00
addAdditionalColumns(materialized_columns, addition);
}
private:
void addAdditionalColumns(NamesAndTypesList & target, const NamesAndTypesList & addition)
{
target.insert(target.end(), addition.begin(), addition.end());
2021-03-24 18:36:31 +00:00
for (const auto & col : addition)
2020-04-14 14:43:09 +00:00
names.insert(col.name);
2020-03-02 19:39:39 +00:00
}
private:
2020-04-14 14:43:09 +00:00
NameSet names;
2020-03-02 19:39:39 +00:00
};
std::vector<DatabaseAndTableWithAlias> getDatabaseAndTables(const ASTSelectQuery & select_query, const String & current_database);
2018-11-01 11:32:44 +00:00
std::optional<DatabaseAndTableWithAlias> getDatabaseAndTable(const ASTSelectQuery & select, size_t table_number);
using TablesWithColumns = std::vector<TableWithColumnNamesAndTypes>;
2019-12-18 03:56:03 +00:00
}