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: more sql prepared statements and quoting (#8431)
Myles Scolnick547e64e191eb
diff --git a/marimo/_data/get_datasets.py b/marimo/_data/get_datasets.pyindex bd76ab81f4..0c8cb4cd66 100644--- a/marimo/_data/get_datasets.py+++ b/marimo/_data/get_datasets.py@@ -595,5 +595,6 @@ def _quote_identifier(identifier: str) -> str:https://duckdb.org/docs/stable/sql/dialect/keywords_and_identifiers"""- escaped = identifier.replace('"', '""')- return f'"{escaped}"'+ from marimo._sql.sql_quoting import quote_sql_identifier++ return quote_sql_identifier(identifier, dialect="duckdb")diff --git a/marimo/_data/sql_summaries.py b/marimo/_data/sql_summaries.pyindex d812ddc572..f87608bf38 100644--- a/marimo/_data/sql_summaries.py+++ b/marimo/_data/sql_summaries.py@@ -3,7 +3,8 @@ from __future__ import annotationsfrom marimo._data.get_datasets import _db_type_to_data_typefrom marimo._data.models import ColumnStats, DataType-from marimo._sql.utils import wrapped_sql+from marimo._sql.sql_quoting import parse_fully_qualified_table_name+from marimo._sql.utils import execute_duckdb_sql, wrapped_sqldef get_sql_stats(@@ -138,30 +139,35 @@ def get_column_type(# First, get the column info and data typeif "." in fully_qualified_table_name:# Fully qualified table name- db_name, schema_name, table_name = _parse_fully_qualified_table_name(+ db_name, schema_name, table_name = parse_fully_qualified_table_name(fully_qualified_table_name)- column_info_query = f"""+ column_info_query = """SELECT data_typeFROM information_schema.columns- WHERE table_name = '{table_name}'- AND table_schema = '{schema_name}'- AND table_catalog = '{db_name}'- AND column_name = '{column_name}'+ WHERE table_name = $1+ AND table_schema = $2+ AND table_catalog = $3+ AND column_name = $4"""+ column_info_result: tuple[str] | None = execute_duckdb_sql(+ column_info_query,+ [table_name, schema_name, db_name, column_name],+ ).fetchone()else:# Simple table nametable_name = fully_qualified_table_name- column_info_query = f"""+ column_info_query = """SELECT data_typeFROM information_schema.columns- WHERE table_name = '{table_name}'- AND column_name = '{column_name}'+ WHERE table_name = $1+ AND column_name = $2"""+ column_info_result = execute_duckdb_sql(+ column_info_query,+ [table_name, column_name],+ ).fetchone()- column_info_result: tuple[str] | None = wrapped_sql(- column_info_query, connection=None- ).fetchone()if column_info_result is None:raise ValueError(f"Column {column_name} not found in table {table_name}"@@ -181,17 +187,3 @@ def get_histogram_data(# TODO: Implement thisreturn []---def _parse_fully_qualified_table_name(- fully_qualified_table_name: str,-) -> tuple[str, str, str]:- """- Parse a fully qualified table name into a database name, schema name, and table name.- """- parts = fully_qualified_table_name.split(".")- if len(parts) != 3:- raise ValueError(- f"Invalid fully qualified table name: {fully_qualified_table_name}"- )- return parts[0], parts[1], parts[2]diff --git a/marimo/_sql/engines/clickhouse.py b/marimo/_sql/engines/clickhouse.pyindex 80b24dbf85..007b5fd556 100644--- a/marimo/_sql/engines/clickhouse.py+++ b/marimo/_sql/engines/clickhouse.py@@ -19,6 +19,7 @@ from marimo._sql.engines.types import (InferenceConfig,SQLConnection,)+from marimo._sql.sql_quoting import quote_sql_identifierfrom marimo._sql.utils import convert_to_output, sql_type_to_data_typefrom marimo._types.ids import VariableName@@ -363,7 +364,8 @@ class ClickhouseServer(SQLConnection[Optional["ClickhouseClient"]]):tables: list[DataTable] = []try:- query = f"SHOW TABLES FROM {database}"+ quoted_db = quote_sql_identifier(database, dialect="clickhouse")+ query = f"SHOW TABLES FROM {quoted_db}"table_df = self._connection.query_df(query)except Exception:LOGGER.warning(@@ -427,8 +429,14 @@ class ClickhouseServer(SQLConnection[Optional["ClickhouseClient"]]):return Nonetry:- query = f"SELECT * FROM system.tables WHERE name = '{table_name}' AND database = '{database_name}'"- table_df = self._connection.query_df(query)+ query = "SELECT * FROM system.tables WHERE name = {table_name:String} AND database = {database_name:String}"+ table_df = self._connection.query_df(+ query,+ parameters={+ "table_name": table_name,+ "database_name": database_name,+ },+ )except Exception:LOGGER.warning(f"Failed to get table details for {table_name} in database {database_name}",@@ -461,7 +469,13 @@ class ClickhouseServer(SQLConnection[Optional["ClickhouseClient"]]):passtry:- query = f"DESCRIBE TABLE {database_name}.{table_name}"+ quoted_db = quote_sql_identifier(+ database_name, dialect="clickhouse"+ )+ quoted_table = quote_sql_identifier(+ table_name, dialect="clickhouse"+ )+ query = f"DESCRIBE TABLE {quoted_db}.{quoted_table}"desc_df = self._connection.query_df(query)except Exception:LOGGER.warning(diff --git a/marimo/_sql/engines/ibis.py b/marimo/_sql/engines/ibis.pyindex b28ac07af3..1ac9b94595 100644--- a/marimo/_sql/engines/ibis.py+++ b/marimo/_sql/engines/ibis.py@@ -338,14 +338,14 @@ class IbisEngine(SQLConnection["SQLBackend"]):"""try:# Query the target catalog directly to get tables that actually exist there- target_catalog_sql = f"""+ target_catalog_sql = """SELECT DISTINCT table_nameFROM information_schema.tables- WHERE table_catalog = '{current_database}'- AND table_schema = '{schema}'+ WHERE table_catalog = $1+ AND table_schema = $2"""result = self._connection.con.execute(- target_catalog_sql+ target_catalog_sql, [current_database, schema]).fetchall()target_catalog_tables = {row[0] for row in result}diff --git a/marimo/_sql/engines/redshift.py b/marimo/_sql/engines/redshift.pyindex f1c41587fa..4bf508b3bb 100644--- a/marimo/_sql/engines/redshift.py+++ b/marimo/_sql/engines/redshift.py@@ -15,6 +15,7 @@ from marimo._data.models import ()from marimo._dependencies.dependencies import DependencyManagerfrom marimo._sql.engines.types import InferenceConfig, SQLConnection+from marimo._sql.sql_quoting import quote_qualified_namefrom marimo._sql.utils import (raise_df_import_error,sql_type_to_data_type,@@ -337,9 +338,10 @@ class RedshiftEngine(SQLConnection["Connection"]):table_type = self._resolve_table_type(table[0][3])- row_count = cursor.execute(- f"SELECT COUNT(*) FROM {database_name}.{schema_name}.{table_name}"+ quoted_name = quote_qualified_name(+ database_name, schema_name, table_name, dialect="redshift")+ row_count = cursor.execute(f"SELECT COUNT(*) FROM {quoted_name}")row = row_count.fetchone()if row is None or row[0] is None:return None@@ -348,7 +350,7 @@ class RedshiftEngine(SQLConnection["Connection"]):try:# [[catalog, schema, table_name, column_name, ordinal_position, column_default, is_nullable, data_type, character_maximum_length, numeric_precision, numeric_scale, remarks]]columns = cursor.execute(- f"SHOW COLUMNS FROM TABLE {database_name}.{schema_name}.{table_name};"+ f"SHOW COLUMNS FROM TABLE {quoted_name};")except Exception as e:LOGGER.debug(diff --git a/marimo/_sql/sql_quoting.py b/marimo/_sql/sql_quoting.pynew file mode 100644index 0000000000..1d20977b5d--- /dev/null+++ b/marimo/_sql/sql_quoting.py@@ -0,0 +1,89 @@+# Copyright 2026 Marimo. All rights reserved.+from __future__ import annotations++import re+++def quote_sql_identifier(identifier: str, *, dialect: str = "duckdb") -> str:+ """+ Quote a SQL identifier for the given dialect, escaping special characters.++ Args:+ identifier: The raw identifier string (database, schema, or table name).+ dialect: The SQL dialect.+ Double-quote style: "duckdb", "redshift", "postgresql"/"postgres".+ Backtick style: "clickhouse", "mysql", "bigquery".+ Unknown dialects return the identifier unquoted.++ Returns:+ The properly quoted identifier string.+ """+ if dialect in ("duckdb", "redshift", "postgresql", "postgres"):+ # Double-quote style: escape embedded " as ""+ escaped = identifier.replace('"', '""')+ return f'"{escaped}"'+ elif dialect in ("clickhouse", "mysql", "bigquery"):+ # Backtick style: escape embedded ` as ``+ escaped = identifier.replace("`", "``")+ return f"`{escaped}`"+ else:+ # Unknown dialect: return unquoted to avoid breaking databases+ # that treat quoted identifiers differently+ return identifier+++def quote_qualified_name(*parts: str, dialect: str = "duckdb") -> str:+ """+ Build a fully qualified name from parts, quoting each one.++ Example:+ quote_qualified_name("my db", "public", "my.table", dialect="duckdb")+ # => '"my db"."public"."my.table"'+ """+ return ".".join(quote_sql_identifier(p, dialect=dialect) for p in parts)+++def parse_fully_qualified_table_name(+ fully_qualified_table_name: str,+) -> tuple[str, str, str]:+ """+ Parse a fully qualified table name into (database, schema, table).++ Handles both quoted and unquoted identifiers:+ - "my.db"."schema"."table" => ("my.db", "schema", "table")+ - db.schema.table => ("db", "schema", "table")++ Raises ValueError for malformed input (unterminated quotes, stray quotes,+ wrong number of parts).+ """+ # Fast path for simple unquoted identifiers (no quotes)+ if '"' not in fully_qualified_table_name:+ parts = fully_qualified_table_name.split(".")+ if len(parts) != 3:+ raise ValueError(+ f"Invalid fully qualified table name: {fully_qualified_table_name}"+ )+ return parts[0], parts[1], parts[2]++ # Each identifier is either:+ # - a quoted identifier: "..." with escaped "" inside+ # - an unquoted identifier: no dots or quotes+ _ident = r'(?:"([^"]*(?:""[^"]*)*)"|([^."]+))'+ pattern = re.compile(rf"^{_ident}\.{_ident}\.{_ident}$")+ match = pattern.fullmatch(fully_qualified_table_name)+ if not match:+ raise ValueError(+ f"Invalid fully qualified table name: {fully_qualified_table_name}"+ )++ def _segment(quoted_group: int, unquoted_group: int) -> str:+ value = match.group(quoted_group)+ if value is not None:+ return value.replace('""', '"')+ return match.group(unquoted_group)++ return (+ _segment(1, 2),+ _segment(3, 4),+ _segment(5, 6),+ )diff --git a/marimo/_sql/utils.py b/marimo/_sql/utils.pyindex cf079281a1..e1138c930b 100644--- a/marimo/_sql/utils.py+++ b/marimo/_sql/utils.py@@ -61,6 +61,46 @@ def wrapped_sql(return relation+def execute_duckdb_sql(+ query: str,+ params: list[Any],+ connection: Optional[duckdb.DuckDBPyConnection] = None,+) -> duckdb.DuckDBPyConnection:+ """Execute a parameterized DuckDB query with kernel globals context.++ Like wrapped_sql, but uses connection.execute() to support+ parameterized queries ($1, $2, ...) for safe value interpolation.+ """+ DependencyManager.duckdb.require("to execute sql")++ if connection is None:+ import duckdb++ connection = cast(duckdb.DuckDBPyConnection, duckdb)++ try:+ ctx = get_context()+ except ContextNotInitializedError:+ return connection.execute(query, params)+ else:+ install_connection = (+ ctx.execution_context.with_connection+ if ctx.execution_context is not None+ else nullcontext+ )+ with install_connection(connection):+ result: duckdb.DuckDBPyConnection = eval(+ "connection.execute(query, params)",+ ctx.globals,+ {+ "query": query,+ "params": params,+ "connection": connection,+ },+ )+ return result++def try_convert_to_polars(*,query: str,diff --git a/tests/_sql/test_sql_quoting.py b/tests/_sql/test_sql_quoting.pynew file mode 100644index 0000000000..a5be8661b0--- /dev/null+++ b/tests/_sql/test_sql_quoting.py@@ -0,0 +1,217 @@+# Copyright 2026 Marimo. All rights reserved.+from __future__ import annotations++import pytest++from marimo._sql.sql_quoting import (+ parse_fully_qualified_table_name,+ quote_qualified_name,+ quote_sql_identifier,+)+++class TestQuoteSqlIdentifier:+ @pytest.mark.parametrize(+ ("identifier", "dialect", "expected"),+ [+ # DuckDB / Redshift / Postgres (double-quote style)+ ("table", "duckdb", '"table"'),+ ("my table", "duckdb", '"my table"'),+ ("nested.namespace", "duckdb", '"nested.namespace"'),+ ('has"quotes', "duckdb", '"has""quotes"'),+ ('double""already', "duckdb", '"double""""already"'),+ ("", "duckdb", '""'),+ ("unicode_ñoño", "duckdb", '"unicode_ñoño"'),+ ("back`ticks", "duckdb", '"back`ticks"'),+ # Redshift uses same double-quote style+ ("table", "redshift", '"table"'),+ ("my table", "redshift", '"my table"'),+ ('has"quotes', "redshift", '"has""quotes"'),+ # PostgreSQL aliases+ ("table", "postgresql", '"table"'),+ ("table", "postgres", '"table"'),+ # ClickHouse / MySQL (backtick style)+ ("table", "clickhouse", "`table`"),+ ("my table", "clickhouse", "`my table`"),+ ("nested.namespace", "clickhouse", "`nested.namespace`"),+ ("has`backtick", "clickhouse", "`has``backtick`"),+ ("double``already", "clickhouse", "`double````already`"),+ ("", "clickhouse", "``"),+ ("unicode_ñoño", "clickhouse", "`unicode_ñoño`"),+ ('has"quotes', "clickhouse", '`has"quotes`'),+ # MySQL same as clickhouse+ ("table", "mysql", "`table`"),+ ("has`backtick", "mysql", "`has``backtick`"),+ # BigQuery uses backtick style+ ("table", "bigquery", "`table`"),+ ("my table", "bigquery", "`my table`"),+ ("has`backtick", "bigquery", "`has``backtick`"),+ # Unknown dialect returns unquoted+ ("table", "sqlite", "table"),+ ("my table", "unknown", "my table"),+ ],+ )+ def test_quote_identifier(+ self, identifier: str, dialect: str, expected: str+ ) -> None:+ assert quote_sql_identifier(identifier, dialect=dialect) == expected++ @pytest.mark.parametrize(+ "identifier",+ [+ "simple",+ "with spaces",+ "with.dots",+ 'with"quotes',+ "with`backticks",+ "with'single'quotes",+ "mixed.dots and spaces",+ "slashes/and/paths",+ ],+ )+ def test_duckdb_roundtrip_safe(self, identifier: str) -> None:+ """Verify that quoting an identifier produces valid DuckDB syntax."""+ quoted = quote_sql_identifier(identifier, dialect="duckdb")+ # Must start and end with double quotes+ assert quoted.startswith('"')+ assert quoted.endswith('"')+ # Inner content should not have unescaped double quotes+ inner = quoted[1:-1]+ # After un-escaping "", we should get back the original+ assert inner.replace('""', '"') == identifier++ @pytest.mark.parametrize(+ "identifier",+ [+ "simple",+ "with spaces",+ "with.dots",+ "with`backticks",+ 'with"quotes',+ ],+ )+ def test_clickhouse_roundtrip_safe(self, identifier: str) -> None:+ """Verify that quoting an identifier produces valid ClickHouse syntax."""+ quoted = quote_sql_identifier(identifier, dialect="clickhouse")+ assert quoted.startswith("`")+ assert quoted.endswith("`")+ inner = quoted[1:-1]+ assert inner.replace("``", "`") == identifier+++class TestQuoteQualifiedName:+ @pytest.mark.parametrize(+ ("parts", "dialect", "expected"),+ [+ # Simple 3-part name+ (+ ("mydb", "public", "users"),+ "duckdb",+ '"mydb"."public"."users"',+ ),+ # Parts with special characters+ (+ ("my.db", "my schema", "my.table"),+ "duckdb",+ '"my.db"."my schema"."my.table"',+ ),+ # ClickHouse 2-part name+ (+ ("default", "events"),+ "clickhouse",+ "`default`.`events`",+ ),+ # Redshift 3-part name+ (+ ("catalog", "schema", "table"),+ "redshift",+ '"catalog"."schema"."table"',+ ),+ # Single part+ (+ ("just_table",),+ "duckdb",+ '"just_table"',+ ),+ # Parts with quotes in them+ (+ ('db"name', "schema", "table"),+ "duckdb",+ '"db""name"."schema"."table"',+ ),+ (+ ("db`name", "table"),+ "clickhouse",+ "`db``name`.`table`",+ ),+ ],+ )+ def test_quote_qualified_name(+ self, parts: tuple[str, ...], dialect: str, expected: str+ ) -> None:+ assert quote_qualified_name(*parts, dialect=dialect) == expected+++class TestParseFullyQualifiedTableName:+ @pytest.mark.parametrize(+ ("fqn", "expected"),+ [+ # Simple unquoted+ ("db.schema.table", ("db", "schema", "table")),+ # All quoted+ ('"db"."schema"."table"', ("db", "schema", "table")),+ # Quoted with dots inside+ (+ '"my.db"."my.schema"."my.table"',+ ("my.db", "my.schema", "my.table"),+ ),+ # Quoted with spaces+ (+ '"my db"."my schema"."my table"',+ ("my db", "my schema", "my table"),+ ),+ # Quoted with escaped double quotes+ ('"my""db"."schema"."table"', ('my"db', "schema", "table")),+ # Mixed quoted and unquoted+ ('"my.db".public.users', ("my.db", "public", "users")),+ # All parts have special chars+ (+ '"db.with.dots"."schema with spaces"."table""quoted"',+ ("db.with.dots", "schema with spaces", 'table"quoted'),+ ),+ ],+ )+ def test_parse_valid(+ self, fqn: str, expected: tuple[str, str, str]+ ) -> None:+ assert parse_fully_qualified_table_name(fqn) == expected++ @pytest.mark.parametrize(+ "fqn",+ [+ "just_a_table",+ "two.parts",+ "four.parts.here.extra",+ "",+ '"single_quoted"',+ '"two"."parts"',+ # Malformed quoted FQNs: unterminated or stray quotes+ '"unterminated',+ '"db"."schema"."table',+ 'db"."schema"."table"',+ '"db".schema"."table',+ 'db.sch"ema.table',+ ],+ )+ def test_parse_invalid(self, fqn: str) -> None:+ with pytest.raises(+ ValueError, match="Invalid fully qualified table name"+ ):+ parse_fully_qualified_table_name(fqn)++ def test_roundtrip_with_quote_qualified_name(self) -> None:+ """quote_qualified_name output should be parseable by parse_fully_qualified_table_name."""+ db, schema, table = "my.db", "my schema", 'table"name'+ fqn = quote_qualified_name(db, schema, table, dialect="duckdb")+ parsed = parse_fully_qualified_table_name(fqn)+ assert parsed == (db, schema, table)