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

    https://github.com/apache/spark/pull/16329#discussion_r93682192
  
    --- Diff: 
examples/src/main/scala/org/apache/spark/examples/sql/UserDefinedTypedAggregation.scala
 ---
    @@ -0,0 +1,87 @@
    +/*
    + * 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.examples.sql
    +
    +// $example on:typed_custom_aggregation$
    +import org.apache.spark.sql.expressions.Aggregator
    +import org.apache.spark.sql.Encoder
    +import org.apache.spark.sql.Encoders
    +import org.apache.spark.sql.SparkSession
    +// $example off:typed_custom_aggregation$
    +
    +object UserDefinedTypedAggregation {
    +
    +  // $example on:typed_custom_aggregation$
    +  case class Employee(name: String, salary: Long)
    +  case class Average(var sum: Long, var count: Long)
    +
    +  object MyAverage extends Aggregator[Employee, Average, Double] {
    +    // A zero value for this aggregation. Should satisfy the property that 
any b + zero = b
    +    def zero: Average = Average(0L, 0L)
    +    // Combine two values to produce a new value. For performance, the 
function may modify `buffer`
    +    // and return it instead of constructing a new object
    +    def reduce(buffer: Average, employee: Employee): Average = {
    +      buffer.sum += employee.salary
    +      buffer.count += 1
    +      buffer
    +    }
    +    // Merge two intermediate values
    +    def merge(b1: Average, b2: Average): Average = Average(b1.sum + 
b2.sum, b1.count + b2.count)
    --- End diff --
    
    Personally, I prefer consistency. When I saw this, I immediately wondered 
whether there is a specific reason you did it this way.
    I'd rather see both methods use the same paradigm. In this case probably 
the immutable one as the option of mutability is already mentioned in the 
comment above.
    Or you can mention it again in the comment on this method if you want to 
provide examples of both. This way it just seems a little confusing.


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