Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make cte_alias a property of db engine spec #22947

Merged
merged 2 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
validate_adhoc_subquery,
)
from superset.datasets.models import Dataset as NewDataset
from superset.db_engine_specs.base import BaseEngineSpec, CTE_ALIAS, TimestampExpression
from superset.db_engine_specs.base import BaseEngineSpec, TimestampExpression
from superset.exceptions import (
AdvancedDataTypeResponseError,
DatasetInvalidPermissionEvaluationException,
Expand Down Expand Up @@ -904,7 +904,7 @@ def get_from_clause(

cte = self.db_engine_spec.get_cte_query(from_sql)
from_clause = (
table(CTE_ALIAS)
table(self.db_engine_spec.cte_alias)
if cte
else TextAsFrom(self.text(from_sql), []).alias(VIRTUAL_TABLE_ALIAS)
)
Expand Down
7 changes: 3 additions & 4 deletions superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@
logger = logging.getLogger()


CTE_ALIAS = "__cte"


class TimeGrain(NamedTuple):
name: str # TODO: redundant field, remove
label: str
Expand Down Expand Up @@ -344,6 +341,8 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
# If True, then it will allow in subquery ,
# if False it will allow as regular CTE
allows_cte_in_subquery = True
# Define alias for CTE
cte_alias = "__cte"
# Whether allow LIMIT clause in the SQL
# If True, then the database engine is allowed for LIMIT clause
# If False, then the database engine is allowed for TOP clause
Expand Down Expand Up @@ -887,7 +886,7 @@ def get_cte_query(cls, sql: str) -> Optional[str]:

# extract rest of the SQLs after CTE
remainder = "".join(str(token) for token in stmt.tokens[idx:]).strip()
return f"WITH {token.value},\n{CTE_ALIAS} AS (\n{remainder}\n)"
return f"WITH {token.value},\n{cls.cte_alias} AS (\n{remainder}\n)"

return None

Expand Down
3 changes: 1 addition & 2 deletions superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
config = app.config
logger = logging.getLogger(__name__)

CTE_ALIAS = "__cte"
VIRTUAL_TABLE_ALIAS = "virtual_table"
ADVANCED_DATA_TYPES = config["ADVANCED_DATA_TYPES"]

Expand Down Expand Up @@ -1046,7 +1045,7 @@ def get_from_clause(

cte = self.db_engine_spec.get_cte_query(from_sql)
from_clause = (
sa.table(CTE_ALIAS)
sa.table(self.db_engine_spec.cte_alias)
if cte
else TextAsFrom(self.text(from_sql), []).alias(VIRTUAL_TABLE_ALIAS)
)
Expand Down