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.

interpolate globals into sql query using string formatting for validate_sql (#7562)

Shahmir Varqha1f9abb97cf6d
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 8151b8b9ba..96f4791c8d 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -2725,7 +2725,9 @@ class SqlCallbacks:
)
return
- _, error_message = engine.execute_in_explain_mode(request.query) # type: ignore
+ _, error_message = engine.execute_in_explain_mode( # type: ignore
+ request.query, self._kernel.globals
+ )
validate_result = SqlCatalogCheckResult(
success=True if error_message is None else False,
error_message=error_message,
diff --git a/marimo/_sql/engines/types.py b/marimo/_sql/engines/types.py
index eda31a9b0a..e14921e8fb 100644
--- a/marimo/_sql/engines/types.py
+++ b/marimo/_sql/engines/types.py
@@ -13,7 +13,10 @@ from marimo._runtime.context.types import (
get_context,
runtime_context_installed,
)
-from marimo._sql.parse import replace_brackets_with_quotes
+from marimo._sql.parse import (
+ format_query_with_globals,
+ replace_brackets_with_quotes,
+)
from marimo._sql.utils import (
is_query_empty,
strip_explain_from_error_message,
@@ -139,13 +142,24 @@ class QueryEngine(BaseEngine[CONN], ABC):
return "auto"
return "auto"
- def execute_in_explain_mode(self, query: str) -> tuple[Any, Optional[str]]:
+ def execute_in_explain_mode(
+ self, query: str, globals_dict: dict[str, Any] | None = None
+ ) -> tuple[Any, Optional[str]]:
"""Execute a query in explain mode. Returns a tuple of the result and an error if there is one."""
+ if globals_dict is None:
+ globals_dict = {}
+
explain_query = wrap_query_with_explain(query)
- quoted_query, _ = replace_brackets_with_quotes(explain_query)
+ if self.dialect == "duckdb":
+ explain_query = format_query_with_globals(
+ explain_query, globals_dict
+ )
+ else:
+ explain_query, _ = replace_brackets_with_quotes(explain_query)
+
try:
- return self.execute(quoted_query), None
+ return self.execute(explain_query), None
except Exception as e:
if is_query_empty(query):
return None, None
diff --git a/marimo/_sql/parse.py b/marimo/_sql/parse.py
index a2eff92b85..87c1ea218f 100644
--- a/marimo/_sql/parse.py
+++ b/marimo/_sql/parse.py
@@ -2,7 +2,7 @@
from __future__ import annotations
import re
-from typing import Literal, Optional, Union
+from typing import Any, Callable, Literal, Optional, Union
import msgspec
@@ -218,3 +218,56 @@ def replace_brackets_with_quotes(sql: str) -> tuple[str, dict[int, int]]:
replaced_sql = re.sub(pattern, replacement_func, sql)
return replaced_sql, offset_record
+
+
+def format_query_with_globals(
+ query: str,
+ globals_dict: dict[str, Any],
+ missing_key_handler: Callable[[str], str] = lambda key: f"'{key}'",
+) -> str:
+ """
+ Format a query by substituting brace expressions with values from globals_dict.
+ Braces inside single-quoted strings (SQL literals) are left untouched.
+ Braces inside double-quoted strings (SQL identifiers) are substituted.
+
+ Args:
+ query: The SQL query with brace expressions like {var}
+ globals_dict: Dictionary mapping variable names to their values
+ missing_key_handler: Function to handle missing keys. Defaults to quoting the key.
+
+ Returns:
+ The formatted query with substitutions applied
+
+ Example:
+ format_query_with_globals("SELECT {col} FROM '{table}'", {"col": "id"})
+ # => "SELECT id FROM '{table}'"
+
+ format_query_with_globals("SELECT * FROM '{table}'", {}, lambda key: key.upper())
+ # => "SELECT id FROM TABLE"
+ """
+ # Quick check - if no braces, return as-is
+ if "{" not in query or "}" not in query:
+ return query
+
+ # Pattern to match single-quoted strings or brace expressions
+ # Groups: 1=single quoted string, 2=full brace, 3=brace content (without braces)
+ pattern = r"(\'(?:[^\'\\]|\\.)*\')|(\{([^}]*)\})"
+
+ def replacement_func(match: re.Match[str]) -> str:
+ raw_query = match.group(0)
+ single_quoted = match.group(1)
+ has_braces = match.group(2) is not None
+
+ # If it's a single-quoted string, return it as-is
+ if single_quoted:
+ return raw_query
+
+ if has_braces:
+ key = match.group(3) # The content inside the braces
+ if key in globals_dict:
+ return str(globals_dict[key])
+ return missing_key_handler(key)
+
+ return raw_query
+
+ return re.sub(pattern, replacement_func, query)
diff --git a/tests/_sql/test_engines.py b/tests/_sql/test_engines.py
index d64d77de4e..082e6db723 100644
--- a/tests/_sql/test_engines.py
+++ b/tests/_sql/test_engines.py
@@ -297,8 +297,8 @@ def test_duckdb_execute_in_explain_mode():
# Test with invalid queries
result, error = engine.execute_in_explain_mode(
- "SELECT * FROM non_existent_table" # catalog error
- )
+ "SELECT * FROM non_existent_table"
+ ) # catalog error
assert result is None
assert error is not None
@@ -315,8 +315,15 @@ def test_duckdb_execute_in_explain_mode():
assert result is None
assert error is None
- # Test with brackets
- result, error = engine.execute_in_explain_mode("SELECT {1}")
+ # Test with braces - substituted from globals
+ result, error = engine.execute_in_explain_mode(
+ "SELECT {val}", {"val": "1"}
+ )
+ assert result is not None
+ assert error is None
+
+ # Test with braces
+ result, error = engine.execute_in_explain_mode("SELECT {missing}", {})
assert result is not None
assert error is None
@@ -368,32 +375,33 @@ def test_sqlalchemy_execute_in_explain_mode():
engine = SQLAlchemyEngine(sqlite_engine)
# Test with valid query
- result, error = engine.execute_in_explain_mode("SELECT 1, 2")
+ result, error = engine.execute_in_explain_mode("SELECT 1, 2", {})
assert result is not None
assert error is None
# Test with invalid queries
result, error = engine.execute_in_explain_mode(
- "SELECT * FROM non_existent_table" # catalog error
+ "SELECT * FROM non_existent_table",
+ {}, # catalog error
)
assert result is None
assert error is not None
- result, error = engine.execute_in_explain_mode("SELECT * FROM ")
+ result, error = engine.execute_in_explain_mode("SELECT * FROM ", {})
assert result is None
assert error is not None
# Test with empty queries
- result, error = engine.execute_in_explain_mode("")
+ result, error = engine.execute_in_explain_mode("", {})
assert result is None
assert error is None
- result, error = engine.execute_in_explain_mode("-- SELECT * FROM ")
+ result, error = engine.execute_in_explain_mode("-- SELECT * FROM ", {})
assert result is None
assert error is None
- # Test with brackets
- result, error = engine.execute_in_explain_mode("SELECT {1}")
+ # Test with braces
+ result, error = engine.execute_in_explain_mode("SELECT {1}", {})
assert result is not None
assert error is None
diff --git a/tests/_sql/test_sql_parse.py b/tests/_sql/test_sql_parse.py
index f1357644fa..0108e4916d 100644
--- a/tests/_sql/test_sql_parse.py
+++ b/tests/_sql/test_sql_parse.py
@@ -10,6 +10,7 @@ from marimo._dependencies.dependencies import DependencyManager
from marimo._sql.parse import (
SqlParseError,
SqlParseResult,
+ format_query_with_globals,
parse_sql,
replace_brackets_with_quotes,
)
@@ -757,3 +758,146 @@ FROM users"""
result_sql, offset_record = replace_brackets_with_quotes(query)
assert result_sql == "SELECT '{id}''{name}''{age}' FROM users"
assert offset_record == {7: 2, 11: 2, 17: 2}
+
+
+class TestFormatQueryWithGlobals:
+ """Test the format_query_with_globals function."""
+
+ def test_basic_substitution(self):
+ """Test basic variable substitution."""
+ query = "SELECT {column} FROM users"
+ result = format_query_with_globals(query, {"column": "id"})
+ assert result == "SELECT id FROM users"
+
+ def test_multiple_substitutions(self):
+ """Test multiple variable substitutions."""
+ query = "SELECT {col1}, {col2} FROM {table}"
+ result = format_query_with_globals(
+ query, {"col1": "id", "col2": "name", "table": "users"}
+ )
+ assert result == "SELECT id, name FROM users"
+
+ def test_missing_variable_quoted(self):
+ """Test that missing variables are returned with quotes."""
+ query = "SELECT {missing} FROM users"
+ result = format_query_with_globals(query, {})
+ assert result == "SELECT 'missing' FROM users"
+
+ def test_mixed_present_and_missing(self):
+ """Test mix of present and missing variables."""
+ query = "SELECT {present}, {missing} FROM users"
+ result = format_query_with_globals(query, {"present": "id"})
+ assert result == "SELECT id, 'missing' FROM users"
+
+ def test_no_brackets_returns_unchanged(self):
+ """Test query without brackets returns unchanged."""
+ query = "SELECT * FROM users"
+ result = format_query_with_globals(query, {"unused": "value"})
+ assert result == "SELECT * FROM users"
+
+ def test_empty_query(self):
+ """Test empty query."""
+ result = format_query_with_globals("", {})
+ assert result == ""
+
+ def test_empty_globals(self):
+ """Test with empty globals dict - missing keys get quoted."""
+ query = "SELECT {var} FROM users"
+ result = format_query_with_globals(query, {})
+ assert result == "SELECT 'var' FROM users"
+
+ def test_quoted_brackets_ignored(self):
+ """Test that brackets inside single quotes are NOT substituted."""
+ query = "SELECT '{id}' FROM users"
+ result = format_query_with_globals(query, {"id": "REPLACED"})
+ assert result == "SELECT '{id}' FROM users"
+
+ def test_double_quoted_brackets_substituted(self):
+ """Test that brackets inside double quotes ARE substituted (identifiers)."""
+ query = 'SELECT "{col}" FROM users'
+ result = format_query_with_globals(query, {"col": "my_column"})
+ assert result == 'SELECT "my_column" FROM users'
+
+ def test_double_quoted_missing_key(self):
+ """Test that missing keys in double quotes get quoted."""
+ query = 'SELECT "{col}" FROM users'
+ result = format_query_with_globals(query, {})
+ assert result == "SELECT \"'col'\" FROM users"
+
+ def test_mixed_quoted_and_unquoted(self):
+ """Test mix of quoted and unquoted brackets."""
+ query = "SELECT '{literal}', {variable} FROM users"
+ result = format_query_with_globals(query, {"variable": "id"})
+ assert result == "SELECT '{literal}', id FROM users"
+
+ def test_numeric_value(self):
+ """Test numeric values in globals."""
+ query = "SELECT * FROM users WHERE id = {id}"
+ result = format_query_with_globals(query, {"id": 42})
+ assert result == "SELECT * FROM users WHERE id = 42"
+
+ def test_string_value_not_auto_quoted(self):
+ """Test that string values are inserted as-is (caller handles quoting)."""
+ query = "SELECT * FROM users WHERE name = {name}"
+ result = format_query_with_globals(query, {"name": "'Alice'"})
+ assert result == "SELECT * FROM users WHERE name = 'Alice'"
+
+ def test_escaped_quotes_in_strings(self):
+ """Test brackets near escaped quotes."""
+ query = "SELECT 'O\\'Brien', {id} FROM users"
+ result = format_query_with_globals(query, {"id": "123"})
+ assert result == "SELECT 'O\\'Brien', 123 FROM users"
+
+ def test_json_in_quoted_string(self):
+ """Test JSON-like content in quoted strings is preserved."""
+ query = "INSERT INTO t VALUES ('{\"id\": 1}', {value})"
+ result = format_query_with_globals(query, {"value": "42"})
+ assert result == "INSERT INTO t VALUES ('{\"id\": 1}', 42)"
+
+ def test_empty_brackets(self):
+ """Test empty bracket expression."""
+ query = "SELECT {} FROM users"
+ result = format_query_with_globals(query, {"": "value"})
+ assert result == "SELECT value FROM users"
+
+ def test_empty_brackets_missing_key(self):
+ """Test empty bracket expression with no matching key."""
+ query = "SELECT {} FROM users"
+ result = format_query_with_globals(query, {})
+ assert result == "SELECT '' FROM users"
+
+ def test_adjacent_brackets(self):
+ """Test adjacent bracket expressions."""
+ query = "SELECT {a}{b} FROM users"
+ result = format_query_with_globals(query, {"a": "1", "b": "2"})
+ assert result == "SELECT 12 FROM users"
+
+ def test_multiline_query(self):
+ """Test multiline query."""
+ query = dedent("""
+ SELECT {col}
+ FROM {table}
+ WHERE id = {id}
+ """)
+ result = format_query_with_globals(
+ query, {"col": "name", "table": "users", "id": "1"}
+ )
+ expected = dedent("""
+ SELECT name
+ FROM users
+ WHERE id = 1
+ """)
+ assert result == expected
+
+ def test_missing_key_handler(self):
+ """Test missing key handler."""
+ query = "SELECT {col} FROM {table}"
+ result_double_quote = format_query_with_globals(
+ query, {"col": "id"}, lambda key: f'"{key}"'
+ )
+ assert result_double_quote == 'SELECT id FROM "table"'
+
+ result_with_uppercase = format_query_with_globals(
+ query, {"col": "id"}, lambda key: key.upper()
+ )
+ assert result_with_uppercase == "SELECT id FROM TABLE"