You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "beliefer (via GitHub)" <gi...@apache.org> on 2023/10/04 09:45:32 UTC

Re: [PR] [SPARK-45352][SQL] Remove window partition if partition expressions are foldable [spark]

beliefer commented on code in PR #43144:
URL: https://github.com/apache/spark/pull/43144#discussion_r1345515920


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -1241,6 +1242,23 @@ object OptimizeRepartition extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Remove window partition if partition expressions are foldable
+ */
+object EliminateWindowPartitions extends Rule[LogicalPlan] {
+  override def apply(plan: LogicalPlan): LogicalPlan = removeWindowExpressionPartitions(plan)
+    .transformWithPruning(_.containsPattern(WINDOW), ruleId) {
+      case w @ Window(_, ps, _, _) if ps.exists(_.foldable) =>
+        w.copy(partitionSpec = ps.filter(!_.foldable))
+    }
+
+  def removeWindowExpressionPartitions(plan: LogicalPlan): LogicalPlan =
+    plan.transformAllExpressionsWithPruning(_.containsPattern(WINDOW_EXPRESSION), ruleId) {
+      case we @ WindowExpression(_, ws @ WindowSpecDefinition(ps, _, _)) if ps.exists(_.foldable) =>
+        we.copy(windowSpec = ws.copy(partitionSpec = ps.filter(!_.foldable)))
+    }
+}

Review Comment:
   It seems we can simplify the code here with `transformWithPruning` only, then we can remove the `removeWindowExpressionPartitions`.



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