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


##########
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:
   If  do as you say, this will cause the `partitionSpec` in `WindowExpression` 
to be inconsistent with the `partitionSpec` in `Window`, which may cause hidden 
dangers.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to