viirya commented on a change in pull request #23873: [SPARK-26975][SQL] Support 
nested-column pruning over limit/sample/repartition
URL: https://github.com/apache/spark/pull/23873#discussion_r260347557
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/NestedColumnPruning.scala
 ##########
 @@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.optimizer
+
+import scala.collection.mutable
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans.QueryPlan
+import org.apache.spark.sql.catalyst.plans.logical._
+import org.apache.spark.sql.types.StructType
+
+/**
+ * This aims to handle nested column pruning pattern inside `ColumnPruning` 
optimizer rule.
+ * If a project or its child references to nested fields, and not all the 
fields
+ * in a nested attribute are used, we can create a new [[CreateNamedStruct]] 
containing only
+ * subset of the fields as a new project in the child.
+ */
+object NestedColumnPruning {
+
+  type ExprMap = Map[Expression, Expression]
+  type RequiredFieldMap = Map[Attribute, Set[GetStructField]]
+
+  def unapply(plan: LogicalPlan): Option[(Map[ExprId, Alias], ExprMap)] = plan 
match {
+    case p @ Project(_, child) if canProjectPushThrough(child) =>
+      getPrunedNestedAttrsAndProjects(getRequiredFieldMap(p, child))
+    case _ => None
+  }
+
+  /**
+   * Add projects to prune unused nested columns.
+   */
+  def prune(
+      plan: LogicalPlan,
+      nestedAttrs: Map[ExprId, Alias],
+      projectsOnNestedAttrs: ExprMap): LogicalPlan = plan match {
+    case Project(projectList, g @ GlobalLimit(_, grandChild: LocalLimit)) =>
+      Project(
+        getNewProjectList(projectList, projectsOnNestedAttrs),
+        g.copy(child = addPrunedProjectToChildren(grandChild, nestedAttrs)))
+    case Project(projectList, child) =>
+      Project(
+        getNewProjectList(projectList, projectsOnNestedAttrs),
+        addPrunedProjectToChildren(child, nestedAttrs))
+    case _ => plan
+  }
+
+  /**
+   * Get a map from a referenced attribute to its set of required fields.
+   */
+  private def getRequiredFieldMap(plans: LogicalPlan*): RequiredFieldMap = {
+    val rootReferenceAttrSet = plans.map(getRootReferences).reduce(_ ++ _)
+    val nestedFieldReferences = plans.map(getNestedFieldReferences).reduce(_ 
++ _)
+
+    nestedFieldReferences
+      .filter(!_.references.subsetOf(rootReferenceAttrSet))
+      .groupBy(_.references)
+      .filter(_._1.size == 1)
 
 Review comment:
   `nestedFieldReferences` contains only `GetStructField`. Will it possibly 
have `_._1.size > 1`?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to