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/03/04 16:46:47 UTC

[GitHub] [spark] tanelk opened a new pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

tanelk opened a new pull request #31740:
URL: https://github.com/apache/spark/pull/31740


   <!--
   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'.
   -->
   
   ### 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.
   -->
   Added an optimizer rule `DeduplicateWindowExpressions`.
   
   ### 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.
   -->
   Duplicate window expressions can be introduced to the same `Window` node by the analyzer rule `ExtractWindowExpressions`, by the optimizer rule `CollapseWindow` or by the user. Currently these duplicate expressions are executed twice.
   
   ### 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
   
   ### 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.
   -->
   UTs


----------------------------------------------------------------
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:
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] tanelk commented on a change in pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -946,6 +947,45 @@ object TransposeWindow extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Replaces duplicate window expressions with an alias in a Project above the Window node.

Review comment:
       I see, thanks for the reference.
   Indeed, the common subexpression elimination seems to eliminate these duplicates (if I did follow the code correctly).
   I would argue, that there still is benefit in removing these duplicates in the optimizer. If nothing else, then it at least would clean up the plan.
   Also the subexpression cache has a limited size (although configurable) and this would reduce the runtime overhead of cache lookups.
   
    




----------------------------------------------------------------
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:
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] SparkQA commented on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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


   **[Test build #135764 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/135764/testReport)** for PR 31740 at commit [`c3fe84e`](https://github.com/apache/spark/commit/c3fe84ebc4ea57e04222c7f08c93aeb8202993d0).


----------------------------------------------------------------
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:
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] SparkQA commented on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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


   **[Test build #135831 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/135831/testReport)** for PR 31740 at commit [`624a41d`](https://github.com/apache/spark/commit/624a41dd5e0232c746c62629bbb4983259ea9b0c).


----------------------------------------------------------------
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:
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 #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/40413/
   


----------------------------------------------------------------
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:
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] SparkQA removed a comment on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

Posted by GitBox <gi...@apache.org>.
SparkQA removed a comment on pull request #31740:
URL: https://github.com/apache/spark/pull/31740#issuecomment-790768064


   **[Test build #135764 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/135764/testReport)** for PR 31740 at commit [`c3fe84e`](https://github.com/apache/spark/commit/c3fe84ebc4ea57e04222c7f08c93aeb8202993d0).


----------------------------------------------------------------
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:
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] SparkQA commented on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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


   Kubernetes integration test starting
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/40347/
   


----------------------------------------------------------------
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:
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] SparkQA commented on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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


   **[Test build #135831 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/135831/testReport)** for PR 31740 at commit [`624a41d`](https://github.com/apache/spark/commit/624a41dd5e0232c746c62629bbb4983259ea9b0c).
    * This patch passes all tests.
    * This patch merges cleanly.
    * This patch adds no public classes.


----------------------------------------------------------------
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:
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] tanelk closed pull request #31740: [SPARK-34623][SQL] Remove duplicate window expressions from the Window node

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


   


-- 
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:
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] SparkQA removed a comment on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

Posted by GitBox <gi...@apache.org>.
SparkQA removed a comment on pull request #31740:
URL: https://github.com/apache/spark/pull/31740#issuecomment-791968345


   **[Test build #135831 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/135831/testReport)** for PR 31740 at commit [`624a41d`](https://github.com/apache/spark/commit/624a41dd5e0232c746c62629bbb4983259ea9b0c).


----------------------------------------------------------------
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:
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] maropu commented on a change in pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -90,6 +90,7 @@ abstract class Optimizer(catalogManager: CatalogManager)
         // Constant folding and strength reduction
         OptimizeRepartition,
         TransposeWindow,
+        DeduplicateWindowExpressions,

Review comment:
       `DeduplicateWindowExpressions` -> `RemoveDeduplicateWindowExprs`?




----------------------------------------------------------------
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:
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] cloud-fan commented on a change in pull request #31740: [SPARK-34623][SQL] Remove duplicate window expressions from the Window node

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on a change in pull request #31740:
URL: https://github.com/apache/spark/pull/31740#discussion_r589271430



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -946,6 +947,45 @@ object TransposeWindow extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Replaces duplicate window expressions with an alias in a Project above the Window node.
+ */
+object RemoveDuplicateWindowExprs extends Rule[LogicalPlan] {

Review comment:
       Shall we do the same thing for project and aggregate? Another big idea is to re-implement subexpression elimination in the plan level.




----------------------------------------------------------------
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:
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 removed a comment on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #31740:
URL: https://github.com/apache/spark/pull/31740#issuecomment-791990345


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/40413/
   


----------------------------------------------------------------
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:
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 removed a comment on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #31740:
URL: https://github.com/apache/spark/pull/31740#issuecomment-790849085


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/40347/
   


----------------------------------------------------------------
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:
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 #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/135831/
   


----------------------------------------------------------------
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:
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] tanelk commented on a change in pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -946,6 +947,45 @@ object TransposeWindow extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Replaces duplicate window expressions with an alias in a Project above the Window node.

