Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed 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 com.facebook.presto.operator.aggregation;
 
 
 import static com.facebook.presto.operator.aggregation.AggregationUtils.mergeVarianceState;
 import static com.facebook.presto.operator.aggregation.AggregationUtils.updateVarianceState;
 import static com.facebook.presto.operator.aggregation.ApproximateUtils.formatApproximateResult;
 import static com.facebook.presto.operator.aggregation.ApproximateUtils.sumError;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 
 @AggregationFunction(value = "sum", approximate = true)
 public final class ApproximateSumAggregations
 {
     private ApproximateSumAggregations() {}
 
     @InputFunction
     public static void input(ApproximateDoubleSumState state, @SqlType(.double value, @SampleWeight long sampleWeight)
     {
         state.setWeightedCount(state.getWeightedCount() + sampleWeight);
         state.setSum(state.getSum() + value * sampleWeight);
         updateVarianceState(statevalue);
     }
 
     public static void combine(ApproximateDoubleSumState stateApproximateDoubleSumState otherState)
     {
         state.setSum(state.getSum() + otherState.getSum());
         state.setWeightedCount(state.getWeightedCount() + otherState.getWeightedCount());
         mergeVarianceState(stateotherState);
     }
 
     public static void output(ApproximateDoubleSumState statedouble confidenceBlockBuilder out)
     {
         if (state.getWeightedCount() == 0) {
             out.appendNull();
             return;
         }
 
         String result = formatApproximateResult(
                 state.getSum(),
                 sumError(state.getCount(), state.getWeightedCount(), state.getM2(), state.getMean()),
                 confidence,
                 false);
         .writeSlice(out, Slices.utf8Slice(result));
     }
 
     @InputFunction
     public static void input(ApproximateLongSumState state, @SqlType(.long value, @SampleWeight long sampleWeight)
     {
         state.setWeightedCount(state.getWeightedCount() + sampleWeight);
         state.setSum(state.getSum() + value * sampleWeight);
         updateVarianceState(statevalue);
     }
 
     public static void combine(ApproximateLongSumState stateApproximateLongSumState otherState)
     {
         state.setSum(state.getSum() + otherState.getSum());
         state.setWeightedCount(state.getWeightedCount() + otherState.getWeightedCount());
         mergeVarianceState(stateotherState);
     }
 
     public static void evaluateFinal(ApproximateLongSumState statedouble confidenceBlockBuilder out)
     {
         if (state.getWeightedCount() == 0) {
             out.appendNull();
             return;
         }
 
         String result = formatApproximateResult(
                 state.getSum(),
                 sumError(state.getCount(), state.getWeightedCount(), state.getM2(), state.getMean()),
                 confidence,
                 true);
         .writeSlice(out, Slices.utf8Slice(result));
     }
 
     public interface ApproximateDoubleSumState
             extends VarianceState
     {
        double getSum();
        void setSum(double value);
        long getWeightedCount();
        void setWeightedCount(long value);
    }
    public interface ApproximateLongSumState
            extends VarianceState
    {
        long getSum();
        void setSum(long value);
        long getWeightedCount();
        void setWeightedCount(long value);
    }
New to GrepCode? Check out our FAQ X