| 93 | } |
| 94 | |
| 95 | void computeGroupingFunctions( |
| 96 | QueryPipeline & pipeline, |
| 97 | const GroupingDescriptions & groupings, |
| 98 | const Names & keys, |
| 99 | const GroupingSetsParamsList & grouping_set_params, |
| 100 | const BuildQueryPipelineSettings & build_settings) |
| 101 | { |
| 102 | if (groupings.empty()) |
| 103 | return; |
| 104 | |
| 105 | const bool ansi_mode = build_settings.context->getSettingsRef().dialect_type != DialectType::CLICKHOUSE; |
| 106 | bool force_grouping_standard_compatibility = build_settings.context->getSettingsRef().force_grouping_standard_compatibility; |
| 107 | if (ansi_mode) |
| 108 | force_grouping_standard_compatibility = true; |
| 109 | |
| 110 | auto actions = std::make_shared<ActionsDAG>(pipeline.getHeader().getColumnsWithTypeAndName()); |
| 111 | ActionsDAG::NodeRawConstPtrs outputs = actions->getOutputs(); |
| 112 | ActionsDAG::NodeRawConstPtrs children; |
| 113 | |
| 114 | if (!grouping_set_params.empty()) |
| 115 | children.push_back(&actions->findInOutputs("__grouping_set")); |
| 116 | |
| 117 | auto get_key_index = [&](const String & key) { return std::find(keys.begin(), keys.end(), key) - keys.begin(); }; |
| 118 | |
| 119 | ColumnNumbersList grouping_sets_indices; |
| 120 | for (const auto & param : grouping_set_params) |
| 121 | { |
| 122 | ColumnNumbers indices; |
| 123 | |
| 124 | if (!param.used_keys.empty() || !param.missing_keys.empty()) |
| 125 | throw Exception(ErrorCodes::LOGICAL_ERROR, "Unable to calculate grouping functions by prepared grouping set param"); |
| 126 | |
| 127 | for (const auto & key_name : param.used_key_names) |
| 128 | indices.push_back(get_key_index(key_name)); |
| 129 | |
| 130 | grouping_sets_indices.emplace_back(std::move(indices)); |
| 131 | } |
| 132 | |
| 133 | for (const auto & grouping : groupings) |
| 134 | { |
| 135 | ColumnNumbers arguments_indexes; |
| 136 | |
| 137 | for (const auto & arg : grouping.argument_names) |
| 138 | arguments_indexes.push_back(get_key_index(arg)); |
| 139 | |
| 140 | if (!grouping_set_params.empty()) // GROUPING SETS, ROLLUP, CUBE |
| 141 | outputs.push_back(&actions->addFunction( |
| 142 | std::make_shared<FunctionToOverloadResolverAdaptor>( |
| 143 | std::make_shared<FunctionGroupingForGroupingSets>(std::move(arguments_indexes), grouping_sets_indices, force_grouping_standard_compatibility)), |
| 144 | children, |
| 145 | grouping.output_name)); |
| 146 | else // ORDINARY |
| 147 | outputs.push_back(&actions->addFunction( |
| 148 | std::make_shared<FunctionToOverloadResolverAdaptor>( |
| 149 | std::make_shared<FunctionGroupingOrdinary>(std::move(arguments_indexes), force_grouping_standard_compatibility)), |
| 150 | children, |
| 151 | grouping.output_name)); |
| 152 | } |
no test coverage detected