Skip to content

Commit

Permalink
Remove option to return as list of tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
omkar-foss committed Aug 23, 2024
1 parent 46a2ebd commit 4c9edf3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 42 deletions.
12 changes: 3 additions & 9 deletions python/deltalake/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,25 +513,19 @@ def version(self) -> int:
def partitions(
self,
partition_filters: Optional[List[Tuple[str, str, Any]]] = None,
as_tuple_list: bool = False,
) -> List[Dict[str, str]] | List[Tuple[str]]:
) -> List[Dict[str, str]]:
"""
Returns the partitions as a list of dicts. Example: `[{'month': '1', 'year': '2020', 'day': '1'}, ...]`
Args:
partition_filters: The partition filters that will be used for getting the matched partitions, defaults to `None` (no filtering).
as_tuple_list: If `True`, returns the partitions as a list of tuples. Example: `[(("day", "5"), ("month", "4"), ("year", "2021")), ...]`
"""

partitions: List[Any] = []
partitions: List[Dict[str, str]] = []
for partition in self._table.get_active_partitions(partition_filters):
if not partition:
continue
if as_tuple_list:
sorted_partition = sorted(partition, key=lambda x: x[0])
partitions.append(tuple(sorted_partition))
else:
partitions.append({k: v for (k, v) in partition})
partitions.append({k: v for (k, v) in partition})
return partitions

def files(
Expand Down
42 changes: 9 additions & 33 deletions python/tests/test_table_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,33 +855,17 @@ def test_partitions_partitioned_table():
assert partition in actual


def test_partitions_tuples_partitioned_table():
table_path = "../crates/test/tests/data/delta-0.8.0-partitioned"
dt = DeltaTable(table_path)
expected = [
(("day", "5"), ("month", "2"), ("year", "2020")),
(("day", "1"), ("month", "1"), ("year", "2020")),
(("day", "5"), ("month", "4"), ("year", "2021")),
(("day", "3"), ("month", "2"), ("year", "2020")),
(("day", "20"), ("month", "12"), ("year", "2021")),
(("day", "4"), ("month", "12"), ("year", "2021")),
]
actual = dt.partitions(as_tuple_list=True)
assert len(expected) == len(actual)
for partition in expected:
partition in actual


def test_partitions_filtering_partitioned_table():
table_path = "../crates/test/tests/data/delta-0.8.0-partitioned"
dt = DeltaTable(table_path)
expected = [
(("day", "5"), ("month", "4"), ("year", "2021")),
(("day", "20"), ("month", "12"), ("year", "2021")),
(("day", "4"), ("month", "12"), ("year", "2021")),
{"day": "5", "month": "4", "year": "2021"},
{"day": "20", "month": "12", "year": "2021"},
{"day": "4", "month": "12", "year": "2021"},
]

partition_filters = [("year", ">=", "2021")]
actual = dt.partitions(partition_filters=partition_filters, as_tuple_list=True)
actual = dt.partitions(partition_filters=partition_filters)
assert len(expected) == len(actual)
for partition in expected:
partition in actual
Expand All @@ -891,18 +875,10 @@ def test_partitions_special_partitioned_table():
table_path = "../crates/test/tests/data/delta-0.8.0-special-partition"
dt = DeltaTable(table_path)

# Partitions as list of dicts (default).
expected_dict = [{"x": "A/A"}, {"x": "B B"}]
actual_dict = dt.partitions()
for partition in expected_dict:
partition in actual_dict

# Partitions as list of tuples.
expected_tuple = [[("x", "B B")], [("x", "A/A")]]
actual_tuple = dt.partitions(as_tuple_list=True)
assert len(expected_tuple) == len(actual_tuple)
for partition in expected_tuple:
partition in actual_tuple
expected = [{"x": "A/A"}, {"x": "B B"}]
actual = dt.partitions()
for partition in expected:
partition in actual


def test_partitions_unpartitioned_table():
Expand Down

0 comments on commit 4c9edf3

Please sign in to comment.