

**How to reproduce**

```sql
CREATE TABLE t_local
(
    `d` DateTime,
    `v` UInt32
)
ENGINE = MergeTree
ORDER BY toStartOfHour(d);

CREATE TABLE t_dist AS t_local
ENGINE = Distributed(test_shard_localhost, currentDatabase(), t_local);

INSERT INTO t_local VALUES (now(), 1);
```

```sql

EXPLAIN PIPELINE
SELECT v
FROM t_dist
ORDER BY
    toStartOfHour(d) ASC,
    v ASC
SETTINGS optimize_monotonous_functions_in_order_by = 0

┌─explain────────────────────────────┐
│ (SettingQuotaAndLimits)            │
│   (Expression)                     │
│   ExpressionTransform              │
│     (Sorting)                      │
│  -> FinishSortingTransform <-      │
│       PartialSortingTransform      │
│         (Expression)               │
│         ExpressionTransform        │
│           (SettingQuotaAndLimits)  │
│             (ReadFromMergeTree)    │
│             MergeTreeInOrder 0 → 1 │
└────────────────────────────────────┘

EXPLAIN PIPELINE
SELECT v
FROM t_dist
ORDER BY
    toStartOfHour(d) ASC,
    v ASC
SETTINGS optimize_monotonous_functions_in_order_by = 1

┌─explain──────────────────────────────┐
│ (SettingQuotaAndLimits)              │
│   (Expression)                       │
│   ExpressionTransform                │
│     (Sorting)                        │
│  -> MergeSortingTransform   <-            │
│       LimitsCheckingTransform        │
│         PartialSortingTransform      │
│           (Expression)               │
│           ExpressionTransform        │
│             (SettingQuotaAndLimits)  │
│               (ReadFromMergeTree)    │
│               MergeTreeInOrder 0 → 1 │
└──────────────────────────────────────┘

EXPLAIN SYNTAX
SELECT v
FROM t_dist
ORDER BY
    toStartOfHour(d) ASC,
    v ASC
SETTINGS optimize_monotonous_functions_in_order_by = 1

Query id: 1add8abb-1f3c-4f50-82df-cadcf162358d

┌─explain────────────────────────────────────────────────┐
│ SELECT v                                               │
│ FROM t_dist                                            │
│ ORDER BY                                               │
│     d ASC,                                             │
│     v ASC                                              │
│ SETTINGS optimize_monotonous_functions_in_order_by = 1 │
└────────────────────────────────────────────────────────┘
```
It happens, because `optimize_monotonous_functions_in_order_by` eliminates monotonous function from `ORDER BY`, while it is used for matching the prefix of primary key and `optimize_read_in_order` doesn't work.
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)
{
    const auto & order_by = select_query->orderBy();
    if (!order_by)
        return;
    for (const auto & child : order_by->children)
    {
        auto * order_by_element = child->as<ASTOrderByElement>();
        if (!order_by_element || order_by_element->children.empty())