Skip to content

Commit 4f1d735

Browse files
committed
Fix: Avoid type coercion when converting pandas dataframes to agate tables in the dbt adapter (#2897)
1 parent 04db61e commit 4f1d735

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

sqlmesh/dbt/util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ def _get_dbt_version() -> t.Tuple[int, int]:
1313
DBT_VERSION = _get_dbt_version()
1414

1515
if DBT_VERSION < (1, 8):
16-
from dbt.clients.agate_helper import table_from_data, empty_table, as_matrix # type: ignore # noqa: F401
16+
from dbt.clients.agate_helper import table_from_data_flat, empty_table, as_matrix # type: ignore # noqa: F401
1717
else:
18-
from dbt_common.clients.agate_helper import table_from_data, empty_table, as_matrix # type: ignore # noqa: F401
18+
from dbt_common.clients.agate_helper import table_from_data_flat, empty_table, as_matrix # type: ignore # noqa: F401
1919

2020

2121
def pandas_to_agate(df: pd.DataFrame) -> agate.Table:
2222
"""
2323
Converts a Pandas DataFrame to an Agate Table
2424
"""
2525

26-
return table_from_data(df.to_dict(orient="records"), df.columns.tolist())
26+
return table_from_data_flat(df.to_dict(orient="records"), df.columns.tolist())

tests/dbt/test_util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from __future__ import annotations
2+
3+
import pandas as pd
4+
from sqlmesh.dbt.util import pandas_to_agate
5+
6+
7+
def test_pandas_to_agate_type_coercion():
8+
df = pd.DataFrame({"data": ["_2024_01_01", "_2024_01_02", "_2024_01_03"]})
9+
agate_rows = pandas_to_agate(df).rows
10+
11+
values = [v[0] for v in agate_rows.values()]
12+
assert values == ["_2024_01_01", "_2024_01_02", "_2024_01_03"]

0 commit comments

Comments
 (0)