| 1007 | |
| 1008 | |
| 1009 | static std::shared_ptr<IJoin> tryCreateJoin( |
| 1010 | JoinAlgorithm algorithm, |
| 1011 | std::shared_ptr<TableJoin> analyzed_join, |
| 1012 | const ColumnsWithTypeAndName & left_sample_columns, |
| 1013 | SharedHeader right_sample_block, |
| 1014 | std::unique_ptr<QueryPlan> & joined_plan, |
| 1015 | ContextPtr context) |
| 1016 | { |
| 1017 | if (analyzed_join->kind() == JoinKind::Paste) |
| 1018 | return std::make_shared<PasteJoin>(analyzed_join, right_sample_block); |
| 1019 | |
| 1020 | if (algorithm == JoinAlgorithm::DIRECT || algorithm == JoinAlgorithm::DEFAULT) |
| 1021 | { |
| 1022 | JoinPtr direct_join = tryKeyValueJoin(analyzed_join, *right_sample_block); |
| 1023 | if (direct_join) |
| 1024 | { |
| 1025 | /// Do not need to execute plan for right part, it's ready. |
| 1026 | joined_plan.reset(); |
| 1027 | return direct_join; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | if (algorithm == JoinAlgorithm::PARTIAL_MERGE || |
| 1032 | algorithm == JoinAlgorithm::PREFER_PARTIAL_MERGE) |
| 1033 | { |
| 1034 | if (MergeJoin::isSupported(analyzed_join)) |
| 1035 | return std::make_shared<MergeJoin>(analyzed_join, right_sample_block); |
| 1036 | } |
| 1037 | |
| 1038 | if (algorithm == JoinAlgorithm::HASH || |
| 1039 | /// partial_merge is preferred, but can't be used for specified kind of join, fallback to hash |
| 1040 | algorithm == JoinAlgorithm::PREFER_PARTIAL_MERGE || |
| 1041 | algorithm == JoinAlgorithm::PARALLEL_HASH || |
| 1042 | algorithm == JoinAlgorithm::DEFAULT) |
| 1043 | { |
| 1044 | const auto & settings = context->getSettingsRef(); |
| 1045 | |
| 1046 | if (analyzed_join->maxBytesBeforeExternalJoin() > 0 && context->getTempDataOnDisk() |
| 1047 | && GraceHashJoin::isSupported(analyzed_join)) |
| 1048 | { |
| 1049 | Block left_sample_block(left_sample_columns); |
| 1050 | if (sanitizeBlock(left_sample_block, false)) |
| 1051 | { |
| 1052 | if (analyzed_join->allowParallelHashJoin()) |
| 1053 | return std::make_shared<SpillingHashJoin>( |
| 1054 | analyzed_join, |
| 1055 | std::make_shared<const Block>(std::move(left_sample_block)), |
| 1056 | right_sample_block, |
| 1057 | context->getTempDataOnDisk(), |
| 1058 | settings[Setting::grace_hash_join_initial_buckets], |
| 1059 | settings[Setting::grace_hash_join_max_buckets], |
| 1060 | settings[Setting::max_threads], |
| 1061 | StatsCollectingParams{}); |
| 1062 | else |
| 1063 | return std::make_shared<SpillingHashJoin>( |
| 1064 | analyzed_join, |
| 1065 | std::make_shared<const Block>(std::move(left_sample_block)), |
| 1066 | right_sample_block, |
no test coverage detected