Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/12926#discussion_r62971803
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
 ---
    @@ -156,22 +157,60 @@ object SamplePushDown extends Rule[LogicalPlan] {
     }
     
     /**
    + * Removes the Project only conducting Alias of its child node.
    + * It is created mainly for removing extra Project added in 
EliminateSerialization rule,
    + * but can also benefit other operators.
    + */
    +object RemoveAliasOnlyProject extends Rule[LogicalPlan] {
    +  // Check if projectList in the Project node has the same attribute names 
and ordering
    +  // as its child node.
    +  private def isAliasOnly(
    +      projectList: Seq[NamedExpression],
    +      childOutput: Seq[Attribute]): Boolean = {
    +    if (!projectList.forall(_.isInstanceOf[Alias]) || projectList.length 
!= childOutput.length) {
    +      return false
    +    } else {
    +      projectList.map(_.asInstanceOf[Alias]).zip(childOutput).forall { 
case (a, o) =>
    +        a.child match {
    +          case attr: Attribute if a.name == attr.name && 
attr.semanticEquals(o) => true
    +          case _ => false
    +        }
    +      }
    +    }
    +  }
    +
    +  def apply(plan: LogicalPlan): LogicalPlan = {
    +    val aliasOnlyProject = plan.find { p =>
    +      p match {
    +        case Project(pList, child) if isAliasOnly(pList, child.output) => 
true
    +        case _ => false
    +      }
    +    }
    +
    +    aliasOnlyProject.map { case p: Project =>
    +      val aliases = p.projectList.map(_.asInstanceOf[Alias])
    +      val attrMap = AttributeMap(aliases.map(a => (a.toAttribute, 
a.child)))
    +      plan.transformAllExpressions {
    +        case a: Attribute if attrMap.contains(a) => attrMap(a)
    +      }.transform {
    +        case op: Project if op.eq(p) => op.child
    +      }
    +    }.getOrElse(plan)
    +  }
    +}
    +
    +/**
      * Removes cases where we are unnecessarily going between the object and 
serialized (InternalRow)
      * representation of data item.  For example back to back map operations.
      */
     object EliminateSerialization extends Rule[LogicalPlan] {
       def apply(plan: LogicalPlan): LogicalPlan = plan transform {
         case d @ DeserializeToObject(_, _, s: SerializeFromObject)
             if d.outputObjectType == s.inputObjectType =>
    -      // A workaround for SPARK-14803. Remove this after it is fixed.
    -      if (d.outputObjectType.isInstanceOf[ObjectType] &&
    -          d.outputObjectType.asInstanceOf[ObjectType].cls == 
classOf[org.apache.spark.sql.Row]) {
    -        s.child
    -      } else {
    -        // Adds an extra Project here, to preserve the output expr id of 
`DeserializeToObject`.
    -        val objAttr = Alias(s.child.output.head, "obj")(exprId = 
d.output.head.exprId)
    -        Project(objAttr :: Nil, s.child)
    -      }
    +      // Adds an extra Project here, to preserve the output expr id of 
`DeserializeToObject`.
    +      // We will remove it later in RemoveAliasOnlyProject rule.
    +      val objAttr = Alias(s.child.output.head, "obj")(exprId = 
d.output.head.exprId)
    --- End diff --
    
    nit: use `Alias(s.child.output.head, s.child.output.head.name)(exprId = 
d.output.head.exprId)` to make sure the alias name is same with the attribute 
name


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to