Repository: mindsdb/mindsdb

Issue:
[Bug]: Mysql API - Forecasting query to timeseries predictor works only if HORIZON > 1
### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current Behavior

Forecasting query execution returns:
```
mysql> SELECT * from MYSQL_LOCAL.test_data.covid AS ta JOIN mindsdb.covid_predictor AS tb WHERE ta.state = "CA" AND ta.time > LATEST;
ERROR 1149 (HY000): 'int' object is not subscriptable
``` if predictor has been trained with HORIZON > 1 (2, 3, 4, ...)

### Expected Behavior

Forecasting work must not depend on HORIZON predictor parameter

### Steps To Reproduce

```markdown
1. Launch local mysql server, setup connection and create new database `test_data`
2. Create table covid `create table covid;`
3. Alter table:

alter table covid add column time date;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table covid add column adult_icu_bed_utilization float;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table covid add column inpatient_beds_utilization float;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table covid add column percent_of_inpatients_with_covid float;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table covid add column inpatient_bed_covid_utilization float;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table covid add column new_case int;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table covid add column pnew_case float;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0
```
4. copy covid data.csv file from benchmark datasets to `/var/lib/mysql-files/`
5. mysql> LOAD DATA INFILE '/var/lib/mysql-files/covid_data.csv' INTO TABLE covid  COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES;
Query OK, 7261 rows affected (0.19 sec)
Records: 7261  Deleted: 0  Skipped: 0  Warnings: 0

6. launch mindsdb and connect to mysql api server
7. create integration to your local database via mindsdb GUI
8. create new ts preodictor:
```
create predictor covid from MYSQL_LOCAL (select * from test_data.covid) as covid_dataset predict pnew_case ORDER BY time GROUP BY state  WINDOW 10 HORIZON 1;
```
9. perform forecast query: 
```
mysql> SELECT * from MYSQL_LOCAL.test_data.covid AS ta JOIN mindsdb.covid AS tb WHERE ta.state = "CA" AND ta.time > LATEST;
ERROR 1149 (HY000): 'int' object is not subscriptable
```
Note that you see `ERROR 1149 (HY000): 'int' object is not subscriptable` error
```


### Anything else?

_No response_


Discussion:
seems like its not even passing HORIZON ->lightwood(nr_predictions) how does mindsdb handle the horizon param
I can still reproduce it. Reopened

Resolving patch:
diff --git a/mindsdb/api/mysql/mysql_proxy/classes/sql_query.py b/mindsdb/api/mysql/mysql_proxy/classes/sql_query.py
--- a/mindsdb/api/mysql/mysql_proxy/classes/sql_query.py
+++ b/mindsdb/api/mysql/mysql_proxy/classes/sql_query.py
@@ -530,9 +530,9 @@ def _parse_query(self, sql):
 
                         window_size = predictor_metadata[predictor]['window']
                         nr_predictions = predictor_metadata[predictor]['nr_predictions']
-                        if len(data) > (window_size + nr_predictions):
+                        if len(data) >= (window_size + nr_predictions):
                             data = data[window_size:]
-                            if len(data) > nr_predictions:
+                            if len(data) > nr_predictions and nr_predictions > 1:
                                 data = data[:-nr_predictions+1]
                 data = [{(key, key): value for key, value in row.items()} for row in data]
 
diff --git a/mindsdb/api/mysql/mysql_proxy/datahub/datanodes/mindsdb_datanode.py b/mindsdb/api/mysql/mysql_proxy/datahub/datanodes/mindsdb_datanode.py
--- a/mindsdb/api/mysql/mysql_proxy/datahub/datanodes/mindsdb_datanode.py
+++ b/mindsdb/api/mysql/mysql_proxy/datahub/datanodes/mindsdb_datanode.py
@@ -329,19 +329,18 @@ def select(self, table, columns=None, where=None, where_data=None, order_by=None
                         date_values = [date_values]
 
                 for i in range(len(rows) - 1):
-                    rows[i][predict] = rows[i][predict][0]
-                    rows[i][order_by_column] = rows[i][order_by_column][0]
+                    if nr_predictions > 1:
+                        rows[i][predict] = rows[i][predict][0]
+                        rows[i][order_by_column] = rows[i][order_by_column][0]
                     for col in ('predicted_value', 'confidence', 'confidence_lower_bound', 'confidence_upper_bound'):
-                        explanations[i][predict][col] = explanations[i][predict][col][0]
+                        if nr_predictions > 1:
+                            explanations[i][predict][col] = explanations[i][predict][col][0]
 
                 last_row = rows.pop()
                 last_explanation = explanations.pop()
                 for i in range(nr_predictions):
                     new_row = copy.deepcopy(last_row)
-                    if nr_predictions == 1:
-                        new_row[predict] = new_row[predict]
-                        new_row[order_by_column] = new_row[order_by_column]
-                    else:
+                    if nr_predictions > 1:
                         new_row[predict] = new_row[predict][i]
                         new_row[order_by_column] = new_row[order_by_column][i]
                     if '__mindsdb_row_id' in new_row and (i > 0 or __mdb_make_predictions is False):
@@ -350,9 +349,7 @@ def select(self, table, columns=None, where=None, where_data=None, order_by=None
 
                     new_explanation = copy.deepcopy(last_explanation)
                     for col in ('predicted_value', 'confidence', 'confidence_lower_bound', 'confidence_upper_bound'):
-                        if nr_predictions == 1:
-                            new_explanation[predict][col] = new_explanation[predict][col]
-                        else:
+                        if nr_predictions > 1:
                             new_explanation[predict][col] = new_explanation[predict][col][i]
                     if i != 0:
                         new_explanation[predict]['anomaly'] = None


Test patch: