cloud-fan commented on a change in pull request #33587:
URL: https://github.com/apache/spark/pull/33587#discussion_r682463016
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -518,7 +518,22 @@ object RemoveNoopOperators extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan.transformUpWithPruning(
_.containsAnyPattern(PROJECT, WINDOW), ruleId) {
// Eliminate no-op Projects
- case p @ Project(_, child) if child.sameOutput(p) => child
+ case p @ Project(output, child) if child.sameOutput(p) =>
+ child match {
+ case _: Project | _: Aggregate =>
Review comment:
let's be more explicit
```
val newChild = child match {
case p: Project =>
val newList = p.projectList.map {
case attr: Attribute => ...
case alias: Alias => ...
case other => other
}
p.copy(projectList = newList)
...
}
if (newChild.output.zip(output).forall ...) ...
```
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala
##########
@@ -333,6 +333,9 @@ abstract class Star extends LeafExpression with
NamedExpression {
override def qualifier: Seq[String] = throw new
UnresolvedException("qualifier")
override def toAttribute: Attribute = throw new
UnresolvedException("toAttribute")
override def newInstance(): NamedExpression = throw new
UnresolvedException("newInstance")
+
+ override def withName(newName: String): NamedExpression =
Review comment:
these changes are not needed any more
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -518,7 +518,36 @@ object RemoveNoopOperators extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan.transformUpWithPruning(
_.containsAnyPattern(PROJECT, WINDOW), ruleId) {
// Eliminate no-op Projects
- case p @ Project(_, child) if child.sameOutput(p) => child
+ case p @ Project(projectList, child) if child.sameOutput(p) =>
+ val newChild = child match {
+ case p: Project =>
+ val newList = p.projectList.map {
Review comment:
shall we create a method to put this code piece?
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -515,10 +515,37 @@ object RemoveRedundantAliases extends Rule[LogicalPlan] {
* Remove no-op operators from the query plan that do not make any
modifications.
*/
object RemoveNoopOperators extends Rule[LogicalPlan] {
+ def resolveOutputAttrName(
Review comment:
nit: `restoreOriginalOutputName`
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -515,10 +515,37 @@ object RemoveRedundantAliases extends Rule[LogicalPlan] {
* Remove no-op operators from the query plan that do not make any
modifications.
*/
object RemoveNoopOperators extends Rule[LogicalPlan] {
+ def resolveOutputAttrName(
+ output: Seq[NamedExpression],
+ projectList: Seq[NamedExpression]): Seq[NamedExpression] = {
+ output.map {
+ case attr: Attribute =>
+
attr.withName(projectList.find(_.semanticEquals(attr)).getOrElse(attr).name)
Review comment:
do we need to call `find`? `child.sameOutput(p)` should guarantee that
the output columns match by position. We just need
```
def restoreOriginalOutputNames(
projectList: Seq[NamedExpression],
originalNames: Seq[String]): Seq[NamedExpression] = {
projectList.zip(originalNames).map {
case (attr: Attribute, name) => attr.withName(name)
...
}
}
```
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -515,10 +515,37 @@ object RemoveRedundantAliases extends Rule[LogicalPlan] {
* Remove no-op operators from the query plan that do not make any
modifications.
*/
object RemoveNoopOperators extends Rule[LogicalPlan] {
+ def restoreOriginalOutputName(
Review comment:
```suggestion
def restoreOriginalOutputNames(
```
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -515,10 +515,33 @@ object RemoveRedundantAliases extends Rule[LogicalPlan] {
* Remove no-op operators from the query plan that do not make any
modifications.
*/
object RemoveNoopOperators extends Rule[LogicalPlan] {
+ def restoreOriginalOutputNames(
+ projectList: Seq[NamedExpression],
+ originalNames: Seq[String]): Seq[NamedExpression] = {
+ projectList.zip(originalNames).map {
+ case (attr: Attribute, name) => attr.withName(name)
+ case (alias: Alias, name) => alias.withName(name)
Review comment:
let's add the default case to be safe:
```
case (other, _) => other
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]