ClickHouse

Форк
0
/
DistributedCreateLocalPlan.cpp 
88 строк · 3.4 Кб
1
#include <Processors/QueryPlan/DistributedCreateLocalPlan.h>
2

3
#include <Common/config_version.h>
4
#include <Common/checkStackSize.h>
5
#include <Core/ProtocolDefines.h>
6
#include <Interpreters/ActionsDAG.h>
7
#include <Interpreters/InterpreterSelectQuery.h>
8
#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
9
#include <Processors/QueryPlan/ExpressionStep.h>
10

11
namespace DB
12
{
13

14
namespace
15
{
16

17
void addConvertingActions(QueryPlan & plan, const Block & header, bool has_missing_objects)
18
{
19
    if (blocksHaveEqualStructure(plan.getCurrentDataStream().header, header))
20
        return;
21

22
    auto mode = has_missing_objects ? ActionsDAG::MatchColumnsMode::Position : ActionsDAG::MatchColumnsMode::Name;
23

24
    auto get_converting_dag = [mode](const Block & block_, const Block & header_)
25
    {
26
        /// Convert header structure to expected.
27
        /// Also we ignore constants from result and replace it with constants from header.
28
        /// It is needed for functions like `now64()` or `randConstant()` because their values may be different.
29
        return ActionsDAG::makeConvertingActions(
30
            block_.getColumnsWithTypeAndName(),
31
            header_.getColumnsWithTypeAndName(),
32
            mode,
33
            true);
34
    };
35

36
    auto convert_actions_dag = get_converting_dag(plan.getCurrentDataStream().header, header);
37
    auto converting = std::make_unique<ExpressionStep>(plan.getCurrentDataStream(), convert_actions_dag);
38
    plan.addStep(std::move(converting));
39
}
40

41
}
42

43
std::unique_ptr<QueryPlan> createLocalPlan(
44
    const ASTPtr & query_ast,
45
    const Block & header,
46
    ContextPtr context,
47
    QueryProcessingStage::Enum processed_stage,
48
    size_t shard_num,
49
    size_t shard_count,
50
    bool has_missing_objects)
51
{
52
    checkStackSize();
53

54
    auto query_plan = std::make_unique<QueryPlan>();
55
    auto new_context = Context::createCopy(context);
56

57
    /// Do not push down limit to local plan, as it will break `rows_before_limit_at_least` counter.
58
    if (processed_stage == QueryProcessingStage::WithMergeableStateAfterAggregationAndLimit)
59
        processed_stage = QueryProcessingStage::WithMergeableStateAfterAggregation;
60

61
    /// Do not apply AST optimizations, because query
62
    /// is already optimized and some optimizations
63
    /// can be applied only for non-distributed tables
64
    /// and we can produce query, inconsistent with remote plans.
65
    auto select_query_options = SelectQueryOptions(processed_stage)
66
        .setShardInfo(static_cast<UInt32>(shard_num), static_cast<UInt32>(shard_count))
67
        .ignoreASTOptimizations();
68

69
    if (context->getSettingsRef().allow_experimental_analyzer)
70
    {
71
        /// For Analyzer, identifier in GROUP BY/ORDER BY/LIMIT BY lists has been resolved to
72
        /// ConstantNode in QueryTree if it is an alias of a constant, so we should not replace
73
        /// ConstantNode with ProjectionNode again(https://github.com/ClickHouse/ClickHouse/issues/62289).
74
        new_context->setSetting("enable_positional_arguments", Field(false));
75
        auto interpreter = InterpreterSelectQueryAnalyzer(query_ast, new_context, select_query_options);
76
        query_plan = std::make_unique<QueryPlan>(std::move(interpreter).extractQueryPlan());
77
    }
78
    else
79
    {
80
        auto interpreter = InterpreterSelectQuery(query_ast, new_context, select_query_options);
81
        interpreter.buildQueryPlan(*query_plan);
82
    }
83

84
    addConvertingActions(*query_plan, header, has_missing_objects);
85
    return query_plan;
86
}
87

88
}
89

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.