viirya commented on a change in pull request #34701:
URL: https://github.com/apache/spark/pull/34701#discussion_r758616810



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -2162,6 +2163,74 @@ object RemoveLiteralFromGroupExpressions extends 
Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Prunes unnecessary fields from a [[Generate]] if it is under a count 
aggregation
+ * query.
+ */
+object CountAggregateOptimization extends Rule[LogicalPlan] {
+  private def isLiteralCountAggFun(func: AggregateFunction): Boolean = func 
match {
+    case c: Count if c.children.size == 1 && 
c.children(0).isInstanceOf[Literal] => true
+    case _ => false
+  }
+
+  private def isCandidate(agg: Aggregate): Boolean = {
+    if (agg.aggregateExpressions.size == 1) {
+      agg.aggregateExpressions(0) match {
+        case Alias(ae: AggregateExpression, _) if 
isLiteralCountAggFun(ae.aggregateFunction) =>
+          true
+        case _ =>
+          false
+      }
+    } else {
+      false
+    }
+  }
+
+  private def pruningFields(agg: Aggregate): LogicalPlan = {
+    agg.transformDownWithPruning(_.containsPattern(GENERATE), ruleId) {
+      case p @ Project(_, g: Generate) if 
p.references.intersect(g.outputSet).isEmpty

Review comment:
       For the example query, the optimized query plan has the `Project`. I 
think it is because column pruning.




-- 
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-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



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

Reply via email to