You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by el...@apache.org on 2026/07/14 20:44:48 UTC

(superset) branch master updated: fix(databricks): classify insufficient-permissions errors as 4xx (#41945)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new d68e84e7315 fix(databricks): classify insufficient-permissions errors as 4xx (#41945)
d68e84e7315 is described below

commit d68e84e7315049325a47270777d35adaeb32b6bb
Author: Elizabeth Thompson <es...@gmail.com>
AuthorDate: Tue Jul 14 13:44:41 2026 -0700

    fix(databricks): classify insufficient-permissions errors as 4xx (#41945)
    
    Co-authored-by: Claude <no...@anthropic.com>
---
 superset/db_engine_specs/databricks.py             |  58 ++++----
 superset/sqllab/sql_json_executer.py               |   8 +-
 .../unit_tests/db_engine_specs/test_databricks.py  |  74 ++++++++++
 tests/unit_tests/sqllab/test_sql_json_executer.py  | 150 +++++++++++++++++++++
 4 files changed, 256 insertions(+), 34 deletions(-)

diff --git a/superset/db_engine_specs/databricks.py b/superset/db_engine_specs/databricks.py
index d4b1f23444c..255a8c15909 100644
--- a/superset/db_engine_specs/databricks.py
+++ b/superset/db_engine_specs/databricks.py
@@ -17,7 +17,9 @@
 from __future__ import annotations
 
 import logging
+import re
 from datetime import datetime
+from re import Pattern
 from typing import Any, Callable, cast, TYPE_CHECKING, TypedDict, Union
 
 from apispec import APISpec
@@ -56,6 +58,10 @@ if TYPE_CHECKING:
 
 logger = logging.getLogger(__name__)
 
+INSUFFICIENT_PERMISSIONS_REGEX: Pattern[str] = re.compile(
+    r"\[INSUFFICIENT_PERMISSIONS\]|\bSQLSTATE:\s*42501\b"
+)
+
 
 try:
     from databricks.sql.utils import ParamEscaper
@@ -263,6 +269,25 @@ class DatabricksBaseEngineSpec(BaseEngineSpec):
     def epoch_to_dttm(cls) -> str:
         return HiveEngineSpec.epoch_to_dttm()
 
+    @classmethod
+    def extract_errors(
+        cls,
+        ex: Exception,
+        context: dict[str, Any] | None = None,
+        database_name: str | None = None,
+    ) -> list[SupersetError]:
+        raw_message = cls._extract_error_message(ex)
+        if INSUFFICIENT_PERMISSIONS_REGEX.search(raw_message):
+            return [
+                SupersetError(
+                    error_type=SupersetErrorType.CONNECTION_DATABASE_PERMISSIONS_ERROR,
+                    message=raw_message,
+                    level=ErrorLevel.WARNING,
+                    extra={"engine_name": cls.engine_name},
+                )
+            ]
+        return super().extract_errors(ex, context, database_name)
+
 
 class DatabricksODBCEngineSpec(DatabricksBaseEngineSpec):
     """Databricks engine spec using ODBC driver for SQL Endpoints."""
@@ -490,46 +515,15 @@ class DatabricksDynamicBaseEngineSpec(BasicParametersMixin, DatabricksBaseEngine
         context: dict[str, Any] | None = None,
         database_name: str | None = None,
     ) -> list[SupersetError]:
-        raw_message = cls._extract_error_message(ex)
-
         context = context or {}
 
         # access_token isn't currently parseable from the
         # databricks error response, but adding it in here
         # for reference if their error message changes
-
         for key, value in cls.context_key_mapping.items():
             context[key] = context.get(value)
 
-        db_engine_custom_errors = cls.get_database_custom_errors(database_name)
-        if not isinstance(db_engine_custom_errors, dict):
-            db_engine_custom_errors = {}
-
-        for regex, (message, error_type, extra) in [
-            *db_engine_custom_errors.items(),
-            *cls.custom_errors.items(),
-        ]:
-            match = regex.search(raw_message)
-            if match:
-                params = {**context, **match.groupdict()}
-                extra["engine_name"] = cls.engine_name
-                return [
-                    SupersetError(
-                        error_type=error_type,
-                        message=message % params,
-                        level=ErrorLevel.ERROR,
-                        extra=extra,
-                    )
-                ]
-
-        return [
-            SupersetError(
-                error_type=SupersetErrorType.GENERIC_DB_ENGINE_ERROR,
-                message=cls._extract_error_message(ex),
-                level=ErrorLevel.ERROR,
-                extra={"engine_name": cls.engine_name},
-            )
-        ]
+        return super().extract_errors(ex, context, database_name)
 
     @classmethod
     def validate_parameters(  # type: ignore
diff --git a/superset/sqllab/sql_json_executer.py b/superset/sqllab/sql_json_executer.py
index d65dbc071d0..690e0d1b682 100644
--- a/superset/sqllab/sql_json_executer.py
+++ b/superset/sqllab/sql_json_executer.py
@@ -107,9 +107,13 @@ class SynchronousSqlJsonExecutor(SqlJsonExecutorBase):
         if data.get("status") == QueryStatus.FAILED:  # type: ignore
             # new error payload with rich context
             if data["errors"]:  # type: ignore
-                raise SupersetErrorsException(
-                    [SupersetError(**params) for params in data["errors"]]  # type: ignore
+                errors = [SupersetError(**params) for params in data["errors"]]  # type: ignore
+                status = (
+                    500
+                    if any(error.level == ErrorLevel.ERROR for error in errors)
+                    else 400
                 )
+                raise SupersetErrorsException(errors, status=status)
             # old string-only error message
             raise SupersetGenericDBErrorException(data["error"])  # type: ignore
 
diff --git a/tests/unit_tests/db_engine_specs/test_databricks.py b/tests/unit_tests/db_engine_specs/test_databricks.py
index f7cfaa5c663..e8fd643a229 100644
--- a/tests/unit_tests/db_engine_specs/test_databricks.py
+++ b/tests/unit_tests/db_engine_specs/test_databricks.py
@@ -235,6 +235,80 @@ def test_extract_errors_with_context() -> None:
     ]
 
 
+@pytest.mark.parametrize(
+    "msg",
+    [
+        # tag only
+        "[INSUFFICIENT_PERMISSIONS] Insufficient privileges: User does not "
+        "have USE CATALOG on Catalog 'platform_production'.",
+        # SQLSTATE only
+        "Insufficient privileges: User does not have USE CATALOG on Catalog "
+        "'platform_production'. SQLSTATE: 42501.",
+        # SQLSTATE with irregular spacing
+        "Insufficient privileges on Catalog 'platform_production'. SQLSTATE:42501",
+        # both, as seen in real Databricks responses
+        "[INSUFFICIENT_PERMISSIONS] Insufficient privileges: User does not "
+        "have USE CATALOG on Catalog 'platform_production'. SQLSTATE: 42501.",
+    ],
+)
+def test_extract_errors_insufficient_permissions(msg: str) -> None:
+    """
+    Test that Databricks catalog/schema permission errors are classified as
+    a client-side, warning-level error instead of a generic server error,
+    regardless of which half of the pattern is present in the raw message.
+    """
+    result = DatabricksNativeEngineSpec.extract_errors(Exception(msg))
+
+    assert result == [
+        SupersetError(
+            message=msg,
+            error_type=SupersetErrorType.CONNECTION_DATABASE_PERMISSIONS_ERROR,
+            level=ErrorLevel.WARNING,
+            extra={
+                "engine_name": "Databricks (legacy)",
+                "issue_codes": [
+                    {
+                        "code": 1017,
+                        "message": "Issue 1017 - User doesn't have the proper permissions.",  # noqa: E501
+                    }
+                ],
+            },
+        )
+    ]
+
+
+def test_extract_errors_insufficient_permissions_covers_odbc() -> None:
+    """
+    The insufficient-permissions classification lives on the shared
+    ``DatabricksBaseEngineSpec`` so that the ODBC ("SQL Endpoint") connector
+    benefits too, not just the native/Python-connector engines.
+    """
+    from superset.db_engine_specs.databricks import DatabricksODBCEngineSpec
+
+    msg = (
+        "[INSUFFICIENT_PERMISSIONS] Insufficient privileges: User does not "
+        "have USE CATALOG on Catalog 'platform_production'. SQLSTATE: 42501."
+    )
+    result = DatabricksODBCEngineSpec.extract_errors(Exception(msg))
+
+    assert result == [
+        SupersetError(
+            message=msg,
+            error_type=SupersetErrorType.CONNECTION_DATABASE_PERMISSIONS_ERROR,
+            level=ErrorLevel.WARNING,
+            extra={
+                "engine_name": "Databricks SQL Endpoint",
+                "issue_codes": [
+                    {
+                        "code": 1017,
+                        "message": "Issue 1017 - User doesn't have the proper permissions.",  # noqa: E501
+                    }
+                ],
+            },
+        )
+    ]
+
+
 @pytest.mark.parametrize(
     "target_type,expected_result",
     [
diff --git a/tests/unit_tests/sqllab/test_sql_json_executer.py b/tests/unit_tests/sqllab/test_sql_json_executer.py
new file mode 100644
index 00000000000..72275ce24f1
--- /dev/null
+++ b/tests/unit_tests/sqllab/test_sql_json_executer.py
@@ -0,0 +1,150 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from typing import Any
+from unittest.mock import MagicMock
+
+import pytest
+
+from superset.exceptions import SupersetErrorsException
+from superset.sqllab.sql_json_executer import SynchronousSqlJsonExecutor
+from superset.utils.core import QueryStatus
+
+
+def _make_executor(data: dict[str, Any]) -> SynchronousSqlJsonExecutor:
+    query_dao = MagicMock()
+    get_sql_results_task = MagicMock(return_value=data)
+    return SynchronousSqlJsonExecutor(
+        query_dao,
+        get_sql_results_task,
+        timeout_duration_in_seconds=60,
+        sqllab_backend_persistence_feature_enable=False,
+    )
+
+
+def _make_execution_context() -> MagicMock:
+    execution_context = MagicMock()
+    execution_context.query.id = 1
+    execution_context.expand_data = False
+    execution_context.select_as_cta = False
+    return execution_context
+
+
+def test_execute_raises_400_when_all_errors_are_warnings() -> None:
+    """
+    All-WARNING rich errors (e.g. a Databricks insufficient-permissions
+    error) should surface as a 4xx client error, not a 500.
+    """
+    data = {
+        "status": QueryStatus.FAILED,
+        "errors": [
+            {
+                "message": "Insufficient privileges on Catalog 'foo'.",
+                "error_type": "CONNECTION_DATABASE_PERMISSIONS_ERROR",
+                "level": "warning",
+                "extra": {},
+            }
+        ],
+    }
+    executor = _make_executor(data)
+
+    with pytest.raises(SupersetErrorsException) as excinfo:
+        executor.execute(_make_execution_context(), "SELECT 1", None)
+
+    assert excinfo.value.status == 400
+
+
+def test_execute_raises_400_when_errors_are_warning_and_info() -> None:
+    """
+    A mix of WARNING and INFO errors (no ERROR-level entries) should still
+    surface as a 4xx, since nothing in the list indicates a server fault.
+    """
+    data = {
+        "status": QueryStatus.FAILED,
+        "errors": [
+            {
+                "message": "Insufficient privileges on Catalog 'foo'.",
+                "error_type": "CONNECTION_DATABASE_PERMISSIONS_ERROR",
+                "level": "warning",
+                "extra": {},
+            },
+            {
+                "message": "For your information.",
+                "error_type": "GENERIC_DB_ENGINE_ERROR",
+                "level": "info",
+                "extra": {},
+            },
+        ],
+    }
+    executor = _make_executor(data)
+
+    with pytest.raises(SupersetErrorsException) as excinfo:
+        executor.execute(_make_execution_context(), "SELECT 1", None)
+
+    assert excinfo.value.status == 400
+
+
+def test_execute_raises_500_when_any_error_is_error_level() -> None:
+    """
+    Existing behavior is preserved: a single ERROR-level rich error still
+    surfaces as a 500.
+    """
+    data = {
+        "status": QueryStatus.FAILED,
+        "errors": [
+            {
+                "message": "Something went wrong.",
+                "error_type": "GENERIC_DB_ENGINE_ERROR",
+                "level": "error",
+                "extra": {},
+            },
+        ],
+    }
+    executor = _make_executor(data)
+
+    with pytest.raises(SupersetErrorsException) as excinfo:
+        executor.execute(_make_execution_context(), "SELECT 1", None)
+
+    assert excinfo.value.status == 500
+
+
+def test_execute_raises_500_when_errors_are_mixed_warning_and_error() -> None:
+    """
+    A mix of WARNING and ERROR still surfaces as a 500: any ERROR wins.
+    """
+    data = {
+        "status": QueryStatus.FAILED,
+        "errors": [
+            {
+                "message": "Insufficient privileges on Catalog 'foo'.",
+                "error_type": "CONNECTION_DATABASE_PERMISSIONS_ERROR",
+                "level": "warning",
+                "extra": {},
+            },
+            {
+                "message": "Something went wrong.",
+                "error_type": "GENERIC_DB_ENGINE_ERROR",
+                "level": "error",
+                "extra": {},
+            },
+        ],
+    }
+    executor = _make_executor(data)
+
+    with pytest.raises(SupersetErrorsException) as excinfo:
+        executor.execute(_make_execution_context(), "SELECT 1", None)
+
+    assert excinfo.value.status == 500