forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
[WIP]: Spark 27463: Cogrouped Pandas Udf POC #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2e0b308
initial commit of cogroup
d80tb7 64ff5ac
minor tidy up
d80tb7 6d039e3
removed incorrect test
d80tb7 d8a5c5d
tidies up test, fixed output cols
d80tb7 73188f6
removed incorrect file
d80tb7 690fa14
Revert: removed incorrect test
d80tb7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from pyspark.sql.dataframe import DataFrame | ||
|
|
||
|
|
||
| class CoGroupedData(object): | ||
|
|
||
| def __init__(self, gd1, gd2): | ||
| self._gd1 = gd1 | ||
| self._gd2 = gd2 | ||
| self.sql_ctx = gd1.sql_ctx | ||
|
|
||
| def apply(self, udf): | ||
| all_cols = self._extract_cols(self._gd1) + self._extract_cols(self._gd2) | ||
| udf_column = udf(*all_cols) | ||
| jdf = self._gd1._jgd.flatMapCoGroupsInPandas(self._gd2._jgd, udf_column._jc.expr()) | ||
| return DataFrame(jdf, self.sql_ctx) | ||
|
|
||
| @staticmethod | ||
| def _extract_cols(gd): | ||
| df = gd._df | ||
| return [df[col] for col in df.columns] | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| import datetime | ||
| import unittest | ||
| import sys | ||
|
|
||
| from collections import OrderedDict | ||
| from decimal import Decimal | ||
|
|
||
| from pyspark.sql import Row | ||
| from pyspark.sql.functions import array, explode, col, lit, udf, sum, pandas_udf, PandasUDFType | ||
| from pyspark.sql.types import * | ||
| from pyspark.testing.sqlutils import ReusedSQLTestCase, have_pandas, have_pyarrow, \ | ||
| pandas_requirement_message, pyarrow_requirement_message | ||
| from pyspark.testing.utils import QuietTest | ||
|
|
||
| if have_pandas: | ||
| import pandas as pd | ||
| from pandas.util.testing import assert_frame_equal | ||
|
|
||
| if have_pyarrow: | ||
| import pyarrow as pa | ||
|
|
||
|
|
||
| """ | ||
| Tests below use pd.DataFrame.assign that will infer mixed types (unicode/str) for column names | ||
| from kwargs w/ Python 2, so need to set check_column_type=False and avoid this check | ||
| """ | ||
| if sys.version < '3': | ||
| _check_column_type = False | ||
| else: | ||
| _check_column_type = True | ||
|
|
||
|
|
||
| @unittest.skipIf( | ||
| not have_pandas or not have_pyarrow, | ||
| pandas_requirement_message or pyarrow_requirement_message) | ||
| class CoGroupedMapPandasUDFTests(ReusedSQLTestCase): | ||
|
|
||
| @property | ||
| def data1(self): | ||
| return self.spark.range(10).toDF('id') \ | ||
| .withColumn("ks", array([lit(i) for i in range(20, 30)])) \ | ||
| .withColumn("k", explode(col('ks')))\ | ||
| .withColumn("v", col('k') * 10)\ | ||
| .drop('ks') | ||
|
|
||
| @property | ||
| def data2(self): | ||
| return self.spark.range(10).toDF('id') \ | ||
| .withColumn("ks", array([lit(i) for i in range(20, 30)])) \ | ||
| .withColumn("k", explode(col('ks'))) \ | ||
| .withColumn("v2", col('k') * 100) \ | ||
| .drop('ks') | ||
|
|
||
| def test_simple(self): | ||
| import pandas as pd | ||
|
|
||
| l = self.data1 | ||
| r = self.data2 | ||
|
|
||
| @pandas_udf('id long, k int, v int, v2 int', PandasUDFType.COGROUPED_MAP) | ||
| def merge_pandas(left, right): | ||
| return pd.merge(left, right, how='outer', on=['k', 'id']) | ||
|
|
||
| # TODO: Grouping by a string fails to resolve here as analyzer cannot determine side | ||
| result = l\ | ||
| .groupby(l.id)\ | ||
| .cogroup(r.groupby(r.id))\ | ||
| .apply(merge_pandas)\ | ||
| .sort(['id', 'k'])\ | ||
| .toPandas() | ||
|
|
||
| expected = pd\ | ||
| .merge(l.toPandas(), r.toPandas(), how='outer', on=['k', 'id']) | ||
|
|
||
| assert_frame_equal(expected, result, check_column_type=_check_column_type) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to read these also using the message reader but for some reason pa.read_schema(self_reader.read_next_message()) didn't work.