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
40 changes: 32 additions & 8 deletions diffly/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,18 +550,26 @@ def num_rows_joined_unequal(self, *subset: str) -> int:
return lazy_len(self.joined_unequal(*subset, lazy=True))

@overload
def left_only(self, *, lazy: Literal[True]) -> pl.LazyFrame: ...
def left_only(
self, *, lazy: Literal[True], maintain_order: bool = False
) -> pl.LazyFrame: ...

@overload
def left_only(self, *, lazy: Literal[False] = False) -> pl.DataFrame: ...
def left_only(
self, *, lazy: Literal[False] = False, maintain_order: bool = False
) -> pl.DataFrame: ...

def left_only(self, *, lazy: bool = False) -> pl.DataFrame | pl.LazyFrame:
def left_only(
self, *, lazy: bool = False, maintain_order: bool = False
) -> pl.DataFrame | pl.LazyFrame:
"""The rows in the left data frame which cannot be joined with a row in the
right data frame.

Args:
lazy: If ``True``, return a lazy frame. Otherwise, return an eager frame
(default).
maintain_order: Whether to maintain the order of the left data frame in the
result.

Returns:
A data frame or lazy frame containing the rows that are only in the left data
Expand All @@ -585,7 +593,11 @@ def left_only(self, *, lazy: bool = False) -> pl.DataFrame | pl.LazyFrame:
"""
primary_key = self._check_primary_key()
result = self.left.join(
self.right, on=primary_key, how="anti", nulls_equal=True
self.right,
on=primary_key,
how="anti",
nulls_equal=True,
maintain_order="left" if maintain_order else None,
)
return result if lazy else result.collect()

Expand All @@ -606,18 +618,26 @@ def num_rows_left_only(self) -> int:
return lazy_len(self.left_only(lazy=True))

@overload
def right_only(self, *, lazy: Literal[True]) -> pl.LazyFrame: ...
def right_only(
self, *, lazy: Literal[True], maintain_order: bool = False
) -> pl.LazyFrame: ...

@overload
def right_only(self, *, lazy: Literal[False] = False) -> pl.DataFrame: ...
def right_only(
self, *, lazy: Literal[False] = False, maintain_order: bool = False
) -> pl.DataFrame: ...

def right_only(self, *, lazy: bool = False) -> pl.DataFrame | pl.LazyFrame:
def right_only(
self, *, lazy: bool = False, maintain_order: bool = False
) -> pl.DataFrame | pl.LazyFrame:
"""The rows in the right data frame which cannot be joined with a row in the
left data frame.

Args:
lazy: If ``True``, return a lazy frame. Otherwise, return an eager frame
(default).
maintain_order: Whether to maintain the order of the right data frame in the
result.

Returns:
A data frame or lazy frame containing the rows that are only in the right data
Expand All @@ -641,7 +661,11 @@ def right_only(self, *, lazy: bool = False) -> pl.DataFrame | pl.LazyFrame:
"""
primary_key = self._check_primary_key()
result = self.right.join(
self.left, on=primary_key, how="anti", nulls_equal=True
self.left,
on=primary_key,
how="anti",
nulls_equal=True,
maintain_order="left" if maintain_order else None,
)
return result if lazy else result.collect()

Expand Down
14 changes: 12 additions & 2 deletions diffly/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,13 +1167,23 @@ def _compute_sample_rows(
assert isinstance(pk, list)

if comp.num_rows_left_only() > 0:
df = comp.left_only(lazy=True).select(pk).head(sample_k_rows_only).collect()
df = (
comp.left_only(lazy=True, maintain_order=True)
.select(pk)
Comment thread
borchero marked this conversation as resolved.
.head(sample_k_rows_only)
.collect()
)
sample_left = [tuple(row) for row in df.iter_rows()]
else:
sample_left = []

if comp.num_rows_right_only() > 0:
df = comp.right_only(lazy=True).select(pk).head(sample_k_rows_only).collect()
df = (
comp.right_only(lazy=True, maintain_order=True)
.select(pk)
.head(sample_k_rows_only)
.collect()
)
sample_right = [tuple(row) for row in df.iter_rows()]
else:
sample_right = []
Expand Down
Loading
Loading