2018-08-22 06:42:37 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-11 14:01:45 +00:00
|
|
|
#include <Core/Names.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/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;
|
2022-11-02 06:11:37 +00:00
|
|
|
class ASTIdentifier;
|
2020-10-26 15:49:00 +00:00
|
|
|
class ASTTableIdentifier;
|
2018-09-27 19:25:18 +00:00
|
|
|
struct ASTTableExpression;
|
|
|
|
|
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
|
|
|
|
{
|
2020-10-26 15:49:00 +00:00
|
|
|
// TODO(ilezhankin): replace with ASTTableIdentifier
|
2018-08-22 06:42:37 +00:00
|
|
|
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;
|
2021-03-24 18:36:31 +00:00
|
|
|
explicit DatabaseAndTableWithAlias(const ASTPtr & identifier_node, const String & current_database = "");
|
2022-11-02 06:11:37 +00:00
|
|
|
explicit DatabaseAndTableWithAlias(const ASTIdentifier & identifier, const String & current_database = "");
|
2021-04-14 15:38:30 +00:00
|
|
|
explicit DatabaseAndTableWithAlias(const ASTTableIdentifier & identifier, const String & current_database = "");
|
2021-03-24 18:36:31 +00:00
|
|
|
explicit 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
|
|
|
}
|
|
|
|
|
2021-06-25 10:46:19 +00:00
|
|
|
bool hasColumn(const String & name) const { return names.contains(name); }
|
2020-04-14 14:43:09 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2021-04-08 08:51:22 +00:00
|
|
|
addAdditionalColumns(materialized_columns, addition);
|
2020-11-27 03:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|