viirya opened a new pull request #26087: [SPARK-29427][SQL] Create 
KeyValueGroupedDataset from existing columns in DataFrame
URL: https://github.com/apache/spark/pull/26087
 
 
   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: 
https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: 
https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., 
'[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a 
faster review.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section 
is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster 
reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class 
hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other 
DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   This PR proposes to add groupByRelationKey API to Dataset. It creates 
KeyValueGroupedDataset instance using existing relational columns, instead of a 
typed function in groupByKey API. Because it leverages existing columns, it can 
use existing data partition, if any, when doing operations like cogroup.
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   Currently if users want to do cogroup on DataFrames, there is no good way to 
do except for KeyValueGroupedDataset. KeyValueGroupedDataset ignores existing 
data partition if any. That is a problem.
   
   ```scala
   // df1 and df2 are certainly partitioned and sorted.
   val df1 = Seq((1, 2, 3), (2, 3, 4)).toDF("a", "b", "c")
     .repartition($"a", $"b").sortWithinPartitions("a", "b")
   val df2 = Seq((1, 2, 4), (2, 3, 5)).toDF("a", "b", "c")
     .repartition($"a", $"b").sortWithinPartitions("a", "b")
   ```
   ```scala
   // This groupByRelationKey won't unnecessarily repartition the data 
   val df3 = df1.groupByRelationKey("a", "b")
     .cogroup(df2.groupByRelationKey("a", "b")) { case (key, data1, data2) =>
       data1.zip(data2).map { p =>
         p._1.getInt(2) + p._2.getInt(2)
       }
   }
   ```
   
   ```
   == Physical Plan ==
   *(5) SerializeFromObject [input[0, int, false] AS value#11206]
   +- CoGroup 
org.apache.spark.sql.DataFrameSuite$$Lambda$4888/206084072@4601674e, 
createexternalrow(a#11172, b#11173, StructField(a,IntegerType,false), 
StructField(b,IntegerTy
   pe,false)), createexternalrow(a#11172, b#11173, c#11174, 
StructField(a,IntegerType,false), StructField(b,IntegerType,false), 
StructField(c,IntegerType,false)), createexterna
   lrow(a#11188, b#11189, c#11190, StructField(a,IntegerType,false), 
StructField(b,IntegerType,false), StructField(c,IntegerType,false)), [a#11172, 
b#11173], [a#11188, b#11189]
   , [a#11172, b#11173, c#11174], [a#11188, b#11189, c#11190], obj#11205: int
      :- *(2) Sort [a#11172 ASC NULLS FIRST, b#11173 ASC NULLS FIRST], false, 0
      :  +- Exchange hashpartitioning(a#11172, b#11173, 5), false, [id=#10174]
      :     +- *(1) Project [_1#11165 AS a#11172, _2#11166 AS b#11173, _3#11167 
AS c#11174]
      :        +- *(1) LocalTableScan [_1#11165, _2#11166, _3#11167]
      +- *(4) Sort [a#11188 ASC NULLS FIRST, b#11189 ASC NULLS FIRST], false, 0
         +- Exchange hashpartitioning(a#11188, b#11189, 5), false, [id=#10179]
            +- *(3) Project [_1#11181 AS a#11188, _2#11182 AS b#11189, _3#11183 
AS c#11190]
               +- *(3) LocalTableScan [_1#11181, _2#11182, _3#11183]
   ```
   
   
   ```scala
   // Current approach creates additional AppendColumns and repartition data 
again
   df1.groupByKey(r => r.getInt(0)).cogroup(df2.groupByKey(r => r.getInt(0))) { 
                                                                                
     case (key, data1, data2) =>
       data1.zip(data2).map { p =>
         p._1.getInt(2) + p._2.getInt(2)
       }
   }
   ```
   
   ```
   == Physical Plan ==
   *(7) SerializeFromObject [input[0, int, false] AS value#11216]
   +- CoGroup 
org.apache.spark.sql.DataFrameSuite$$Lambda$4892/905560656@19f7e6c5, 
value#11211: int, createexternalrow(a#11172, b#11173, c#11174, 
StructField(a,IntegerType,false), StructField(b,IntegerType,false), 
StructField(c,IntegerType,false)), createexternalrow(a#11188, b#11189, c#11190, 
StructField(a,IntegerType,false), StructField(b,IntegerType,false), 
StructField(c,IntegerType,false)), [value#11211], [value#11213], [a#11172, 
b#11173, c#11174], [a#11188, b#11189, c#11190], obj#11215: int                  
    
      :- *(3) Sort [value#11211 ASC NULLS FIRST], false, 0
      :  +- Exchange hashpartitioning(value#11211, 5), true, [id=#10442]
      :     +- AppendColumns 
org.apache.spark.sql.DataFrameSuite$$Lambda$4889/2021090091@6396e053, 
createexternalrow(a#11172, b#11173, c#11174, StructField(a,IntegerType,false), 
StructField(b,IntegerType,false), StructField(c,IntegerType,false)), [input[0, 
int, false] AS value#11211]                                                     
           
      :        +- *(2) Sort [a#11172 ASC NULLS FIRST, b#11173 ASC NULLS FIRST], 
false, 0
      :           +- Exchange hashpartitioning(a#11172, b#11173, 5), false, 
[id=#10437]
      :              +- *(1) Project [_1#11165 AS a#11172, _2#11166 AS b#11173, 
_3#11167 AS c#11174]                                                            
               
      :                 +- *(1) LocalTableScan [_1#11165, _2#11166, _3#11167]
      +- *(6) Sort [value#11213 ASC NULLS FIRST], false, 0
         +- Exchange hashpartitioning(value#11213, 5), true, [id=#10452]
            +- AppendColumns 
org.apache.spark.sql.DataFrameSuite$$Lambda$4891/1736834504@798dbf14, 
createexternalrow(a#11188, b#11189, c#11190, StructField(a,IntegerType,false), 
StructField(b,IntegerType,false), StructField(c,IntegerType,false)), [input[0, 
int, false] AS value#11213]                                                     
           
               +- *(5) Sort [a#11188 ASC NULLS FIRST, b#11189 ASC NULLS FIRST], 
false, 0
                  +- Exchange hashpartitioning(a#11188, b#11189, 5), false, 
[id=#10447]
                     +- *(4) Project [_1#11181 AS a#11188, _2#11182 AS b#11189, 
_3#11183 AS c#11190]                                                            
               
                        +- *(4) LocalTableScan [_1#11181, _2#11182, _3#11183]
   ```
   
   ### Does this PR introduce any user-facing change?
   <!--
   If yes, please clarify the previous behavior and the change this PR proposes 
- provide the console output, description and/or an example to show the 
behavior difference if possible.
   If no, write 'No'.
   -->
   
   Yes, this adds a new groupByRelationKey API to Dataset.
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some 
test cases that check the changes thoroughly including negative and positive 
cases if possible.
   If it was tested in a way different from regular unit tests, please clarify 
how you tested step by step, ideally copy and paste-able, so that other 
reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why 
it was difficult to add.
   -->
   
   Unit test.
   

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to