Review comment:
       Isn't the subexpression elimination wholestage codegen specific?




----------------------------------------------------------------
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:
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] maropu commented on a change in pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -946,6 +947,45 @@ object TransposeWindow extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Replaces duplicate window expressions with an alias in a Project above the Window node.

Review comment:
       The interpreted mode has the optimization, too, e.g., `SubExprEvaluationRuntime`.




----------------------------------------------------------------
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:
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] SparkQA commented on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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


   **[Test build #135764 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/135764/testReport)** for PR 31740 at commit [`c3fe84e`](https://github.com/apache/spark/commit/c3fe84ebc4ea57e04222c7f08c93aeb8202993d0).
    * This patch passes all tests.
    * This patch merges cleanly.
    * This patch adds no public classes.


----------------------------------------------------------------
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:
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 removed a comment on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #31740:
URL: https://github.com/apache/spark/pull/31740#issuecomment-790973419


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/135764/
   


----------------------------------------------------------------
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:
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] SparkQA commented on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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


   Kubernetes integration test status success
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/40347/
   


----------------------------------------------------------------
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:
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] maropu commented on a change in pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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



##########
File path: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/DeduplicateWindowExpressionsSuite.scala
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.
+ */
+
+package org.apache.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.catalyst.dsl.expressions._
+import org.apache.spark.sql.catalyst.dsl.plans._
+import org.apache.spark.sql.catalyst.expressions.Literal
+import org.apache.spark.sql.catalyst.plans.PlanTest
+import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.RuleExecutor
+
+class DeduplicateWindowExpressionsSuite extends PlanTest {
+  object Optimize extends RuleExecutor[LogicalPlan] {
+    val batches =
+      Batch("DeduplicateWindowExpressions", Once, DeduplicateWindowExpressions) :: Nil
+  }
+
+  val testRelation = LocalRelation($"a".int, $"b".int)
+
+  val a = testRelation.output(0)
+  val b = testRelation.output(1)
+
+  test("Deduplicate equal expressions") {
+    val query = testRelation
+      .window(Seq(sum(a).as("a_sum1"), sum(a).as("a_sum2")), Seq.empty, Seq.empty)
+      .analyze
+
+    val optimized = Optimize.execute(query)
+
+    val expected = testRelation
+      .window(Seq(sum(a).as("a_sum1")), Seq.empty, Seq.empty)
+      .select($"a", $"b", $"a_sum1", $"a_sum1" as "a_sum2")
+      .analyze
+
+    comparePlans(optimized, expected)
+  }
+
+  test("Deduplicate equivalent expressions") {
+    val query = testRelation
+      .window(
+        Seq(sum(Literal(2) * a).as("a_sum1"), sum(a * Literal(2)).as("a_sum2")),
+        Seq.empty, Seq.empty)
+      .analyze
+
+    val optimized = Optimize.execute(query)
+
+    val expected = testRelation
+      .window(Seq(sum(Literal(2) * a).as("a_sum1")), Seq.empty, Seq.empty)
+      .select($"a", $"b", $"a_sum1", $"a_sum1" as "a_sum2")
+      .analyze
+
+    comparePlans(optimized, expected)
+  }
+
+  test("Do not deduplicate distinct expressions") {
+    val query = testRelation
+      .window(Seq(sum(a).as("a_sum"), sum(b).as("b_sum")), Seq.empty, Seq.empty)
+      .analyze
+
+    val optimized = Optimize.execute(query)
+
+    comparePlans(optimized, query)
+  }
+

Review comment:
       nit: remove the blank.




----------------------------------------------------------------
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:
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 removed a comment on pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #31740:
URL: https://github.com/apache/spark/pull/31740#issuecomment-792043503


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/135831/
   


----------------------------------------------------------------
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:
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] maropu commented on a change in pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -946,6 +947,45 @@ object TransposeWindow extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Replaces duplicate window expressions with an alias in a Project above the Window node.

Review comment:
       Looks like this is the case that the logic of sub common exprs should handle? cc: @viirya @cloud-fan 




----------------------------------------------------------------
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:
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 #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/40347/
   


----------------------------------------------------------------
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:
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 #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/135764/
   


----------------------------------------------------------------
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:
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] tanelk commented on a change in pull request #31740: [SPARK_34623][SQL] Remove duplicate window expressions from the Window node

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



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -946,6 +947,45 @@ object TransposeWindow extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Replaces duplicate window expressions with an alias in a Project above the Window node.

Review comment:
       I see, thanks for the reference.
   Indeed, the common subexpression elimination seems to eliminate these duplicates (if I did follow the code correctly).
   I would argue, that there still is benefit in removing these duplicates in the optimizer. If nothing else, then it at least would clean up the plan - when I saw the duplicates in the plan, then it prompted me to try to rewrite the query in such way that it would avoid them.
   Also the subexpression cache has a limited size (although configurable) and this would reduce the runtime overhead of cache lookups.
   
    




----------------------------------------------------------------
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:
users@infra.apache.org



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