From ecd9aa060f81097eecc14b52492355abe71a4108 Mon Sep 17 00:00:00 2001 From: Elizabeth Thompson Date: Thu, 19 Nov 2026 17:03:53 +0000 Subject: [PATCH 1/3] fix(api): return 422 instead of 500 when table_metadata reflection fails DatabaseRestApi.table_metadata calls db_engine_spec.get_table_metadata, which reflects pk/fk/index/comment info via SQLAlchemy. A database that declares the postgres dialect but doesn't fully implement its catalog (e.g. a postgres-wire-protocol engine missing pg_catalog.pg_index columns the postgres dialect now queries) raises a SQLAlchemyError here that was previously unhandled, crashing to a 500. The sibling deprecated endpoint already catches this the same way; bring the current endpoint in line. Co-Authored-By: Claude Sonnet 5 --- superset/databases/api.py | 26 ++++++++++++++++++- .../integration_tests/databases/api_tests.py | 21 +++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/superset/databases/api.py b/superset/databases/api.py index efea9d9690cc..59a2743a5baa 100644 --- a/superset/databases/api.py +++ b/superset/databases/api.py @@ -1196,6 +1196,8 @@ def table_metadata(self, pk: int) -> FlaskResponse: $ref: '#/components/responses/401' 404: $ref: '#/components/responses/404' + 422: + $ref: '#/components/responses/422' 500: $ref: '#/components/responses/500' """ @@ -1227,8 +1229,30 @@ def table_metadata(self, pk: int) -> FlaskResponse: # Partition info is engine-agnostic at this layer: the generic dispatch # passes it to the engine spec, which decides whether to use it. Non-ODPS # specs ignore the parameter. - payload = database.db_engine_spec.get_table_metadata(database, table, partition) + try: + payload = database.db_engine_spec.get_table_metadata( + database, table, partition + ) + except SQLAlchemyError as ex: + # Reflection (get_pk_constraint/get_foreign_keys/get_indexes/ + # get_table_comment) issues catalog queries tailored to the + # declared dialect. A customer database that only partially + # implements that dialect (e.g. a postgres-wire-protocol engine + # missing pg_catalog.pg_index columns SQLAlchemy's postgres + # dialect now queries) raises here - that's the target + # database's limitation, not a Superset bug: answer with a + # typed 422 and a warning instead of an unhandled 500 + ERROR, + # matching the sibling table_metadata_deprecated endpoint above. + self.incr_stats("error", self.table_metadata.__name__) + logger.warning( + "table_metadata: reflection failed for database=%s table=%s", + pk, + table_name, + exc_info=True, + ) + return self.response_422(error_msg_from_exception(ex)) + self.incr_stats("success", self.table_metadata.__name__) return self.response(200, **payload) @expose("//table_metadata/extra/", methods=["GET"]) diff --git a/tests/integration_tests/databases/api_tests.py b/tests/integration_tests/databases/api_tests.py index ecdfdd876f5d..82e03a151288 100644 --- a/tests/integration_tests/databases/api_tests.py +++ b/tests/integration_tests/databases/api_tests.py @@ -2069,6 +2069,27 @@ def test_get_table_metadata_no_db_permission(self): rv = self.client.get(uri) assert rv.status_code == 404 + def test_get_table_metadata_reflection_error(self): + """ + Database API: Test get table metadata returns 422, not 500, when the + target database's reflection (pk/fk/index/comment introspection) raises + a SQLAlchemyError, e.g. a postgres-wire-protocol engine missing catalog + columns SQLAlchemy's postgres dialect queries. + """ + example_db = get_example_database() + self.login(ADMIN_USERNAME) + uri = ( + f"api/v1/database/{example_db.id}/table_metadata/" + "?name=birth_names&schema=null" + ) + with mock.patch.object( + example_db.db_engine_spec, + "get_table_metadata", + side_effect=SQLAlchemyError("Binder Error: Referenced table not found"), + ): + rv = self.client.get(uri) + assert rv.status_code == 422 + @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") def test_get_table_extra_metadata_deprecated(self): """ From 66ed22a9d0a098481037e61bad4c015e67274e7b Mon Sep 17 00:00:00 2001 From: Elizabeth Thompson Date: Fri, 20 Nov 2026 17:03:25 +0000 Subject: [PATCH 2/3] docs(openapi): regenerate spec for table_metadata 422 response Co-Authored-By: Claude Sonnet 5.5 --- docs/static/resources/openapi.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/static/resources/openapi.json b/docs/static/resources/openapi.json index 8a31b8c39978..fd99b1b23353 100644 --- a/docs/static/resources/openapi.json +++ b/docs/static/resources/openapi.json @@ -24344,6 +24344,9 @@ "404": { "$ref": "#/components/responses/404" }, + "422": { + "$ref": "#/components/responses/422" + }, "500": { "$ref": "#/components/responses/500" } From bdea9335b2fefee28942f31b4284f01df64554ab Mon Sep 17 00:00:00 2001 From: Elizabeth Thompson Date: Mon, 23 Nov 2026 16:03:23 +0000 Subject: [PATCH 3/3] fix(databases): drop redundant manual statsd counters in table_metadata @statsd_metrics already records success/warning for the response, so the extra incr_stats calls double-counted. Also make a comment timeless. Co-Authored-By: Claude Sonnet 5.5 --- superset/databases/api.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/superset/databases/api.py b/superset/databases/api.py index 88474c591074..b691946ff20b 100644 --- a/superset/databases/api.py +++ b/superset/databases/api.py @@ -1240,11 +1240,10 @@ def table_metadata(self, pk: int) -> FlaskResponse: # declared dialect. A customer database that only partially # implements that dialect (e.g. a postgres-wire-protocol engine # missing pg_catalog.pg_index columns SQLAlchemy's postgres - # dialect now queries) raises here - that's the target + # dialect queries) raises here - that's the target # database's limitation, not a Superset bug: answer with a # typed 422 and a warning instead of an unhandled 500 + ERROR, # matching the sibling table_metadata_deprecated endpoint above. - self.incr_stats("error", self.table_metadata.__name__) logger.warning( "table_metadata: reflection failed for database=%s table=%s", pk, @@ -1253,7 +1252,6 @@ def table_metadata(self, pk: int) -> FlaskResponse: ) return self.response_422(error_msg_from_exception(ex)) - self.incr_stats("success", self.table_metadata.__name__) return self.response(200, **payload) @expose("//table_metadata/extra/", methods=["GET"])