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.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static com.facebook.presto.util.Failures.checkCondition;
 
 @AggregationFunction("approx_distinct")
 {
     private static final double DEFAULT_STANDARD_ERROR = 0.023;
     private static final double LOWEST_MAX_STANDARD_ERROR = 0.01150;
     private static final double HIGHEST_MAX_STANDARD_ERROR = 0.26000;
 
 
     @InputFunction
     public static void input(HyperLogLogState state, @SqlType(.long value)
     {
         input(statevalue);
     }
 
     @InputFunction
     public static void input(HyperLogLogState state, @SqlType(.long value, @SqlType(.double maxStandardError)
     {
         HyperLogLog hll = getOrCreateHyperLogLog(statemaxStandardError);
         state.addMemoryUsage(-hll.estimatedInMemorySize());
         hll.add(value);
         state.addMemoryUsage(hll.estimatedInMemorySize());
     }
 
     @InputFunction
     public static void input(HyperLogLogState state, @SqlType(.double value)
     {
         input(statevalue);
     }
 
     @InputFunction
     public static void input(HyperLogLogState state, @SqlType(.double value, @SqlType(.double maxStandardError)
     {
         input(state, Double.doubleToLongBits(value), maxStandardError);
     }
 
     @InputFunction
     public static void input(HyperLogLogState state, @SqlType(.Slice value)
     {
         input(statevalue);
     }
 
     @InputFunction
     public static void input(HyperLogLogState state, @SqlType(.Slice value, @SqlType(.double maxStandardError)
     {
         HyperLogLog hll = getOrCreateHyperLogLog(statemaxStandardError);
         state.addMemoryUsage(-hll.estimatedInMemorySize());
         hll.add(value);
         state.addMemoryUsage(hll.estimatedInMemorySize());
     }
 
     private static HyperLogLog getOrCreateHyperLogLog(HyperLogLogState statedouble maxStandardError)
     {
         HyperLogLog hll = state.getHyperLogLog();
         if (hll == null) {
             hll = HyperLogLog.newInstance(standardErrorToBuckets(maxStandardError));
             state.setHyperLogLog(hll);
             state.addMemoryUsage(hll.estimatedInMemorySize());
         }
         return hll;
     }
 
     static int standardErrorToBuckets(double maxStandardError)
     {
        checkCondition(maxStandardError >=  && maxStandardError <= ,
                ,
                "Max standard error must be in [%s, %s]: %s"maxStandardError);
        return log2Ceiling((int) Math.ceil(1.0816 / (maxStandardError * maxStandardError)));
    }
    private static int log2Ceiling(int value)
    {
        return Integer.highestOneBit(value - 1) << 1;
    }
    public static void combineState(HyperLogLogState stateHyperLogLogState otherState)
    {
        HyperLogLog input = otherState.getHyperLogLog();
        HyperLogLog previous = state.getHyperLogLog();
        if (previous == null) {
            state.setHyperLogLog(input);
            state.addMemoryUsage(input.estimatedInMemorySize());
        }
        else {
            state.addMemoryUsage(-previous.estimatedInMemorySize());
            previous.mergeWith(input);
            state.addMemoryUsage(previous.estimatedInMemorySize());
        }
    }
    public static void evaluateFinal(HyperLogLogState stateBlockBuilder out)
    {
        HyperLogLog hyperLogLog = state.getHyperLogLog();
        if (hyperLogLog == null) {
            .writeLong(out, 0);
        }
        else {
            .writeLong(outhyperLogLog.cardinality());
        }
    }
New to GrepCode? Check out our FAQ X