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

    https://github.com/apache/spark/pull/7813#discussion_r36063600
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/UnsafeHybridAggregationIterator.scala
 ---
    @@ -0,0 +1,398 @@
    +/*
    + * 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.aggregate
    +
    +import org.apache.spark.sql.execution.{UnsafeKeyValueSorter, 
UnsafeFixedWidthAggregationMap}
    +import org.apache.spark.unsafe.KVIterator
    +import org.apache.spark.{SparkEnv, TaskContext}
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.expressions._
    +import org.apache.spark.sql.catalyst.expressions.aggregate._
    +import org.apache.spark.sql.types.StructType
    +
    +/**
    + * An iterator used to evaluate [[AggregateFunction2]].
    + * It first tries to use in-memory hash-based aggregation. If we cannot 
allocate more
    + * space for the hash map, we spill the sorted map entries, free the map, 
and then
    + * switch to sort-based aggregation.
    + */
    +class UnsafeHybridAggregationIterator(
    +    groupingKeyAttributes: Seq[Attribute],
    +    valueAttributes: Seq[Attribute],
    +    inputKVIterator: KVIterator[UnsafeRow, InternalRow],
    +    nonCompleteAggregateExpressions: Seq[AggregateExpression2],
    +    nonCompleteAggregateAttributes: Seq[Attribute],
    +    completeAggregateExpressions: Seq[AggregateExpression2],
    +    completeAggregateAttributes: Seq[Attribute],
    +    initialInputBufferOffset: Int,
    +    resultExpressions: Seq[NamedExpression],
    +    newMutableProjection: (Seq[Expression], Seq[Attribute]) => (() => 
MutableProjection),
    +    outputsUnsafeRows: Boolean)
    +  extends AggregationIterator(
    +    groupingKeyAttributes,
    +    valueAttributes,
    +    nonCompleteAggregateExpressions,
    +    nonCompleteAggregateAttributes,
    +    completeAggregateExpressions,
    +    completeAggregateAttributes,
    +    initialInputBufferOffset,
    +    resultExpressions,
    +    newMutableProjection,
    +    outputsUnsafeRows) {
    +
    +  require(groupingKeyAttributes.nonEmpty)
    +
    +  
///////////////////////////////////////////////////////////////////////////
    +  // Unsafe Aggregation buffers
    +  
///////////////////////////////////////////////////////////////////////////
    +
    +  // This is the Unsafe Aggregation Map used to store all buffers.
    +  private[this] val buffers = new UnsafeFixedWidthAggregationMap(
    +    newBuffer,
    +    
StructType.fromAttributes(allAggregateFunctions.flatMap(_.bufferAttributes)),
    +    StructType.fromAttributes(groupingKeyAttributes),
    +    TaskContext.get.taskMemoryManager(),
    +    SparkEnv.get.shuffleMemoryManager,
    +    1024 * 16, // initial capacity
    +    SparkEnv.get.conf.getSizeAsBytes("spark.buffer.pageSize", "64m"),
    +    false // disable tracking of performance metrics
    +  )
    +
    +  override protected def newBuffer: UnsafeRow = {
    +    val bufferSchema = allAggregateFunctions.flatMap(_.bufferAttributes)
    +    val bufferRowSize: Int = bufferSchema.length
    +
    +    val genericMutableBuffer = new GenericMutableRow(bufferRowSize)
    +    val unsafeProjection =
    +      UnsafeProjection.create(bufferSchema.map(_.dataType))
    +    val buffer = unsafeProjection.apply(genericMutableBuffer)
    +    initializeBuffer(buffer)
    +    buffer
    +  }
    +
    +  
///////////////////////////////////////////////////////////////////////////
    +  // Methods and variables related to switching to sort-based aggregation
    +  
///////////////////////////////////////////////////////////////////////////
    +  private[this] var sortBased = false
    +
    +  private[this] var sortBasedAggregationIterator: 
SortBasedAggregationIterator = _
    +
    +  // The value part of the input KV iterator is used to store original 
input values of
    +  // aggregate functions, we need to convert them to aggregation buffers.
    +  private def processOriginalInput(
    --- End diff --
    
    this one too -- it increases the complexity with extra abstraction but 
doesn't really reduce code. it also hurts performance.



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