Github user gatorsmile commented on a diff in the pull request: https://github.com/apache/spark/pull/11555#discussion_r55442915 --- Diff: sql/hive/src/main/scala/org/apache/spark/sql/hive/SQLBuilder.scala --- @@ -338,20 +353,37 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi | _: Sample ) => plan - case plan: Project => - wrapChildWithSubquery(plan) + case plan: Project => wrapChildWithSubquery(plan) + + case w @ Window(_, _, _, _, + _: SubqueryAlias + | _: Filter + | _: Join + | _: MetastoreRelation + | OneRowRelation + | _: LocalLimit + | _: GlobalLimit + | _: Sample + ) => w + + case w: Window => wrapChildWithSubquery(w) } - def wrapChildWithSubquery(project: Project): Project = project match { - case Project(projectList, child) => - val alias = SQLBuilder.newSubqueryName - val childAttributes = child.outputSet - val aliasedProjectList = projectList.map(_.transform { - case a: Attribute if childAttributes.contains(a) => - a.withQualifiers(alias :: Nil) - }.asInstanceOf[NamedExpression]) + private def wrapChildWithSubquery(plan: UnaryNode): LogicalPlan = { + val newChild = SubqueryAlias(SQLBuilder.newSubqueryName, plan.child) + plan.withNewChildren(Seq(newChild)) + } + } - Project(aliasedProjectList, SubqueryAlias(alias, child)) + object UpdateQualifiers extends Rule[LogicalPlan] { + override def apply(tree: LogicalPlan): LogicalPlan = tree transformUp { + case plan => + val inputAttributes = plan.children.flatMap(_.output) + plan transformExpressions { + case a: AttributeReference if !plan.producedAttributes.contains(a) => --- End diff -- `producedAttributes` is the list of attributes that are added by this operator. For example, `Generate` will produce some attributes that do not exist in the child node.
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org