mirror of
https://github.com/apache/superset.git
synced 2026-04-07 18:35:15 +00:00
fix: Leverage actual database for rendering Jinjarized SQL (#27646)
This commit is contained in:
@@ -25,8 +25,7 @@ import re
|
||||
import urllib.parse
|
||||
from collections.abc import Iterable, Iterator
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, cast, Generic, TypeVar
|
||||
from unittest.mock import Mock
|
||||
from typing import Any, cast, Generic, TYPE_CHECKING, TypeVar
|
||||
|
||||
import sqlglot
|
||||
import sqlparse
|
||||
@@ -75,6 +74,9 @@ try:
|
||||
except (ImportError, ModuleNotFoundError):
|
||||
sqloxide_parse = None
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from superset.models.core import Database
|
||||
|
||||
RESULT_OPERATIONS = {"UNION", "INTERSECT", "EXCEPT", "SELECT"}
|
||||
ON_KEYWORD = "ON"
|
||||
PRECEDES_TABLE_NAME = {"FROM", "JOIN", "DESCRIBE", "WITH", "LEFT JOIN", "RIGHT JOIN"}
|
||||
@@ -1509,7 +1511,7 @@ def extract_table_references(
|
||||
}
|
||||
|
||||
|
||||
def extract_tables_from_jinja_sql(sql: str, engine: str | None = None) -> set[Table]:
|
||||
def extract_tables_from_jinja_sql(sql: str, database: Database) -> set[Table]:
|
||||
"""
|
||||
Extract all table references in the Jinjafied SQL statement.
|
||||
|
||||
@@ -1522,7 +1524,7 @@ def extract_tables_from_jinja_sql(sql: str, engine: str | None = None) -> set[Ta
|
||||
SQLGlot.
|
||||
|
||||
:param sql: The Jinjafied SQL statement
|
||||
:param engine: The associated database engine
|
||||
:param database: The database associated with the SQL statement
|
||||
:returns: The set of tables referenced in the SQL statement
|
||||
:raises SupersetSecurityException: If SQLGlot is unable to parse the SQL statement
|
||||
"""
|
||||
@@ -1531,8 +1533,7 @@ def extract_tables_from_jinja_sql(sql: str, engine: str | None = None) -> set[Ta
|
||||
get_template_processor,
|
||||
)
|
||||
|
||||
# Mock the required database as the processor signature is exposed publically.
|
||||
processor = get_template_processor(database=Mock(backend=engine))
|
||||
processor = get_template_processor(database)
|
||||
template = processor.env.parse(sql)
|
||||
|
||||
tables = set()
|
||||
@@ -1562,6 +1563,6 @@ def extract_tables_from_jinja_sql(sql: str, engine: str | None = None) -> set[Ta
|
||||
tables
|
||||
| ParsedQuery(
|
||||
sql_statement=processor.process_template(template),
|
||||
engine=engine,
|
||||
engine=database.db_engine_spec.engine,
|
||||
).tables
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user