| 1443 | } |
| 1444 | |
| 1445 | bool SelectQueryExpressionAnalyzer::appendGroupBy(ExpressionActionsChain & chain, bool only_types, bool optimize_aggregation_in_order, |
| 1446 | ManyExpressionActions & group_by_elements_actions) |
| 1447 | { |
| 1448 | const auto * select_query = getAggregatingQuery(); |
| 1449 | |
| 1450 | if (!select_query->groupBy()) |
| 1451 | return false; |
| 1452 | |
| 1453 | ExpressionActionsChainSteps::Step & step = chain.lastStep(columns_after_join); |
| 1454 | |
| 1455 | ASTs asts = select_query->groupBy()->children; |
| 1456 | NameSet group_by_keys; |
| 1457 | if (select_query->group_by_with_grouping_sets) |
| 1458 | { |
| 1459 | for (const auto & ast : asts) |
| 1460 | { |
| 1461 | for (const auto & ast_element : ast->children) |
| 1462 | { |
| 1463 | step.addRequiredOutput(ast_element->getColumnName()); |
| 1464 | group_by_keys.insert(ast_element->getColumnName()); |
| 1465 | getRootActions(ast_element, only_types, step.actions()->dag); |
| 1466 | } |
| 1467 | } |
| 1468 | } |
| 1469 | else |
| 1470 | { |
| 1471 | for (const auto & ast : asts) |
| 1472 | { |
| 1473 | step.addRequiredOutput(ast->getColumnName()); |
| 1474 | group_by_keys.insert(ast->getColumnName()); |
| 1475 | getRootActions(ast, only_types, step.actions()->dag); |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | for (const auto & result_column : step.getResultColumns()) |
| 1480 | { |
| 1481 | if (group_by_keys.contains(result_column.name)) |
| 1482 | validateGroupByKeyType(result_column.type); |
| 1483 | } |
| 1484 | |
| 1485 | if (optimize_aggregation_in_order) |
| 1486 | { |
| 1487 | for (auto & child : asts) |
| 1488 | { |
| 1489 | ActionsDAG actions_dag(columns_after_join); |
| 1490 | getRootActions(child, only_types, actions_dag); |
| 1491 | group_by_elements_actions.emplace_back( |
| 1492 | std::make_shared<ExpressionActions>(std::move(actions_dag), ExpressionActionsSettings(getContext(), CompileExpressions::yes))); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | return true; |
| 1497 | } |
| 1498 | |
| 1499 | void SelectQueryExpressionAnalyzer::validateGroupByKeyType(const DB::DataTypePtr & key_type) const |
| 1500 | { |
no test coverage detected