aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Analyzer/WindowNode.cpp
blob: 0fbe7c51bc7c2f2105ddf2aa9b2294c470ad5cdd (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
#include <Analyzer/WindowNode.h>

#include <Common/SipHash.h>

#include <IO/WriteBufferFromString.h>
#include <IO/Operators.h>

#include <Parsers/ASTWindowDefinition.h>

namespace DB
{

WindowNode::WindowNode(WindowFrame window_frame_)
    : IQueryTreeNode(children_size)
    , window_frame(std::move(window_frame_))
{
    children[partition_by_child_index] = std::make_shared<ListNode>();
    children[order_by_child_index] = std::make_shared<ListNode>();
}

void WindowNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const
{
    buffer << std::string(indent, ' ') << "WINDOW id: " << format_state.getNodeId(this);

    if (hasAlias())
        buffer << ", alias: " << getAlias();

    if (!parent_window_name.empty())
        buffer << ", parent_window_name: " << parent_window_name;

    buffer << ", frame_type: " << window_frame.type;

    auto window_frame_bound_type_to_string = [](WindowFrame::BoundaryType boundary_type, bool boundary_preceding)
    {
        std::string value;

        if (boundary_type == WindowFrame::BoundaryType::Unbounded)
            value = "unbounded";
        else if (boundary_type == WindowFrame::BoundaryType::Current)
            value = "current";
        else if (boundary_type == WindowFrame::BoundaryType::Offset)
            value = "offset";

        if (boundary_type != WindowFrame::BoundaryType::Current)
        {
            if (boundary_preceding)
                value += " preceding";
            else
                value += " following";
        }

        return value;
    };

    buffer << ", frame_begin_type: " << window_frame_bound_type_to_string(window_frame.begin_type, window_frame.begin_preceding);
    buffer << ", frame_end_type: " << window_frame_bound_type_to_string(window_frame.end_type, window_frame.end_preceding);

    if (hasPartitionBy())
    {
        buffer << '\n' << std::string(indent + 2, ' ') << "PARTITION BY\n";
        getPartitionBy().dumpTreeImpl(buffer, format_state, indent + 4);
    }

    if (hasOrderBy())
    {
        buffer << '\n' << std::string(indent + 2, ' ') << "ORDER BY\n";
        getOrderBy().dumpTreeImpl(buffer, format_state, indent + 4);
    }

    if (hasFrameBeginOffset())
    {
        buffer << '\n' << std::string(indent + 2, ' ') << "FRAME BEGIN OFFSET\n";
        getFrameBeginOffsetNode()->dumpTreeImpl(buffer, format_state, indent + 4);
    }

    if (hasFrameEndOffset())
    {
        buffer << '\n' << std::string(indent + 2, ' ') << "FRAME END OFFSET\n";
        getFrameEndOffsetNode()->dumpTreeImpl(buffer, format_state, indent + 4);
    }
}

bool WindowNode::isEqualImpl(const IQueryTreeNode & rhs) const
{
    const auto & rhs_typed = assert_cast<const WindowNode &>(rhs);

    return window_frame == rhs_typed.window_frame && parent_window_name == rhs_typed.parent_window_name;
}

void WindowNode::updateTreeHashImpl(HashState & hash_state) const
{
    hash_state.update(window_frame.is_default);
    hash_state.update(window_frame.type);
    hash_state.update(window_frame.begin_type);
    hash_state.update(window_frame.begin_preceding);
    hash_state.update(window_frame.end_type);
    hash_state.update(window_frame.end_preceding);

    hash_state.update(parent_window_name);
}

QueryTreeNodePtr WindowNode::cloneImpl() const
{
    auto window_node = std::make_shared<WindowNode>(window_frame);
    window_node->parent_window_name = parent_window_name;

    return window_node;
}

ASTPtr WindowNode::toASTImpl(const ConvertToASTOptions & options) const
{
    auto window_definition = std::make_shared<ASTWindowDefinition>();

    window_definition->parent_window_name = parent_window_name;

    if (hasPartitionBy())
    {
        window_definition->children.push_back(getPartitionByNode()->toAST(options));
        window_definition->partition_by = window_definition->children.back();
    }

    if (hasOrderBy())
    {
        window_definition->children.push_back(getOrderByNode()->toAST(options));
        window_definition->order_by = window_definition->children.back();
    }

    window_definition->frame_is_default = window_frame.is_default;
    window_definition->frame_type = window_frame.type;
    window_definition->frame_begin_type = window_frame.begin_type;
    window_definition->frame_begin_preceding = window_frame.begin_preceding;

    if (hasFrameBeginOffset())
    {
        window_definition->children.push_back(getFrameBeginOffsetNode()->toAST(options));
        window_definition->frame_begin_offset = window_definition->children.back();
    }

    window_definition->frame_end_type = window_frame.end_type;
    window_definition->frame_end_preceding = window_frame.end_preceding;
    if (hasFrameEndOffset())
    {
        window_definition->children.push_back(getFrameEndOffsetNode()->toAST(options));
        window_definition->frame_end_offset = window_definition->children.back();
    }

    return window_definition;
}

}