Skip to content
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
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Features
* Create explicit schemas for Polars dataframes, handling stringified invalid dates.


Bug Fixes
--------
* Display DATE and TIME scalars returned by `.|` transforms.


Documentation
--------
* Note transform limitations when the result contains invalid dates.
Expand Down
2 changes: 2 additions & 0 deletions mycli/packages/dataframes/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def _is_coerceable_scalarish(value: Any) -> bool:
complex,
bytes,
str,
datetime.date,
datetime.datetime,
datetime.time,
datetime.timedelta,
decimal.Decimal,
fractions.Fraction,
Expand Down
42 changes: 42 additions & 0 deletions mycli_test/pytests/test_polars_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,48 @@ def test_run_polars_transform_coerces_scalar_to_dataframe(
assert result == SQLResult(header=[column_name], rows=[(value,)])


@pytest.mark.parametrize(
('values', 'max_value'),
[
([datetime.date(2026, 1, 2), datetime.date(2026, 3, 4)], datetime.date(2026, 3, 4)),
([datetime.time(9, 30), datetime.time(17, 5)], datetime.time(17, 5)),
([datetime.datetime(2026, 1, 2, 3, 4), datetime.datetime(2026, 3, 4, 5, 6)], datetime.datetime(2026, 3, 4, 5, 6)),
([datetime.timedelta(minutes=30), datetime.timedelta(hours=1)], datetime.timedelta(hours=1)),
],
)
def test_run_polars_transform_coerces_temporal_scalar_to_dataframe(values: list[Any], max_value: Any) -> None:
pl = pytest.importorskip('polars')
expression = "df['d'].max()"
transform = PolarsTransform('SELECT d FROM t', expression, compile(expression, '<test>', 'eval'), pl, None)

result = run_polars_transform(
transform,
iter([SQLResult(header=['d'], rows=[(value,) for value in values])]),
)

assert result == SQLResult(header=[str(max_value)], rows=[(max_value,)])


@pytest.mark.parametrize(
('mysql_type', 'rows', 'max_value'),
[
(FIELD_TYPE.DATE, [(datetime.date(2026, 1, 2),), (None,), (datetime.date(2026, 3, 4),)], datetime.date(2026, 3, 4)),
(FIELD_TYPE.TIME, [(datetime.time(9, 30),), (None,), (datetime.time(17, 5),)], datetime.time(17, 5)),
],
)
def test_run_polars_transform_coerces_cursor_temporal_scalar_to_dataframe(mysql_type: int, rows: list[tuple[Any]], max_value: Any) -> None:
pl = pytest.importorskip('polars')
cursor = MagicMock(spec=Cursor)
cursor.description = [('d', mysql_type, None, None, None, None, True)]
cursor.__iter__.return_value = iter(rows)
expression = "df['d'].max()"
transform = PolarsTransform('SELECT d FROM t', expression, compile(expression, '<test>', 'eval'), pl, None)

result = run_polars_transform(transform, [SQLResult(header=['d'], rows=cursor)])

assert result == SQLResult(header=[str(max_value)], rows=[(max_value,)])


def test_run_polars_transform_reports_invalid_scalar() -> None:
transform = PolarsTransform(
sql='SELECT id FROM orders',
Expand Down
Loading