aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Analyzer/SortNode.h
blob: b860fd19a900f225680fa12e1e4fbd719aa129c0 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#pragma once

#include <Columns/Collator.h>

#include <Analyzer/IQueryTreeNode.h>
#include <Analyzer/ListNode.h>

namespace DB
{

/** Sort node represents sort description for expression that is part of ORDER BY in query tree.
  * Example: SELECT * FROM test_table ORDER BY sort_column_1, sort_column_2;
  * Sort node optionally contain collation, fill from, fill to, and fill step.
  */
class SortNode;
using SortNodePtr = std::shared_ptr<SortNode>;

enum class SortDirection
{
    ASCENDING = 0,
    DESCENDING = 1
};

const char * toString(SortDirection sort_direction);

class SortNode final : public IQueryTreeNode
{
public:
    /// Initialize sort node with sort expression
    explicit SortNode(QueryTreeNodePtr expression_,
        SortDirection sort_direction_ = SortDirection::ASCENDING,
        std::optional<SortDirection> nulls_sort_direction_ = {},
        std::shared_ptr<Collator> collator_ = nullptr,
        bool with_fill = false);

    /// Get sort expression
    const QueryTreeNodePtr & getExpression() const
    {
        return children[sort_expression_child_index];
    }

    /// Get sort expression
    QueryTreeNodePtr & getExpression()
    {
        return children[sort_expression_child_index];
    }

    /// Returns true if sort node has with fill, false otherwise
    bool withFill() const
    {
        return with_fill;
    }

    /// Returns true if sort node has fill from, false otherwise
    bool hasFillFrom() const
    {
        return children[fill_from_child_index] != nullptr;
    }

    /// Get fill from
    const QueryTreeNodePtr & getFillFrom() const
    {
        return children[fill_from_child_index];
    }

    /// Get fill from
    QueryTreeNodePtr & getFillFrom()
    {
        return children[fill_from_child_index];
    }

    /// Returns true if sort node has fill to, false otherwise
    bool hasFillTo() const
    {
        return children[fill_to_child_index] != nullptr;
    }

    /// Get fill to
    const QueryTreeNodePtr & getFillTo() const
    {
        return children[fill_to_child_index];
    }

    /// Get fill to
    QueryTreeNodePtr & getFillTo()
    {
        return children[fill_to_child_index];
    }

    /// Returns true if sort node has fill step, false otherwise
    bool hasFillStep() const
    {
        return children[fill_step_child_index] != nullptr;
    }

    /// Get fill step
    const QueryTreeNodePtr & getFillStep() const
    {
        return children[fill_step_child_index];
    }

    /// Get fill step
    QueryTreeNodePtr & getFillStep()
    {
        return children[fill_step_child_index];
    }

    /// Get collator
    const std::shared_ptr<Collator> & getCollator() const
    {
        return collator;
    }

    /// Get sort direction
    SortDirection getSortDirection() const
    {
        return sort_direction;
    }

    /// Get nulls sort direction
    std::optional<SortDirection> getNullsSortDirection() const
    {
        return nulls_sort_direction;
    }

    QueryTreeNodeType getNodeType() const override
    {
        return QueryTreeNodeType::SORT;
    }

    void dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const override;

protected:
    bool isEqualImpl(const IQueryTreeNode & rhs) const override;

    void updateTreeHashImpl(HashState & hash_state) const override;

    QueryTreeNodePtr cloneImpl() const override;

    ASTPtr toASTImpl(const ConvertToASTOptions & options) const override;

private:
    static constexpr size_t sort_expression_child_index = 0;
    static constexpr size_t fill_from_child_index = 1;
    static constexpr size_t fill_to_child_index = 2;
    static constexpr size_t fill_step_child_index = 3;
    static constexpr size_t children_size = fill_step_child_index + 1;

    SortDirection sort_direction = SortDirection::ASCENDING;
    std::optional<SortDirection> nulls_sort_direction;
    std::shared_ptr<Collator> collator;
    bool with_fill = false;
};

}