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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
runtime fixes for parse_sql, if duckdb not installed (#6527)
Shahmir Varqha598135e53566
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 090a5f4047..687a34bd8b 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -2488,8 +2488,7 @@ class SqlCallbacks:def __init__(self, kernel: Kernel):self._kernel = kernel- @kernel_tracer.start_as_current_span("validate_sql_query")- async def validate_sql(self, request: ValidateSQLRequest) -> None:+ async def _validate_sql_query(self, request: ValidateSQLRequest) -> None:"""Validate an SQL queryThis will validate:@@ -2568,6 +2567,19 @@ class SqlCallbacks:error=None,).broadcast()+ @kernel_tracer.start_as_current_span("validate_sql")+ async def validate_sql(self, request: ValidateSQLRequest) -> None:+ """Validate an SQL query"""++ try:+ await self._validate_sql_query(request)+ except Exception as e:+ LOGGER.exception("Failed to validate SQL query")+ ValidateSQLResult(+ request_id=request.request_id,+ error="Failed to validate SQL query: " + str(e),+ ).broadcast()+class SecretsCallbacks:def __init__(self, kernel: Kernel):diff --git a/marimo/_sql/parse.py b/marimo/_sql/parse.pyindex 9208e9bb16..d82ca684bf 100644--- a/marimo/_sql/parse.py+++ b/marimo/_sql/parse.py@@ -65,14 +65,13 @@ def parse_sql("""dialect = dialect.strip().lower()- # Handle DuckDB- if "duckdb" in dialect:- return _parse_sql_duckdb(query)-- return None, "Unsupported dialect: " + dialect---# DuckDB+ try:+ if "duckdb" in dialect:+ return _parse_sql_duckdb(query)+ else:+ return None, "Unsupported dialect: " + dialect+ except Exception as e:+ return None, str(e)class DuckDBParseError(msgspec.Struct):@@ -85,7 +84,8 @@ class DuckDBParseError(msgspec.Struct):# skip to reduce the response size# the response doesn't matter too much, we are interested in the errors-JSON_SERIALIZE_QUERY = "SELECT JSON_SERIALIZE_SQL(CAST(? AS VARCHAR), skip_null := true, skip_empty := true, skip_default := true)"+JSON_SERIALIZE_LEGACY_QUERY = "SELECT JSON_SERIALIZE_SQL(CAST(? AS VARCHAR), skip_null := true, skip_empty := true, skip_default := true)"+JSON_SERIALIZE_QUERY = "SELECT JSON_SERIALIZE_SQL(?, skip_null := true, skip_empty := true, skip_default := true)"def _parse_sql_duckdb(@@ -98,11 +98,18 @@ def _parse_sql_duckdb(- Invalid function names do not throw errors- Some syntax errors do not throw errors since they are not errors in the AST parser"""- DependencyManager.duckdb.require("to parse sql")+ if not DependencyManager.duckdb.has():+ return None, "DuckDB not installed"import duckdb- relation = duckdb.execute(JSON_SERIALIZE_QUERY, [query])+ json_serialize_query = (+ JSON_SERIALIZE_QUERY+ if duckdb.__version__ >= "1.1.0"+ else JSON_SERIALIZE_LEGACY_QUERY+ )++ relation = duckdb.execute(json_serialize_query, [query])fetch_result = relation.fetchone()if fetch_result is None:return None, "No result from DuckDB parse query"diff --git a/tests/_sql/test_sql_parse.py b/tests/_sql/test_sql_parse.pyindex 6a397d61d8..52bb1c4050 100644--- a/tests/_sql/test_sql_parse.py+++ b/tests/_sql/test_sql_parse.py@@ -421,3 +421,11 @@ class TestEdgeCases:assert result is not Noneassert result.success is Trueassert result.errors == []++[email protected](HAS_DUCKDB, reason="DuckDB is installed")+def test_fails_gracefully_no_duckdb():+ """Test that the function fails gracefully if DuckDB is not installed."""+ result, error = parse_sql("SELECT 1", "duckdb")+ assert result is None+ assert error is not None