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 2021/10/17 00:44:42 UTC

[GitHub] [spark] Kimahriman opened a new pull request #34294: [SPARK-37019][SQL] Add codegen support to array transform

Kimahriman opened a new pull request #34294:
URL: https://github.com/apache/spark/pull/34294


   <!--
   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.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### 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 adds codegen support to ArrayTransform. This is my first time playing around with codegen, so definitely looking for any feedback. I ran into several issues along the way which you'll see in some checks I had to add. Specifically:
   - I added lambda variable tracking to the codegen context, to make sure a function can't be split out while there is any active lambda variables
   - Made sure lambda functions themselves can never be considered for subexpression elimination
   - I still have to set the atomic references in the codegen to support any children with CodegenFallback or just that fallback to interpreted in their codegen path
   
   Questions I have:
   - Does it make sense to support both the traditional ExprCode approach with lambda variables while also setting the atomic reference value for fallback cases? Or should I just use the atomic reference to get the value everywhere even in codegen cases since I have to set it anyway and simplify the code a little bit?
   - Obviously any other corner cases anyone can think of?
   
   ### 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.
   -->
   To improve performance of transform operations, letting the children be codegen'd and participate in WholeStageCodegen
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   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 possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   No, only performance improvements.
   
   ### 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.
   -->
   Existing unit tests, let me know if there's other codegen-specific unit tests I should add.


-- 
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] Kimahriman commented on pull request #34294: [SPARK-37019][SQL] Add codegen support to array transform

Posted by GitBox <gi...@apache.org>.
Kimahriman commented on pull request #34294:
URL: https://github.com/apache/spark/pull/34294#issuecomment-956889103


   Already ran into an issue testing this out in a real use case where making it non-splittable caused a function to grow to big and revert back to interpreted, so I changed to just always use the atomic variable reference to read the value so it can be splittable. New sample codegen:
   
   ```
   /* 039 */         final int project_numElements_0 = localtablescan_value_0.numElements();
   /* 040 */
   /* 041 */         ArrayData project_arrayData_0 = ArrayData.allocateArrayData(
   /* 042 */           4, project_numElements_0, " transform failed.");
   /* 043 */
   /* 044 */         for (int project_i_0 = 0; project_i_0 < project_numElements_0; project_i_0++) {
   /* 045 */           if (localtablescan_value_0.isNullAt(project_i_0)) {
   /* 046 */             ((java.util.concurrent.atomic.AtomicReference) references[2] /* x_229 */).set(null);
   /* 047 */           } else {
   /* 048 */             ((java.util.concurrent.atomic.AtomicReference) references[2] /* x_229 */).set(localtablescan_value_0.getInt(project_i_0));
   /* 049 */           }
   /* 050 */
   /* 051 */           boolean project_isNull_3 = true;
   /* 052 */           int project_value_3 = -1;
   /* 053 */           Object project_tmpAtomic_0 = ((java.util.concurrent.atomic.AtomicReference) references[1] /* x_229 */).get();
   /* 054 */           int project_value_4 = -1;
   /* 055 */           boolean project_isNull_4 = project_tmpAtomic_0 == null;
   /* 056 */           if (!project_isNull_4) {
   /* 057 */             project_value_4 = (Integer)project_tmpAtomic_0;
   /* 058 */           }
   /* 059 */           if (!project_isNull_4) {
   /* 060 */             project_isNull_3 = false; // resultCode could change nullability.
   /* 061 */
   /* 062 */             project_value_3 = project_value_4 + 1;
   /* 063 */
   /* 064 */           }
   /* 065 */           boolean project_isNull_2 = project_isNull_3;
   /* 066 */           int project_value_2 = project_value_3;
   /* 067 */
   /* 068 */           if (project_isNull_2) {
   /* 069 */             project_arrayData_0.setNullAt(project_i_0);
   /* 070 */           } else {
   /* 071 */             project_arrayData_0.setInt(project_i_0, project_value_2);
   /* 072 */           }
   /* 073 */
   /* 074 */         }
   /* 075 */         project_value_0 = project_arrayData_0;
   /* 076 */
   /* 077 */       }
   ```


-- 
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] Kimahriman commented on pull request #34294: [SPARK-37019][SQL] Add codegen support to array transform

Posted by GitBox <gi...@apache.org>.
Kimahriman commented on pull request #34294:
URL: https://github.com/apache/spark/pull/34294#issuecomment-956889103






-- 
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 #34294: [SPARK-37019][SQL] Add codegen support to array transform

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #34294:
URL: https://github.com/apache/spark/pull/34294#issuecomment-944253884


   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] Kimahriman edited a comment on pull request #34294: [SPARK-37019][SQL] Add codegen support to array transform

