Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dlt/sources/sql_database/schema_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ def sqla_col_to_column_schema(
col["scale"] = sql_t.scale
elif sql_t.decimal_return_scale is not None:
col["scale"] = sql_t.decimal_return_scale
elif isinstance(sql_t, sqltypes.Float):
# SQLAlchemy 2.1 makes Float (and REAL/DOUBLE) no longer a subclass of Numeric,
# so the Numeric branch above no longer matches; float types always assume "double"
col["data_type"] = "double"
elif isinstance(sql_t, sqltypes.SmallInteger):
col["data_type"] = "bigint"
if add_precision:
Expand Down
35 changes: 35 additions & 0 deletions tests/sources/sql_database/test_schema_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,38 @@ def test_get_table_references() -> None:

refs = get_table_references(child)
assert refs == []


@pytest.mark.parametrize(
"sql_type",
[sa.Float(), sa.REAL()],
ids=["float", "real"],
)
def test_float_types_mapped_to_double(sql_type: sa.types.TypeEngine) -> None:
"""FLOAT/REAL/DOUBLE map to data_type="double", incl. SA 2.1 where Float is not a Numeric subclass."""
metadata = sa.MetaData()
table = sa.Table("t", metadata, sa.Column("col", sql_type))
col_schema = sqla_col_to_column_schema(table.c.col, "full")
assert col_schema is not None
assert col_schema["data_type"] == "double"


def test_double_mapped_to_double_sa2() -> None:
"""sa.Double (SA >= 2.0) also maps to data_type="double"."""
sa2 = pytest.importorskip("sqlalchemy", minversion="2.0")
metadata = sa2.MetaData()
table = sa2.Table("t", metadata, sa2.Column("col", sa2.Double()))
col_schema = sqla_col_to_column_schema(table.c.col, "full")
assert col_schema is not None
assert col_schema["data_type"] == "double"


def test_numeric_mapped_to_decimal_not_shadowed() -> None:
"""Numeric(10, 2) still maps to data_type="decimal" with precision/scale."""
metadata = sa.MetaData()
table = sa.Table("t", metadata, sa.Column("col", sa.Numeric(10, 2)))
col_schema = sqla_col_to_column_schema(table.c.col, "full")
assert col_schema is not None
assert col_schema["data_type"] == "decimal"
assert col_schema["precision"] == 10
assert col_schema["scale"] == 2