```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)
```
{
    if (!select.select() || select.select()->children.empty())
        return;
    const ASTSelectQuery * subselect = getSimpleSubselect(select);
    if (!subselect)
        return;
    std::unordered_set<String> distinct_names = getDistinctNames(*subselect);
    std::unordered_set<String> selected_names;
    
    for (const auto & id : select.select()->children)
    {
        const auto * identifier = id->as<ASTIdentifier>();
        if (!identifier)
            return;
        String name = identifier->shortName();
        if (!distinct_names.contains(name))
            return; 
        selected_names.insert(name);
    }
    
    
    if (selected_names.size() != distinct_names.size())
        return;
    select.distinct = false;
}
void optimizeMonotonousFunctionsInOrderBy(ASTSelectQuery * select_query, ContextPtr context,