

**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.
{
    
    OptimizeIfWithConstantConditionVisitor(aliases).visit(query);
    if (if_chain_to_multiif)
        OptimizeIfChainsVisitor().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
        && result.storage->supportsSubcolumns() && result.metadata_snapshot)
        optimizeFunctionsToSubcolumns(query, result.metadata_snapshot);
    optimizeIf(query, result.aliases, settings.optimize_if_chain_to_multiif);
    
    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)
    {
        optimizeWithConstraints(select_query, result.aliases, result.source_columns_set,