```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)
```
{
    RewriteCountVariantsVisitor::visit(query);
}
void TreeOptimizer::apply(ASTPtr & query, TreeRewriterResult & result,
                          const std::vector<TableWithColumnNamesAndTypes> & tables_with_columns, ContextPtr context)
{
    const auto & settings = context->getSettingsRef();
    auto * select_query = query->as<ASTSelectQuery>();
    if (!select_query)
        throw Exception("Select analyze for not select asts.", ErrorCodes::LOGICAL_ERROR);
    if (settings.optimize_functions_to_subcolumns && result.storage_snapshot && result.storage->supportsSubcolumns())
        optimizeFunctionsToSubcolumns(query, result.storage_snapshot->metadata);
    
    if (settings.optimize_arithmetic_operations_in_aggregate_functions)
        optimizeAggregationFunctions(query);
    bool converted_to_cnf = false;
    if (settings.convert_query_to_cnf)
        converted_to_cnf = convertQueryToCNF(select_query);
    if (converted_to_cnf && settings.optimize_using_constraints && result.storage_snapshot)
    {
        optimizeWithConstraints(select_query, result.aliases, result.source_columns_set,
            tables_with_columns, result.storage_snapshot->metadata, settings.optimize_append_index);
        if (settings.optimize_substitute_columns)
            optimizeSubstituteColumn(select_query, result.aliases, result.source_columns_set,
                tables_with_columns, result.storage_snapshot->metadata, result.storage);
    }