aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/ComparisonTupleEliminationVisitor.cpp
blob: 4f06f345b96bdcb13159b5bc6436407c3e8afec3 (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
#include <Interpreters/ComparisonTupleEliminationVisitor.h>
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTLiteral.h>

namespace DB
{

namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
}

namespace
{

ASTs splitTuple(const ASTPtr & node)
{
    if (const auto * func = node->as<ASTFunction>(); func && func->name == "tuple")
        return func->arguments->children;

    if (const auto * literal = node->as<ASTLiteral>(); literal && literal->value.getType() == Field::Types::Tuple)
    {
        ASTs result;
        const auto & tuple = literal->value.get<const Tuple &>();
        for (const auto & child : tuple)
            result.emplace_back(std::make_shared<ASTLiteral>(child));
        return result;
    }

    return {};
}

ASTPtr concatWithAnd(const ASTs & nodes)
{
    if (nodes.empty())
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot concat empty list of nodes");

    if (nodes.size() == 1)
        return nodes[0];

    auto result = makeASTFunction("and");
    result->arguments->children = nodes;
    return result;
}

class SplitTupleComparsionExpressionMatcher
{
public:
    using Data = ComparisonTupleEliminationMatcher::Data;

    static bool needChildVisit(const ASTPtr &, const ASTPtr &) { return true; }
    static void visit(ASTPtr & ast, Data &)
    {
        auto * func = ast->as<ASTFunction>();
        if (!func || func->arguments->children.size() != 2)
            return;

        if (func->name != "equals" && func->name != "notEquals")
            return;

        auto lhs = splitTuple(func->arguments->children[0]);
        auto rhs = splitTuple(func->arguments->children[1]);
        if (lhs.size() != rhs.size() || lhs.empty())
            return;

        ASTs new_args;
        new_args.reserve(lhs.size());
        for (size_t i = 0; i < lhs.size(); ++i)
        {
            new_args.emplace_back(makeASTFunction("equals", lhs[i], rhs[i]));
        }

        if (func->name == "notEquals")
            ast = makeASTFunction("not", concatWithAnd(new_args));
        else
            ast = concatWithAnd(new_args);
    }
};

using SplitTupleComparsionExpressionVisitor = InDepthNodeVisitor<SplitTupleComparsionExpressionMatcher, true>;

}

bool ComparisonTupleEliminationMatcher::needChildVisit(ASTPtr &, const ASTPtr &)
{
    return true;
}

void ComparisonTupleEliminationMatcher::visit(ASTPtr & ast, Data & data)
{
    auto * select_ast = ast->as<ASTSelectQuery>();
    if (!select_ast || !select_ast->where())
        return;

    if (select_ast->where())
        SplitTupleComparsionExpressionVisitor(data).visit(select_ast->refWhere());
}

}