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

    https://github.com/apache/spark/pull/17191#discussion_r105679116
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ---
    @@ -775,11 +775,21 @@ class Analyzer(
     
           case q: LogicalPlan =>
             logTrace(s"Attempting to resolve ${q.simpleString}")
    -        q transformExpressionsUp  {
    +        // We need to replace unresolved attributes with the resolved ones 
that this plan `q` holds
    +        // as expressions. For example, in `SELECT a AS a1, a1 + 1 AS b`, 
it replaces the unresolved
    +        // `a1` of `a1 + 1 AS b` with the resolved `a1` of `a AS a1`.
    +        val resolvedExprs = q.expressions.filter {
    +          case ne: NamedExpression if ne.resolved => true
    +          case _ => false
    +        }.map(_.asInstanceOf[NamedExpression])
    +        q.transformExpressionsUp  {
               case u @ UnresolvedAttribute(nameParts) =>
    -            // Leave unchanged if resolution fails.  Hopefully will be 
resolved next round.
    -            val result =
    -              withPosition(u) { q.resolveChildren(nameParts, 
resolver).getOrElse(u) }
    +            // Leave unchanged if resolution fails. Hopefully will be 
resolved next round.
    +            val result = withPosition(u) {
    +                q.resolveChildren(nameParts, resolver).getOrElse {
    +                  resolvedExprs.find(ne => resolver(ne.name, 
u.name)).getOrElse(u)
    +                }
    +              }
    --- End diff --
    
    With your new code, unresolved attributes will get a second chance to be 
resolved with any aliases in **ALL** operators. Is this an intended behaviour? 
If it is, you'd better update the PR description. The description currently 
refers to aliases in SELECT to be used in GROUP BY.
    
    Using your definition in the new test cases:
    
    `Seq((1, "a", 0), (2, "a", 1), (1, "a", 2)).toDF("k1", "k2", 
"v").createOrReplaceTempView("t")`
    
    You can see that when there is a name collision, it shows a different 
behaviour. The one below has the unresolved `kx` in GROUP BY clause resolved to 
`k1`.
    
    `sql("select k1 as kx from t group by kx")`
    
    While this one below returns an error that `k1` in SELECT clause is not 
found in GROUP BY clause.
    
    `sql("select k1 as k2 from t group by k2")`
    
    I think it'd better to go back to the drawing board and make a decision 
what we are going to support before we verify the code works as designed.



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

Reply via email to