Skip to content

Commit aeca4da

Browse files
torrmalMinuraPunchihewaStpMax
authored
have native query passthrough (#12264)
Co-authored-by: Minura Punchihewa <minurapunchihewa17@gmail.com> Co-authored-by: Max Stepanov <stpmax@yandex.ru>
1 parent dcc4b82 commit aeca4da

2 files changed

Lines changed: 81 additions & 46 deletions

File tree

mindsdb/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__title__ = "MindsDB"
22
__package_name__ = "mindsdb"
3-
__version__ = "26.0.0"
3+
__version__ = "26.0.1"
44
__description__ = "MindsDB's AI SQL Server enables developers to build AI tools that need access to real-time data to perform their tasks"
55
__email__ = "jorge@mindsdb.com"
66
__author__ = "MindsDB Inc"

mindsdb/api/http/namespaces/sql.py

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from mindsdb.api.executor.data_types.response_type import (
1818
RESPONSE_TYPE as SQL_RESPONSE_TYPE,
1919
)
20+
from mindsdb.api.executor.sql_query.result_set import ResultSet
2021

2122
from mindsdb.integrations.utilities.query_traversal import query_traversal
2223
from mindsdb.api.executor.exceptions import ExecutorException, UnknownError
@@ -62,56 +63,90 @@ def post(self):
6263
with profiler.Context("http_query_processing"):
6364
mysql_proxy = FakeMysqlProxy()
6465
mysql_proxy.set_context(context)
65-
try:
66-
result: SQLAnswer = mysql_proxy.process_query(query)
67-
query_response: dict = result.dump_http_response()
68-
except ExecutorException as e:
69-
# classified error
70-
error_type = "expected"
71-
query_response = {
72-
"type": SQL_RESPONSE_TYPE.ERROR,
73-
"error_code": 0,
74-
"error_message": str(e),
75-
}
76-
logger.warning(f"Error query processing: {e}")
77-
except QueryError as e:
78-
error_type = "expected" if e.is_expected else "unexpected"
79-
query_response = {
80-
"type": SQL_RESPONSE_TYPE.ERROR,
81-
"error_code": 0,
82-
"error_message": str(e),
83-
}
84-
if e.is_expected:
85-
logger.warning(f"Query failed due to expected reason: {e}")
66+
67+
if context.get("native_query"):
68+
db = context.get("db")
69+
if not db:
70+
return {
71+
"type": "error",
72+
"error_code": 0,
73+
"error_message": "native_query requires 'db' in context",
74+
}, 400
75+
76+
logger.debug(f"Running query natively for database {db}")
77+
78+
try:
79+
handler = mysql_proxy.session.integration_controller.get_data_handler(db)
80+
result = handler.native_query(query)
81+
except Exception as e:
82+
query_response = {"type": "error", "error_code": 0, "error_message": str(e)}
8683
else:
84+
if result.type == SQL_RESPONSE_TYPE.ERROR:
85+
query_response = {"type": "error", "error_code": 0, "error_message": result.error_message}
86+
elif result.type == SQL_RESPONSE_TYPE.OK:
87+
query_response = {"type": "ok"}
88+
else:
89+
df = result.data_frame
90+
result_set = ResultSet.from_df(df)
91+
query_response = {
92+
"type": "table",
93+
"column_names": result_set.get_column_names(),
94+
"data": result_set.to_lists(json_types=True),
95+
}
96+
97+
query_response["context"] = mysql_proxy.get_context()
98+
99+
else:
100+
try:
101+
result: SQLAnswer = mysql_proxy.process_query(query)
102+
query_response: dict = result.dump_http_response()
103+
except ExecutorException as e:
104+
# classified error
105+
error_type = "expected"
106+
query_response = {
107+
"type": SQL_RESPONSE_TYPE.ERROR,
108+
"error_code": 0,
109+
"error_message": str(e),
110+
}
111+
logger.warning(f"Error query processing: {e}")
112+
except QueryError as e:
113+
error_type = "expected" if e.is_expected else "unexpected"
114+
query_response = {
115+
"type": SQL_RESPONSE_TYPE.ERROR,
116+
"error_code": 0,
117+
"error_message": str(e),
118+
}
119+
if e.is_expected:
120+
logger.warning(f"Query failed due to expected reason: {e}")
121+
else:
122+
logger.exception("Error query processing:")
123+
except UnknownError as e:
124+
# unclassified
125+
error_type = "unexpected"
126+
query_response = {
127+
"type": SQL_RESPONSE_TYPE.ERROR,
128+
"error_code": 0,
129+
"error_message": str(e),
130+
}
131+
logger.exception("Error query processing:")
132+
133+
except Exception as e:
134+
error_type = "unexpected"
135+
query_response = {
136+
"type": SQL_RESPONSE_TYPE.ERROR,
137+
"error_code": 0,
138+
"error_message": str(e),
139+
}
87140
logger.exception("Error query processing:")
88-
except UnknownError as e:
89-
# unclassified
90-
error_type = "unexpected"
91-
query_response = {
92-
"type": SQL_RESPONSE_TYPE.ERROR,
93-
"error_code": 0,
94-
"error_message": str(e),
95-
}
96-
logger.exception("Error query processing:")
97-
98-
except Exception as e:
99-
error_type = "unexpected"
100-
query_response = {
101-
"type": SQL_RESPONSE_TYPE.ERROR,
102-
"error_code": 0,
103-
"error_message": str(e),
104-
}
105-
logger.exception("Error query processing:")
106141

107-
if query_response.get("type") == SQL_RESPONSE_TYPE.ERROR:
108-
error_type = "expected"
109-
error_code = query_response.get("error_code")
110-
error_text = query_response.get("error_message")
142+
if query_response.get("type") == SQL_RESPONSE_TYPE.ERROR:
143+
error_type = "expected"
144+
error_code = query_response.get("error_code")
145+
error_text = query_response.get("error_message")
111146

112-
context = mysql_proxy.get_context()
147+
context = mysql_proxy.get_context()
113148

114-
query_response["context"] = context
149+
query_response["context"] = context
115150

116151
hooks.after_api_query(
117152
company_id=ctx.company_id,

0 commit comments

Comments
 (0)