Github user wangxiaojing commented on a diff in the pull request: https://github.com/apache/spark/pull/2953#discussion_r19527805 --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/WindowFunction.scala --- @@ -0,0 +1,353 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.execution + +import java.util.HashMap + +import org.apache.spark.annotation.DeveloperApi +import org.apache.spark.sql.catalyst.expressions._ +import org.apache.spark.sql.catalyst.plans.physical.AllTuples +import org.apache.spark.sql.catalyst.plans.physical.ClusteredDistribution +import org.apache.spark.sql.catalyst.errors._ +import scala.collection.mutable.ArrayBuffer +import org.apache.spark.util.collection.CompactBuffer +import org.apache.spark.sql.catalyst.plans.physical.ClusteredDistribution +import org.apache.spark.sql.catalyst.expressions.AttributeReference +import org.apache.spark.sql.catalyst.expressions.InterpretedMutableProjection +import org.apache.spark.sql.catalyst.expressions.Alias +import org.apache.spark.sql.catalyst.types._ +import org.apache.spark.sql.catalyst.dsl.plans._ +import org.apache.spark.sql.catalyst.dsl.expressions._ +import org.apache.spark.sql.catalyst.plans.logical.SortPartitions + + +/** + * :: DeveloperApi :: + * Groups input data by `partitionExpressions` and computes the `computeExpressions` for each + * group. + * @param partitionExpressions expressions that are evaluated to determine partition. + * @param functionExpressions expressions that are computed for each partition. + * @param child the input data source. + */ +@DeveloperApi +case class WindowFunction( + partitionExpressions: Seq[Expression], + functionExpressions: Seq[NamedExpression], + child: SparkPlan) + extends UnaryNode { + + override def requiredChildDistribution = + if (partitionExpressions == Nil) { + AllTuples :: Nil + } else { + ClusteredDistribution(partitionExpressions) :: Nil + } + + // HACK: Generators don't correctly preserve their output through serializations so we grab + // out child's output attributes statically here. + private[this] val childOutput = child.output + + override def output = functionExpressions.map(_.toAttribute) + + /** A list of functions that need to be computed for each partition. */ + private[this] val computeExpressions = new ArrayBuffer[AggregateExpression] + + private[this] val otherExpressions = new ArrayBuffer[NamedExpression] + + functionExpressions.foreach { sel => + sel.collect { + case func: AggregateExpression => computeExpressions += func + case other: NamedExpression if (!other.isInstanceOf[Alias]) => otherExpressions += other + } + } + + private[this] val functionAttributes = computeExpressions.map { func => + func -> AttributeReference(s"funcResult:$func", func.dataType, func.nullable)()} + + /** The schema of the result of all evaluations */ + private[this] val resultAttributes = + otherExpressions.map(_.toAttribute) ++ functionAttributes.map(_._2) + + private[this] val resultMap = + (otherExpressions.map { other => other -> other.toAttribute } ++ functionAttributes + ).toMap + + + private[this] val resultExpressions = functionExpressions.map { sel => + sel.transform { + case e: Expression if resultMap.contains(e) => resultMap(e) + } + } + + private[this] val sortExpressions = + if (child.isInstanceOf[SortPartitions]) { + child.asInstanceOf[SortPartitions].sortExpressions + } + else if (child.isInstanceOf[Sort]) { + child.asInstanceOf[Sort].sortOrder + } + else null + + /** Creates a new function buffer for a partition. */ + private[this] def newFunctionBuffer(): Array[AggregateFunction] = { + val buffer = new Array[AggregateFunction](computeExpressions.length) + var i = 0 + while (i < computeExpressions.length) { + val baseExpr = BindReferences.bindReference(computeExpressions(i), childOutput) + baseExpr.windowRange = computeExpressions(i).windowRange + buffer(i) = baseExpr.newInstance() + i += 1 + } + buffer + } + + private[this] def computeFunctions(rows: CompactBuffer[Row]): Array[Iterator[Any]] = { + val aggrFunctions = newFunctionBuffer() + val functionResults = new Array[Iterator[Any]](aggrFunctions.length) + var i = 0 + while (i < aggrFunctions.length) { + val aggrFunction = aggrFunctions(i) + val base = aggrFunction.base + if (base.windowRange == null) { + if (sortExpressions != null) { + if (aggrFunction.dataType.isInstanceOf[ArrayType]) { + rows.foreach(aggrFunction.update) + functionResults(i) = aggrFunction.eval(EmptyRow).asInstanceOf[Seq[Any]].iterator + } else { + functionResults(i) = rows.map(row => { + aggrFunction.update(row) + aggrFunction.eval(EmptyRow) + }).iterator + } + } else { + rows.foreach(aggrFunction.update) + functionResults(i) = aggrFunction.eval(EmptyRow) match { + case r: Seq[_] => r.iterator + case other => (0 to rows.size - 1).map(r => other).iterator + } + } + + } else { + functionResults(i) = + if (base.windowRange.windowType == "ROWS_RANGE") rowsWindowFunction(base, rows).iterator + else valueWindowFunction(base, rows).iterator + } + i += 1 + } + functionResults + } + + private[this] def rowsWindowFunction(base: AggregateExpression, + rows: CompactBuffer[Row]): CompactBuffer[Any] = { + + val rangeResults = new CompactBuffer[Any]() + var rowIndex = 0 + while (rowIndex < rows.size) { + + val windowRange = base.windowRange + var start = + if (windowRange.preceding == Int.MaxValue) 0 + else rowIndex - windowRange.preceding + if (start < 0) start = 0 + var end = + if (windowRange.following == Int.MaxValue) { + rows.size - 1 + } else { + rowIndex + windowRange.following + } + if (end > rows.size - 1) end = rows.size - 1 + + //new aggregate function --- End diff -- Space after //
--- 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