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.

cache parsed SQL statements (#1820)

Akshay Agrawal8b29453427fa
diff --git a/marimo/_ast/cell.py b/marimo/_ast/cell.py
index 711ea5a31a..c1c2451f23 100644
--- a/marimo/_ast/cell.py
+++ b/marimo/_ast/cell.py
@@ -86,6 +86,11 @@ class CellOutput:
output: Any = None
+class ParsedSQLStatements:
+ parsed: Optional[list[str]] = None
+
+
@dataclasses.dataclass(frozen=True)
class CellImpl:
# hash of code
@@ -111,6 +116,10 @@ class CellImpl:
_stale: CellStaleState = dataclasses.field(default_factory=CellStaleState)
# cells can optionally hold a reference to their output
_output: CellOutput = dataclasses.field(default_factory=CellOutput)
+ # parsed sql statements
+ _sqls: ParsedSQLStatements = dataclasses.field(
+ default_factory=ParsedSQLStatements
+ )
def configure(self, update: dict[str, Any] | CellConfig) -> CellImpl:
"""Update the cell config.
@@ -127,13 +136,18 @@ class CellImpl:
@property
def sqls(self) -> list[str]:
"""Return a list of SQL statements for this cell."""
+ if self._sqls.parsed is not None:
+ return self._sqls.parsed
+
try:
visitor = SQLVisitor()
visitor.visit(ast.parse(self.code))
sqls = visitor.get_sqls()
- return sqls
+ self._sqls.parsed = sqls
except Exception:
- return []
+ self._sqls.parsed = []
+
+ return self._sqls.parsed
@property
def stale(self) -> bool:
diff --git a/marimo/_smoke_tests/altair/altair_brush.py b/marimo/_smoke_tests/altair/altair_brush.py
index 9580c00337..58d5688a6b 100644
--- a/marimo/_smoke_tests/altair/altair_brush.py
+++ b/marimo/_smoke_tests/altair/altair_brush.py
@@ -1,3 +1,4 @@
+# Copyright 2024 Marimo. All rights reserved.
import marimo
__generated_with = "0.7.5"