AngersZhuuuu commented on a change in pull request #28490: URL: https://github.com/apache/spark/pull/28490#discussion_r436322459
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala ########## @@ -1479,11 +1486,70 @@ class Analyzer( // Skip the having clause here, this will be handled in ResolveAggregateFunctions. case h: UnresolvedHaving => h + case p: LogicalPlan if needResolveStructField(p) => + logTrace(s"Attempting to resolve ${p.simpleString(SQLConf.get.maxToStringFields)}") + val resolved = p.mapExpressions(resolveExpressionTopDown(_, p)) + val structFieldMap = new mutable.HashMap[String, Alias] + resolved.transformExpressions { + case a @ Alias(struct: GetStructField, _) => + if (structFieldMap.contains(struct.sql)) { + val exprId = structFieldMap.getOrElse(struct.sql, a).exprId + Alias(a.child, a.name)(exprId, a.qualifier, a.explicitMetadata) + } else { + structFieldMap.put(struct.sql, a) + a + } + case e => e + } + case q: LogicalPlan => logTrace(s"Attempting to resolve ${q.simpleString(SQLConf.get.maxToStringFields)}") q.mapExpressions(resolveExpressionTopDown(_, q)) } + def needResolveStructField(plan: LogicalPlan): Boolean = { + plan match { + case UnresolvedHaving(havingCondition, a: Aggregate) + if containSameStructFields(a.groupingExpressions.flatMap(_.references), + a.aggregateExpressions.flatMap(_.references), + Some(havingCondition.references.toSeq)) => true + case Aggregate(groupingExpressions, aggregateExpressions, _) + if containSameStructFields(groupingExpressions.flatMap(_.references), + aggregateExpressions.flatMap(_.references)) => true + case GroupingSets(selectedGroupByExprs, groupByExprs, _, aggregations) + if containSameStructFields(groupByExprs.flatMap(_.references), + aggregations.flatMap(_.references), + Some(selectedGroupByExprs.flatMap(_.flatMap(_.references)))) => true + case _ => false + } + } + + def containSameStructFields( Review comment: > It is not enough just to check if both sides (`grpExprs` and `aggExprs`) have struct fields here? GroupingSets need check selected cols too, so all need to check. ---------------------------------------------------------------- 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: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org