**How to reproduce**
* Working query
```sql
WITH arrayJoin(range(2)) AS delta
SELECT
    toDate(time) + toIntervalDay(delta) AS dt,
    version()
FROM 
(
    SELECT NOW() AS time
)

┌─────────dt─┬─version()──┐
│ 2020-11-01 │ 20.10.3.30 │
│ 2020-11-02 │ 20.10.3.30 │
└────────────┴────────────┘

2 rows in set. Elapsed: 0.006 sec. 

```

* After adding ORDER BY dt
```sql
WITH arrayJoin(range(2)) AS delta
SELECT
    toDate(time) + toIntervalDay(delta) AS dt,
    version()
FROM 
(
    SELECT NOW() AS time
)
ORDER BY dt ASC


Received exception from server (version 20.10.3):
Code: 43. DB::Exception: Received from localhost:9000. DB::Exception: Illegal type Date of argument of function range. 

0 rows in set. Elapsed: 0.022 sec. 
```
* Try to change range(2) to equivalent array [0,1]
```sql
WITH arrayJoin([0, 1]) AS delta
SELECT
    toDate(time) + toIntervalDay(delta) AS dt,
    version()
FROM 
(
    SELECT NOW() AS time
)
ORDER BY dt ASC


Received exception from server (version 20.10.3):
Code: 43. DB::Exception: Received from localhost:9000. DB::Exception: Argument for function arrayJoin must be Array.. 

0 rows in set. Elapsed: 0.004 sec. 
```

**Expected behavior**
Correctly ordered result set

**Additional context**
Both queries with errors correctly run on version 20.7.2.30 
```sql
WITH arrayJoin(range(2)) AS delta
SELECT 
    toDate(time) + toIntervalDay(delta) AS dt,
    version()
FROM 
(
    SELECT NOW() AS time
)
ORDER BY dt ASC

┌─────────dt─┬─version()─┐
│ 2020-11-01 │ 20.7.2.30 │
│ 2020-11-02 │ 20.7.2.30 │
└────────────┴───────────┘

2 rows in set. Elapsed: 0.003 sec. 
```
```sql
WITH arrayJoin([0, 1]) AS delta
SELECT 
    toDate(time) + toIntervalDay(delta) AS dt,
    version()
FROM 
(
    SELECT NOW() AS time
)
ORDER BY dt ASC

┌─────────dt─┬─version()─┐
│ 2020-11-01 │ 20.7.2.30 │
│ 2020-11-02 │ 20.7.2.30 │
└────────────┴───────────┘

2 rows in set. Elapsed: 0.001 sec. 
```


{
public:
    struct Data
    {
        const TablesWithColumns & tables;
        const Context & context;
        const std::unordered_set<String> & group_by_function_hashes;
        Monotonicity monotonicity{true, true, true};
        ASTIdentifier * identifier = nullptr;
        DataTypePtr arg_data_type = {};
        void reject() { monotonicity.is_monotonic = false; }
        bool isRejected() const { return !monotonicity.is_monotonic; }
        bool canOptimize(const ASTFunction & ast_function) const
        {
            
            auto hash = ast_function.getTreeHash();
            String key = toString(hash.first) + '_' + toString(hash.second);
            if (group_by_function_hashes.count(key))
                return false;
            
            if (AggregateFunctionFactory::instance().isAggregateFunctionName(ast_function.name))
                return false;
            return true;
        }
        bool extractIdentifierAndType(const ASTFunction & ast_function)