Skip to content
Closed
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
82 changes: 80 additions & 2 deletions python/pyspark/sql/connect/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ def union(self, other: "DataFrame") -> "DataFrame":
def unionAll(self, other: "DataFrame") -> "DataFrame":
if other._plan is None:
raise ValueError("Argument to Union does not contain a valid plan.")
return DataFrame.withPlan(plan.UnionAll(self._plan, other._plan), session=self._session)
return DataFrame.withPlan(
plan.SetOperation(self._plan, other._plan, "union", is_all=True), session=self._session
)

def unionByName(self, other: "DataFrame", allowMissingColumns: bool = False) -> "DataFrame":
"""Returns a new :class:`DataFrame` containing union of rows in this and another
Expand All @@ -317,7 +319,83 @@ def unionByName(self, other: "DataFrame", allowMissingColumns: bool = False) ->
if other._plan is None:
raise ValueError("Argument to UnionByName does not contain a valid plan.")
return DataFrame.withPlan(
plan.UnionAll(self._plan, other._plan, allowMissingColumns), session=self._session
plan.SetOperation(
self._plan, other._plan, "union", is_all=True, by_name=allowMissingColumns
),
session=self._session,
)

def exceptAll(self, other: "DataFrame") -> "DataFrame":
"""Return a new :class:`DataFrame` containing rows in this :class:`DataFrame` but
not in another :class:`DataFrame` while preserving duplicates.

This is equivalent to `EXCEPT ALL` in SQL.
As standard in SQL, this function resolves columns by position (not by name).

.. versionadded:: 3.4.0

Parameters
----------
other : :class:`DataFrame`
The other :class:`DataFrame` to compare to.

Returns
-------
:class:`DataFrame`
"""
return DataFrame.withPlan(
plan.SetOperation(self._plan, other._plan, "except", is_all=True), session=self._session
)

def intersect(self, other: "DataFrame") -> "DataFrame":
"""Return a new :class:`DataFrame` containing rows only in
both this :class:`DataFrame` and another :class:`DataFrame`.
Note that any duplicates are removed. To preserve duplicates
use :func:`intersectAll`.

.. versionadded:: 3.4.0

Parameters
----------
other : :class:`DataFrame`
Another :class:`DataFrame` that needs to be combined.

Returns
-------
:class:`DataFrame`
Combined DataFrame.

Notes
-----
This is equivalent to `INTERSECT` in SQL.
"""
return DataFrame.withPlan(
plan.SetOperation(self._plan, other._plan, "intersect", is_all=False),
session=self._session,
)

def intersectAll(self, other: "DataFrame") -> "DataFrame":
"""Return a new :class:`DataFrame` containing rows in both this :class:`DataFrame`
and another :class:`DataFrame` while preserving duplicates.

This is equivalent to `INTERSECT ALL` in SQL. As standard in SQL, this function
resolves columns by position (not by name).

.. versionadded:: 3.4.0

Parameters
----------
other : :class:`DataFrame`
Another :class:`DataFrame` that needs to be combined.

Returns
-------
:class:`DataFrame`
Combined DataFrame.
"""
return DataFrame.withPlan(
plan.SetOperation(self._plan, other._plan, "intersect", is_all=True),
session=self._session,
)

def where(self, condition: Expression) -> "DataFrame":
Expand Down
38 changes: 30 additions & 8 deletions python/pyspark/sql/connect/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,21 +605,43 @@ def _repr_html_(self) -> str:
"""


class UnionAll(LogicalPlan):
class SetOperation(LogicalPlan):
def __init__(
self, child: Optional["LogicalPlan"], other: "LogicalPlan", by_name: bool = False
self,
child: Optional["LogicalPlan"],
other: Optional["LogicalPlan"],
set_op: str,
is_all: bool = True,
by_name: bool = False,
) -> None:
super().__init__(child)
self.other = other
self.by_name = by_name
self.is_all = is_all
self.set_op = set_op

