1#pragma once
2
3#include <Core/Names.h>
4#include <Core/Types.h>
5#include <Core/NamesAndTypes.h>
6#include <Parsers/IAST_fwd.h>
7
8#include <memory>
9#include <optional>
10
11
12namespace DB
13{
14
15class ASTSelectQuery;
16class ASTIdentifier;
17struct ASTTableExpression;
18class Context;
19
20
21/// Extracts database name (and/or alias) from table expression or identifier
22struct DatabaseAndTableWithAlias
23{
24 String database;
25 String table;
26 String alias;
27
28 DatabaseAndTableWithAlias() = default;
29 DatabaseAndTableWithAlias(const ASTPtr & identifier_node, const String & current_database = "");
30 DatabaseAndTableWithAlias(const ASTIdentifier & identifier, const String & current_database = "");
31 DatabaseAndTableWithAlias(const ASTTableExpression & table_expression, const String & current_database = "");
32
33 /// "alias." or "table." if alias is empty
34 String getQualifiedNamePrefix(bool with_dot = true) const;
35
36 /// Check if it satisfies another db_table name. @note opterion is not symmetric.
37 bool satisfies(const DatabaseAndTableWithAlias & table, bool table_may_be_an_alias);
38};
39
40struct TableWithColumnNames
41{
42 DatabaseAndTableWithAlias table;
43 Names columns;
44 Names hidden_columns;
45
46 TableWithColumnNames(const DatabaseAndTableWithAlias & table_, const Names & columns_)
47 : table(table_)
48 , columns(columns_)
49 {}
50
51 void addHiddenColumns(const NamesAndTypesList & addition)
52 {
53 for (auto & column : addition)
54 hidden_columns.push_back(column.name);
55 }
56
57 bool hasColumn(const String & name) const
58 {
59 if (columns_set.empty())
60 {
61 columns_set.insert(columns.begin(), columns.end());
62 columns_set.insert(hidden_columns.begin(), hidden_columns.end());
63 }
64
65 return columns_set.count(name);
66 }
67
68private:
69 mutable NameSet columns_set;
70};
71
72std::vector<DatabaseAndTableWithAlias> getDatabaseAndTables(const ASTSelectQuery & select_query, const String & current_database);
73std::optional<DatabaseAndTableWithAlias> getDatabaseAndTable(const ASTSelectQuery & select, size_t table_number);
74
75}
76