Skip to content

fix: catch no table error #32640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions superset/db_engine_specs/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
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
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
Expand Down Expand Up @@ -1257,26 +1259,31 @@ def get_extra_table_metadata(
) -> 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
Comment on lines +1284 to +1286
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Table Name in Error Message category Error Handling

Tell me more
What is the issue?

The error message doesn't include the table name or schema which makes it harder for users to identify which table is missing.

Why this matters

Without the table name in the error message, users have to spend more time debugging and identifying which table caused the error.

Suggested change ∙ Feature Preview

Include table details in the error message for better error reporting:

raise SupersetDBAPIProgrammingError(
                f"Table '{table.schema}.{table.table}' doesn't exist in the database"
            ) from ex

Report a problem with this comment

💬 Looking for more details? Reply to this comment to chat with Korbit.


metadata["view"] = cast(
Any,
Expand Down
19 changes: 19 additions & 0 deletions tests/integration_tests/db_engine_specs/presto_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
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
Expand Down Expand Up @@ -570,6 +572,23 @@ def test_presto_get_extra_table_metadata(self):
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"]}])
Expand Down
Loading