aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/QueryWithOutputSettingsPushDownVisitor.cpp
blob: 8cf0d0063aed9a54b5da4872d96341e102516018 (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
#include <Common/SettingsChanges.h>
#include <Parsers/QueryWithOutputSettingsPushDownVisitor.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/ASTSetQuery.h>
#include <Parsers/ASTSubquery.h>

#include <iterator>
#include <algorithm>

namespace DB
{

bool QueryWithOutputSettingsPushDownMatcher::needChildVisit(ASTPtr & node, const ASTPtr & child)
{
    if (node->as<ASTSelectWithUnionQuery>())
        return true;
    if (node->as<ASTSubquery>())
        return true;
    if (child->as<ASTSelectQuery>())
        return true;
    return false;
}

void QueryWithOutputSettingsPushDownMatcher::visit(ASTPtr & ast, Data & data)
{
    if (auto * select_query = ast->as<ASTSelectQuery>())
        visit(*select_query, ast, data);
}

void QueryWithOutputSettingsPushDownMatcher::visit(ASTSelectQuery & select_query, ASTPtr &, Data & data)
{
    ASTPtr select_settings_ast = select_query.settings();
    if (!select_settings_ast)
    {
        select_query.setExpression(ASTSelectQuery::Expression::SETTINGS, data.settings_ast->clone());
        return;
    }

    SettingsChanges & select_settings = select_settings_ast->as<ASTSetQuery &>().changes;
    SettingsChanges & settings = data.settings_ast->as<ASTSetQuery &>().changes;

    for (auto & setting : settings)
    {
        auto it = std::find_if(select_settings.begin(), select_settings.end(), [&](auto & select_setting)
        {
            return select_setting.name == setting.name;
        });
        if (it == select_settings.end())
            select_settings.push_back(setting);
        else
            it->value = setting.value;
    }
}

}