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

#include <Parsers/ASTConstraintDeclaration.h>
#include <Interpreters/ExpressionActions.h>
#include <Interpreters/TreeCNFConverter.h>
#include <Interpreters/ComparisonGraph.h>

#include <Analyzer/Passes/CNF.h>

namespace DB
{

using ConstraintsExpressions = std::vector<ExpressionActionsPtr>;

struct ConstraintsDescription
{
public:
    ConstraintsDescription() { update(); }
    explicit ConstraintsDescription(const ASTs & constraints_);
    ConstraintsDescription(const ConstraintsDescription & other);
    ConstraintsDescription & operator=(const ConstraintsDescription & other);

    ConstraintsDescription(ConstraintsDescription && other) noexcept;
    ConstraintsDescription & operator=(ConstraintsDescription && other) noexcept;

    bool empty() const { return constraints.empty(); }
    String toString() const;

    static ConstraintsDescription parse(const String & str);

    enum class ConstraintType : UInt8
    {
        CHECK = 1,
        ASSUME = 2,
        ALWAYS_TRUE = CHECK | ASSUME,
        ALL = CHECK | ASSUME,
    };

    ASTs filterConstraints(ConstraintType selection) const;

    const ASTs & getConstraints() const;

    const std::vector<std::vector<CNFQuery::AtomicFormula>> & getConstraintData() const;
    std::vector<CNFQuery::AtomicFormula> getAtomicConstraintData() const;

    const ComparisonGraph<ASTPtr> & getGraph() const;

    ConstraintsExpressions getExpressions(ContextPtr context, const NamesAndTypesList & source_columns_) const;

    struct AtomId
    {
        size_t group_id;
        size_t atom_id;
    };

    using AtomIds = std::vector<AtomId>;

    std::optional<AtomIds> getAtomIds(const ASTPtr & ast) const;
    std::vector<CNFQuery::AtomicFormula> getAtomsById(const AtomIds & ids) const;

    class QueryTreeData
    {
    public:
        const QueryTreeNodes & getConstraints() const;
        const std::vector<std::vector<Analyzer::CNF::AtomicFormula>> & getConstraintData() const;
        std::optional<AtomIds> getAtomIds(const QueryTreeNodePtrWithHash & node_with_hash) const;
        std::vector<Analyzer::CNF::AtomicFormula> getAtomsById(const AtomIds & ids) const;
        const ComparisonGraph<QueryTreeNodePtr> & getGraph() const;
    private:
        QueryTreeNodes constraints;
        std::vector<std::vector<Analyzer::CNF::AtomicFormula>> cnf_constraints;
        QueryTreeNodePtrWithHashMap<AtomIds> query_node_to_atom_ids;
        std::unique_ptr<ComparisonGraph<QueryTreeNodePtr>> graph;

        friend ConstraintsDescription;
    };

    QueryTreeData getQueryTreeData(const ContextPtr & context, const QueryTreeNodePtr & table_node) const;

private:
    std::vector<std::vector<CNFQuery::AtomicFormula>> buildConstraintData() const;
    std::unique_ptr<ComparisonGraph<ASTPtr>> buildGraph() const;
    void update();

    ASTs constraints;

    std::vector<std::vector<CNFQuery::AtomicFormula>> cnf_constraints;
    std::map<IAST::Hash, AtomIds> ast_to_atom_ids;

    std::unique_ptr<ComparisonGraph<ASTPtr>> graph;
};

}