You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by be...@apache.org on 2023/11/08 13:00:00 UTC

(superset) branch robust_has_table_query updated (06b3274365 -> f3beba70f3)

This is an automated email from the ASF dual-hosted git repository.

beto pushed a change to branch robust_has_table_query
in repository https://gitbox.apache.org/repos/asf/superset.git


    omit 06b3274365 Fix query error
    omit d454f48c2c Fix lint and tests
    omit c8aa2d7304 feat: robust(er) adhoc query validation
     new f3beba70f3 feat: robust(er) adhoc query validation

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (06b3274365)
            \
             N -- N -- N   refs/heads/robust_has_table_query (f3beba70f3)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:


(superset) 01/01: feat: robust(er) adhoc query validation

Posted by be...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

beto pushed a commit to branch robust_has_table_query
in repository https://gitbox.apache.org/repos/asf/superset.git

commit f3beba70f307e44f58b61d145b1a00b89a60af7f
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Thu May 11 15:37:29 2023 -0700

    feat: robust(er) adhoc query validation
---
 requirements/base.txt               |   3 +
 requirements/development.txt        |   7 --
 requirements/testing.txt            |   5 --
 setup.py                            |   1 +
 superset/connectors/sqla/models.py  |  31 +-------
 superset/models/helpers.py          |  81 ++++++++++++--------
 superset/models/sql_lab.py          |   3 +-
 superset/sql_parse.py               | 146 ++++++++++++++----------------------
 tests/unit_tests/sql_parse_tests.py |  36 ++++-----
 9 files changed, 136 insertions(+), 177 deletions(-)

diff --git a/requirements/base.txt b/requirements/base.txt
index 3370eceac4..78f715f415 100644
--- a/requirements/base.txt
+++ b/requirements/base.txt
@@ -220,6 +220,7 @@ packaging==23.1
     #   apache-superset
     #   apispec
     #   deprecation
+    #   gunicorn
     #   limits
     #   marshmallow
     #   shillelagh
@@ -318,6 +319,8 @@ sqlalchemy-utils==0.38.3
     # via
     #   apache-superset
     #   flask-appbuilder
+sqloxide==0.1.36
+    # via apache-superset
 sqlparse==0.4.4
     # via apache-superset
 sshtunnel==0.4.0
diff --git a/requirements/development.txt b/requirements/development.txt
index 8eea1ae4ca..1c811bd33a 100644
--- a/requirements/development.txt
+++ b/requirements/development.txt
@@ -104,17 +104,10 @@ pylint==2.17.4
     # via -r requirements/development.in
 python-ldap==3.4.3
     # via -r requirements/development.in
-requests==2.31.0
-    # via
-    #   pydruid
-    #   tableschema
-    #   tabulator
 rfc3986==2.0.0
     # via tableschema
 s3transfer==0.6.1
     # via boto3
-sqloxide==0.1.33
-    # via -r requirements/development.in
 stack-data==0.6.2
     # via ipython
 tableschema==1.20.2
diff --git a/requirements/testing.txt b/requirements/testing.txt
index da79433632..1d1ebc2232 100644
--- a/requirements/testing.txt
+++ b/requirements/testing.txt
@@ -12,8 +12,6 @@
     #   -r requirements/base.in
     #   -r requirements/development.in
     #   -r requirements/testing.in
-apsw==3.42.0.1
-    # via shillelagh
 cmdstanpy==1.1.0
     # via prophet
 contourpy==1.0.7
@@ -50,7 +48,6 @@ google-auth==2.17.3
     #   google-cloud-core
     #   pandas-gbq
     #   pydata-google-auth
-    #   shillelagh
     #   sqlalchemy-bigquery
 google-auth-oauthlib==1.0.0
     # via
@@ -143,8 +140,6 @@ rsa==4.9
     # via google-auth
 setuptools-git==1.2
     # via prophet
