You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2019/12/23 00:44:51 UTC

[GitHub] [spark] maropu commented on a change in pull request #26978: [SPARK-29721][SQL] Prune unnecessary nested fields from Generate without Project

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

 ##########
 File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/NestedColumnAliasing.scala
 ##########
 @@ -155,6 +155,55 @@ 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 {
+    case p @ Project(_, g: Generate) if SQLConf.get.nestedPruningOnExpressions &&
+        canPruneGenerator(g.generator) =>
+      Some(p)
+    case g: Generate if SQLConf.get.nestedPruningOnExpressions &&
+        canPruneGenerator(g.generator) =>
+      Some(g)
+    case _ => None
+  }
+
+  private def pruneGenerate(
+      g: Generate,
+      nestedFieldToAlias: Map[ExtractValue, Alias],
+      attrToAliases: Map[ExprId, Seq[Alias]]): LogicalPlan = {
+    val newGenerator = g.generator.transform {
+      case f: ExtractValue if nestedFieldToAlias.contains(f) =>
+        nestedFieldToAlias(f).toAttribute
+    }.asInstanceOf[Generator]
+
+    // Defer updating `Generate.unrequiredChildIndex` to next round of `ColumnPruning`.
+    val newGenerate = g.copy(generator = newGenerator)
+
+    NestedColumnAliasing.replaceChildrenWithAliases(newGenerate, attrToAliases)
+  }
+
+  // Prunes unnecessary nested columns from `Generate`.
+  def prune(plan: LogicalPlan): LogicalPlan = plan match {
+    case p @ Project(projectList, g: Generate) =>
 
 Review comment:
   It seems this pr has the same pattern matching cases here and in the extractor above: https://github.com/apache/spark/pull/26978/files#diff-43334bab9616cc53e8797b9afa9fc7aaR166
   To avoid the duplicated pattern matching, `GeneratorNestedColumnAliasing ` cannot just return pruned logical plans in the extractor side for`ColumnPruning`?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

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