| 56 | } |
| 57 | |
| 58 | QueryPipelinePtr UnionStep::updatePipeline(QueryPipelines pipelines, const BuildQueryPipelineSettings & settings) |
| 59 | { |
| 60 | auto pipeline = std::make_unique<QueryPipeline>(); |
| 61 | QueryPipelineProcessorsCollector collector(*pipeline, this); |
| 62 | |
| 63 | if (pipelines.empty()) |
| 64 | { |
| 65 | pipeline->init(Pipe(std::make_shared<NullSource>(output_stream->header))); |
| 66 | processors = collector.detachProcessors(); |
| 67 | return pipeline; |
| 68 | } |
| 69 | |
| 70 | size_t index = 0; |
| 71 | for (auto & cur_pipeline : pipelines) |
| 72 | { |
| 73 | ASTPtr expr_list = std::make_shared<ASTExpressionList>(); |
| 74 | NamesWithAliases output_names; |
| 75 | bool need_rename = false; |
| 76 | for (const auto & item : output_stream->header) |
| 77 | { |
| 78 | auto rename_from = output_to_inputs.at(item.name).at(index); |
| 79 | output_names.emplace_back(rename_from, item.name); |
| 80 | ASTPtr identifier = std::make_shared<ASTIdentifier>(rename_from); |
| 81 | identifier->setAlias(item.name); |
| 82 | expr_list->children.emplace_back(identifier); |
| 83 | if (item.name != rename_from) |
| 84 | { |
| 85 | need_rename = true; |
| 86 | } |
| 87 | } |
| 88 | if (need_rename) |
| 89 | { |
| 90 | auto project_action |
| 91 | = createExpressionActions(settings.context, cur_pipeline->getHeader().getNamesAndTypesList(), output_names, expr_list); |
| 92 | auto expression = std::make_shared<ExpressionActions>(project_action, settings.getActionsSettings()); |
| 93 | cur_pipeline->addSimpleTransform( |
| 94 | [&](const Block & header_) { return std::make_shared<ExpressionTransform>(header_, expression); }); |
| 95 | |
| 96 | if (!blocksHaveEqualStructure(cur_pipeline->getHeader(), getOutputStream().header)) |
| 97 | { |
| 98 | auto actions_dag = ActionsDAG::makeConvertingActions( |
| 99 | cur_pipeline->getHeader().getColumnsWithTypeAndName(), |
| 100 | getOutputStream().header.getColumnsWithTypeAndName(), |
| 101 | ActionsDAG::MatchColumnsMode::Position); |
| 102 | auto converting_actions = std::make_shared<ExpressionActions>(std::move(actions_dag)); |
| 103 | cur_pipeline->addSimpleTransform( |
| 104 | [&](const Block & cur_header) { return std::make_shared<ExpressionTransform>(cur_header, converting_actions); }); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /// Headers for union must be equal. |
| 109 | /// But, just in case, convert it to the same header if not. |
| 110 | if (!isCompatibleHeader(cur_pipeline->getHeader(), getOutputStream().header)) |
| 111 | { |
| 112 | auto converting_dag = ActionsDAG::makeConvertingActions( |
| 113 | cur_pipeline->getHeader().getColumnsWithTypeAndName(), |
| 114 | getOutputStream().header.getColumnsWithTypeAndName(), |
| 115 | ActionsDAG::MatchColumnsMode::Name); |
nothing calls this directly
no test coverage detected