| 134 | } |
| 135 | |
| 136 | ExecutePlanElement ProjectionMatchContext::buildExecutePlanElement(PartGroup part_group, const SelectQueryInfo & query_info) const |
| 137 | { |
| 138 | std::shared_ptr<FilterStep> rewritten_filter_step = nullptr; |
| 139 | std::shared_ptr<ProjectionStep> rewritten_projection_step = nullptr; |
| 140 | auto require_columns = requiredColumns(); |
| 141 | |
| 142 | NamesAndTypes names_and_types; |
| 143 | for (const auto & column: require_columns) |
| 144 | names_and_types.emplace_back(column, column_types.at(column)); |
| 145 | |
| 146 | DataStream input_stream {.header = Block{names_and_types}}; |
| 147 | |
| 148 | if (rewritten_filter) |
| 149 | rewritten_filter_step = std::make_shared<FilterStep>(input_stream, rewritten_filter); |
| 150 | |
| 151 | rewritten_projection_step = std::make_shared<ProjectionStep>(rewritten_filter ? rewritten_filter_step->getOutputStream(): input_stream, |
| 152 | rewritten_assignments, rewritten_types); |
| 153 | |
| 154 | ActionsDAGPtr prewhere_actions; |
| 155 | |
| 156 | if (query_info.prewhere_info) |
| 157 | { |
| 158 | auto & prewhere_info = query_info.prewhere_info; |
| 159 | prewhere_actions = prewhere_info->prewhere_actions->clone(); |
| 160 | prewhere_actions->foldActionsByProjection( |
| 161 | required_column_set, projection_desc.sample_block_for_keys, prewhere_info->prewhere_column_name, false); |
| 162 | |
| 163 | // This is hacky. Since `query_info.query` is not the original query, `prewhere_actions` may prune off |
| 164 | // columns mistakenly. Here, we add used required columns back. |
| 165 | // For example. projection desc: SELECT toDate(at), count() GROUP BY toDate(at) |
| 166 | // query: SELECT toDate(at), count() PREWHERE toDate(at) = '2022-01-01' GROUP BY toDate(at) |
| 167 | auto & outputs = prewhere_actions->getOutputs(); |
| 168 | for (const auto & input: prewhere_actions->getInputs()) |
| 169 | if (std::find(outputs.begin(), outputs.end(), input) == outputs.end()) |
| 170 | outputs.emplace_back(input); |
| 171 | } |
| 172 | |
| 173 | return ExecutePlanElement { |
| 174 | std::move(part_group), |
| 175 | read_analysis, |
| 176 | &projection_desc, |
| 177 | std::move(require_columns), |
| 178 | rewritten_projection_step, |
| 179 | rewritten_filter_step, |
| 180 | prewhere_actions |
| 181 | }; |
| 182 | } |
| 183 | |
| 184 | struct PartSchemaKey |
| 185 | { |
no test coverage detected