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 2024/09/27 14:44:03 UTC

(superset) 01/01: chore: improve DML check

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

beto pushed a commit to branch improve-dml-check
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 6c14cf88f8292e9ab53205c3c2f214f197db6978
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Fri Sep 27 10:43:50 2024 -0400

    chore: improve DML check
---
 pyproject.toml        |  2 +-
 requirements/base.txt |  2 +-
 superset/sql_lab.py   | 29 +++++++++++++++++++++--------
 3 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/pyproject.toml b/pyproject.toml
index 2318f6fca1..e1f32afa78 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -89,7 +89,7 @@ dependencies = [
     "slack_sdk>=3.19.0, <4",
     "sqlalchemy>=1.4, <2",
     "sqlalchemy-utils>=0.38.3, <0.39",
-    "sqlglot>=23.0.2,<24",
+    "sqlglot>=25.24.0,<26",
     "sqlparse>=0.5.0",
     "tabulate>=0.8.9, <0.9",
     "typing-extensions>=4, <5",
diff --git a/requirements/base.txt b/requirements/base.txt
index 22540e3d7a..904b1a18fc 100644
--- a/requirements/base.txt
+++ b/requirements/base.txt
@@ -350,7 +350,7 @@ sqlalchemy-utils==0.38.3
     # via
     #   apache-superset
     #   flask-appbuilder
-sqlglot==23.6.3
+sqlglot==25.24.0
     # via apache-superset
 sqlparse==0.5.0
     # via apache-superset
diff --git a/superset/sql_lab.py b/superset/sql_lab.py
index 3d3b2898fa..8bea000eda 100644
--- a/superset/sql_lab.py
+++ b/superset/sql_lab.py
@@ -46,6 +46,7 @@ from superset.exceptions import (
     OAuth2RedirectError,
     SupersetErrorException,
     SupersetErrorsException,
+    SupersetParseError,
 )
 from superset.extensions import celery_app, event_logger
 from superset.models.core import Database
@@ -236,15 +237,27 @@ def execute_sql_statement(  # pylint: disable=too-many-statements, too-many-loca
     # We are testing to see if more rows exist than the limit.
     increased_limit = None if query.limit is None else query.limit + 1
 
-    parsed_statement = SQLStatement(sql_statement, engine=db_engine_spec.engine)
-    if parsed_statement.is_mutating() and not database.allow_dml:
-        raise SupersetErrorException(
-            SupersetError(
-                message=__("Only SELECT statements are allowed against this database."),
-                error_type=SupersetErrorType.DML_NOT_ALLOWED_ERROR,
-                level=ErrorLevel.ERROR,
+    if not database.allow_dml or True:
+        try:
+            parsed_statement = SQLStatement(sql_statement, engine=db_engine_spec.engine)
+            disallowed = parsed_statement.is_mutating()
+        except SupersetParseError:
+            # if we fail to parse teh query, disallow by default
+            disallowed = True
+
+        if disallowed:
+            raise SupersetErrorException(
+                SupersetError(
+                    message=__(
+                        "This database does not allow for DDL/DML, and the query "
+                        "could not be parsed to confirm it is a read-only query. Please "
+                        "contact your administrator for more assistance."
+                    ),
+                    error_type=SupersetErrorType.DML_NOT_ALLOWED_ERROR,
+                    level=ErrorLevel.ERROR,
+                )
             )
-        )
+
     if apply_ctas:
         if not query.tmp_table_name:
             start_dttm = datetime.fromtimestamp(query.start_time)