| 314 | } |
| 315 | |
| 316 | void SortingStep::scatterByPartitionIfNeeded(QueryPipelineBuilder& pipeline) |
| 317 | { |
| 318 | size_t threads = pipeline.getNumThreads(); |
| 319 | size_t streams = pipeline.getNumStreams(); |
| 320 | |
| 321 | if (!partition_by_description.empty() && threads > 1) |
| 322 | { |
| 323 | /// We are going to shuffle the data from streams to threads. This will create (threads * streams) connections in the pipeline. |
| 324 | /// Let's limit this by some sane value to avoid explosion. |
| 325 | const size_t connection_count_limit = 1000000; |
| 326 | if (threads * streams > connection_count_limit) |
| 327 | throw Exception(ErrorCodes::LIMIT_EXCEEDED, "Parallelism limit exceeded in SortingStep: {} threads X {} streams, limit {}, try to reduce `max_threads` value", |
| 328 | threads, streams, connection_count_limit); |
| 329 | |
| 330 | auto stream_header = pipeline.getSharedHeader(); |
| 331 | |
| 332 | ColumnNumbers key_columns; |
| 333 | key_columns.reserve(partition_by_description.size()); |
| 334 | for (auto & col : partition_by_description) |
| 335 | { |
| 336 | key_columns.push_back(stream_header->getPositionByName(col.column_name)); |
| 337 | } |
| 338 | |
| 339 | pipeline.transform([&](const OutputPortRawPtrs & ports) |
| 340 | { |
| 341 | Processors processors; |
| 342 | for (auto * port : ports) |
| 343 | { |
| 344 | auto scatter = std::make_shared<ScatterByPartitionTransform>(stream_header, threads, key_columns); |
| 345 | connect(*port, scatter->getInputs().front()); |
| 346 | processors.push_back(scatter); |
| 347 | } |
| 348 | return processors; |
| 349 | }); |
| 350 | |
| 351 | if (streams > 1) |
| 352 | { |
| 353 | pipeline.transform([&](const OutputPortRawPtrs & ports) |
| 354 | { |
| 355 | Processors processors; |
| 356 | for (size_t i = 0; i < threads; ++i) |
| 357 | { |
| 358 | size_t output_it = i; |
| 359 | auto resize = std::make_shared<ResizeProcessor>(stream_header, streams, 1); |
| 360 | auto & inputs = resize->getInputs(); |
| 361 | |
| 362 | for (auto input_it = inputs.begin(); input_it != inputs.end(); output_it += threads, ++input_it) |
| 363 | connect(*ports[output_it], *input_it); |
| 364 | processors.push_back(resize); |
| 365 | } |
| 366 | return processors; |
| 367 | }); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void SortingStep::finishSorting( |
| 373 | QueryPipelineBuilder & pipeline, const SortDescription & input_sort_desc, const SortDescription & result_sort_desc, const UInt64 limit_) |
nothing calls this directly
no test coverage detected