aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/DatabaseAndTableWithAlias.h
blob: 58327ff1d8182eb509e3948da0d680a699c43906 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once

#include <Core/Names.h>
#include <base/types.h>
#include <Core/NamesAndTypes.h>
#include <Parsers/IAST_fwd.h>

#include <memory>
#include <optional>
#include <Core/UUID.h>


namespace DB
{

class ASTSelectQuery;
class ASTIdentifier;
class ASTTableIdentifier;
struct ASTTableExpression;

/// Extracts database name (and/or alias) from table expression or identifier
struct DatabaseAndTableWithAlias
{
    // TODO(ilezhankin): replace with ASTTableIdentifier
    String database;
    String table;
    String alias;
    UUID uuid = UUIDHelpers::Nil;

    DatabaseAndTableWithAlias() = default;
    explicit DatabaseAndTableWithAlias(const ASTPtr & identifier_node, const String & current_database = "");
    explicit DatabaseAndTableWithAlias(const ASTIdentifier & identifier, const String & current_database = "");
    explicit DatabaseAndTableWithAlias(const ASTTableIdentifier & 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;

    /// Check if it satisfies another db_table name. @note opterion is not symmetric.
    bool satisfies(const DatabaseAndTableWithAlias & table, bool table_may_be_an_alias) const;

    /// Exactly the same table name
    bool same(const DatabaseAndTableWithAlias & db_table) const
    {
        return database == db_table.database && table == db_table.table && alias == db_table.alias && uuid == db_table.uuid;
    }
};

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;

    TableWithColumnNamesAndTypes(const DatabaseAndTableWithAlias & table_, const NamesAndTypesList & columns_)
        : table(table_)
        , columns(columns_)
    {
        for (auto & col : columns)
            names.insert(col.name);
    }

    bool hasColumn(const String & name) const { return names.contains(name); }

    void addHiddenColumns(const NamesAndTypesList & addition)
    {
        addAdditionalColumns(hidden_columns, addition);
    }

    void addAliasColumns(const NamesAndTypesList & addition)
    {
        addAdditionalColumns(alias_columns, addition);
    }

    void addMaterializedColumns(const NamesAndTypesList & addition)
    {
        addAdditionalColumns(materialized_columns, addition);
    }

private:
    void addAdditionalColumns(NamesAndTypesList & target, const NamesAndTypesList & addition)
    {
        target.insert(target.end(), addition.begin(), addition.end());
        for (const auto & col : addition)
            names.insert(col.name);
    }

    NameSet names;
};

std::vector<DatabaseAndTableWithAlias> getDatabaseAndTables(const ASTSelectQuery & select_query, const String & current_database);
std::optional<DatabaseAndTableWithAlias> getDatabaseAndTable(const ASTSelectQuery & select, size_t table_number);

using TablesWithColumns = std::vector<TableWithColumnNamesAndTypes>;

}