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

#include <optional>
#include <vector>
#include <memory>
#include <unordered_map>

#include <Parsers/ASTAlterQuery.h>
#include <Storages/IStorage_fwd.h>
#include <DataTypes/IDataType.h>
#include <Core/Names.h>

namespace DB
{

class Context;
class WriteBuffer;
class ReadBuffer;

/// Represents set of actions which should be applied
/// to values from set of columns which satisfy predicate.
struct MutationCommand
{
    ASTPtr ast = {}; /// The AST of the whole command

    enum Type
    {
        EMPTY,     /// Not used.
        DELETE,
        UPDATE,
        MATERIALIZE_INDEX,
        MATERIALIZE_PROJECTION,
        READ_COLUMN, /// Read column and apply conversions (MODIFY COLUMN alter query).
        DROP_COLUMN,
        DROP_INDEX,
        DROP_PROJECTION,
        MATERIALIZE_TTL,
        RENAME_COLUMN,
        MATERIALIZE_COLUMN,
        ALTER_WITHOUT_MUTATION, /// pure metadata command, currently unusned
    };

    Type type = EMPTY;

    /// WHERE part of mutation
    ASTPtr predicate = {};

    /// Columns with corresponding actions
    std::unordered_map<String, ASTPtr> column_to_update_expression = {};

    /// For MATERIALIZE INDEX and PROJECTION
    String index_name = {};
    String projection_name = {};

    /// For MATERIALIZE INDEX, UPDATE and DELETE.
    ASTPtr partition = {};

    /// For reads, drops and etc.
    String column_name = {};
    DataTypePtr data_type = {}; /// Maybe empty if we just want to drop column

    /// We need just clear column, not drop from metadata.
    bool clear = false;

    /// Column rename_to
    String rename_to = {};

    /// If parse_alter_commands, than consider more Alter commands as mutation commands
    static std::optional<MutationCommand> parse(ASTAlterCommand * command, bool parse_alter_commands = false);

    /// This command shouldn't stick with other commands
    bool isBarrierCommand() const;
};

/// Multiple mutation commands, possible from different ALTER queries
class MutationCommands : public std::vector<MutationCommand>
{
public:
    std::shared_ptr<ASTExpressionList> ast(bool with_pure_metadata_commands = false) const;

    void writeText(WriteBuffer & out, bool with_pure_metadata_commands) const;
    void readText(ReadBuffer & in);
    std::string toString() const;
    bool hasNonEmptyMutationCommands() const;

    /// These set of commands contain barrier command and shouldn't
    /// stick with other commands. Commands from one set have already been validated
    /// to be executed without issues on the creation state.
    bool containBarrierCommand() const;
};

using MutationCommandsConstPtr = std::shared_ptr<MutationCommands>;

}