Posted by GitBox <gi...@apache.org>.
Kimahriman edited a comment on pull request #34294:
URL: https://github.com/apache/spark/pull/34294#issuecomment-949585166


   Codegen example for `df.selectExpr("transform(i, (x, i) -> x + i)")`:
   
   ```
   /* 038 */       if (!localtablescan_isNull_0) {
   /* 039 */         final int project_numElements_0 = localtablescan_value_0.numElements();
   /* 040 */
   /* 041 */         ArrayData project_arrayData_0 = ArrayData.allocateArrayData(
   /* 042 */           4, project_numElements_0, " transform failed.");
   /* 043 */
   /* 044 */         for (int project_i_0 = 0; project_i_0 < project_numElements_0; project_i_0++) {
   /* 045 */           int lambda_x_6 = localtablescan_value_0.getInt(project_i_0);
   /* 046 */           boolean lambda_x_6_isNull = localtablescan_value_0.isNullAt(project_i_0);
   /* 047 */           ((java.util.concurrent.atomic.AtomicReference) references[1] /* x_6 */).set(lambda_x_6);
   /* 048 */
   /* 049 */           int lambda_i_7 = project_i_0;
   /* 050 */           ((java.util.concurrent.atomic.AtomicReference) references[2] /* i_7 */).set(lambda_i_7);
   /* 051 */
   /* 052 */           boolean project_isNull_3 = true;
   /* 053 */           int project_value_3 = -1;
   /* 054 */
   /* 055 */           if (!lambda_x_6_isNull) {
   /* 056 */             project_isNull_3 = false; // resultCode could change nullability.
   /* 057 */
   /* 058 */             project_value_3 = lambda_x_6 + lambda_i_7;
   /* 059 */
   /* 060 */           }
   /* 061 */           boolean project_isNull_2 = project_isNull_3;
   /* 062 */           int project_value_2 = project_value_3;
   /* 063 */
   /* 064 */           if (project_isNull_2) {
   /* 065 */             project_arrayData_0.setNullAt(project_i_0);
   /* 066 */           } else {
   /* 067 */             project_arrayData_0.setInt(project_i_0, project_value_2);
   /* 068 */           }
   /* 069 */
   /* 070 */         }
   /* 071 */         project_value_0 = project_arrayData_0;
   /* 072 */
   /* 073 */       }
   ```


-- 
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] Kimahriman commented on pull request #34294: [SPARK-37019][SQL] Add codegen support to array transform

Posted by GitBox <gi...@apache.org>.
Kimahriman commented on pull request #34294:
URL: https://github.com/apache/spark/pull/34294#issuecomment-949585038






-- 
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] Kimahriman commented on pull request #34294: [SPARK-37019][SQL] Add codegen support to array transform

Posted by GitBox <gi...@apache.org>.
Kimahriman commented on pull request #34294:
URL: https://github.com/apache/spark/pull/34294#issuecomment-956889103






-- 
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] Kimahriman commented on pull request #34294: [SPARK-37019][SQL] Add codegen support to array transform

Posted by GitBox <gi...@apache.org>.
Kimahriman commented on pull request #34294:
URL: https://github.com/apache/spark/pull/34294#issuecomment-966321195


   Closing in favor of https://github.com/apache/spark/pull/34558


-- 
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] Kimahriman closed pull request #34294: [SPARK-37019][SQL] Add codegen support to array transform

Posted by GitBox <gi...@apache.org>.
Kimahriman closed pull request #34294:
URL: https://github.com/apache/spark/pull/34294


   


-- 
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] Kimahriman commented on a change in pull request #34294: [SPARK-37019][SQL] Add codegen support to array transform

Posted by GitBox <gi...@apache.org>.
Kimahriman commented on a change in pull request #34294:
URL: https://github.com/apache/spark/pull/34294#discussion_r729754395



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/higherOrderFunctions.scala
##########
@@ -338,6 +390,68 @@ case class ArrayTransform(
     result
   }
 
+  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {

Review comment:
       Some of this can probably be abstracted out into the parent traits, but I figured that will be easier to do when implementing a second 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] Kimahriman commented on pull request #34294: [SPARK-37019][SQL] Add codegen support to array transform

Posted by GitBox <gi...@apache.org>.
Kimahriman commented on pull request #34294:
URL: https://github.com/apache/spark/pull/34294#issuecomment-956891207


   ping @viirya @HyukjinKwon since you two usually look at my PRs or might know the right other people to ping for thoughts :)


-- 
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