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

    https://github.com/apache/spark/pull/8066#discussion_r46452998
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
 ---
    @@ -919,3 +920,63 @@ object RemoveLiteralFromGroupExpressions extends 
Rule[LogicalPlan] {
           a.copy(groupingExpressions = newGrouping)
       }
     }
    +
    +/**
    + * Remove the unnecessary evaluation from SortOrder, if they are already 
in child's output.
    + *
    + * As an example, "select a + 1, b from t order by a + 1" will be analyzed 
into:
    + * {{{
    + *   Project(reference#2, b#0,
    + *     Sort('a#1 + 1,
    + *       Project(('a + 1).as("_c0")#2, b#0, a#1),
    + *         Relation)
    + * }}}
    + * Then this rule can optimize it into:
    + * {{{
    + *   Project(reference#2, b#0,
    + *     Sort(reference#2,
    + *       Project(('a + 1).as("_c0")#2, b#0, a#1),
    + *         Relation)
    + * }}}
    + * Finally other optimize rules(column pruning, project collapse) can turn 
it into:
    + * {{{
    + *   Sort(reference#2,
    + *     Project(('a + 1).as("_c0")#2, b#0),
    + *       Relation)
    + * }}}
    + */
    +object RemoveUnnecessarySortOrderEvaluation extends Rule[LogicalPlan] {
    +
    +  private def optimizeSortOrders(orders: Seq[SortOrder], childOutput: 
Seq[NamedExpression]) = {
    +    orders.map { order =>
    +      val newChild = order.child transformDown { case expr =>
    +        val index = childOutput.indexWhere {
    +          case Alias(child, _) => child semanticEquals expr
    +          case other => other semanticEquals expr
    +        }
    +        if (index == -1) {
    +          expr
    +        } else {
    +          childOutput(index).toAttribute
    +        }
    --- End diff --
    
    Would this be simpler as a `find`?


---
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