viirya commented on a change in pull request #27517: [WIP][SPARK-29721][SQL] 
Prune unnecessary nested fields from Generate without Project
URL: https://github.com/apache/spark/pull/27517#discussion_r376899012
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/NestedColumnAliasing.scala
 ##########
 @@ -155,6 +161,56 @@ object NestedColumnAliasing {
     case MapType(keyType, valueType, _) => totalFieldNum(keyType) + 
totalFieldNum(valueType)
     case _ => 1 // UDT and others
   }
+}
+
+/**
+ * This prunes unnessary nested columns from `Generate` and optional `Project` 
on top
+ * of it.
+ */
+object GeneratorNestedColumnAliasing {
+  def unapply(plan: LogicalPlan): Option[LogicalPlan] = plan match {
+    // Either `nestedPruningOnExpressions` or `nestedSchemaPruningEnabled` is 
enabled, we
+    // need to prune nested columns through Project and under Generate. The 
difference is
+    // when `nestedSchemaPruningEnabled` is on, nested columns will be pruned 
further at
+    // file format readers if it is supported.
+    case Project(projectList, g: Generate) if 
(SQLConf.get.nestedPruningOnExpressions ||
+        SQLConf.get.nestedSchemaPruningEnabled) && 
canPruneGenerator(g.generator) =>
+      // On top on `Generate`, a `Project` that might have nested column 
accessors.
+      // We try to get alias maps for both project list and generator's 
children expressions.
+      NestedColumnAliasing.getAliasSubMap(projectList ++ 
g.generator.children).map {
+        case (nestedFieldToAlias, attrToAliases) =>
+          val newChild = pruneGenerate(g, nestedFieldToAlias, attrToAliases)
+          Project(NestedColumnAliasing.getNewProjectList(projectList, 
nestedFieldToAlias), newChild)
+      }
+
+    case g: Generate if SQLConf.get.nestedSchemaPruningEnabled &&
+        canPruneGenerator(g.generator) =>
+      // For the child outputs required by the operator on top of `Generate`, 
we do not want
+      // to prune it.
 
 Review comment:
   Ok. Let me improve this comment.

----------------------------------------------------------------
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


With regards,
Apache Git Services

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

Reply via email to