-shillelagh[gsheetsapi]==1.2.6
-    # via apache-superset
 sqlalchemy-bigquery==1.6.1
     # via apache-superset
 statsd==4.0.1
diff --git a/setup.py b/setup.py
index 6190eaf65c..d941952a0e 100644
--- a/setup.py
+++ b/setup.py
@@ -125,6 +125,7 @@ setup(
         "slack_sdk>=3.19.0, <4",
         "sqlalchemy>=1.4, <2",
         "sqlalchemy-utils>=0.38.3, <0.39",
+        "sqloxide>=0.1, <0.2",
         "sqlparse>=0.4.4, <0.5",
         "tabulate>=0.8.9, <0.9",
         "typing-extensions>=4, <5",
diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py
index e366940ff2..0323fee995 100644
--- a/superset/connectors/sqla/models.py
+++ b/superset/connectors/sqla/models.py
@@ -82,10 +82,8 @@ from superset.db_engine_specs.base import BaseEngineSpec, TimestampExpression
 from superset.exceptions import (
     ColumnNotFoundException,
     DatasetInvalidPermissionEvaluationException,
-    QueryClauseValidationException,
     QueryObjectValidationError,
     SupersetGenericDBErrorException,
-    SupersetSecurityException,
 )
 from superset.jinja_context import (
     BaseTemplateProcessor,
@@ -98,11 +96,11 @@ from superset.models.helpers import (
     AuditMixinNullable,
     CertificationMixin,
     ExploreMixin,
+    process_sql_expression,
     QueryResult,
     QueryStringExtended,
-    validate_adhoc_subquery,
 )
-from superset.sql_parse import ParsedQuery, sanitize_clause
+from superset.sql_parse import ParsedQuery
 from superset.superset_typing import (
     AdhocColumn,
     AdhocMetric,
@@ -473,27 +471,6 @@ sqlatable_user = Table(
 )
 
 
-def _process_sql_expression(
-    expression: str | None,
-    database_id: int,
-    schema: str,
-    template_processor: BaseTemplateProcessor | None = None,
-) -> str | None:
-    if template_processor and expression:
-        expression = template_processor.process_template(expression)
-    if expression:
-        try:
-            expression = validate_adhoc_subquery(
-                expression,
-                database_id,
-                schema,
-            )
-            expression = sanitize_clause(expression)
-        except (QueryClauseValidationException, SupersetSecurityException) as ex:
-            raise QueryObjectValidationError(ex.message) from ex
-    return expression
-
-
 class SqlaTable(
     Model, BaseDatasource, ExploreMixin
 ):  # pylint: disable=too-many-public-methods
@@ -956,7 +933,7 @@ class SqlaTable(
                 sqla_column = column(column_name)
             sqla_metric = self.sqla_aggregations[metric["aggregate"]](sqla_column)
         elif expression_type == utils.AdhocMetricExpressionType.SQL:
-            expression = _process_sql_expression(
+            expression = process_sql_expression(
                 expression=metric["sqlExpression"],
                 database_id=self.database_id,
                 schema=self.schema,
@@ -986,7 +963,7 @@ class SqlaTable(
         :rtype: sqlalchemy.sql.column
         """
         label = utils.get_column_name(col)
-        expression = _process_sql_expression(
+        expression = process_sql_expression(
             expression=col["sqlExpression"],
             database_id=self.database_id,
             schema=self.schema,
diff --git a/superset/models/helpers.py b/superset/models/helpers.py
index 83ec9ba37c..aade61779e 100644
--- a/superset/models/helpers.py
+++ b/superset/models/helpers.py
@@ -43,7 +43,7 @@ from flask_appbuilder.models.mixins import AuditMixin
 from flask_appbuilder.security.sqla.models import User
 from flask_babel import lazy_gettext as _
 from jinja2.exceptions import TemplateError
-from sqlalchemy import and_, Column, or_, UniqueConstraint
+from sqlalchemy import and_, Column, dialects, or_, UniqueConstraint
 from sqlalchemy.ext.declarative import declared_attr
 from sqlalchemy.orm import Mapper, Session, validates
 from sqlalchemy.orm.exc import MultipleResultsFound
@@ -103,9 +103,10 @@ ADVANCED_DATA_TYPES = config["ADVANCED_DATA_TYPES"]
 
 
 def validate_adhoc_subquery(
-    sql: str,
+    expression: str,
     database_id: int,
     default_schema: str,
+    sqla_dialect: Optional[str] = None,
 ) -> str:
     """
     Check if adhoc SQL contains sub-queries or nested sub-queries with table.
@@ -118,8 +119,11 @@ def validate_adhoc_subquery(
     nested sub-queries with table
     """
     statements = []
-    for statement in sqlparse.parse(sql):
-        if has_table_query(statement):
+    for statement in sqlparse.parse(expression):
+        # build a proper SQL query from the expression for sqloxide
+        sql = f"SELECT {expression}"
+
+        if has_table_query(sql, sqla_dialect):
             if not is_feature_enabled("ALLOW_ADHOC_SUBQUERY"):
                 raise SupersetSecurityException(
                     SupersetError(
@@ -134,6 +138,32 @@ def validate_adhoc_subquery(
     return ";\n".join(str(statement) for statement in statements)
 
 
+def process_sql_expression(
+    expression: Optional[str],
+    database_id: int,
+    schema: str,
+    template_processor: Optional[BaseTemplateProcessor],
+    sqla_dialect: Optional[str] = None,
+) -> Optional[str]:
+    """
+    Apply template and validate ad-hoc expressions.
+    """
+    if template_processor and expression:
+        expression = template_processor.process_template(expression)
+    if expression:
+        expression = validate_adhoc_subquery(
+            expression,
+            database_id,
+            schema,
+            sqla_dialect,
+        )
+        try:
+            expression = sanitize_clause(expression)
+        except (QueryClauseValidationException, SupersetSecurityException) as ex:
+            raise QueryObjectValidationError(ex.message) from ex
+    return expression
+
+
 def json_to_dict(json_str: str) -> dict[Any, Any]:
     if json_str:
         val = re.sub(",[ \t\r\n]+}", "}", json_str)
@@ -833,27 +863,6 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
                 )
             ) from ex
 
-    def _process_sql_expression(
-        self,
-        expression: Optional[str],
-        database_id: int,
-        schema: str,
-        template_processor: Optional[BaseTemplateProcessor],
-    ) -> Optional[str]:
-        if template_processor and expression:
-            expression = template_processor.process_template(expression)
-        if expression:
-            expression = validate_adhoc_subquery(
-                expression,
-                database_id,
-                schema,
-            )
-            try:
-                expression = sanitize_clause(expression)
-            except QueryClauseValidationException as ex:
-                raise QueryObjectValidationError(ex.message) from ex
-        return expression
-
     def make_sqla_column_compatible(
         self, sqla_col: ColumnElement, label: Optional[str] = None
     ) -> ColumnElement:
@@ -1133,11 +1142,12 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
             sqla_column = sa.column(column_name)
             sqla_metric = self.sqla_aggregations[metric["aggregate"]](sqla_column)
         elif expression_type == utils.AdhocMetricExpressionType.SQL:
-            expression = self._process_sql_expression(
+            expression = process_sql_expression(
                 expression=metric["sqlExpression"],
                 database_id=self.database_id,
                 schema=self.schema,
                 template_processor=template_processor,
+                sqla_dialect=self.db_engine_spec.engine,
             )
             sqla_metric = literal_column(expression)
         else:
@@ -1555,11 +1565,12 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
             if isinstance(col, dict):
                 col = cast(AdhocMetric, col)
                 if col.get("sqlExpression"):
-                    col["sqlExpression"] = self._process_sql_expression(
+                    col["sqlExpression"] = process_sql_expression(
                         expression=col["sqlExpression"],
                         database_id=self.database_id,
                         schema=self.schema,
                         template_processor=template_processor,
+                        sqla_dialect=self.db_engine_spec.engine,
                     )
                 if utils.is_adhoc_metric(col):
                     # add adhoc sort by column to columns_by_name if not exists
@@ -1597,6 +1608,10 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
         columns = [col for col in columns if col != utils.DTTM_ALIAS]
         dttm_col = columns_by_name.get(granularity) if granularity else None
 
+        # use quoter for quoting column names
+        dialect = dialects.registry.load(self.db_engine_spec.engine)
+        quoter = dialect().identifier_preparer.quote
+
         if need_groupby:
             # dedup columns while preserving order
             columns = groupby or columns
@@ -1618,9 +1633,10 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
                         )
                     else:
                         selected = validate_adhoc_subquery(
-                            selected,
+                            quoter(selected),
                             self.database_id,
                             self.schema,
+                            self.db_engine_spec.engine,
                         )
                         outer = literal_column(f"({selected})")
                         outer = self.make_sqla_column_compatible(outer, selected)
@@ -1640,13 +1656,14 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
                     _sql = selected["sqlExpression"]
                     _column_label = selected["label"]
                 elif isinstance(selected, str):
-                    _sql = selected
+                    _sql = quoter(selected)
                     _column_label = selected
 
                 selected = validate_adhoc_subquery(
                     _sql,
                     self.database_id,
                     self.schema,
+                    self.db_engine_spec.engine,
                 )
 
                 select_exprs.append(
@@ -1913,11 +1930,12 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
                             msg=ex.message,
                         )
                     ) from ex
-                where = self._process_sql_expression(
+                where = process_sql_expression(
                     expression=where,
                     database_id=self.database_id,
                     schema=self.schema,
                     template_processor=template_processor,
+                    sqla_dialect=self.db_engine_spec.engine,
                 )
                 where_clause_and += [self.text(where)]
             having = extras.get("having")
@@ -1931,11 +1949,12 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
                             msg=ex.message,
                         )
                     ) from ex
-                having = self._process_sql_expression(
+                having = process_sql_expression(
                     expression=having,
                     database_id=self.database_id,
                     schema=self.schema,
                     template_processor=template_processor,
+                    sqla_dialect=self.db_engine_spec.engine,
                 )
                 having_clause_and += [self.text(having)]
 
diff --git a/superset/models/sql_lab.py b/superset/models/sql_lab.py
index ffd19fb0e4..76773d7069 100644
--- a/superset/models/sql_lab.py
+++ b/superset/models/sql_lab.py
@@ -52,6 +52,7 @@ from superset.models.helpers import (
     ExploreMixin,
     ExtraJSONMixin,
     ImportExportMixin,
+    process_sql_expression,
 )
 from superset.sql_parse import CtasMethod, ParsedQuery, Table
 from superset.sqllab.limiting_factor import LimitingFactor
@@ -345,7 +346,7 @@ class Query(
         :rtype: sqlalchemy.sql.column
         """
         label = get_column_name(col)
-        expression = self._process_sql_expression(
+        expression = process_sql_expression(
             expression=col["sqlExpression"],
             database_id=self.database_id,
             schema=self.schema,
diff --git a/superset/sql_parse.py b/superset/sql_parse.py
index d75551bef0..59f688116a 100644
--- a/superset/sql_parse.py
+++ b/superset/sql_parse.py
@@ -23,6 +23,7 @@ from urllib import parse
 
 import sqlparse
 from sqlalchemy import and_
+from sqloxide import parse_sql  # pylint: disable=no-name-in-module
 from sqlparse import keywords
 from sqlparse.lexer import Lexer
 from sqlparse.sql import (
@@ -50,16 +51,31 @@ from sqlparse.utils import imt
 from superset.exceptions import QueryClauseValidationException
 from superset.utils.backports import StrEnum
 
-try:
-    from sqloxide import parse_sql as sqloxide_parse
-except:  # pylint: disable=bare-except
-    sqloxide_parse = None
-
 RESULT_OPERATIONS = {"UNION", "INTERSECT", "EXCEPT", "SELECT"}
 ON_KEYWORD = "ON"
 PRECEDES_TABLE_NAME = {"FROM", "JOIN", "DESCRIBE", "WITH", "LEFT JOIN", "RIGHT JOIN"}
 CTE_PREFIX = "CTE__"
 
+# mapping between sqloxide and SQLAlchemy dialects
+SQLOXIDE_DIALECTS = {
+    "ansi": {"trino", "trinonative", "presto"},
+    "hive": {"hive", "databricks"},
+    "ms": {"mssql"},
+    "mysql": {"mysql"},
+    "postgres": {
+        "cockroachdb",
+        "hana",
+        "netezza",
+        "postgres",
+        "postgresql",
+        "redshift",
+        "vertica",
+    },
+    "snowflake": {"snowflake"},
+    "sqlite": {"sqlite", "gsheets", "shillelagh"},
+    "clickhouse": {"clickhouse"},
+}
+
 logger = logging.getLogger(__name__)
 
 # TODO: Workaround for https://github.com/andialbrecht/sqlparse/issues/652.
@@ -251,16 +267,15 @@ class ParsedQuery:
         for statement in parsed:
             # Check if this is a CTE
             if statement.is_group and statement[0].ttype == Keyword.CTE:
-                if sqloxide_parse is not None:
-                    try:
-                        if not self._check_cte_is_select(
-                            sqloxide_parse(self.strip_comments(), dialect="ansi")
-                        ):
-                            return False
-                    except ValueError:
-                        # sqloxide was not able to parse the query, so let's continue with
-                        # sqlparse
-                        pass
+                try:
+                    if not self._check_cte_is_select(
+                        parse_sql(self.strip_comments(), dialect="ansi")
+                    ):
+                        return False
+                except ValueError:
+                    # sqloxide was not able to parse the query, so let's continue with
+                    # sqlparse
+                    pass
                 inner_cte = self.get_inner_cte_expression(statement.tokens) or []
                 # Check if the inner CTE is a not a SELECT
                 if any(token.ttype == DDL for token in inner_cte) or any(
@@ -548,21 +563,11 @@ def sanitize_clause(clause: str) -> str:
     return clause
 
 
-class InsertRLSState(StrEnum):
-    """
-    State machine that scans for WHERE and ON clauses referencing tables.
-    """
-
-    SCANNING = "SCANNING"
-    SEEN_SOURCE = "SEEN_SOURCE"
-    FOUND_TABLE = "FOUND_TABLE"
-
-
-def has_table_query(token_list: TokenList) -> bool:
+def has_table_query(sql: str, sqla_dialect: Optional[str] = None) -> bool:
     """
-    Return if a statement has a query reading from a table.
+    Return if the sql has a subquery reading from a table.
 
-        >>> has_table_query(sqlparse.parse("COUNT(*)")[0])
+        >>> has_table_query(sqlparse.parse("SELECT COUNT(*)")[0])
         False
         >>> has_table_query(sqlparse.parse("SELECT * FROM table")[0])
         True
@@ -573,31 +578,17 @@ def has_table_query(token_list: TokenList) -> bool:
         False
 
     """
-    state = InsertRLSState.SCANNING
-    for token in token_list.tokens:
-        # Ignore comments
-        if isinstance(token, sqlparse.sql.Comment):
-            continue
+    return bool(extract_table_references(sql, sqla_dialect))
 
-        # Recurse into child token list
-        if isinstance(token, TokenList) and has_table_query(token):
-            return True
 
-        # Found a source keyword (FROM/JOIN)
-        if imt(token, m=[(Keyword, "FROM"), (Keyword, "JOIN")]):
-            state = InsertRLSState.SEEN_SOURCE
-
-        # Found identifier/keyword after FROM/JOIN
-        elif state == InsertRLSState.SEEN_SOURCE and (
-            isinstance(token, sqlparse.sql.Identifier) or token.ttype == Keyword
-        ):
-            return True
-
-        # Found nothing, leaving source
-        elif state == InsertRLSState.SEEN_SOURCE and token.ttype != Whitespace:
-            state = InsertRLSState.SCANNING
+class InsertRLSState(StrEnum):
+    """
+    State machine that scans for WHERE and ON clauses referencing tables.
+    """
 
-    return False
+    SCANNING = "SCANNING"
+    SEEN_SOURCE = "SEEN_SOURCE"
+    FOUND_TABLE = "FOUND_TABLE"
 
 
 def add_table_name(rls: TokenList, table: str) -> None:
@@ -777,57 +768,34 @@ def insert_rls(
     return token_list
 
 
-# mapping between sqloxide and SQLAlchemy dialects
-SQLOXITE_DIALECTS = {
-    "ansi": {"trino", "trinonative", "presto"},
-    "hive": {"hive", "databricks"},
-    "ms": {"mssql"},
-    "mysql": {"mysql"},
-    "postgres": {
-        "cockroachdb",
-        "hana",
-        "netezza",
-        "postgres",
-        "postgresql",
-        "redshift",
-        "vertica",
-    },
-    "snowflake": {"snowflake"},
-    "sqlite": {"sqlite", "gsheets", "shillelagh"},
-    "clickhouse": {"clickhouse"},
-}
-
 RE_JINJA_VAR = re.compile(r"\{\{[^\{\}]+\}\}")
 RE_JINJA_BLOCK = re.compile(r"\{[%#][^\{\}%#]+[%#]\}")
 
 
 def extract_table_references(
-    sql_text: str, sqla_dialect: str, show_warning: bool = True
+    sql_text: str,
+    sqla_dialect: Optional[str] = None,
+    show_warning: bool = True,
 ) -> set["Table"]:
     """
     Return all the dependencies from a SQL sql_text.
     """
-    dialect = "generic"
-    tree = None
-
-    if sqloxide_parse:
-        for dialect, sqla_dialects in SQLOXITE_DIALECTS.items():
+    sqloxide_dialect = "generic"
+    if sqla_dialect:
+        for sqloxide_dialect, sqla_dialects in SQLOXIDE_DIALECTS.items():
             if sqla_dialect in sqla_dialects:
                 break
-        sql_text = RE_JINJA_BLOCK.sub(" ", sql_text)
-        sql_text = RE_JINJA_VAR.sub("abc", sql_text)
-        try:
-            tree = sqloxide_parse(sql_text, dialect=dialect)
-        except Exception as ex:  # pylint: disable=broad-except
-            if show_warning:
-                logger.warning(
-                    "\nUnable to parse query with sqloxide:\n%s\n%s", sql_text, ex
-                )
 
-    # fallback to sqlparse
-    if not tree:
-        parsed = ParsedQuery(sql_text)
-        return parsed.tables
+    sql_text = RE_JINJA_BLOCK.sub(" ", sql_text)
+    sql_text = RE_JINJA_VAR.sub("abc", sql_text)
+    try:
+        tree = parse_sql(sql_text, dialect=sqloxide_dialect)
+    except Exception as ex:
+        if show_warning:
+            logger.warning(
+                "\nUnable to parse query with sqloxide:\n%s\n%s", sql_text, ex
+            )
+        raise ex
 
     def find_nodes_by_key(element: Any, target: str) -> Iterator[Any]:
         """
diff --git a/tests/unit_tests/sql_parse_tests.py b/tests/unit_tests/sql_parse_tests.py
index 341ba9d789..9ed919ddf0 100644
--- a/tests/unit_tests/sql_parse_tests.py
+++ b/tests/unit_tests/sql_parse_tests.py
@@ -1286,16 +1286,16 @@ def test_sqlparse_issue_652():
 @pytest.mark.parametrize(
     "sql,expected",
     [
-        ("SELECT * FROM table", True),
-        ("SELECT a FROM (SELECT 1 AS a) JOIN (SELECT * FROM table)", True),
+        ("SELECT * FROM 'table'", True),
+        ("SELECT a FROM (SELECT 1 AS a) JOIN (SELECT * FROM 'table')", True),
         ("(SELECT COUNT(DISTINCT name) AS foo FROM    birth_names)", True),
-        ("COUNT(*)", False),
+        ("SELECT COUNT(*)", False),
         ("SELECT a FROM (SELECT 1 AS a)", False),
-        ("SELECT a FROM (SELECT 1 AS a) JOIN table", True),
+        ("SELECT a FROM (SELECT 1 AS a) JOIN 'table'", True),
         ("SELECT * FROM (SELECT 1 AS foo, 2 AS bar) ORDER BY foo ASC, bar", False),
         ("SELECT * FROM other_table", True),
-        ("extract(HOUR from from_unixtime(hour_ts)", False),
-        ("(SELECT * FROM table)", True),
+        ("SELECT extract(HOUR from from_unixtime(hour_ts))", False),
+        ("(SELECT * FROM 'table')", True),
         ("(SELECT COUNT(DISTINCT name) from birth_names)", True),
         (
             "(SELECT table_name FROM information_schema.tables WHERE table_name LIKE '%user%' LIMIT 1)",
@@ -1305,6 +1305,10 @@ def test_sqlparse_issue_652():
             "(SELECT table_name FROM /**/ information_schema.tables WHERE table_name LIKE '%user%' LIMIT 1)",
             True,
         ),
+        (
+            "SELECT table_name FROM (information_schema.tables) WHERE table_name LIKE '%user%' LIMIT 1",
+            True,
+        ),
     ],
 )
 def test_has_table_query(sql: str, expected: bool) -> None:
@@ -1314,8 +1318,7 @@ def test_has_table_query(sql: str, expected: bool) -> None:
     This is used to prevent ad-hoc metrics from querying unauthorized tables, bypassing
     row-level security.
     """
-    statement = sqlparse.parse(sql)[0]
-    assert has_table_query(statement) == expected
+    assert has_table_query(sql) == expected
 
 
 @pytest.mark.parametrize(
@@ -1601,21 +1604,20 @@ def test_extract_table_references(mocker: MockerFixture) -> None:
         Table(table="other_table", schema=None, catalog=None),
     }
 
-    # test falling back to sqlparse
-    logger = mocker.patch("superset.sql_parse.logger")
-    sql = "SELECT * FROM table UNION ALL SELECT * FROM other_table"
+    sql = "SELECT * FROM 'table' UNION ALL SELECT * FROM other_table"
     assert extract_table_references(
         sql,
         "trino",
-    ) == {Table(table="other_table", schema=None, catalog=None)}
-    logger.warning.assert_called_once()
+    ) == {
+        Table(table="table", schema=None, catalog=None),
+        Table(table="other_table", schema=None, catalog=None),
+    }
 
-    logger = mocker.patch("superset.migrations.shared.utils.logger")
-    sql = "SELECT * FROM table UNION ALL SELECT * FROM other_table"
+    sql = "SELECT * FROM 'table' UNION ALL SELECT * FROM other_table"
     assert extract_table_references(sql, "trino", show_warning=False) == {
-        Table(table="other_table", schema=None, catalog=None)
+        Table(table="table", schema=None, catalog=None),
+        Table(table="other_table", schema=None, catalog=None),
     }
-    logger.warning.assert_not_called()
 
 
 def test_is_select() -> None: