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

    https://github.com/apache/flink/pull/1600#discussion_r52181382
  
    --- Diff: 
flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/plan/functions/aggregate/SumAggregate.scala
 ---
    @@ -0,0 +1,130 @@
    +/*
    + * 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.flink.api.table.plan.functions.aggregate
    +
    +abstract class SumAggregate[T] extends Aggregate[T]{
    +
    +}
    +
    +// TinyInt sum aggregate return Int as aggregated value.
    +class TinyIntSumAggregate extends SumAggregate[Int] {
    +
    +  private var sumValue: Int = 0
    +
    +  override def initiateAggregate: Unit = {
    +    sumValue = 0
    +  }
    +
    +
    +  override def getAggregated(): Int = {
    +    sumValue
    +  }
    +
    +  override def aggregate(value: Any): Unit = {
    +    sumValue += value.asInstanceOf[Byte]
    +  }
    +}
    +
    +// SmallInt sum aggregate return Int as aggregated value.
    +class SmallIntSumAggregate extends SumAggregate[Int] {
    +
    +  private var sumValue: Int = 0
    +
    +  override def initiateAggregate: Unit = {
    +    sumValue = 0
    +  }
    +
    +  override def getAggregated(): Int = {
    +    sumValue
    +  }
    +
    +  override def aggregate(value: Any): Unit = {
    +    sumValue += value.asInstanceOf[Short]
    +  }
    +}
    +
    +// Int sum aggregate return Int as aggregated value.
    +class IntSumAggregate extends SumAggregate[Int] {
    +
    +  private var sumValue: Int = 0
    +
    +  override def initiateAggregate: Unit = {
    +    sumValue = 0
    +  }
    +
    +
    +  override def getAggregated(): Int = {
    +    sumValue
    +  }
    +
    +  override def aggregate(value: Any): Unit = {
    +    sumValue += value.asInstanceOf[Int]
    +  }
    +}
    +
    +// Long sum aggregate return Long as aggregated value.
    +class LongSumAggregate extends SumAggregate[Long] {
    +
    +  private var sumValue: Long = 0L
    +
    +  override def initiateAggregate: Unit = {
    +    sumValue = 0
    +  }
    +
    +  override def aggregate(value: Any): Unit = {
    +    sumValue += value.asInstanceOf[Long]
    +  }
    +
    +  override def getAggregated(): Long = {
    +    sumValue
    +  }
    +}
    +
    +// Float sum aggregate return Float as aggregated value.
    +class FloatSumAggregate extends SumAggregate[Float] {
    +  private var sumValue: Float = 0
    +
    +  override def initiateAggregate: Unit = {
    +    sumValue = 0
    +  }
    +
    +  override def aggregate(value: Any): Unit = {
    +    sumValue += value.asInstanceOf[Float]
    +  }
    +
    +  override def getAggregated(): Float = {
    +    sumValue
    +  }
    +}
    +
    +// Double sum aggregate return Double as aggregated value.
    +class DoubleSumAggregate extends SumAggregate[Double] {
    +  private var sumValue: Double = 0
    +
    +  override def initiateAggregate: Unit = {
    +    sumValue = 0
    +  }
    +
    +  override def aggregate(value: Any): Unit = {
    +    sumValue += value.asInstanceOf[Double]
    +  }
    +
    +  override def getAggregated(): Double = {
    +    sumValue
    +  }
    +}
    --- End diff --
    
    This is a lot of duplicated code. Could we make it a bit less redundant by 
doing something similar to?
    
    ```
    class SAggregate[I: spire.math.Numeric, T: spire.math.Numeric] extends 
Aggregate[T] {
    
      var result: T = _
      /**
        * Initialize the aggregate state.
        */
      override def initiateAggregate: Unit = {
        result = implicitly[Numeric[T]].zero
      }
    
      /**
        * Feed the aggregate field value.
        *
        * @param value
        */
      override def aggregate(value: Any): Unit = {
        val input: I = value.asInstanceOf[I]
    
        val numericInput = implicitly[spire.math.Numeric[I]]
        val numericResult = implicitly[Numeric[T]]
    
        result = numericResult.plus(result, numericInput.toType[T](input))
      }
    
      /**
        * Return final aggregated value.
        *
        * @return
        */
      override def getAggregated(): T = {
        result
      }
    }
    ```
    
    The same holds true for the other aggregation functions.


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

Reply via email to