Skip to content

Commit dda17ec

Browse files
committed
fix: use catalog name on generated queries
1 parent a9cd585 commit dda17ec

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

docker/pythonpath_dev/superset_config_docker_light.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
# Import all settings from the main config first
2121
from flask_caching.backends.filesystemcache import FileSystemCache
22+
2223
from superset_config import * # noqa: F403
2324

2425
# Override caching to use simple in-memory cache instead of Redis

superset/connectors/sqla/models.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,10 +1368,23 @@ def get_template_processor(self, **kwargs: Any) -> BaseTemplateProcessor:
13681368
return get_template_processor(table=self, database=self.database, **kwargs)
13691369

13701370
def get_sqla_table(self) -> TableClause:
1371-
tbl = table(self.table_name)
1371+
# For databases that support cross-catalog queries (like BigQuery),
1372+
# include the catalog in the table identifier to generate
1373+
# project.dataset.table format
1374+
if self.catalog and self.database.db_engine_spec.supports_cross_catalog_queries:
1375+
# SQLAlchemy doesn't have built-in catalog support for TableClause,
1376+
# so we need to construct the full identifier manually
1377+
if self.schema:
1378+
full_name = f"{self.catalog}.{self.schema}.{self.table_name}"
1379+
else:
1380+
full_name = f"{self.catalog}.{self.table_name}"
1381+
1382+
return table(full_name)
1383+
13721384
if self.schema:
1373-
tbl.schema = self.schema
1374-
return tbl
1385+
return table(self.table_name, schema=self.schema)
1386+
1387+
return table(self.table_name)
13751388

13761389
def get_from_clause(
13771390
self,

tests/unit_tests/connectors/sqla/models_test.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,94 @@ def test_fetch_metadata_empty_comment_field_handling(mocker: MockerFixture) -> N
605605

606606
# Valid comment should be set
607607
assert columns_by_name["col_with_valid_comment"].description == "Valid comment"
608+
609+
610+
@pytest.mark.parametrize(
611+
"supports_cross_catalog,table_name,catalog,schema,expected_name,expected_schema",
612+
[
613+
# Database supports cross-catalog queries (like BigQuery)
614+
(
615+
True,
616+
"test_table",
617+
"test_project",
618+
"test_dataset",
619+
"test_project.test_dataset.test_table",
620+
None,
621+
),
622+
# Database supports cross-catalog queries, catalog only (no schema)
623+
(
624+
True,
625+
"test_table",
626+
"test_project",
627+
None,
628+
"test_project.test_table",
629+
None,
630+
),
631+
# Database supports cross-catalog queries, schema only (no catalog)
632+
(
633+
True,
634+
"test_table",
635+
None,
636+
"test_schema",
637+
"test_table",
638+
"test_schema",
639+
),
640+
# Database supports cross-catalog queries, no catalog or schema
641+
(
642+
True,
643+
"test_table",
644+
None,
645+
None,
646+
"test_table",
647+
None,
648+
),
649+
# Database doesn't support cross-catalog queries, catalog ignored
650+
(
651+
False,
652+
"test_table",
653+
"test_catalog",
654+
"test_schema",
655+
"test_table",
656+
"test_schema",
657+
),
658+
# Database doesn't support cross-catalog queries, no schema
659+
(
660+
False,
661+
"test_table",
662+
"test_catalog",
663+
None,
664+
"test_table",
665+
None,
666+
),
667+
],
668+
)
669+
def test_get_sqla_table_with_catalog(
670+
mocker: MockerFixture,
671+
supports_cross_catalog: bool,
672+
table_name: str,
673+
catalog: str | None,
674+
schema: str | None,
675+
expected_name: str,
676+
expected_schema: str | None,
677+
) -> None:
678+
"""Test that get_sqla_table handles catalog inclusion correctly based on
679+
database cross-catalog support
680+
"""
681+
# Mock database with specified cross-catalog support
682+
database = mocker.MagicMock()
683+
database.db_engine_spec.supports_cross_catalog_queries = supports_cross_catalog
684+
685+
# Create table with specified parameters
686+
table = SqlaTable(
687+
table_name=table_name,
688+
database=database,
689+
schema=schema,
690+
catalog=catalog,
691+
)
692+
693+
# Get the SQLAlchemy table representation
694+
sqla_table = table.get_sqla_table()
695+
696+
# Verify expected table name and schema
697+
assert sqla_table.name == expected_name
698+
assert sqla_table.schema == expected_schema

0 commit comments

Comments
 (0)