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

#include <IO/WriteBuffer.h>
#include <IO/WriteHelpers.h>
#include <IO/Operators.h>

#include <Parsers/ASTTablesInSelectQuery.h>
#include <Parsers/ASTExpressionList.h>

#include <Analyzer/Utils.h>
#include <Analyzer/ColumnNode.h>

namespace DB
{

ArrayJoinNode::ArrayJoinNode(QueryTreeNodePtr table_expression_, QueryTreeNodePtr join_expressions_, bool is_left_)
    : IQueryTreeNode(children_size)
    , is_left(is_left_)
{
    children[table_expression_child_index] = std::move(table_expression_);
    children[join_expressions_child_index] = std::move(join_expressions_);
}

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

    buffer << '\n' << std::string(indent + 2, ' ') << "TABLE EXPRESSION\n";
    getTableExpression()->dumpTreeImpl(buffer, format_state, indent + 4);

    buffer << '\n' << std::string(indent + 2, ' ') << "JOIN EXPRESSIONS\n";
    getJoinExpressionsNode()->dumpTreeImpl(buffer, format_state, indent + 4);
}

bool ArrayJoinNode::isEqualImpl(const IQueryTreeNode & rhs) const
{
    const auto & rhs_typed = assert_cast<const ArrayJoinNode &>(rhs);
    return is_left == rhs_typed.is_left;
}

void ArrayJoinNode::updateTreeHashImpl(HashState & state) const
{
    state.update(is_left);
}

QueryTreeNodePtr ArrayJoinNode::cloneImpl() const
{
    return std::make_shared<ArrayJoinNode>(getTableExpression(), getJoinExpressionsNode(), is_left);
}

ASTPtr ArrayJoinNode::toASTImpl(const ConvertToASTOptions & options) const
{
    auto array_join_ast = std::make_shared<ASTArrayJoin>();
    array_join_ast->kind = is_left ? ASTArrayJoin::Kind::Left : ASTArrayJoin::Kind::Inner;

    auto array_join_expressions_ast = std::make_shared<ASTExpressionList>();
    const auto & array_join_expressions = getJoinExpressions().getNodes();

    for (const auto & array_join_expression : array_join_expressions)
    {
        ASTPtr array_join_expression_ast;

        auto * column_node = array_join_expression->as<ColumnNode>();
        if (column_node && column_node->getExpression())
            array_join_expression_ast = column_node->getExpression()->toAST(options);
        else
            array_join_expression_ast = array_join_expression->toAST(options);

        array_join_expression_ast->setAlias(array_join_expression->getAlias());
        array_join_expressions_ast->children.push_back(std::move(array_join_expression_ast));
    }

    array_join_ast->children.push_back(std::move(array_join_expressions_ast));
    array_join_ast->expression_list = array_join_ast->children.back();

    ASTPtr tables_in_select_query_ast = std::make_shared<ASTTablesInSelectQuery>();
    addTableExpressionOrJoinIntoTablesInSelectQuery(tables_in_select_query_ast, children[table_expression_child_index], options);

    auto array_join_query_element_ast = std::make_shared<ASTTablesInSelectQueryElement>();
    array_join_query_element_ast->children.push_back(std::move(array_join_ast));
    array_join_query_element_ast->array_join = array_join_query_element_ast->children.back();

    tables_in_select_query_ast->children.push_back(std::move(array_join_query_element_ast));

    return tables_in_select_query_ast;
}

}