spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liancheng <...@git.apache.org>
Subject [GitHub] spark pull request #14106: [SPARK-16448] RemoveAliasOnlyProject should not r...
Date Wed, 13 Jul 2016 08:40:05 GMT
Github user liancheng commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14106#discussion_r70585442
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
---
    @@ -165,36 +165,48 @@ object PushProjectThroughSample extends Rule[LogicalPlan] {
      * 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.
    +  /**
    +   * Returns true if the project list is semantically same with child output, after strip
alias on
    +   * attribute.
    +   */
       private def isAliasOnly(
           projectList: Seq[NamedExpression],
           childOutput: Seq[Attribute]): Boolean = {
    -    if (!projectList.forall(_.isInstanceOf[Alias]) || projectList.length != childOutput.length)
{
    +    if (projectList.length != childOutput.length) {
           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
    -        }
    +      stripAliasOnAttribute(projectList).zip(childOutput).forall {
    +        case (a: Attribute, o) if a semanticEquals o => true
    +        case _ => false
           }
         }
       }
     
    +  private def stripAliasOnAttribute(projectList: Seq[NamedExpression]) = {
    +    projectList.map {
    +      // Alias with metadata can not be striped, or the metadata will be lost.
    +      // If the alias name is different from attribute name, we can't strip it either,
or we may
    +      // accidentally change the output schema name of the root plan.
    +      case a @ Alias(attr: Attribute, name) if a.metadata == Metadata.empty &&
name == attr.name =>
    +        attr
    +      case other => other
    +    }
    +  }
    +
       def apply(plan: LogicalPlan): LogicalPlan = {
    -    val aliasOnlyProject = plan.find {
    -      case Project(pList, child) if isAliasOnly(pList, child.output) => true
    -      case _ => false
    +    val aliasOnlyProject = plan.collectFirst {
    +      case p @ Project(pList, child) if isAliasOnly(pList, child.output) => p
         }
     
    -    aliasOnlyProject.map { case p: Project =>
    -      val aliases = p.projectList.map(_.asInstanceOf[Alias])
    -      val attrMap = AttributeMap(aliases.map(a => (a.toAttribute, a.child)))
    +    aliasOnlyProject.map { case proj =>
    +      val attributesToReplace = proj.output.zip(proj.child.output).filterNot {
    +        case (a1, a2) => a1 semanticEquals a2
    +      }
    +      val attrMap = AttributeMap(attributesToReplace)
           plan.transformAllExpressions {
             case a: Attribute if attrMap.contains(a) => attrMap(a)
           }.transform {
    -        case op: Project if op.eq(p) => op.child
    +        case plan: Project if plan eq proj => plan.child
           }
         }.getOrElse(plan)
       }
    --- End diff --
    
    Can we use a `plan.transform` to implement this rule?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


Mime
View raw message