Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix datetime conversion from date column #7

Merged
merged 1 commit into from
Sep 30, 2024
Merged
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
15 changes: 15 additions & 0 deletions dagster_mssql_bcp_tests/bcp_polars/test_bcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import pendulum
import tempfile
import datetime


class TestPolarsBCP:
Expand Down Expand Up @@ -368,6 +369,20 @@ def test_process_datetime(self, polars_io: polars_mssql_bcp.PolarsBCP):
df = polars_io._process_datetime(input, schema)
pl_testing.assert_frame_equal(df, expected)


schema = polars_mssql_bcp.AssetSchema(
[
{'name': 'a', 'type': 'DATETIME2'},
]
)

input = pl.date_range(datetime.date(2021,1,1), datetime.date(2021,1,3), eager=True).alias('a').to_frame()
df = polars_io._process_datetime(input, schema)
expected = pl.DataFrame(
{'a': ["2021-01-01 00:00:00+00:00", "2021-01-02 00:00:00+00:00", "2021-01-03 00:00:00+00:00"]}
)
pl_testing.assert_frame_equal(df, expected)

def test_save_csv(self, polars_io):
with tempfile.TemporaryDirectory() as dir:
polars_io._save_csv(
Expand Down
10 changes: 9 additions & 1 deletion src/dagster_mssql_bcp/bcp_polars/polars_mssql_bcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _process_datetime(
This is what BCP expects. 2024-01-01 00:00:00+00:00 from 2024-01-01T00:00:00Z
"""

dt_columns = data.select(cs.datetime()).columns
dt_columns = data.select(cs.datetime(), cs.date(), cs.time()).columns

data = data.with_columns(
[
Expand All @@ -99,6 +99,14 @@ def _process_datetime(
]
)

date_cols = data.select(cs.date()).columns
data = data.with_columns(
[
pl.col(_).cast(pl.Datetime)
for _ in date_cols
]
)

dt_columns_in_tz = data.select(cs.datetime(time_zone="*")).columns
data = data.with_columns(
[
Expand Down