Repository: mindsdb/mindsdb

Issue:
KeyError: 'typing' when creating new Datasource from MySQL
Using [airplane_satisfaction dataset](https://www.kaggle.com/teejmahal20/airline-passenger-satisfaction?select=train.csv) to create a data source from Scout through MySQL throws bellow error:

```
Traceback (most recent call last):
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask_restx/api.py", line 375, in wrapper
    resp = resource(*args, **kwargs)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask/views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask_restx/resource.py", line 44, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask_restx/marshalling.py", line 248, in wrapper
    resp = f(*args, **kwargs)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/mindsdb/api/http/namespaces/datasource.py", line 113, in put
    ca.default_store.save_datasource(name, source_type, request.json)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/mindsdb/interfaces/datastore/datastore.py", line 185, in save_datasource
    df_with_types = cast_df_columns_types(df, self.get_analysis(df)['data_analysis_v2'])
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/mindsdb/interfaces/datastore/sqlite_helpers.py", line 54, in cast_df_columns_types
    print(f'Error: cant convert type of DS column {name} to {new_type}')
UnboundLocalError: local variable 'new_type' referenced before assignment
ERROR:mindsdb.api.http.initialize:Exception on /api/datasources/AirplaneData [PUT]
Traceback (most recent call last):
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/mindsdb/interfaces/datastore/sqlite_helpers.py", line 44, in cast_df_columns_types
    col_type = stats[name]['typing']['data_type']
KeyError: 'typing'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask_restx/api.py", line 375, in wrapper
    resp = resource(*args, **kwargs)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask/views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask_restx/resource.py", line 44, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/flask_restx/marshalling.py", line 248, in wrapper
    resp = f(*args, **kwargs)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/mindsdb/api/http/namespaces/datasource.py", line 113, in put
    ca.default_store.save_datasource(name, source_type, request.json)
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/mindsdb/interfaces/datastore/datastore.py", line 185, in save_datasource
    df_with_types = cast_df_columns_types(df, self.get_analysis(df)['data_analysis_v2'])
  File "/home/zoran/MyProjects/mindsdb-examples/mdb/lib/python3.7/site-packages/mindsdb/interfaces/datastore/sqlite_helpers.py", line 54, in cast_df_columns_types
    print(f'Error: cant convert type of DS column {name} to {new_type}')
UnboundLocalError: local variable 'new_type' referenced before assignment

```


Discussion:
@ZoranPandovski That because ds have column without name (first column). That was fixed in [this pr](https://github.com/mindsdb/mindsdb/pull/854). Since that, it should be error 'Each column in datasource must have unique name'. But a this moment gui dont show error, [here is issue](https://github.com/mindsdb/mindsdb_gui_web/issues/265).
@StpMax I removed that one. There are no id or empty columns in my DB. Check the dump I've sent you.

Resolving patch:
diff --git a/mindsdb/interfaces/datastore/sqlite_helpers.py b/mindsdb/interfaces/datastore/sqlite_helpers.py
--- a/mindsdb/interfaces/datastore/sqlite_helpers.py
+++ b/mindsdb/interfaces/datastore/sqlite_helpers.py
@@ -2,11 +2,13 @@
 from mindsdb_native.libs.constants.mindsdb import DATA_TYPES, DATA_SUBTYPES
 import re
 
+
 def create_sqlite_db(path, data_frame):
     con = sqlite3.connect(path)
     data_frame.to_sql(name='data', con=con, index=False)
     con.close()
 
+
 def cast_df_columns_types(df, stats):
     types_map = {
         DATA_TYPES.NUMERIC: {
@@ -41,11 +43,14 @@ def cast_df_columns_types(df, stats):
     for column in columns:
         try:
             name = column['name']
-            col_type = stats[name]['typing']['data_type']
-            col_subtype = stats[name]['typing']['data_subtype']
-            new_type = types_map[col_type][col_subtype]
+            if stats[name].get('empty', {}).get('is_empty', False):
+                new_type = types_map[DATA_TYPES.NUMERIC][DATA_SUBTYPES.INT]
+            else:
+                col_type = stats[name]['typing']['data_type']
+                col_subtype = stats[name]['typing']['data_subtype']
+                new_type = types_map[col_type][col_subtype]
             if new_type == 'int64' or new_type == 'float64':
-                df[name] = df[name].apply(lambda x: x.replace(',','.') if isinstance(x, str) else x)
+                df[name] = df[name].apply(lambda x: x.replace(',', '.') if isinstance(x, str) else x)
             if new_type == 'int64':
                 df = df.astype({name: 'float64'})
             df = df.astype({name: new_type})
@@ -55,6 +60,7 @@ def cast_df_columns_types(df, stats):
 
     return df
 
+
 def parse_filter(key, value):
     result = re.search(r'filter(_*.*)\[(.*)\]', key)
     operator = result.groups()[0].strip('_') or 'like'
@@ -96,12 +102,14 @@ def prepare_sql_where(where):
         where = ''
     return where, marks
 
+
 def get_sqlite_columns_names(cursor):
     cursor.execute('pragma table_info(data);')
     column_name_index = [x[0] for x in cursor.description].index('name')
     columns = cursor.fetchall()
     return [x[column_name_index] for x in columns]
 
+
 def get_sqlite_data(db_path, where, limit, offset):
     where = [] if where is None else where
 


Test patch:
diff --git a/tests/integration_tests/flows/test_clickhouse.py b/tests/integration_tests/flows/test_clickhouse.py
--- a/tests/integration_tests/flows/test_clickhouse.py
+++ b/tests/integration_tests/flows/test_clickhouse.py
@@ -157,7 +157,7 @@ def test_2_insert_predictor(self):
             (
                 '{TEST_PREDICTOR_NAME}',
                 'rental_price, location',
-                'select * from test.{TEST_DATA_TABLE} limit 100',
+                'select * from test.{TEST_DATA_TABLE} limit 800',
                 '{{"join_learn_process": true, "stop_training_in_x_seconds": 3}}'
             );
         """)