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
fix: unparsable sql cells shouldnt fail (#1913)
Akshay Agrawal4b4053104d1f
diff --git a/marimo/_ast/visitor.py b/marimo/_ast/visitor.pyindex 59494faf51..c8dfa50864 100644--- a/marimo/_ast/visitor.py+++ b/marimo/_ast/visitor.py@@ -7,10 +7,13 @@ from dataclasses import dataclass, fieldfrom typing import Literal, Optionalfrom uuid import uuid4+from marimo import _loggersfrom marimo._data.sql_visitor import normalize_sql_f_stringfrom marimo._dependencies.dependencies import DependencyManagerfrom marimo._utils.variables import is_local+LOGGER = _loggers.marimo_logger()+Name = str@@ -368,13 +371,33 @@ class ScopedVisitor(ast.NodeVisitor):# Add all tables in the query to the ref scopetry:statements = duckdb.extract_statements(sql)- for statement in statements:+ except duckdb.ProgrammingError:+ # The user's sql query may have a syntax error,+ # or duckdb failed for an unknown reason; don't+ # break marimo.+ self.generic_visit(node)+ return+ except BaseException as e:+ # We catch base exceptions because we don't want to+ # fail due to bugs in duckdb -- users code should+ # be saveable no matter what+ LOGGER.warning("Unexpected duckdb error %s", e)+ self.generic_visit(node)+ return++ for statement in statements:+ try:tables = duckdb.get_table_names(statement.query)- for table in tables:- self._add_ref(table, deleted=False)- except (duckdb.ParserException, duckdb.InvalidInputException):- # The user's sql query may have a syntax error- pass+ except duckdb.ProgrammingError:+ self.generic_visit(node)+ return+ except BaseException as e:+ LOGGER.warning("Unexpected duckdb error %s", e)+ self.generic_visit(node)+ return++ for table in tables:+ self._add_ref(table, deleted=False)# Visit arguments, keyword args, etc.self.generic_visit(node)diff --git a/tests/_ast/test_visitor.py b/tests/_ast/test_visitor.pyindex 17bae18b94..3e48a07fa7 100644--- a/tests/_ast/test_visitor.py+++ b/tests/_ast/test_visitor.py@@ -983,3 +983,18 @@ def test_sql_from_another_module() -> None:v.visit(mod)assert v.defs == set(["df"])assert v.refs == set(["lib"])++[email protected](not HAS_DEPS, reason="Requires duckdb")+def test_unparsable_sql_doesnt_fail() -> None:+ code = "\n".join(+ [+ # duckdb will raise a BinderError, but codegen shouldnt fail+ "df = mo.sql('select * from cars where cars.foo = ANY({bar})')",+ ]+ )+ v = visitor.ScopedVisitor()+ mod = ast.parse(code)+ v.visit(mod)+ assert v.defs == set(["df"])+ assert v.refs == set(["mo"])