Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: check for missing cursor for sqlalchemy native output (#5823)

Shahmir Varqhaa2f85d13b040
diff --git a/marimo/_sql/engines/sqlalchemy.py b/marimo/_sql/engines/sqlalchemy.py
index 6eeb3e3478..ec90d915a1 100644
--- a/marimo/_sql/engines/sqlalchemy.py
+++ b/marimo/_sql/engines/sqlalchemy.py
@@ -444,25 +444,27 @@ class SQLAlchemyEngine(SQLConnection["Engine"]):
@staticmethod
def get_cursor_metadata(
result: CursorResult[Any],
- ) -> Optional[dict[str, Any]]:
+ ) -> dict[str, Any]:
try:
- description = result.cursor.description
- column_info = {
- "column_names": [col[0] for col in description],
- "type_code": [col[1] for col in description],
- "display_size": [col[2] for col in description],
- "internal_size": [col[3] for col in description],
- "precision": [col[4] for col in description],
- "scale": [col[5] for col in description],
- "null_ok": [col[6] for col in description],
- }
+ column_info = None
+ if result.cursor is not None:
+ description = result.cursor.description
+ column_info = {
+ "column_names": [col[0] for col in description],
+ "type_code": [col[1] for col in description],
+ "display_size": [col[2] for col in description],
+ "internal_size": [col[3] for col in description],
+ "precision": [col[4] for col in description],
+ "scale": [col[5] for col in description],
+ "null_ok": [col[6] for col in description],
+ }
if result.context.isddl:
sql_statement_type = "DDL"
elif result.context.is_crud:
sql_statement_type = "DML"
else:
- sql_statement_type = "Query"
+ sql_statement_type = "Query / Unknown"
data = {
"result_type": str(type(result)),
@@ -477,4 +479,7 @@ class SQLAlchemyEngine(SQLConnection["Engine"]):
LOGGER.warning(
"Failed to convert cursor result to df", exc_info=True
)
- return None
+ return {
+ "result_type": str(type(result)),
+ "error": "Failed to convert cursor result to df",
+ }
diff --git a/marimo/_sql/sql.py b/marimo/_sql/sql.py
index 8cb97a84fc..37dffad802 100644
--- a/marimo/_sql/sql.py
+++ b/marimo/_sql/sql.py
@@ -115,13 +115,9 @@ def sql(
# In this case, we need to convert it to a DataFrame
display_df = df
if SQLAlchemyEngine.is_cursor_result(df):
- result = SQLAlchemyEngine.get_cursor_metadata(df)
- if result is not None:
- display_df = result
+ display_df = SQLAlchemyEngine.get_cursor_metadata(df)
elif DBAPIEngine.is_dbapi_cursor(df):
- result = DBAPIEngine.get_cursor_metadata(df)
- if result is not None:
- display_df = result
+ display_df = DBAPIEngine.get_cursor_metadata(df)
t = table.table(
display_df,
diff --git a/tests/_sql/test_sqlalchemy.py b/tests/_sql/test_sqlalchemy.py
index 1a617a45c9..95f988709b 100644
--- a/tests/_sql/test_sqlalchemy.py
+++ b/tests/_sql/test_sqlalchemy.py
@@ -708,4 +708,4 @@ def test_sqlalchemy_engine_get_cursor_metadata(
assert SQLAlchemyEngine.is_cursor_result(result)
metadata = SQLAlchemyEngine.get_cursor_metadata(result)
assert metadata is not None
- assert metadata["sql_statement_type"] == "Query"
+ assert metadata["sql_statement_type"] == "Query / Unknown"