```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)
```
String key = toString(hash.first) + '_' + toString(hash.second);
            group_by_hashes.insert(key);
        }
    }
    auto sorting_key_columns = result.storage_snapshot ? result.storage_snapshot->metadata->getSortingKeyColumns() : Names{};
    bool is_sorting_key_prefix = true;
    for (size_t i = 0; i < order_by->children.size(); ++i)
    {
        auto * order_by_element = order_by->children[i]->as<ASTOrderByElement>();
        auto & ast_func = order_by_element->children[0];
        if (!ast_func->as<ASTFunction>())
            continue;
        if (i >= sorting_key_columns.size() || ast_func->getColumnName() != sorting_key_columns[i])
            is_sorting_key_prefix = false;
        
        
        if (is_sorting_key_prefix)
            continue;
        MonotonicityCheckVisitor::Data data{tables_with_columns, context, group_by_hashes};
        MonotonicityCheckVisitor(data).visit(ast_func);
        if (!data.isRejected())
        {
            ast_func = data.identifier->clone();
            ast_func->setAlias("");
            if (!data.monotonicity.is_positive)
                order_by_element->direction *= -1;
        }
    }
}
void optimizeRedundantFunctionsInOrderBy(const ASTSelectQuery * select_query, ContextPtr context)