You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2026/07/21 11:27:32 UTC
(superset) branch master updated: fix(trino): quote nested ROW field paths per-segment in physical column expressions (#41946)
This is an automated email from the ASF dual-hosted git repository.
rusackas 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 f7517168675 fix(trino): quote nested ROW field paths per-segment in physical column expressions (#41946)
f7517168675 is described below
commit f75171686759f3d9dab63dd78024e92df9dceb87
Author: Evan Rusackas <ev...@preset.io>
AuthorDate: Tue Jul 21 04:27:24 2026 -0700
fix(trino): quote nested ROW field paths per-segment in physical column expressions (#41946)
Co-authored-by: Claude Opus 4.8 <no...@anthropic.com>
---
superset/connectors/sqla/models.py | 18 ++++-
superset/db_engine_specs/trino.py | 7 +-
superset/superset_typing.py | 6 ++
tests/unit_tests/connectors/sqla/models_test.py | 87 +++++++++++++++++++++++++
tests/unit_tests/db_engine_specs/test_trino.py | 5 ++
5 files changed, 119 insertions(+), 4 deletions(-)
diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py
index a834e043267..a68137e2611 100644
--- a/superset/connectors/sqla/models.py
+++ b/superset/connectors/sqla/models.py
@@ -2012,6 +2012,11 @@ class SqlaTable(
columns = []
for col in new_columns:
old_column = old_columns_by_name.pop(col["column_name"], None)
+ # Some engine specs (e.g. Trino, when expanding nested `ROW` columns)
+ # provide an explicit SQL expression that must be used to select the
+ # physical column, since simply quoting the dotted `column_name` as a
+ # single identifier is not valid syntax for these nested fields.
+ expression: str = col.get("expression") or ""
if not old_column:
results.added.append(col["column_name"])
new_column = TableColumn(
@@ -2024,12 +2029,14 @@ class SqlaTable(
if col.get("comment"):
new_column.description = col["comment"]
db_engine_spec.alter_new_orm_column(new_column)
+ if expression:
+ new_column.expression = expression
else:
new_column = old_column
if new_column.type != col["type"]:
results.modified.append(col["column_name"])
new_column.type = col["type"]
- new_column.expression = ""
+ new_column.expression = expression
# Set description from comment field if available
if col.get("comment"):
new_column.description = col["comment"]
@@ -2039,8 +2046,13 @@ class SqlaTable(
if not any_date_col and new_column.is_temporal:
any_date_col = col["column_name"]
- # add back calculated (virtual) columns
- columns.extend([col for col in old_columns if col.expression])
+ # Add back calculated (virtual) columns, i.e. those that weren't matched
+ # against `new_columns` above and are thus still present in
+ # `old_columns_by_name`. Columns that were matched are already appended to
+ # `columns` in the loop above, and re-adding them here (e.g. via `old_columns`)
+ # would duplicate any synced physical column that also carries a truthy
+ # `expression`, such as Trino's expanded nested `ROW` fields.
+ columns.extend([col for col in old_columns_by_name.values() if col.expression])
self.columns = columns
if not self.main_dttm_col:
diff --git a/superset/db_engine_specs/trino.py b/superset/db_engine_specs/trino.py
index 0747a8ce4a2..906db82eba0 100644
--- a/superset/db_engine_specs/trino.py
+++ b/superset/db_engine_specs/trino.py
@@ -602,7 +602,11 @@ class TrinoEngineSpec(PrestoBaseEngineSpec):
Expanded columns are named foo.bar.baz and we provide a query_as property to
instruct the base engine spec how to correctly query them: instead of quoting
the whole string they have to be quoted like "foo"."bar"."baz" and we then
- alias them to the full dotted string for ease of reference.
+ alias them to the full dotted string for ease of reference. We also provide
+ an `expression` property with the same correctly quoted path (without the
+ alias), so that a physical `TableColumn` created from this metadata selects
+ the column correctly instead of quoting the whole dotted name as a single
+ (invalid) identifier.
"""
# pylint: disable=import-outside-toplevel
from trino.sqlalchemy import datatype
@@ -625,6 +629,7 @@ class TrinoEngineSpec(PrestoBaseEngineSpec):
column_name=name,
type=inner_type,
is_dttm=is_dttm,
+ expression=query_name,
query_as=f'{query_name} AS "{name}"',
)
cols.extend(cls._expand_columns(inner_col))
diff --git a/superset/superset_typing.py b/superset/superset_typing.py
index 8081fc4675b..3b32d8c549c 100644
--- a/superset/superset_typing.py
+++ b/superset/superset_typing.py
@@ -132,6 +132,12 @@ class ResultSetColumnType(TypedDict):
query_as: NotRequired[Any]
+ # A SQL expression (without an alias) that should be used to select this
+ # column, e.g. for a nested field whose path segments must each be quoted
+ # separately rather than the whole dotted name being quoted as a single
+ # identifier.
+ expression: NotRequired[Any]
+
CacheConfig: TypeAlias = dict[str, Any]
DbapiDescriptionRow: TypeAlias = tuple[
diff --git a/tests/unit_tests/connectors/sqla/models_test.py b/tests/unit_tests/connectors/sqla/models_test.py
index 2909bf4f308..1d5d0dc3d13 100644
--- a/tests/unit_tests/connectors/sqla/models_test.py
+++ b/tests/unit_tests/connectors/sqla/models_test.py
@@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.
+from unittest.mock import MagicMock
+
import pandas as pd
import pytest
from pytest_mock import MockerFixture
@@ -542,6 +544,91 @@ def test_fetch_metadata_with_comment_field_existing_columns(
assert columns_by_name["name"].description == "Updated name description"
+def test_fetch_metadata_sets_expression_for_expanded_nested_columns(
+ mocker: MockerFixture,
+) -> None:
+ """
+ Test that fetch_metadata uses the `expression` hint provided by the db engine
+ spec (e.g. Trino's expansion of nested `ROW` columns via `expand_rows`) to set
+ the physical `TableColumn.expression`.
+
+ Without this, a nested column like `metadata.uuid` would have no expression,
+ causing SQLAlchemy to render the whole dotted `column_name` as a single quoted
+ identifier (`"metadata.uuid"`), which Trino rejects, instead of the correct
+ per-segment quoting (`"metadata"."uuid"`).
+
+ See: https://github.com/apache/superset/issues/27034
+ """
+ # Mock database
+ database: MagicMock = mocker.MagicMock()
+ database.get_metrics.return_value = []
+
+ # Mock db_engine_spec
+ mock_db_engine_spec: MagicMock = mocker.MagicMock()
+ mock_db_engine_spec.alter_new_orm_column = mocker.MagicMock()
+ database.db_engine_spec = mock_db_engine_spec
+
+ # Create table with a pre-existing (already synced) expanded column, to also
+ # cover the "sync columns from source" (re-fetch) code path
+ table: SqlaTable = SqlaTable(table_name="test_table_nested", database=database)
+ table.id = 1
+
+ existing_col: TableColumn = TableColumn(
+ column_name="metadata.uuid",
+ type="VARCHAR",
+ table=table,
+ expression="",
+ )
+ table.columns = [existing_col]
+
+ mock_columns: list[dict[str, str]] = [
+ {
+ "column_name": "metadata",
+ "type": "ROW",
+ },
+ {
+ "column_name": "metadata.uuid",
+ "type": "VARCHAR",
+ "expression": '"metadata"."uuid"',
+ "query_as": '"metadata"."uuid" AS "metadata.uuid"',
+ },
+ ]
+
+ mock_session: MagicMock = mocker.patch("superset.connectors.sqla.models.db.session")
+ mock_session.query.return_value.filter.return_value.all.return_value = [
+ existing_col
+ ]
+ mocker.patch.object(table, "external_metadata", return_value=mock_columns)
+ mocker.patch(
+ "superset.connectors.sqla.models.config", {"SQLA_TABLE_MUTATOR": lambda x: None}
+ )
+
+ table.fetch_metadata()
+
+ columns_by_name: dict[str, TableColumn] = {
+ col.column_name: col for col in table.columns
+ }
+ assert len(table.columns) == len(mock_columns)
+ assert not columns_by_name["metadata"].expression
+ assert columns_by_name["metadata.uuid"].expression == '"metadata"."uuid"'
+
+ # Re-run fetch_metadata a second time to simulate re-syncing columns from
+ # source. The previously synced physical column now carries a truthy
+ # `expression`, which must not cause it to be duplicated in `self.columns`.
+ mock_session_2 = mocker.patch("superset.connectors.sqla.models.db.session")
+ mock_session_2.query.return_value.filter.return_value.all.return_value = list(
+ table.columns
+ )
+ mocker.patch.object(table, "external_metadata", return_value=mock_columns)
+
+ table.fetch_metadata()
+
+ assert len(table.columns) == len(mock_columns)
+ columns_by_name = {col.column_name: col for col in table.columns}
+ assert not columns_by_name["metadata"].expression
+ assert columns_by_name["metadata.uuid"].expression == '"metadata"."uuid"'
+
+
def test_fetch_metadata_mixed_comment_scenarios(mocker: MockerFixture) -> None:
"""Test fetch_metadata with mix of new/existing columns and with/without
comments
diff --git a/tests/unit_tests/db_engine_specs/test_trino.py b/tests/unit_tests/db_engine_specs/test_trino.py
index 462c7efbebc..64b2ffa94a5 100644
--- a/tests/unit_tests/db_engine_specs/test_trino.py
+++ b/tests/unit_tests/db_engine_specs/test_trino.py
@@ -762,6 +762,7 @@ def test_get_columns_expand_rows(mocker: MockerFixture):
column_name="field1.a",
type=types.VARCHAR(),
is_dttm=False,
+ expression='"field1"."a"',
query_as='"field1"."a" AS "field1.a"',
),
ResultSetColumnType(
@@ -769,6 +770,7 @@ def test_get_columns_expand_rows(mocker: MockerFixture):
column_name="field1.b",
type=types.DATE(),
is_dttm=True,
+ expression='"field1"."b"',
query_as='"field1"."b" AS "field1.b"',
),
ResultSetColumnType(
@@ -779,6 +781,7 @@ def test_get_columns_expand_rows(mocker: MockerFixture):
column_name="field2.r1",
type=datatype.parse_sqltype("row(a varchar, b varchar)"),
is_dttm=False,
+ expression='"field2"."r1"',
query_as='"field2"."r1" AS "field2.r1"',
),
ResultSetColumnType(
@@ -786,6 +789,7 @@ def test_get_columns_expand_rows(mocker: MockerFixture):
column_name="field2.r1.a",
type=types.VARCHAR(),
is_dttm=False,
+ expression='"field2"."r1"."a"',
query_as='"field2"."r1"."a" AS "field2.r1.a"',
),
ResultSetColumnType(
@@ -793,6 +797,7 @@ def test_get_columns_expand_rows(mocker: MockerFixture):
column_name="field2.r1.b",
type=types.VARCHAR(),
is_dttm=False,
+ expression='"field2"."r1"."b"',
query_as='"field2"."r1"."b" AS "field2.r1.b"',
),
ResultSetColumnType(