diff --git a/superset/db_engine_specs/base.py b/superset/db_engine_specs/base.py index 3767cdf541597..37e10e27419b8 100644 --- a/superset/db_engine_specs/base.py +++ b/superset/db_engine_specs/base.py @@ -743,7 +743,10 @@ def fetch_data( return cursor.fetchmany(limit) data = cursor.fetchall() description = cursor.description or [] - column_type_mutators = { + # Create a mapping between column name and a mutator function to normalize + # values with. The first two items in the description row are + # the column name and type. + column_mutators = { row[0]: func for row in description if ( @@ -752,11 +755,11 @@ def fetch_data( ) ) } - if column_type_mutators: + if column_mutators: indexes = {row[0]: idx for idx, row in enumerate(description)} for row_idx, row in enumerate(data): new_row = list(row) - for col, func in column_type_mutators.items(): + for col, func in column_mutators.items(): col_idx = indexes[col] new_row[col_idx] = func(row[col_idx]) data[row_idx] = tuple(new_row) diff --git a/tests/unit_tests/db_engine_specs/test_mysql.py b/tests/unit_tests/db_engine_specs/test_mysql.py index dad00034af05d..7e593e04c8f42 100644 --- a/tests/unit_tests/db_engine_specs/test_mysql.py +++ b/tests/unit_tests/db_engine_specs/test_mysql.py @@ -236,6 +236,11 @@ def test_get_schema_from_engine_params() -> None: [("dec", "decimal(12,6)"), ("str", "varchar(3)")], [(Decimal("1.23456"), "abc")], ), + ( + [[None, "abc"]], + [("dec", "decimal(12,6)"), ("str", "varchar(3)")], + [(None, "abc")], + ), ( [["1.23456", "abc"]], [("dec", "varchar(255)"), ("str", "varchar(3)")], diff --git a/tests/unit_tests/db_engine_specs/test_trino.py b/tests/unit_tests/db_engine_specs/test_trino.py index 7ddbd4f1f9f4a..d2e75a0842292 100644 --- a/tests/unit_tests/db_engine_specs/test_trino.py +++ b/tests/unit_tests/db_engine_specs/test_trino.py @@ -384,6 +384,11 @@ def test_handle_cursor_early_cancel( [("dec", "decimal(12,6)"), ("str", "varchar(3)")], [(Decimal("1.23456"), "abc")], ), + ( + [[None, "abc"]], + [("dec", "decimal(12,6)"), ("str", "varchar(3)")], + [(None, "abc")], + ), ( [["1.23456", "abc"]], [("dec", "varchar(255)"), ("str", "varchar(3)")],