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

    https://github.com/apache/spark/pull/5604#discussion_r29544223
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ---
    @@ -529,6 +533,133 @@ class Analyzer(
               makeGeneratorOutput(p.generator, p.generatorOutput), p.child)
         }
       }
    +
    +  object ResolveWindowFunction extends Rule[LogicalPlan] {
    +    def hasWindowFunction(projectList: Seq[NamedExpression]): Boolean = {
    +      projectList.foreach ( _.foreach {
    +        case window: WindowExpression => return true
    +        case _ =>
    +      })
    +
    +      return false
    +    }
    +
    +    def hasWindowFunction(expr: NamedExpression): Boolean = {
    +      expr.foreach {
    +        case window: WindowExpression => return true
    +        case _ =>
    +      }
    +
    +      return false
    +    }
    +
    +    /**
    +     * From a Seq of [[NamedExpression]]s, extract window expressions and
    +     * other regular expressions.
    +     */
    +    def extract(
    +        expressions: Seq[NamedExpression]): (Seq[NamedExpression], 
Seq[NamedExpression]) = {
    +      val (windowExpressions, regularExpressions) = 
expressions.partition(hasWindowFunction)
    +      // Also need to extract all UnresolvedAttribute from 
windowExpressions.
    +      // For example, in the case of SUM(x) OVER (...), we need to extract 
x.
    +      val attributes = windowExpressions.flatMap(_.collect {
    +        case attribute: Attribute => attribute
    +      })
    +
    +      // Figure out which ones are missing from the regularExpressions,
    +      // so that we can add them.
    +      val requiredAttributes = AttributeSet(attributes)
    +      val missingInProject = requiredAttributes -- regularExpressions
    +
    +      (windowExpressions, regularExpressions ++ missingInProject)
    +    }
    +
    +    def addWindow(windowExpressions: Seq[NamedExpression], child: 
LogicalPlan): LogicalPlan = {
    +      val groupedWindowExpression = windowExpressions.groupBy { expr =>
    +        expr.collect {
    +          case window: WindowExpression => window.windowSpec
    +        }.head
    +      }.toSeq
    +
    +      var currentChild = child
    +      var i = 0
    +      while (i < groupedWindowExpression.size) {
    +        val (windowSpec, windowExpressions) = groupedWindowExpression(i)
    +        currentChild = Window(currentChild.output, windowExpressions, 
windowSpec, currentChild)
    +
    +        i += 1
    +      }
    +
    +      currentChild
    +    }
    +
    +    // We have to use transformDown at here to make sure the rule of 
"Aggregate with Having clause"
    +    // will be triggered.
    +    def apply(plan: LogicalPlan): LogicalPlan = plan transformDown {
    +      // Fill WindowSpecDefinitions. This one work with unresolved 
children.
    +      case WithWindowDefinition(windowDefinitions, child) =>
    +        child.transform {
    +          case plan => plan.transformExpressions {
    +            case UnresolvedWindowExpression(c, 
WindowSpecReference(windowName)) =>
    +              val errorMessage =
    +                s"Window specification $windowName is not defined in the 
WINDOW clause."
    +              val windowSpecDefinition =
    +                windowDefinitions
    +                  .get(windowName)
    +                  .getOrElse(throw new AnalysisException(errorMessage))
    +              WindowExpression(c, windowSpecDefinition)
    +          }
    +        }
    +
    +      // Aggregate with Having clause
    +      case f @ Filter(condition, a @ Aggregate(groupingExprs, 
aggregateExprs, child))
    --- End diff --
    
    how about cube/rollup, we probably should consider them here


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