Indhumathi27 commented on code in PR #4257:
URL: https://github.com/apache/carbondata/pull/4257#discussion_r842664289


##########
integration/spark/src/main/scala/org/apache/spark/sql/optimizer/MVRewrite.scala:
##########
@@ -800,7 +822,54 @@ class MVRewrite(catalog: MVCatalogInSpark, logicalPlan: 
LogicalPlan,
         val planWrapper = groupBy.modularPlan.get.asInstanceOf[MVPlanWrapper]
         val plan = planWrapper.modularPlan.asInstanceOf[Select]
         val updatedPlanOutputList = getUpdatedOutputList(plan.outputList, 
groupBy.modularPlan)
-        val outputListMapping = groupBy.outputList zip updatedPlanOutputList
+        var columnIndex = -1
+        // get column from list having the given aggregate and column name.
+        def getColumnFromOutputList(updatedPlanOutputList: 
Seq[NamedExpression], aggregate: String,
+            colName: String): NamedExpression = {
+          val nextIndex = columnIndex + 1
+          if ((nextIndex) < updatedPlanOutputList.size &&
+              updatedPlanOutputList(nextIndex).name.contains(aggregate) &&
+              updatedPlanOutputList(nextIndex).name.contains(colName)) {
+            columnIndex += 1
+            updatedPlanOutputList(columnIndex)
+          } else {
+            updatedPlanOutputList.find(x => x.name.contains(aggregate) &&
+                                            x.name.contains(colName)).get
+          }
+        }
+        val outputListMapping = if 
(groupBy.outputList.exists(_.sql.contains("avg("))) {
+          // for each avg attribute, updatedPlanOutputList has 2 attributes 
(sum and count),
+          // so direct mapping of groupBy.outputList and updatedPlanOutputList 
is not possible.
+          // If query has avg, then get the sum, count attributes in the list 
and map accordingly.
+          for (exp <- groupBy.outputList) yield {
+            exp match {
+              case Alias(aggregateExpression: AggregateExpression, _)
+                if aggregateExpression.aggregateFunction.isInstanceOf[Average] 
=>
+                val colName = aggregateExpression.collectLeaves().head
+                  .asInstanceOf[AttributeReference].name
+                val sumAtt = getColumnFromOutputList(updatedPlanOutputList, 
"sum", colName)

Review Comment:
   ```suggestion
                   val sumAttr = getColumnFromOutputList(updatedPlanOutputList, 
"sum", colName)
   ```



-- 
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: dev-unsubscr...@carbondata.apache.org

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

Reply via email to