Repository: mindsdb/mindsdb

Issue:
Render queries to external DBs considering its syntax
different databases have different syntax. At this moment we render all queries using mysql syntax


Discussion:

Resolving patch:
diff --git a/mindsdb/api/mysql/mysql_proxy/datahub/datanodes/integration_datanode.py b/mindsdb/api/mysql/mysql_proxy/datahub/datanodes/integration_datanode.py
--- a/mindsdb/api/mysql/mysql_proxy/datahub/datanodes/integration_datanode.py
+++ b/mindsdb/api/mysql/mysql_proxy/datahub/datanodes/integration_datanode.py
@@ -1,14 +1,17 @@
 import pandas as pd
+from mindsdb_sql.render.sqlalchemy_render import SqlalchemyRender
 
 from mindsdb.api.mysql.mysql_proxy.datahub.datanodes.datanode import DataNode
+from mindsdb.utilities.log import log
 
 
 class IntegrationDataNode(DataNode):
     type = 'integration'
 
-    def __init__(self, integration_name, data_store):
+    def __init__(self, integration_name, data_store, ds_type):
         self.integration_name = integration_name
         self.data_store = data_store
+        self.ds_type = ds_type
 
     def get_type(self):
         return self.type
@@ -23,9 +26,18 @@ def get_table_columns(self, tableName):
         return []
 
     def select(self, query):
-        sql_query = str(query)
-
-        dso, _creation_info = self.data_store.create_datasource(self.integration_name, {'query': sql_query})
+        if self.ds_type in ('postgres', 'snowflake'):
+            dialect = 'postgres'
+        else:
+            dialect = 'mysql'
+        render = SqlalchemyRender(dialect)
+        try:
+            query_str = render.get_string(query, with_failback=False)
+        except Exception as e:
+            log.error(f"Exception during query casting to '{dialect}' dialect. Query: {query}. Error: {e}")
+            query_str = render.get_string(query, with_failback=True)
+
+        dso, _creation_info = self.data_store.create_datasource(self.integration_name, {'query': query_str})
         data = dso.df.to_dict(orient='records')
         column_names = list(dso.df.columns)
 
diff --git a/mindsdb/api/mysql/mysql_proxy/datahub/information_schema.py b/mindsdb/api/mysql/mysql_proxy/datahub/information_schema.py
--- a/mindsdb/api/mysql/mysql_proxy/datahub/information_schema.py
+++ b/mindsdb/api/mysql/mysql_proxy/datahub/information_schema.py
@@ -58,7 +58,8 @@ def get(self, name):
         datasource_names = self.datasource_interface.get_db_integrations().keys()
         for datasource_name in datasource_names:
             if datasource_name.lower() == name_lower:
-                return IntegrationDataNode(datasource_name, self.data_store)
+                datasource = self.datasource_interface.get_db_integration(name=datasource_name)
+                return IntegrationDataNode(datasource_name, self.data_store, ds_type=datasource['type'])
 
         return None
 


Test patch:
diff --git a/tests/integration_tests/flows/test_http.py b/tests/integration_tests/flows/test_http.py
--- a/tests/integration_tests/flows/test_http.py
+++ b/tests/integration_tests/flows/test_http.py
@@ -240,9 +240,10 @@ def test_94_edit_json_ai(self):
         # Edit it
         json_ai = predictor_data['json_ai']
         json_ai['problem_definition']
-        mixers = json_ai['outputs']['rental_price']['mixers']
+        mixers = json_ai['model']['args']['submodels']
         keep_only = [x for x in mixers if x['module'] != 'Regression']
-        json_ai['outputs']['rental_price']['mixers'] = keep_only
+        json_ai['model']['args']['submodels'] = keep_only
+
 
         # Upload it
         r = requests.put(