Github user gatorsmile commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11555#discussion_r55205891
  
    --- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/SQLBuilder.scala ---
    @@ -204,11 +207,70 @@ class SQLBuilder(logicalPlan: LogicalPlan, 
sqlContext: SQLContext) extends Loggi
       private def build(segments: String*): String =
         segments.map(_.trim).filter(_.nonEmpty).mkString(" ")
     
    +  /**
    +   * Given a seq of qualifiers(names and their corresponding 
[[AttributeSet]]), transform the given
    +   * expression tree, if an [[Attribute]] belongs to one of the 
[[AttributeSet]]s, update its
    +   * qualifier with the corresponding name of the [[AttributeSet]].
    +   */
    +  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)
    +          }
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Finds the outer most [[SubqueryAlias]] nodes in the input logical 
plan and return their alias
    +   * names and outputSet.
    +   */
    +  private def findOutermostQualifiers(input: LogicalPlan): Seq[(String, 
AttributeSet)] = {
    --- End diff --
    
    I have another alternative. We are facing the same issue everywhere when we 
add an extra Qualifier or remove an extra Qualifier. How about adding another 
rule/batch below the existing Batch("Canonicalizer") For example, 
    ```scala
          Batch("Replace Qualifier", Once,
            ReplaceQualifier)
    ```
    The rule is simple. We always can get the qualifier from the inputSet if we 
are doing in bottom up traversal. I did not do a full test last night. Below is 
the code draft:
    
    ```scala
        object ReplaceQualifier extends Rule[LogicalPlan] {
          override def apply(tree: LogicalPlan): LogicalPlan = tree transformUp 
{ case plan =>
              plan transformExpressions {
                case e: AttributeReference => 
e.withQualifiers(getQualifier(plan.inputSet, e))
              }
          }
    
          private def getQualifier(inputSet: AttributeSet, e: 
AttributeReference): Seq[String] = {
            inputSet.collectFirst {
              case a if a.semanticEquals(e) => a.qualifiers
            }.getOrElse(Seq.empty[String])
          }
        }
    ``` 
    



---
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

Reply via email to