Github user viirya commented on a diff in the pull request:
https://github.com/apache/spark/pull/7593#discussion_r35298084
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
---
@@ -910,6 +911,48 @@ class Analyzer(
Project(finalProjectList, withWindow)
}
}
+
+ /**
+ * Removes all still-need-evaluate ordering expressions from sort and
use an inner project to
+ * materialize them, finally use a outer project to project them away to
keep the result same.
+ * Then we can make sure we only sort by [[AttributeReference]]s.
+ *
+ * As an example,
+ * {{{
+ * Sort('a, 'b + 1,
+ * Relation('a, 'b))
+ * }}}
+ * will be turned into:
+ * {{{
+ * Project('a, 'b,
+ * Sort('a, '_sortCondition,
+ * Project('a, 'b, ('b + 1).as("_sortCondition"),
+ * Relation('a, 'b))))
+ * }}}
+ */
+ object RemoveEvaluationFromSort extends Rule[LogicalPlan] {
+ override def apply(plan: LogicalPlan): LogicalPlan = plan transform {
+ case s @ Sort(ordering, global, child)
+ if s.expressions.forall(_.resolved) && s.childrenResolved &&
!s.hasNoEvaluation =>
+
+ val (ref, needEval) =
ordering.partition(_.child.isInstanceOf[AttributeReference])
+
+ val namedExpr = needEval.map(_.child match {
+ case n: NamedExpression => n
+ case e => Alias(e, "_sortCondition")()
--- End diff --
Is it possible that we will have multiple conditions needed to alias?
---
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]