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 2025/03/11 00:00:11 UTC
(superset) 01/01: catch no table error
This is an automated email from the ASF dual-hosted git repository.
elizabeth pushed a commit to branch elizabeth/catch-presto-error
in repository https://gitbox.apache.org/repos/asf/superset.git
commit da2d7f05745cb6c0b19ce2294a3aec6be62b9d36
Author: Elizabeth Thompson <es...@gmail.com>
AuthorDate: Mon Mar 10 16:59:53 2025 -0700
catch no table error
---
superset/db_engine_specs/presto.py | 45 +++++++++++++---------
.../db_engine_specs/presto_tests.py | 19 +++++++++
2 files changed, 45 insertions(+), 19 deletions(-)
diff --git a/superset/db_engine_specs/presto.py b/superset/db_engine_specs/presto.py
index c3c8f61829..7d1d9b1b17 100644
--- a/superset/db_engine_specs/presto.py
+++ b/superset/db_engine_specs/presto.py
@@ -38,6 +38,7 @@ from sqlalchemy.engine.base import Engine
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.engine.result import Row as ResultRow
from sqlalchemy.engine.url import URL
+from sqlalchemy.exc import NoSuchTableError
from sqlalchemy.sql.expression import ColumnClause, Select
from superset import cache_manager, db, is_feature_enabled
@@ -45,6 +46,7 @@ from superset.common.db_query_status import QueryStatus
from superset.constants import TimeGrain
from superset.databases.utils import make_url_safe
from superset.db_engine_specs.base import BaseEngineSpec
+from superset.db_engine_specs.exceptions import SupersetDBAPIProgrammingError
from superset.errors import SupersetErrorType
from superset.exceptions import SupersetTemplateException
from superset.models.sql_lab import Query
@@ -1257,26 +1259,31 @@ class PrestoEngineSpec(PrestoBaseEngineSpec):
) -> dict[str, Any]:
metadata = {}
- if indexes := database.get_indexes(table):
- col_names, latest_parts = cls.latest_partition(
- database,
- table,
- show_first=True,
- indexes=indexes,
- )
-
- if not latest_parts:
- latest_parts = tuple([None] * len(col_names))
-
- metadata["partitions"] = {
- "cols": sorted(indexes[0].get("column_names", [])),
- "latest": dict(zip(col_names, latest_parts, strict=False)),
- "partitionQuery": cls._partition_query(
- table=table,
+ try:
+ if indexes := database.get_indexes(table):
+ col_names, latest_parts = cls.latest_partition(
+ database,
+ table,
+ show_first=True,
indexes=indexes,
- database=database,
- ),
- }
+ )
+
+ if not latest_parts:
+ latest_parts = tuple([None] * len(col_names))
+
+ metadata["partitions"] = {
+ "cols": sorted(indexes[0].get("column_names", [])),
+ "latest": dict(zip(col_names, latest_parts, strict=False)),
+ "partitionQuery": cls._partition_query(
+ table=table,
+ indexes=indexes,
+ database=database,
+ ),
+ }
+ except NoSuchTableError as ex:
+ raise SupersetDBAPIProgrammingError(
+ "Table doesn't seem to exist on the database"
+ ) from ex
metadata["view"] = cast(
Any,
diff --git a/tests/integration_tests/db_engine_specs/presto_tests.py b/tests/integration_tests/db_engine_specs/presto_tests.py
index c57bec8800..c8071835e6 100644
--- a/tests/integration_tests/db_engine_specs/presto_tests.py
+++ b/tests/integration_tests/db_engine_specs/presto_tests.py
@@ -19,8 +19,10 @@ from textwrap import dedent
from unittest import mock, skipUnless
import pandas as pd
+import pytest
from flask.ctx import AppContext
from sqlalchemy import types # noqa: F401
+from sqlalchemy.exc import NoSuchTableError
from sqlalchemy.sql import select
from superset.db_engine_specs.presto import PrestoEngineSpec
@@ -570,6 +572,23 @@ class TestPrestoDbEngineSpec(TestDbEngineSpec):
assert result["partitions"]["cols"] == ["ds", "hour"]
assert result["partitions"]["latest"] == {"ds": "01-01-19", "hour": 1}
+ def test_get_extra_table_metadata_no_table_found(self):
+ """
+ Test get_extra_table_metadata when a NoSuchTableError (simulating NoTableFound)
+ is raised by the database.get_df method.
+ """
+ # Setup a fake database
+ database = mock.MagicMock()
+ database.get_indexes.return_value = [] # No indexes
+ database.get_extra.return_value = {}
+ # Simulate that the table is not found
+ database.get_df.side_effect = NoSuchTableError("Table not found")
+
+ with pytest.raises(NoSuchTableError):
+ PrestoEngineSpec.get_extra_table_metadata(
+ database, Table("test_table", "test_schema")
+ )
+
def test_presto_where_latest_partition(self):
db = mock.Mock()
db.get_indexes = mock.Mock(return_value=[{"column_names": ["ds", "hour"]}])