cloud-fan commented on a change in pull request #34701:
URL: https://github.com/apache/spark/pull/34701#discussion_r758294477
##########
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
+ && g.generator.isInstanceOf[ExplodeBase] =>
+ g.generator.children.head.dataType match {
+ case ArrayType(StructType(fields), _) =>
+ val atomicFields = fields.collect {
+ case f: StructField if f.dataType.isInstanceOf[AtomicType] => f
+ }
+ val extractor = if (atomicFields.size > 0) {
+ // Pick an arbitrary atomic field, if any
+ ExtractValue(g.generator.children.head,
+ Literal(atomicFields(0).name), SQLConf.get.resolver)
Review comment:
nit: `conf.resolver`
--
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]