aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/RewriteArrayExistsFunctionVisitor.cpp
blob: 8d1a37647ad9ee4a4146a308b72879fe4f1997a5 (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
#include <Interpreters/RewriteArrayExistsFunctionVisitor.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTLiteral.h>

namespace DB
{
void RewriteArrayExistsFunctionMatcher::visit(ASTPtr & ast, Data & data)
{
    if (auto * func = ast->as<ASTFunction>())
    {
        if (func->is_window_function)
            return;

        visit(*func, ast, data);
    }
}

void RewriteArrayExistsFunctionMatcher::visit(const ASTFunction & func, ASTPtr & ast, Data &)
{
    if (func.name != "arrayExists" || !func.arguments)
        return;

    auto & array_exists_arguments = func.arguments->children;
    if (array_exists_arguments.size() != 2)
        return;

    /// lambda function must be like: x -> x = elem
    const auto * lambda_func = array_exists_arguments[0]->as<ASTFunction>();
    if (!lambda_func || !lambda_func->is_lambda_function)
        return;

    const auto & lambda_func_arguments = lambda_func->arguments->children;
    if (lambda_func_arguments.size() != 2)
        return;

    const auto * tuple_func = lambda_func_arguments[0]->as<ASTFunction>();
    if (!tuple_func || tuple_func->name != "tuple")
        return;

    const auto & tuple_arguments = tuple_func->arguments->children;
    if (tuple_arguments.size() != 1)
        return;

    const auto * id = tuple_arguments[0]->as<ASTIdentifier>();
    if (!id)
        return;

    const auto * filter_func = lambda_func_arguments[1]->as<ASTFunction>();
    if (!filter_func || filter_func->name != "equals")
        return;

    auto & filter_arguments = filter_func->arguments->children;
    if (filter_arguments.size() != 2)
        return;

    const ASTIdentifier * filter_id = nullptr;
    if ((filter_id = filter_arguments[0]->as<ASTIdentifier>()) && filter_arguments[1]->as<ASTLiteral>()
        && filter_id->full_name == id->full_name)
    {
        /// arrayExists(x -> x = elem, arr) -> has(arr, elem)
        auto new_func = makeASTFunction("has", std::move(array_exists_arguments[1]), std::move(filter_arguments[1]));
        new_func->setAlias(func.alias);
        ast = std::move(new_func);
        return;
    }
    else if (
        (filter_id = filter_arguments[1]->as<ASTIdentifier>()) && filter_arguments[0]->as<ASTLiteral>()
        && filter_id->full_name == id->full_name)
    {
        /// arrayExists(x -> elem = x, arr) -> has(arr, elem)
        auto new_func = makeASTFunction("has", std::move(array_exists_arguments[1]), std::move(filter_arguments[0]));
        new_func->setAlias(func.alias);
        ast = std::move(new_func);
        return;
    }
}

}