aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/KeyDescription.h
blob: 527a36124aa9cd9eb9a70ef7f1029676aaa9b553 (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
#pragma once

#include <Interpreters/ExpressionActions.h>
#include <Parsers/IAST_fwd.h>
#include <Storages/ColumnsDescription.h>

namespace DB
{
/// Common structure for primary, partition and other storage keys
struct KeyDescription
{
    /// User defined AST in CREATE/ALTER query. This field may be empty, but key
    /// can exists because some of them maybe set implicitly (for example,
    /// primary key in merge tree can be part of sorting key)
    ASTPtr definition_ast;

    /// ASTExpressionList with key fields, example: (x, toStartOfMonth(date))).
    ASTPtr expression_list_ast;

    /// Expression from expression_list_ast created by ExpressionAnalyzer. Useful,
    /// when you need to get required columns for key, example: a, date, b.
    ExpressionActionsPtr expression;

    /// Sample block with key columns (names, types, empty column)
    Block sample_block;

    /// Column names in key definition, example: x, toStartOfMonth(date), a * b.
    Names column_names;

    /// Types from sample block ordered in columns order.
    DataTypes data_types;

    /// Additional key column added by storage type. Never changes after
    /// initialization with non empty value. Doesn't stored in definition_ast,
    /// but added to expression_list_ast and all its derivatives.
    std::optional<String> additional_column;

    /// Parse key structure from key definition. Requires all columns, available
    /// in storage.
    static KeyDescription getKeyFromAST(
        const ASTPtr & definition_ast,
        const ColumnsDescription & columns,
        ContextPtr context);

    /// Sorting key can contain additional column defined by storage type (like
    /// Version column in VersionedCollapsingMergeTree).
    static KeyDescription getSortingKeyFromAST(
        const ASTPtr & definition_ast,
        const ColumnsDescription & columns,
        ContextPtr context,
        const std::optional<String> & additional_column);

    /// Build an empty key description. It's different from the default constructor with some
    /// additional initializations.
    static KeyDescription buildEmptyKey();

    /// Recalculate all expressions and fields for key with new columns without
    /// changes in constant fields. Just wrapper for static methods.
    void recalculateWithNewColumns(
        const ColumnsDescription & new_columns,
        ContextPtr context);

    /// Recalculate all expressions and fields for key with new ast without
    /// changes in constant fields. Just wrapper for static methods.
    void recalculateWithNewAST(
        const ASTPtr & new_ast,
        const ColumnsDescription & columns,
        ContextPtr context);

    KeyDescription() = default;

    /// We need custom copy constructors because we don't want
    /// unintentionaly share AST variables and modify them.
    KeyDescription(const KeyDescription & other);
    KeyDescription & operator=(const KeyDescription & other);

    /// Substitute modulo with moduloLegacy. Used in KeyCondition to allow proper comparison with keys.
    static bool moduloToModuloLegacyRecursive(ASTPtr node_expr);

    /// Parse description from string
    static KeyDescription parse(const String & str, const ColumnsDescription & columns, ContextPtr context);
};

}