2018-08-22 06:42:37 +00:00
|
|
|
#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>
|
2019-12-13 18:46:55 +00:00
|
|
|
#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
|
|
|
|
2018-08-22 06:42:37 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-10-26 15:13:02 +00:00
|
|
|
class ASTSelectQuery;
|
2018-09-27 19:25:18 +00:00
|
|
|
class ASTIdentifier;
|
|
|
|
struct ASTTableExpression;
|
2019-02-07 19:18:40 +00:00
|
|
|
class Context;
|
2018-09-27 19:25:18 +00:00
|
|
|
|
|
|
|
|
2018-10-30 16:31:21 +00:00
|
|
|
/// Extracts database name (and/or alias) from table expression or identifier
|
2018-08-22 06:42:37 +00:00
|
|
|
struct DatabaseAndTableWithAlias
|
|
|
|
{
|
|
|
|
String database;
|
|
|
|
String table;
|
|
|
|
String alias;
|
2020-03-12 18:04:29 +00:00
|
|
|
UUID uuid = UUIDHelpers::Nil;
|
2018-08-22 06:42:37 +00:00
|
|
|
|
2019-01-10 18:58:55 +00:00
|
|
|
DatabaseAndTableWithAlias() = default;
|
2019-01-14 18:15:04 +00:00
|
|
|
DatabaseAndTableWithAlias(const ASTPtr & identifier_node, const String & current_database = "");
|
2018-10-30 16:31:21 +00:00
|
|
|
DatabaseAndTableWithAlias(const ASTIdentifier & identifier, const String & current_database = "");
|
2019-02-01 16:36:40 +00:00
|
|
|
DatabaseAndTableWithAlias(const ASTTableExpression & table_expression, const String & current_database = "");
|
2018-10-30 16:31:21 +00:00
|
|
|
|
2019-01-25 15:42:24 +00:00
|
|
|
/// "alias." or "table." if alias is empty
|
2019-03-04 19:40:58 +00:00
|
|
|
String getQualifiedNamePrefix(bool with_dot = true) const;
|
2018-08-22 06:42:37 +00:00
|
|
|
|
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;
|
2020-03-08 11:07:05 +00:00
|
|
|
|
|
|
|
/// 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-08 11:07:05 +00:00
|
|
|
}
|
2018-08-22 06:42:37 +00:00
|
|
|
};
|
|
|
|
|
2020-03-02 19:39:39 +00:00
|
|
|
struct TableWithColumnNamesAndTypes
|
|
|
|
{
|
|
|
|
DatabaseAndTableWithAlias table;
|
|
|
|
NamesAndTypesList columns;
|
2020-11-27 03:25:19 +00:00
|
|
|
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-03-08 11:07:05 +00:00
|
|
|
{
|
2020-04-14 14:43:09 +00:00
|
|
|
for (auto & col : columns)
|
|
|
|
names.insert(col.name);
|
2020-03-08 11:07:05 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2020-11-27 03:25:19 +00:00
|
|
|
addAdditionalColumns(hidden_columns, addition);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addAliasColumns(const NamesAndTypesList & addition)
|
|
|
|
{
|
|
|
|
addAdditionalColumns(alias_columns, addition);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addMaterializedColumns(const NamesAndTypesList & addition)
|
|
|
|
{
|
|
|
|
addAdditionalColumns(alias_columns, addition);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void addAdditionalColumns(NamesAndTypesList & target, const NamesAndTypesList & addition)
|
|
|
|
{
|
|
|
|
target.insert(target.end(), addition.begin(), addition.end());
|
2020-04-14 14:43:09 +00:00
|
|
|
for (auto & col : addition)
|
|
|
|
names.insert(col.name);
|
2020-03-02 19:39:39 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 03:25:19 +00:00
|
|
|
|
2020-03-08 11:07:05 +00:00
|
|
|
private:
|
2020-04-14 14:43:09 +00:00
|
|
|
NameSet names;
|
2020-03-02 19:39:39 +00:00
|
|
|
};
|
|
|
|
|
2018-10-30 16:31:21 +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);
|
2018-10-26 15:13:02 +00:00
|
|
|
|
2020-06-05 21:17:00 +00:00
|
|
|
using TablesWithColumns = std::vector<TableWithColumnNamesAndTypes>;
|
2019-12-18 03:56:03 +00:00
|
|
|
|
2018-09-27 19:25:18 +00:00
|
|
|
}
|