You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/01/01 02:37:44 UTC
[GitHub] [spark] aa1371 opened a new pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
aa1371 opened a new pull request #35083:
URL: https://github.com/apache/spark/pull/35083
JIRA: https://issues.apache.org/jira/browse/SPARK-37798
Pandas currently supports a `how="cross"` merge which provides a cartesian product of the left/right tables. This can be achieved by doing a `spark.sql.dataframe.join(..., on=None, how="inner")`.
Additionally, I am currently in the middle of adding conditional merging in the pandas API (see PR here: https://github.com/pandas-dev/pandas/pull/42964). This is much easier to achieve in spark, since the functionality is already available, and we can trivially expose it in the pyspark pandas API. Due to the demand of this functionality (countless SO/pandas issues either asking how to do this, or asking questions that would be solved by this), I think that this would be worth adding even before it makes it into the core pandas API.
These changes will be purely incremental on top of the existing API, and will be completely backwards compatible.
Still need to add tests and docstring examples.
**Examples:**
**Example DFs:**
```
>>> df1 = pd.DataFrame([['Bill', 23], ['Mary', 33], ['Ted', 36]], columns=['name', 'age'])
>>> df2 = pd.DataFrame([['President', 35], ['Senator', 30]], columns=['job', 'min_age'])
>>> df1
name age
0 Bill 23
1 Mary 33
2 Ted 36
>>> df2
job min_age
0 President 35
1 Senator 30
```
**Cross Merge Example:**
```
>>> df1.merge(df2, how="cross")
name age job min_age
0 Bill 23 President 35
1 Bill 23 Senator 30
2 Mary 33 President 35
3 Mary 33 Senator 30
4 Ted 36 President 35
5 Ted 36 Senator 30
```
**Conditional Merge Example:**
```
>>> df1.merge(df2, on=lambda left, right: left.age >= right.min_age)
name age job min_age
0 Mary 33 Senator 30
1 Ted 36 President 35
2 Ted 36 Senator 30
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777188497
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
@HyukjinKwon - I just pushed an update to address cleaning up the logic in the merge function (the first half of the function, up until the creation of the `joined_table`). And actually, I hope you don't mind, but I've also included the cross and conditional merge logic into the PR as well, because it actually is only a minimal marginal addition to the cleanup refactor to implement both features.
If we only want the PR to address cleaning up the function (i.e. no behavior change/new features), the only change we would need to make to the PR is to reduce this block of code:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7720-L7737
To this subset of that block of code above:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7731-L7737
I think there will still be a bit to digest in the PR, but it's all related to the null-op cleanup refactor, after that's been processed the new feature additions are very simple to follow.
I think this would be a potentially good way to move forward, let me know your thoughts on this approach.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777194747
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
I just pushed an update to address cleaning up the logic in the merge function (the first half of the function, up until the creation of the `joined_table`). And actually, I hope you don't mind, but I've also included the cross and conditional merge logic into the PR as well, because it actually is only a minimal marginal addition to the cleanup refactor to implement both features. (This is the reason I kept the changes in this PR rather than a new PR)
If we only want the PR to address cleaning up the function (i.e. no behavior change/new features), the only change we would need to make to the PR is to reduce this block of code:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7720-L7737
To this subset of that block of code above:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7731-L7737
I think there will still be a bit to digest in the PR, but it's all related to the null-op cleanup refactor, after that's been processed the new feature additions (from the first snippet above) are very simple to follow. I've added some comments to explain the cleanup reorg part.
I think this would be a good way to move forward, let me know your thoughts on this approach.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777188497
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
@HyukjinKwon - I just pushed an update to address cleaning up the logic in the merge function (the first half of the function, up until the creation of the `joined_table`). And actually, I hope you don't mind, but I've also included the cross and conditional merge logic into the PR as well, because it actually is only a minimal marginal addition to the cleanup refactor to implement both features. (This is the reason I kept the changes in this PR rather than a new PR)
If we only want the PR to address cleaning up the function (i.e. no behavior change/new features), the only change we would need to make to the PR is to reduce this block of code:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7720-L7737
To this subset of that block of code above:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7731-L7737
I think there will still be a bit to digest in the PR, but it's all related to the null-op cleanup refactor, after that's been processed the new feature additions (from the first snippet above) are very simple to follow.
I think this would be a good way to move forward, let me know your thoughts on this approach.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777194245
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7637,102 +7706,35 @@ def merge(
As described in #263, joining string columns currently returns None for missing values
instead of NaN.
"""
-
- def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
- if os is None:
- return []
- elif is_name_like_tuple(os):
- return [cast(Label, os)]
- elif is_name_like_value(os):
- return [(os,)]
- else:
- return [o if is_name_like_tuple(o) else (o,) for o in os]
-
if isinstance(right, ps.Series):
right = right.to_frame()
- if on:
Review comment:
The logic from 7654-7693 has been moved verbatim to `self._resolve_merge_key_names`
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7637,102 +7706,35 @@ def merge(
As described in #263, joining string columns currently returns None for missing values
instead of NaN.
"""
-
- def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
- if os is None:
- return []
- elif is_name_like_tuple(os):
- return [cast(Label, os)]
- elif is_name_like_value(os):
- return [(os,)]
- else:
- return [o if is_name_like_tuple(o) else (o,) for o in os]
-
if isinstance(right, ps.Series):
right = right.to_frame()
- if on:
- if left_on or right_on:
- raise ValueError(
- 'Can only pass argument "on" OR "left_on" and "right_on", '
- "not a combination of both."
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(on)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(on)))
- else:
- # TODO: need special handling for multi-index.
- if left_index:
- left_key_names = self._internal.index_spark_column_names
- else:
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(left_on)))
- if right_index:
- right_key_names = right._internal.index_spark_column_names
- else:
- right_key_names = list(
- map(right._internal.spark_column_name_for, to_list(right_on))
- )
-
- if left_key_names and not right_key_names:
- raise ValueError("Must pass right_on or right_index=True")
- if right_key_names and not left_key_names:
- raise ValueError("Must pass left_on or left_index=True")
- if not left_key_names and not right_key_names:
- common = list(self.columns.intersection(right.columns))
- if len(common) == 0:
- raise ValueError(
- "No common columns to perform merge on. Merge options: "
- "left_on=None, right_on=None, left_index=False, right_index=False"
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(common)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(common)))
- if len(left_key_names) != len(right_key_names):
- raise ValueError("len(left_keys) must equal len(right_keys)")
-
- # We should distinguish the name to avoid ambiguous column name after merging.
- right_prefix = "__right_"
- right_key_names = [right_prefix + right_key_name for right_key_name in right_key_names]
-
- how = validate_how(how)
-
- def resolve(internal: InternalFrame, side: str) -> InternalFrame:
- rename = lambda col: "__{}_{}".format(side, col)
- internal = internal.resolved_copy
- sdf = internal.spark_frame
- sdf = sdf.select(
- *[
- scol_for(sdf, col).alias(rename(col))
- for col in sdf.columns
- if col not in HIDDEN_COLUMNS
- ],
- *HIDDEN_COLUMNS,
- )
- return internal.copy(
- spark_frame=sdf,
- index_spark_columns=[
- scol_for(sdf, rename(col)) for col in internal.index_spark_column_names
- ],
- index_fields=[
- field.copy(name=rename(field.name)) for field in internal.index_fields
- ],
- data_spark_columns=[
- scol_for(sdf, rename(col)) for col in internal.data_spark_column_names
- ],
- data_fields=[field.copy(name=rename(field.name)) for field in internal.data_fields],
- )
-
- left_internal = self._internal.resolved_copy
- right_internal = resolve(right._internal, "right")
+ left_internal = _resolve_internal_merge_frame(self._internal)
+ right_internal = _resolve_internal_merge_frame(right._internal, "right")
left_table = left_internal.spark_frame.alias("left_table")
right_table = right_internal.spark_frame.alias("right_table")
- left_key_columns = [scol_for(left_table, label) for label in left_key_names]
- right_key_columns = [scol_for(right_table, label) for label in right_key_names]
+ how = validate_how(how)
- join_condition = reduce(
- lambda x, y: x & y,
- [lkey == rkey for lkey, rkey in zip(left_key_columns, right_key_columns)],
- )
+ left_key_names = []
+ right_key_names = []
+ if how == "cross":
+ if any((on, left_on, right_on, left_index, right_index)):
+ raise ValueError('Cannot provide any join conditions for `how="cross"`')
+ join_condition = None
+ how = "inner"
+ else:
+ if callable(on):
+ join_condition = on(left_table, right_table)
+ else:
+ # join condition based on column names
+ left_key_names, right_key_names = self._resolve_merge_key_names(
+ right, on, left_on, right_on, left_index, right_index
+ )
+ join_condition = _resolve_merge_join_condition_from_key_names(
+ left_table, right_table, left_key_names, right_key_names
+ )
Review comment:
If we keep only this highlighted portion of the block from 7720-7737, then this entire PR serves only as a code reorg, with no behavior or feature changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777188497
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
@HyukjinKwon - I just pushed an update to address cleaning up the logic in the merge function (the first half of the function, up until the creation of the `joined_table`). And actually, I hope you don't mind, but I've also included the cross and conditional merge logic into the PR as well, because it actually is only a minimal marginal addition to the cleanup refactor to implement both features.
If we only want the PR to address cleaning up the function (i.e. no behavior change/new features), the only change we would need to make to the PR is to reduce this block of code:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7720-L7737
To this subset of that block of code:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7731-L7737
I think there will still be a bit to digest in the PR, but it's all related to the null-op cleanup refactor, after that's been processed the new feature additions are very simple to follow.
I think this would be a potentially good way to move forward, let me know your thoughts on this approach.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
HyukjinKwon commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777090230
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
Can we minimize the codes moving around? It's difficult to review. You can make a separate PR to refactor this first.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777188497
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
@HyukjinKwon - I just pushed an update to address cleaning up the logic in the merge function (the first half of the function, up until the creation of the `joined_table`). And actually, I hope you don't mind, but I've also included the cross and conditional merge logic into the PR as well, because it actually is only a minimal marginal addition to the cleanup refactor to implement both features. (This is the reason I kept the changes in this PR rather than a new PR)
If we only want the PR to address cleaning up the function (i.e. no behavior change/new features), the only change we would need to make to the PR is to reduce this block of code:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7720-L7737
To this subset of that block of code above:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7731-L7737
I think there will still be a bit to digest in the PR, but it's all related to the null-op cleanup refactor, after that's been processed the new feature additions are very simple to follow.
I think this would be a potentially good way to move forward, let me know your thoughts on this approach.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777194572
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7637,102 +7706,35 @@ def merge(
As described in #263, joining string columns currently returns None for missing values
instead of NaN.
"""
-
- def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
- if os is None:
- return []
- elif is_name_like_tuple(os):
- return [cast(Label, os)]
- elif is_name_like_value(os):
- return [(os,)]
- else:
- return [o if is_name_like_tuple(o) else (o,) for o in os]
-
if isinstance(right, ps.Series):
right = right.to_frame()
- if on:
- if left_on or right_on:
- raise ValueError(
- 'Can only pass argument "on" OR "left_on" and "right_on", '
- "not a combination of both."
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(on)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(on)))
- else:
- # TODO: need special handling for multi-index.
- if left_index:
- left_key_names = self._internal.index_spark_column_names
- else:
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(left_on)))
- if right_index:
- right_key_names = right._internal.index_spark_column_names
- else:
- right_key_names = list(
- map(right._internal.spark_column_name_for, to_list(right_on))
- )
-
- if left_key_names and not right_key_names:
- raise ValueError("Must pass right_on or right_index=True")
- if right_key_names and not left_key_names:
- raise ValueError("Must pass left_on or left_index=True")
- if not left_key_names and not right_key_names:
- common = list(self.columns.intersection(right.columns))
- if len(common) == 0:
- raise ValueError(
- "No common columns to perform merge on. Merge options: "
- "left_on=None, right_on=None, left_index=False, right_index=False"
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(common)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(common)))
- if len(left_key_names) != len(right_key_names):
- raise ValueError("len(left_keys) must equal len(right_keys)")
-
- # We should distinguish the name to avoid ambiguous column name after merging.
- right_prefix = "__right_"
- right_key_names = [right_prefix + right_key_name for right_key_name in right_key_names]
-
- how = validate_how(how)
-
- def resolve(internal: InternalFrame, side: str) -> InternalFrame:
- rename = lambda col: "__{}_{}".format(side, col)
- internal = internal.resolved_copy
- sdf = internal.spark_frame
- sdf = sdf.select(
- *[
- scol_for(sdf, col).alias(rename(col))
- for col in sdf.columns
- if col not in HIDDEN_COLUMNS
- ],
- *HIDDEN_COLUMNS,
- )
- return internal.copy(
- spark_frame=sdf,
- index_spark_columns=[
- scol_for(sdf, rename(col)) for col in internal.index_spark_column_names
- ],
- index_fields=[
- field.copy(name=rename(field.name)) for field in internal.index_fields
- ],
- data_spark_columns=[
- scol_for(sdf, rename(col)) for col in internal.data_spark_column_names
- ],
- data_fields=[field.copy(name=rename(field.name)) for field in internal.data_fields],
- )
-
- left_internal = self._internal.resolved_copy
- right_internal = resolve(right._internal, "right")
+ left_internal = _resolve_internal_merge_frame(self._internal)
+ right_internal = _resolve_internal_merge_frame(right._internal, "right")
left_table = left_internal.spark_frame.alias("left_table")
right_table = right_internal.spark_frame.alias("right_table")
- left_key_columns = [scol_for(left_table, label) for label in left_key_names]
- right_key_columns = [scol_for(right_table, label) for label in right_key_names]
+ how = validate_how(how)
- join_condition = reduce(
- lambda x, y: x & y,
- [lkey == rkey for lkey, rkey in zip(left_key_columns, right_key_columns)],
- )
Review comment:
This logic has been moved to `_resolve_merge_join_condition_from_key_names ` in the global scope.
(not including the `validate_how` call)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777194433
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7637,102 +7706,35 @@ def merge(
As described in #263, joining string columns currently returns None for missing values
instead of NaN.
"""
-
- def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
- if os is None:
- return []
- elif is_name_like_tuple(os):
- return [cast(Label, os)]
- elif is_name_like_value(os):
- return [(os,)]
- else:
- return [o if is_name_like_tuple(o) else (o,) for o in os]
-
if isinstance(right, ps.Series):
right = right.to_frame()
- if on:
- if left_on or right_on:
- raise ValueError(
- 'Can only pass argument "on" OR "left_on" and "right_on", '
- "not a combination of both."
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(on)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(on)))
- else:
- # TODO: need special handling for multi-index.
- if left_index:
- left_key_names = self._internal.index_spark_column_names
- else:
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(left_on)))
- if right_index:
- right_key_names = right._internal.index_spark_column_names
- else:
- right_key_names = list(
- map(right._internal.spark_column_name_for, to_list(right_on))
- )
-
- if left_key_names and not right_key_names:
- raise ValueError("Must pass right_on or right_index=True")
- if right_key_names and not left_key_names:
- raise ValueError("Must pass left_on or left_index=True")
- if not left_key_names and not right_key_names:
- common = list(self.columns.intersection(right.columns))
- if len(common) == 0:
- raise ValueError(
- "No common columns to perform merge on. Merge options: "
- "left_on=None, right_on=None, left_index=False, right_index=False"
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(common)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(common)))
- if len(left_key_names) != len(right_key_names):
- raise ValueError("len(left_keys) must equal len(right_keys)")
-
- # We should distinguish the name to avoid ambiguous column name after merging.
- right_prefix = "__right_"
- right_key_names = [right_prefix + right_key_name for right_key_name in right_key_names]
Review comment:
The logic from 7654-7693 has been moved verbatim to `self._resolve_merge_key_names`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777188497
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
@HyukjinKwon - I just pushed an update to address cleaning up the logic in the merge function (the first half of the function, up until the creation of the `joined_table`). And actually, I hope you don't mind, but I've also included the cross and conditional merge logic into the PR as well, because it actually is only a minimal marginal addition to the cleanup refactor to implement both features. (This is the reason I kept the changes in this PR rather than a new PR)
If we only want the PR to address cleaning up the function (i.e. no behavior change/new features), the only change we would need to make to the PR is to reduce this block of code:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7720-L7737
To this subset of that block of code above:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7731-L7737
I think there will still be a bit to digest in the PR, but it's all related to the null-op cleanup refactor, after that's been processed the new feature additions are very simple to follow.
I think this would be a good way to move forward, let me know your thoughts on this approach.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon commented on pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
HyukjinKwon commented on pull request #35083:
URL: https://github.com/apache/spark/pull/35083#issuecomment-1003801448
@aa1371 mind enabling GitHub Actions in your forked repository? Apache Spark repo uses the resources of GitHub Actions in your forked repository (see also https://github.com/apache/spark/pull/35083/checks?check_run_id=4686215216).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777097272
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
Sure I can do this. FYI, unfortunately, as part of this refactor implementation a large chunk of code was required to be indented one level, which will still cause a large diff block, even though the changes are fairly minimal.
How would you like me to structure the PRs? Would you rather:
(1) create a isolated code repositioning branch that will be merged to master in one PR, and then a second join-feature update branch that will be separately merged to master in a second PR?
Or (2) create a code repositioning branch, with a PR to master, and then another join-feature update branch with a PR to the code repositioning branch. The join feature branch will get merged into the other branch first, then that branch to master.
I think (2) makes more sense, since if we don't agree on the implementation of the join feature, then the code repositioning will just be unnecessarily merged in noise.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zero323 edited a comment on pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
zero323 edited a comment on pull request #35083:
URL: https://github.com/apache/spark/pull/35083#issuecomment-1008213524
> Also, is it possible to see the specific formatting issues caught by the black formatter in the CI?
Not at the moment.
> It seems to contain some different black configuration than when I run it dev/format-python locally
This seems to be your current offender
```patch
diff --git a/python/pyspark/pandas/frame.py b/python/pyspark/pandas/frame.py
index 949e3e601b..da91da59bc 100644
--- a/python/pyspark/pandas/frame.py
+++ b/python/pyspark/pandas/frame.py
@@ -359,7 +359,8 @@ RIGHT_MERGE_PREFIX = MERGE_PREFIX_TEMPLATE.format("right")
def _resolve_internal_merge_frame(
- internal: InternalFrame, side: Optional[str] = None,
+ internal: InternalFrame,
+ side: Optional[str] = None,
) -> InternalFrame:
internal = internal.resolved_copy
if side is None:
```
In general you might want to resync this with master (also, please make sure that you use matching black version ‒ 21.12b0 at the moment. It should be detected automatically, but who knows?).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777194266
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7637,102 +7706,35 @@ def merge(
As described in #263, joining string columns currently returns None for missing values
instead of NaN.
"""
-
- def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
- if os is None:
- return []
- elif is_name_like_tuple(os):
- return [cast(Label, os)]
- elif is_name_like_value(os):
- return [(os,)]
- else:
- return [o if is_name_like_tuple(o) else (o,) for o in os]
-
if isinstance(right, ps.Series):
right = right.to_frame()
- if on:
- if left_on or right_on:
- raise ValueError(
- 'Can only pass argument "on" OR "left_on" and "right_on", '
- "not a combination of both."
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(on)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(on)))
- else:
- # TODO: need special handling for multi-index.
- if left_index:
- left_key_names = self._internal.index_spark_column_names
- else:
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(left_on)))
- if right_index:
- right_key_names = right._internal.index_spark_column_names
- else:
- right_key_names = list(
- map(right._internal.spark_column_name_for, to_list(right_on))
- )
-
- if left_key_names and not right_key_names:
- raise ValueError("Must pass right_on or right_index=True")
- if right_key_names and not left_key_names:
- raise ValueError("Must pass left_on or left_index=True")
- if not left_key_names and not right_key_names:
- common = list(self.columns.intersection(right.columns))
- if len(common) == 0:
- raise ValueError(
- "No common columns to perform merge on. Merge options: "
- "left_on=None, right_on=None, left_index=False, right_index=False"
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(common)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(common)))
- if len(left_key_names) != len(right_key_names):
- raise ValueError("len(left_keys) must equal len(right_keys)")
-
- # We should distinguish the name to avoid ambiguous column name after merging.
- right_prefix = "__right_"
- right_key_names = [right_prefix + right_key_name for right_key_name in right_key_names]
-
- how = validate_how(how)
-
- def resolve(internal: InternalFrame, side: str) -> InternalFrame:
- rename = lambda col: "__{}_{}".format(side, col)
- internal = internal.resolved_copy
- sdf = internal.spark_frame
- sdf = sdf.select(
- *[
- scol_for(sdf, col).alias(rename(col))
- for col in sdf.columns
- if col not in HIDDEN_COLUMNS
- ],
- *HIDDEN_COLUMNS,
- )
- return internal.copy(
- spark_frame=sdf,
- index_spark_columns=[
- scol_for(sdf, rename(col)) for col in internal.index_spark_column_names
- ],
- index_fields=[
- field.copy(name=rename(field.name)) for field in internal.index_fields
- ],
- data_spark_columns=[
- scol_for(sdf, rename(col)) for col in internal.data_spark_column_names
- ],
- data_fields=[field.copy(name=rename(field.name)) for field in internal.data_fields],
- )
-
- left_internal = self._internal.resolved_copy
- right_internal = resolve(right._internal, "right")
+ left_internal = _resolve_internal_merge_frame(self._internal)
+ right_internal = _resolve_internal_merge_frame(right._internal, "right")
left_table = left_internal.spark_frame.alias("left_table")
right_table = right_internal.spark_frame.alias("right_table")
- left_key_columns = [scol_for(left_table, label) for label in left_key_names]
- right_key_columns = [scol_for(right_table, label) for label in right_key_names]
+ how = validate_how(how)
- join_condition = reduce(
- lambda x, y: x & y,
- [lkey == rkey for lkey, rkey in zip(left_key_columns, right_key_columns)],
- )
+ left_key_names = []
+ right_key_names = []
+ if how == "cross":
+ if any((on, left_on, right_on, left_index, right_index)):
+ raise ValueError('Cannot provide any join conditions for `how="cross"`')
+ join_condition = None
+ how = "inner"
+ else:
+ if callable(on):
+ join_condition = on(left_table, right_table)
+ else:
+ # join condition based on column names
+ left_key_names, right_key_names = self._resolve_merge_key_names(
+ right, on, left_on, right_on, left_index, right_index
+ )
+ join_condition = _resolve_merge_join_condition_from_key_names(
+ left_table, right_table, left_key_names, right_key_names
+ )
Review comment:
If we keep only this highlighted portion of the block from 7730-7747, then this entire PR serves only as a code reorg, with no behavior or feature changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777188497
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
@HyukjinKwon - I just pushed an update to address cleaning up the logic in the merge function (the first half of the function, up until the creation of the `joined_table`). And actually, I hope you don't mind, but I've also included the cross and conditional merge logic into the PR as well, because it actually is only a minimal marginal addition to the cleanup refactor to implement both features. (This is the reason I kept the changes in this PR rather than a new PR)
If we only want the PR to address cleaning up the function (i.e. no behavior change/new features), the only change we would need to make to the PR is to reduce this block of code:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7720-L7737
To this subset of that block of code above:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7731-L7737
I think there will still be a bit to digest in the PR, but it's all related to the null-op cleanup refactor, after that's been processed the new feature additions (from the first snippet above) are very simple to follow.
I think this would be a good way to move forward, let me know your thoughts on this approach.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
HyukjinKwon commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777151289
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
Actually, I would prefer to create an isolated PR that refactors this into, for example, a separate private function or nested function as an example.
I think it's fine even without merging the join feature - this logic you are moving seems better to be separate and factored out in any event.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
HyukjinKwon commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777151289
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
Actually, I would prefer to create an isolated PR that refactors this into, for example, a separate private function or nested function as an example.
I think it's fine even without merging the join feature - this logic you are moving seems better be separate and factored out in any event.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on pull request #35083:
URL: https://github.com/apache/spark/pull/35083#issuecomment-1007846355
@HyukjinKwon - thoughts on the PR in its current state?
Also, is it possible to see the specific formatting issues caught by the black formatter in the CI? It seems to contain some different black configuration than when I run it dev/format-python locally
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] AmplabJenkins commented on pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #35083:
URL: https://github.com/apache/spark/pull/35083#issuecomment-1003504652
Can one of the admins verify this patch?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] HyukjinKwon commented on pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
HyukjinKwon commented on pull request #35083:
URL: https://github.com/apache/spark/pull/35083#issuecomment-1003801480
cc @itholic @ueshin @xinrong-databricks FYI
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777194331
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7637,102 +7706,35 @@ def merge(
As described in #263, joining string columns currently returns None for missing values
instead of NaN.
"""
-
- def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
Review comment:
moved to global scope to declutter this function
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on pull request #35083:
URL: https://github.com/apache/spark/pull/35083#issuecomment-1003883135
added some preliminary tests, can add onto these later.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777194747
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
@HyukjinKwon - I just pushed an update to address cleaning up the logic in the merge function (the first half of the function, up until the creation of the `joined_table`). And actually, I hope you don't mind, but I've also included the cross and conditional merge logic into the PR as well, because it actually is only a minimal marginal addition to the cleanup refactor to implement both features. (This is the reason I kept the changes in this PR rather than a new PR)
If we only want the PR to address cleaning up the function (i.e. no behavior change/new features), the only change we would need to make to the PR is to reduce this block of code:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7720-L7737
To this subset of that block of code above:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7731-L7737
I think there will still be a bit to digest in the PR, but it's all related to the null-op cleanup refactor, after that's been processed the new feature additions (from the first snippet above) are very simple to follow. I've added some comments to explain the cleanup reorg part.
I think this would be a good way to move forward, let me know your thoughts on this approach.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on pull request #35083:
URL: https://github.com/apache/spark/pull/35083#issuecomment-1003697145
@HyukjinKwon - explanation of latest update here: https://github.com/apache/spark/pull/35083#discussion_r777194747
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on pull request #35083:
URL: https://github.com/apache/spark/pull/35083#issuecomment-1003805833
@HyukjinKwon - workflows enabled. FYI - tests have not been added for these features yet. Will wait until there is buy-in on them first.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on pull request #35083:
URL: https://github.com/apache/spark/pull/35083#issuecomment-1007846355
@HyukjinKwon - thoughts on the PR in its current state?
Also, is it possible to see the specific formatting issues caught by the black formatter in the CI? It seems to contain some different black configuration than when I run it dev/format-python locally
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zero323 commented on pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
zero323 commented on pull request #35083:
URL: https://github.com/apache/spark/pull/35083#issuecomment-1008213524
> Also, is it possible to see the specific formatting issues caught by the black formatter in the CI?
Not at the moment.
> It seems to contain some different black configuration than when I run it dev/format-python locally
This seems to be your current offender
```patch
diff --git a/python/pyspark/pandas/frame.py b/python/pyspark/pandas/frame.py
index 949e3e601b..da91da59bc 100644
--- a/python/pyspark/pandas/frame.py
+++ b/python/pyspark/pandas/frame.py
@@ -359,7 +359,8 @@ RIGHT_MERGE_PREFIX = MERGE_PREFIX_TEMPLATE.format("right")
def _resolve_internal_merge_frame(
- internal: InternalFrame, side: Optional[str] = None,
+ internal: InternalFrame,
+ side: Optional[str] = None,
) -> InternalFrame:
internal = internal.resolved_copy
if side is None:
```
In general you might want to resync this with master.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777194403
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7637,102 +7706,35 @@ def merge(
As described in #263, joining string columns currently returns None for missing values
instead of NaN.
"""
-
- def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
- if os is None:
- return []
- elif is_name_like_tuple(os):
- return [cast(Label, os)]
- elif is_name_like_value(os):
- return [(os,)]
- else:
- return [o if is_name_like_tuple(o) else (o,) for o in os]
-
if isinstance(right, ps.Series):
right = right.to_frame()
- if on:
- if left_on or right_on:
- raise ValueError(
- 'Can only pass argument "on" OR "left_on" and "right_on", '
- "not a combination of both."
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(on)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(on)))
- else:
- # TODO: need special handling for multi-index.
- if left_index:
- left_key_names = self._internal.index_spark_column_names
- else:
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(left_on)))
- if right_index:
- right_key_names = right._internal.index_spark_column_names
- else:
- right_key_names = list(
- map(right._internal.spark_column_name_for, to_list(right_on))
- )
-
- if left_key_names and not right_key_names:
- raise ValueError("Must pass right_on or right_index=True")
- if right_key_names and not left_key_names:
- raise ValueError("Must pass left_on or left_index=True")
- if not left_key_names and not right_key_names:
- common = list(self.columns.intersection(right.columns))
- if len(common) == 0:
- raise ValueError(
- "No common columns to perform merge on. Merge options: "
- "left_on=None, right_on=None, left_index=False, right_index=False"
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(common)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(common)))
- if len(left_key_names) != len(right_key_names):
- raise ValueError("len(left_keys) must equal len(right_keys)")
-
- # We should distinguish the name to avoid ambiguous column name after merging.
- right_prefix = "__right_"
- right_key_names = [right_prefix + right_key_name for right_key_name in right_key_names]
-
- how = validate_how(how)
-
- def resolve(internal: InternalFrame, side: str) -> InternalFrame:
Review comment:
moved to global scope to declutter this function (renamed to `_resolve_internal_merge_frame `)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777194403
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7637,102 +7706,35 @@ def merge(
As described in #263, joining string columns currently returns None for missing values
instead of NaN.
"""
-
- def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
- if os is None:
- return []
- elif is_name_like_tuple(os):
- return [cast(Label, os)]
- elif is_name_like_value(os):
- return [(os,)]
- else:
- return [o if is_name_like_tuple(o) else (o,) for o in os]
-
if isinstance(right, ps.Series):
right = right.to_frame()
- if on:
- if left_on or right_on:
- raise ValueError(
- 'Can only pass argument "on" OR "left_on" and "right_on", '
- "not a combination of both."
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(on)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(on)))
- else:
- # TODO: need special handling for multi-index.
- if left_index:
- left_key_names = self._internal.index_spark_column_names
- else:
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(left_on)))
- if right_index:
- right_key_names = right._internal.index_spark_column_names
- else:
- right_key_names = list(
- map(right._internal.spark_column_name_for, to_list(right_on))
- )
-
- if left_key_names and not right_key_names:
- raise ValueError("Must pass right_on or right_index=True")
- if right_key_names and not left_key_names:
- raise ValueError("Must pass left_on or left_index=True")
- if not left_key_names and not right_key_names:
- common = list(self.columns.intersection(right.columns))
- if len(common) == 0:
- raise ValueError(
- "No common columns to perform merge on. Merge options: "
- "left_on=None, right_on=None, left_index=False, right_index=False"
- )
- left_key_names = list(map(self._internal.spark_column_name_for, to_list(common)))
- right_key_names = list(map(right._internal.spark_column_name_for, to_list(common)))
- if len(left_key_names) != len(right_key_names):
- raise ValueError("len(left_keys) must equal len(right_keys)")
-
- # We should distinguish the name to avoid ambiguous column name after merging.
- right_prefix = "__right_"
- right_key_names = [right_prefix + right_key_name for right_key_name in right_key_names]
-
- how = validate_how(how)
-
- def resolve(internal: InternalFrame, side: str) -> InternalFrame:
Review comment:
moved to global scope to declutter this function
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 commented on a change in pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 commented on a change in pull request #35083:
URL: https://github.com/apache/spark/pull/35083#discussion_r777188497
##########
File path: python/pyspark/pandas/frame.py
##########
@@ -7648,52 +7654,6 @@ def to_list(os: Optional[Union[Name, List[Name]]]) -> List[Label]:
else:
return [o if is_name_like_tuple(o) else (o,) for o in os]
- if isinstance(right, ps.Series):
Review comment:
@HyukjinKwon - I just pushed an update to address cleaning up the logic in the merge function (the first half of the function, up until the creation of the `joined_table`). And actually, I hope you don't mind, but I've also included the cross and conditional merge logic into the PR as well, because it actually is only a minimal marginal addition to the cleanup refactor to implement both features.
If we only want the PR to address cleaning up the function (i.e. no behavior change/new features), the only change we would need to make to the PR is to reduce this block of code:
https://github.com/apache/spark/blob/f052ff0c90de487342a2da71a56cdee2b1b3099f/python/pyspark/pandas/frame.py#L7720-L7737
To this subset of that block of code:
```
left_key_names, right_key_names = self._resolve_merge_key_names(
right, on, left_on, right_on, left_index, right_index
)
join_condition = _resolve_merge_join_condition_from_key_names(
left_table, right_table, left_key_names, right_key_names
)
```
I think there will still be a bit to digest in the PR, but it's all related to the null-op cleanup refactor, after that's been processed the new feature additions are very simple to follow.
I think this would be a potentially good way to move forward, let me know your thoughts on this approach.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] aa1371 edited a comment on pull request #35083: [WIP][SPARK-37798] PySpark Pandas API: Cross and conditional merging
Posted by GitBox <gi...@apache.org>.
aa1371 edited a comment on pull request #35083:
URL: https://github.com/apache/spark/pull/35083#issuecomment-1003805833
@HyukjinKwon - workflows enabled.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org