rusackas commented on code in PR #41946:
URL: https://github.com/apache/superset/pull/41946#discussion_r3564075521
##########
superset/connectors/sqla/models.py:
##########
@@ -2005,6 +2005,11 @@ def fetch_metadata(self) -> MetadataResult:
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 = col.get("expression") or ""
Review Comment:
Added the type annotation.
##########
tests/unit_tests/connectors/sqla/models_test.py:
##########
@@ -542,6 +542,72 @@ 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 = mocker.MagicMock()
+ database.get_metrics.return_value = []
+
+ # Mock db_engine_spec
+ mock_db_engine_spec = 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(table_name="test_table_nested", database=database)
+ table.id = 1
+
+ existing_col = TableColumn(
+ column_name="metadata.uuid",
+ type="VARCHAR",
+ table=table,
+ expression="",
+ )
+ table.columns = [existing_col]
+
+ mock_columns = [
+ {
+ "column_name": "metadata",
+ "type": "ROW",
+ },
+ {
+ "column_name": "metadata.uuid",
+ "type": "VARCHAR",
+ "expression": '"metadata"."uuid"',
+ "query_as": '"metadata"."uuid" AS "metadata.uuid"',
+ },
+ ]
Review Comment:
Added the type annotation.
##########
superset/connectors/sqla/models.py:
##########
@@ -2017,12 +2022,14 @@ def fetch_metadata(self) -> MetadataResult:
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
Review Comment:
Confirmed and fixed. Mutating `new_column.expression` in place also mutated
the same object still referenced by `old_columns`, so the final "add back
calculated columns" step re-added already-synced physical columns whenever they
had a truthy expression. Switched that step to pull only from the columns still
left unmatched in `old_columns_by_name`, and added a second fetch_metadata()
pass to the test to catch the duplication.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]