Skip to content

Commit

Permalink
fix: reorder encode_partition_value() checks and add tests (#1733)
Browse files Browse the repository at this point in the history
# Description
The `isinstance(val, datetime)` check was after `isinstance(val, date)`
which meant that it was never found. I added a test for each encoding
type.

---------

Co-authored-by: Robert Pack <[email protected]>
  • Loading branch information
ldacey and roeap authored Oct 17, 2023
1 parent 19e5cd5 commit 1860158
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions python/deltalake/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def encode_partition_value(val: Any) -> str:
return val
elif isinstance(val, (int, float)):
return str(val)
elif isinstance(val, date):
return val.isoformat()
elif isinstance(val, datetime):
return val.isoformat(sep=" ")
elif isinstance(val, date):
return val.isoformat()
elif isinstance(val, bytes):
return val.decode("unicode_escape", "backslashreplace")
else:
Expand Down
34 changes: 33 additions & 1 deletion python/tests/test_table_read.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os
from datetime import datetime
from datetime import date, datetime
from pathlib import Path
from threading import Barrier, Thread
from types import SimpleNamespace
from typing import Any
from unittest.mock import Mock

from packaging import version

from deltalake._util import encode_partition_value
from deltalake.exceptions import DeltaProtocolError
from deltalake.table import ProtocolVersions
from deltalake.writer import write_deltalake
Expand Down Expand Up @@ -695,3 +697,33 @@ def test_issue_1653_filter_bool_partition(tmp_path: Path):
)
== 1
)


@pytest.mark.parametrize(
"input_value, expected",
[
(True, "true"),
(False, "false"),
(1, "1"),
(1.5, "1.5"),
("string", "string"),
(date(2023, 10, 17), "2023-10-17"),
(datetime(2023, 10, 17, 12, 34, 56), "2023-10-17 12:34:56"),
(b"bytes", "bytes"),
([True, False], ["true", "false"]),
([1, 2], ["1", "2"]),
([1.5, 2.5], ["1.5", "2.5"]),
(["a", "b"], ["a", "b"]),
([date(2023, 10, 17), date(2023, 10, 18)], ["2023-10-17", "2023-10-18"]),
(
[datetime(2023, 10, 17, 12, 34, 56), datetime(2023, 10, 18, 12, 34, 56)],
["2023-10-17 12:34:56", "2023-10-18 12:34:56"],
),
([b"bytes", b"testbytes"], ["bytes", "testbytes"]),
],
)
def test_encode_partition_value(input_value: Any, expected: str) -> None:
if isinstance(input_value, list):
assert [encode_partition_value(val) for val in input_value] == expected
else:
assert encode_partition_value(input_value) == expected

0 comments on commit 1860158

Please sign in to comment.