From 31f5340412c49a2e0bbe8cb00acfc20d2932a0c8 Mon Sep 17 00:00:00 2001 From: Balthazar Rouberol Date: Wed, 24 Jan 2024 15:23:52 +0100 Subject: [PATCH] [FIX-25636] fix: re-enable schema previsualization for Trino/Presto table/schemas This patch fixes failures occuring when performing a schema preview of a Presto table. The `PrestoBaseEngineSpec.where_latest_partition` attempts to construct SQLAlchemy `Column` objects based on a name and a type. However, this leads to the following error in our case: ```console sqlalchemy.exc.ArgumentError: 'SchemaItem' object, such as a 'Column' or a 'Constraint' expected, got 'VARCHAR' ``` This comes from the fact that we run `Column('column_name', 'VARCHAR')` instead of `Column('column_name', sqlalchemy.types.VARCHAR)`. We fix this particular error by passing the _actual_ type class, and not just a string. > [!NOTE] > This also fixes the same issue for Trino tables, as `TrinoEngineSpec` inherits > from `PrestoBaseEngineSpec`, the Presto db client class. Fixes #25962 Fixes #25962 --- superset/db_engine_specs/presto.py | 5 ++++- tests/unit_tests/db_engine_specs/test_presto.py | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/superset/db_engine_specs/presto.py b/superset/db_engine_specs/presto.py index 44f8f9668a224..561c91cbb65c7 100644 --- a/superset/db_engine_specs/presto.py +++ b/superset/db_engine_specs/presto.py @@ -507,7 +507,10 @@ def where_latest_partition( # pylint: disable=too-many-arguments } for col_name, value in zip(col_names, values): - col_type = column_type_by_name.get(col_name) + col_type = None + if col_type_name := column_type_by_name.get(col_name): + if col_type_class := getattr(types, col_type_name, None): + col_type = col_type_class() if isinstance(col_type, types.DATE): col_type = Date() diff --git a/tests/unit_tests/db_engine_specs/test_presto.py b/tests/unit_tests/db_engine_specs/test_presto.py index 8d57d4ed1a8c3..04eade7fd1681 100644 --- a/tests/unit_tests/db_engine_specs/test_presto.py +++ b/tests/unit_tests/db_engine_specs/test_presto.py @@ -115,10 +115,10 @@ def test_get_schema_from_engine_params() -> None: @pytest.mark.parametrize( ["column_type", "column_value", "expected_value"], [ - (types.DATE(), "2023-05-01", "DATE '2023-05-01'"), - (types.TIMESTAMP(), "2023-05-01", "TIMESTAMP '2023-05-01'"), - (types.VARCHAR(), "2023-05-01", "'2023-05-01'"), - (types.INT(), 1234, "1234"), + ("DATE", "2023-05-01", "DATE '2023-05-01'"), + ("TIMESTAMP", "2023-05-01", "TIMESTAMP '2023-05-01'"), + ("VARCHAR", "2023-05-01", "'2023-05-01'"), + ("INT", 1234, "1234"), ], ) def test_where_latest_partition(