Github user zsxwing commented on a diff in the pull request:
https://github.com/apache/spark/pull/12926#discussion_r62935527
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
---
@@ -156,22 +157,67 @@ 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 checkAliasOnly(
+ 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 processedPlan = plan.find { p =>
+ p match {
+ case Project(pList, child) if checkAliasOnly(pList, child.output)
=> true
+ case _ => false
+ }
+ }.map { case p: Project =>
+ val attrMap = p.projectList.map { a =>
+ val alias = a.asInstanceOf[Alias]
+ val replaceFrom = alias.toAttribute.exprId
+ val replaceTo = alias.child.asInstanceOf[Attribute]
+ (replaceFrom, replaceTo)
+ }.toMap
+ plan.transformAllExpressions {
+ case a: Attribute if attrMap.contains(a.exprId) =>
attrMap(a.exprId)
+ }.transform {
+ case op: Project if op == p => op.child
+ }
+ }
+ if (processedPlan.isDefined) {
--- End diff --
nit: Why not `processedPlan.getOrElse(plan)`?
---
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]