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

    https://github.com/apache/spark/pull/9280#discussion_r43116722
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/distinctFallback.scala
 ---
    @@ -0,0 +1,173 @@
    +/*
    + * 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.catalyst.expressions.aggregate
    +
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.expressions._
    +import 
org.apache.spark.sql.catalyst.expressions.codegen.{GeneratedExpressionCode, 
CodeGenContext, CodegenFallback}
    +import org.apache.spark.sql.types.{AbstractDataType, DataType}
    +import org.apache.spark.util.collection.OpenHashSet
    +
    +/**
    + * Fallback operator for distinct operators. This will be used when a user 
issues multiple
    + * different distinct expressions in a query.
    + *
    + * The operator uses the OpenHashSetUDT for de-duplicating values. It is, 
as a result, not possible
    + * to use UnsafeRow based aggregation.
    + */
    +case class DistinctAggregateFallback(function: AggregateFunction2) extends 
DeclarativeAggregate {
    +  override def inputTypes: Seq[AbstractDataType] = function.inputTypes
    +  override def nullable: Boolean = function.nullable
    +  override def dataType: DataType = function.dataType
    +  override def children: Seq[Expression] = Seq(function)
    +
    +  private[this] val input = function.children match {
    +    case child :: Nil => child
    +    case children => CreateStruct(children) // TODO can we test this?
    +  }
    +  private[this] val items = AttributeReference("itemSet", new 
OpenHashSetUDT(input.dataType))()
    +
    +  override def aggBufferAttributes: Seq[AttributeReference] = Seq(items)
    +  override val initialValues: Seq[Expression] = Seq(NewSet(input.dataType))
    +  override val updateExpressions: Seq[Expression] = 
Seq(AddItemToSet(input, items))
    +  override val mergeExpressions: Seq[Expression] = 
Seq(CombineSets(items.left, items.right))
    +  override val evaluateExpression: Expression = function match {
    +    case f: Count => CountSet(items)
    +    case f: DeclarativeAggregate => 
ReduceSetUsingDeclarativeAggregate(items, f)
    +    case f: ImperativeAggregate => 
ReduceSetUsingImperativeAggregate(items, f)
    +  }
    +}
    +
    +case class ReduceSetUsingImperativeAggregate(left: Expression, right: 
ImperativeAggregate)
    +  extends BinaryExpression with CodegenFallback {
    +
    +  override def dataType: DataType = right.dataType
    +
    +  private[this] val single = right.children.size == 1
    +
    +  // TODO can we assume that the offsets are 0 when we haven't touched 
them yet?
    +  private[this] val function = right
    +    .withNewInputAggBufferOffset(0)
    +    .withNewMutableAggBufferOffset(0)
    +
    +  @transient private[this] lazy val buffer =
    +    new SpecificMutableRow(right.aggBufferAttributes.map(_.dataType))
    +
    +  @transient private[this] lazy val singleValueInput = new 
GenericMutableRow(1)
    +
    +  override def eval(input: InternalRow): Any = {
    +    val result = left.eval(input).asInstanceOf[OpenHashSet[Any]]
    +    if (result != null) {
    +      right.initialize(buffer)
    +      val iterator = result.iterator
    +      if (single) {
    +        while (iterator.hasNext) {
    +          singleValueInput.update(0, iterator.next())
    +          function.update(buffer, singleValueInput)
    +        }
    +      } else {
    +        while (iterator.hasNext) {
    +          function.update(buffer, 
iterator.next().asInstanceOf[InternalRow])
    +        }
    +      }
    +      function.eval(buffer)
    +    } else null
    +  }
    +}
    +
    +case class ReduceSetUsingDeclarativeAggregate(left: Expression, right: 
DeclarativeAggregate)
    +  extends Expression with CodegenFallback {
    +  override def children: Seq[Expression] = Seq(left)
    +  override def nullable: Boolean = right.nullable
    +  override def dataType: DataType = right.dataType
    +
    +  private[this] val single = right.children.size == 1
    +
    +  private[this] val inputOrdinal = right.children.size
    +
    +  @transient private[this] lazy val initial =
    +    InterpretedMutableProjection(right.initialValues).target(buffer)
    +
    +  @transient private[this] lazy val update = {
    +    val boundRefs = (right.aggBufferAttributes ++ 
right.children).zipWithIndex.map {
    +      case (e, i) => (e, new BoundReference(i, e.dataType, e.nullable))
    +    }.toMap
    +    val boundExpressions = 
right.updateExpressions.map(_.transform(boundRefs))
    +    new InterpretedMutableProjection(boundExpressions).target(buffer)
    +  }
    +
    +  @transient private[this] lazy val evaluate =
    +    BindReferences.bindReference(right.evaluateExpression, 
right.aggBufferAttributes)
    +
    +  @transient private[this] lazy val buffer = {
    +    val singleType = if (single) {
    +      Seq(right.children.head.dataType)
    +    } else {
    +      Seq.empty
    +    }
    +    new SpecificMutableRow(right.inputAggBufferAttributes.map(_.dataType) 
++ singleType)
    +  }
    +
    +  @transient private[this] lazy val joinRow = new JoinedRow
    +
    +  override def eval(input: InternalRow): Any = {
    +    val result = left.eval(input).asInstanceOf[OpenHashSet[Any]]
    +    if (result != null) {
    +      initial(EmptyRow)
    +      val iterator = result.iterator
    +      if (single) {
    +        while (iterator.hasNext) {
    +          buffer.update(inputOrdinal, iterator.next())
    +          update(buffer)
    +        }
    +      } else {
    +        while (iterator.hasNext) {
    +          joinRow(buffer, iterator.next().asInstanceOf[InternalRow])
    +          update(joinRow)
    +        }
    +      }
    +      evaluate.eval(buffer)
    +    } else null
    +  }
    +}
    +
    +case class DropAnyNull(child: Expression) extends UnaryExpression {
    +  override def nullable: Boolean = false
    +  override def dataType: DataType = child.dataType
    --- End diff --
    
    Removed....


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