def plan(self, session: Optional["RemoteSparkSession"]) -> proto.Relation:
assert self._child is not None
rel = proto.Relation()
rel.set_op.left_input.CopyFrom(self._child.plan(session))
rel.set_op.right_input.CopyFrom(self.other.plan(session))
rel.set_op.set_op_type = proto.SetOperation.SET_OP_TYPE_UNION
rel.set_op.is_all = True
if self._child is not None:
rel.set_op.left_input.CopyFrom(self._child.plan(session))
if self.other is not None:
rel.set_op.right_input.CopyFrom(self.other.plan(session))
if self.set_op == "union":
rel.set_op.set_op_type = proto.SetOperation.SET_OP_TYPE_UNION
elif self.set_op == "intersect":
rel.set_op.set_op_type = proto.SetOperation.SET_OP_TYPE_INTERSECT
elif self.set_op == "except":
rel.set_op.set_op_type = proto.SetOperation.SET_OP_TYPE_EXCEPT
else:
raise NotImplementedError(
"""
Unsupported set operation type: %s.
"""
% rel.set_op.set_op_type
)

rel.set_op.is_all = self.is_all
rel.set_op.by_name = self.by_name
return rel

Expand All @@ -631,7 +653,7 @@ def print(self, indent: int = 0) -> str:
o = " " * (indent + LogicalPlan.INDENT)
n = indent + LogicalPlan.INDENT * 2
return (
f"{i}UnionAll\n{o}child1=\n{self._child.print(n)}"
f"{i}SetOperation\n{o}child1=\n{self._child.print(n)}"
f"\n{o}child2=\n{self.other.print(n)}"
)

Expand All @@ -642,7 +664,7 @@ def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Union</b><br />
<b>SetOperation</b><br />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq: why don't we have Except, Union, etc separately?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HyukjinKwon I hope this discussion can answer your question #38166 (comment)....

Left: {self._child._repr_html_()}
Right: {self.other._repr_html_()}
</li>
Expand Down
22 changes: 22 additions & 0 deletions python/pyspark/sql/tests/connect/test_connect_plan_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,32 @@ def test_union(self):
df2 = self.connect.readTable(table_name=self.tbl_name)
plan1 = df1.union(df2)._plan.to_proto(self.connect)
self.assertTrue(plan1.root.set_op.is_all)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_UNION, plan1.root.set_op.set_op_type)
plan2 = df1.union(df2)._plan.to_proto(self.connect)
self.assertTrue(plan2.root.set_op.is_all)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_UNION, plan2.root.set_op.set_op_type)
plan3 = df1.unionByName(df2, True)._plan.to_proto(self.connect)
self.assertTrue(plan3.root.set_op.by_name)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_UNION, plan3.root.set_op.set_op_type)

def test_except(self):
# SPARK-41010: test `except` API for Python client.
df1 = self.connect.readTable(table_name=self.tbl_name)
df2 = self.connect.readTable(table_name=self.tbl_name)
plan1 = df1.exceptAll(df2)._plan.to_proto(self.connect)
self.assertTrue(plan1.root.set_op.is_all)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_EXCEPT, plan1.root.set_op.set_op_type)

def test_intersect(self):
# SPARK-41010: test `intersect` API for Python client.
df1 = self.connect.readTable(table_name=self.tbl_name)
df2 = self.connect.readTable(table_name=self.tbl_name)
plan1 = df1.intersect(df2)._plan.to_proto(self.connect)
self.assertFalse(plan1.root.set_op.is_all)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_INTERSECT, plan1.root.set_op.set_op_type)
plan2 = df1.intersectAll(df2)._plan.to_proto(self.connect)
self.assertTrue(plan2.root.set_op.is_all)
self.assertEqual(proto.SetOperation.SET_OP_TYPE_INTERSECT, plan2.root.set_op.set_op_type)


if __name__ == "__main__":
Expand Down