Github user marmbrus commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7957#discussion_r37118409
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ---
    @@ -968,3 +961,61 @@ object EliminateSubQueries extends Rule[LogicalPlan] {
         case Subquery(_, child) => child
       }
     }
    +
    +/**
    + * Cleans up unnecessary Aliases inside the plan. Basically we only need 
Alias as a top level
    + * expression in Project(project list) or Aggregate(aggregate expressions) 
or
    + * Window(window expressions).
    + */
    +object CleanupAliases extends Rule[LogicalPlan] {
    +  private def trimAliases(e: Expression): Expression = {
    +    var stop = false
    +    e.transformDown {
    +      // CreateStruct is a special case, we need to retain its top level 
Aliases as they decide the
    +      // name of StructField. We also need to stop transform down this 
expression, or the Aliases
    +      // under CreateStruct will be mistakenly trimmed.
    +      case c: CreateStruct if !stop =>
    +        stop = true
    +        c.copy(children = c.children.map(trimNonTopLevelAliases))
    +      case c: CreateStructUnsafe if !stop =>
    +        stop = true
    +        c.copy(children = c.children.map(trimNonTopLevelAliases))
    +      case Alias(child, _) if !stop => child
    +    }
    +  }
    +
    +  def trimNonTopLevelAliases(e: Expression): Expression = e match {
    +    case a: Alias =>
    +      Alias(trimAliases(a.child), a.name)(a.exprId, a.qualifiers, 
a.explicitMetadata)
    +    case other => trimAliases(other)
    +  }
    +
    +  override def apply(plan: LogicalPlan): LogicalPlan = plan 
resolveOperators {
    +    case Project(projectList, child) =>
    +      val cleanedProjectList =
    +        
projectList.map(trimNonTopLevelAliases(_).asInstanceOf[NamedExpression])
    +      Project(cleanedProjectList, child)
    +
    +    case Aggregate(grouping, aggs, child) =>
    +      val cleanedAggs = 
aggs.map(trimNonTopLevelAliases(_).asInstanceOf[NamedExpression])
    +      Aggregate(grouping.map(trimAliases), cleanedAggs, child)
    +
    +    case w @ Window(projectList, windowExprs, partitionSpec, orderSpec, 
child) =>
    +      val cleanedWindowExprs =
    +        windowExprs.map(e => 
trimNonTopLevelAliases(e).asInstanceOf[NamedExpression])
    +      Window(projectList, cleanedWindowExprs, 
partitionSpec.map(trimAliases),
    +        orderSpec.map(trimAliases(_).asInstanceOf[SortOrder]), child)
    +
    +    case other =>
    +      var stop = false
    --- End diff --
    
    Not for this release, but this makes me think that we are abusing aliases.  
I would rather that resolved expressions past the analyzer move the names out 
of the subexpressions and into the `CreateStruct` expression itself.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

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

Reply via email to