Github user liancheng commented on a diff in the pull request:
https://github.com/apache/spark/pull/11555#discussion_r55198266
--- Diff:
sql/hive/src/main/scala/org/apache/spark/sql/hive/SQLBuilder.scala ---
@@ -204,11 +208,55 @@ class SQLBuilder(logicalPlan: LogicalPlan,
sqlContext: SQLContext) extends Loggi
private def build(segments: String*): String =
segments.map(_.trim).filter(_.nonEmpty).mkString(" ")
+ private def updateQualifier(
+ expr: Expression,
+ qualifiers: Seq[(String, AttributeSet)]): Expression = {
+ if (qualifiers.isEmpty) {
+ expr
+ } else {
+ expr transform {
+ case a: Attribute =>
+ val index = qualifiers.indexWhere {
+ case (_, inputAttributes) => inputAttributes.contains(a)
+ }
+ if (index == -1) {
+ a
+ } else {
+ a.withQualifiers(qualifiers(index)._1 :: Nil)
+ }
+ }
+ }
+ }
+
+ private def findQualifiers(input: LogicalPlan): Seq[(String,
AttributeSet)] = {
+ val results = mutable.ArrayBuffer.empty[(String, AttributeSet)]
+ val nodes = mutable.Stack(input)
+
+ while (nodes.nonEmpty) {
+ val node = nodes.pop()
+ node match {
+ case SubqueryAlias(alias, child) => results += alias ->
child.outputSet
+ case _ => node.children.foreach(nodes.push)
+ }
+ }
+
+ results.toSeq
+ }
--- End diff --
So this method is basically a DFS search for all the outermost
`SubqueryAlias` operators. Maybe the following version is clearer:
```scala
def findOutermostQualifiers(input: LogicalPlan): Seq[(String,
AttributeSet)] = {
input.collectFirst {
case SubqueryAlias(alias, child) => Seq(alias -> child.outputSet)
case plan => plan.children.flatMap(findOutermostQualifiers)
}.toSeq.flatten
}
```
---
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]