```sql
SELECT toStartOfHour(time) AS timex, id, count()
FROM
(
    SELECT
        concat('id', toString(number % 3)) AS id,
        toDateTime('2020-01-01') + (number * 60) AS time
    FROM numbers(100)
)
GROUP BY
    GROUPING SETS ( (timex, id), (timex))
ORDER BY timex ASC;

Received exception from server (version 22.5.1):
Code: 215. DB::Exception: Received from localhost:9000. 
DB::Exception: Column `time` is not under aggregate function and not in GROUP BY: 
While processing time ASC. (NOT_AN_AGGREGATE)
```
optimizeGroupBy(select_query, context);
    
    if (settings.optimize_group_by_function_keys)
        optimizeGroupByFunctionKeys(select_query);
    
    if (settings.optimize_move_functions_out_of_any)
        optimizeAnyFunctions(query);
    if (settings.optimize_normalize_count_variants)
        optimizeCountConstantAndSumOne(query);
    if (settings.optimize_rewrite_sum_if_to_count_if)
        optimizeSumIfFunctions(query);
    
    if (settings.optimize_injective_functions_inside_uniq)
        optimizeInjectiveFunctionsInsideUniq(query, context);
    
    if (settings.optimize_aggregators_of_group_by_keys
        && !select_query->group_by_with_totals
        && !select_query->group_by_with_rollup
        && !select_query->group_by_with_cube)
    {
        optimizeAggregateFunctionsOfGroupByKeys(select_query, query);
    }
    
    if (settings.optimize_duplicate_order_by_and_distinct)
    {
        optimizeDuplicateOrderBy(query, context);
        
        
        if (!settings.distributed_group_by_no_merge)
            optimizeDuplicateDistinct(*select_query);