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

#include <iterator>
#include <algorithm>

namespace DB
{

bool InsertQuerySettingsPushDownMatcher::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 InsertQuerySettingsPushDownMatcher::visit(ASTPtr & ast, Data & data)
{
    if (auto * select_query = ast->as<ASTSelectQuery>())
        visit(*select_query, ast, data);
}

void InsertQuerySettingsPushDownMatcher::visit(ASTSelectQuery & select_query, ASTPtr &, Data & data)
{
    ASTPtr select_settings_ast = select_query.settings();
    if (!select_settings_ast)
        return;

    auto & insert_settings_ast = data.insert_settings_ast;

    if (!insert_settings_ast)
    {
        insert_settings_ast = select_settings_ast->clone();
        return;
    }

    SettingsChanges & select_settings = select_settings_ast->as<ASTSetQuery &>().changes;
    SettingsChanges & insert_settings = insert_settings_ast->as<ASTSetQuery &>().changes;

    for (auto & setting : select_settings)
    {
        auto it = std::find_if(insert_settings.begin(), insert_settings.end(), [&](auto & select_setting)
        {
            return select_setting.name == setting.name;
        });
        if (it == insert_settings.end())
            insert_settings.push_back(setting);
        else
        {
            /// Do not overwrite setting that was passed for INSERT
            /// by settings that was passed for SELECT
        }
    }
}

}