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.

improvement: skip meta schemas for sql datasource discovery (#4443)

Shahmir Varqhad63be6578e95
diff --git a/marimo/_dependencies/dependencies.py b/marimo/_dependencies/dependencies.py
index 674efd1852..51336f973f 100644
--- a/marimo/_dependencies/dependencies.py
+++ b/marimo/_dependencies/dependencies.py
@@ -171,7 +171,6 @@ class DependencyManager:
duckdb = Dependency("duckdb")
chdb = Dependency("chdb")
clickhouse_connect = Dependency("clickhouse_connect")
- timeplus_connect = Dependency("timeplus_connect")
sqlglot = Dependency("sqlglot")
pillow = Dependency("PIL")
plotly = Dependency("plotly")
diff --git a/marimo/_sql/engines/clickhouse.py b/marimo/_sql/engines/clickhouse.py
index b864568c29..a480662891 100644
--- a/marimo/_sql/engines/clickhouse.py
+++ b/marimo/_sql/engines/clickhouse.py
@@ -141,6 +141,7 @@ class ClickhouseServer(SQLEngine):
) -> None:
self._connection = connection
self._engine_name = engine_name
+ self._meta_dbs = ["system", "information_schema"]
@property
def source(self) -> str:
@@ -238,11 +239,8 @@ class ClickhouseServer(SQLEngine):
db_names = db_df[db_df.columns[0]].tolist()
for db in db_names:
db_name = cast(str, db)
- if (
- # Skip meta db's, TODO: do this for other engines too.
- db_name.lower() in ["system", "information_schema"]
- or not include_tables
- ):
+ # Skip introspection for meta tables for performance
+ if db_name.lower() in self._meta_dbs or not include_tables:
tables = []
else:
tables = self.get_tables_in_schema(
diff --git a/marimo/_sql/engines/sqlalchemy.py b/marimo/_sql/engines/sqlalchemy.py
index 80c2ad5956..448dda7bab 100644
--- a/marimo/_sql/engines/sqlalchemy.py
+++ b/marimo/_sql/engines/sqlalchemy.py
@@ -248,21 +248,24 @@ class SQLAlchemyEngine(SQLEngine):
schemas: list[Schema] = []
for schema in schema_names:
- schemas.append(
- Schema(
- name=schema,
- tables=self.get_tables_in_schema(
- schema=schema,
- database=database if database is not None else "",
- include_table_details=include_table_details,
- )
- if include_tables
- else [],
+ tables: list[DataTable] = []
+ meta_schemas = self._get_meta_schemas()
+ if schema.lower() not in meta_schemas and include_tables:
+ tables = self.get_tables_in_schema(
+ schema=schema,
+ database=database if database is not None else "",
+ include_table_details=include_table_details,
)
- )
+ schemas.append(Schema(name=schema, tables=tables))
return schemas
+ def _get_meta_schemas(self) -> list[str]:
+ dialect = self.dialect.lower()
+ if dialect == "postgresql":
+ return ["information_schema", "pg_catalog"]
+ return ["information_schema"]
+
def get_tables_in_schema(
self, *, schema: str, database: str, include_table_details: bool
) -> list[DataTable]:
diff --git a/tests/_sql/test_sqlalchemy.py b/tests/_sql/test_sqlalchemy.py
index 922f6c0085..268d53bfd0 100644
--- a/tests/_sql/test_sqlalchemy.py
+++ b/tests/_sql/test_sqlalchemy.py
@@ -84,6 +84,28 @@ def sqlite_engine() -> sa.Engine:
return engine
+def sqlite_engine_meta() -> sa.Engine:
+ """Create a temporary SQLite database with information_schema.
+ Use a mock information_schema"""
+
+ import sqlalchemy as sa
+
+ engine = sa.create_engine("sqlite:///:memory:")
+
+ sql("ATTACH ':memory:' AS information_schema", engine=engine)
+ sql(
+ "CREATE TABLE information_schema.tables (table_name TEXT)",
+ engine=engine,
+ )
+ sql(
+ "INSERT INTO information_schema.tables VALUES ('tables')",
+ engine=engine,
+ )
+
+ return engine
+
+
@pytest.mark.skipif(not HAS_SQLALCHEMY, reason="SQLAlchemy not installed")
def test_sqlalchemy_engine_dialect(sqlite_engine: sa.Engine) -> None:
"""Test SQLAlchemyEngine dialect property."""
@@ -337,6 +359,24 @@ def test_sqlalchemy_engine_get_tables_in_schema(
assert tables[0] == expected_table
+def test_sqlalchemy_skip_meta_schemas(
+ sqlite_engine_meta: sa.Engine,
+) -> None:
+ """Test SQLAlchemyEngine get_tables method."""
+ engine = SQLAlchemyEngine(
+ sqlite_engine_meta, engine_name=VariableName("test_sqlite")
+ )
+ databases = engine.get_databases(
+ include_schemas=True, include_tables=True, include_table_details=True
+ )
+ assert len(databases[0].schemas) == 2
+ assert databases[0].schemas[0].name == "main"
+ assert databases[0].schemas[1].name == "information_schema"
+
+ information_schema = databases[0].schemas[1]
+ assert information_schema.tables == []
+
+
@pytest.mark.skipif(not HAS_SQLALCHEMY, reason="SQLAlchemy not installed")
def test_sqlalchemy_engine_get_schemas(sqlite_engine: sa.Engine) -> None:
"""Test SQLAlchemyEngine get_schemas method."""