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 java.util.Map;
 
 import static com.facebook.presto.operator.aggregation.AggregationTestUtils.getFinalBlock;
 import static com.facebook.presto.operator.aggregation.AggregationTestUtils.getIntermediateBlock;
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
 
 {
     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapperProvider().get();
     private final AccumulatorFactory factory;
     private final Page input;
 
     {
         FunctionRegistry functionRegistry = new FunctionRegistry(new TypeRegistry(), true);
         InternalAggregationFunction function = functionRegistry.resolveFunction(QualifiedName.of("numeric_histogram"), ImmutableList.of(.getTypeSignature(), .getTypeSignature(), .getTypeSignature()), false).getAggregationFunction();
          = function.bind(ImmutableList.of(0, 1, 2), Optional.empty(), Optional.empty(), 1.0);
 
         int numberOfBuckets = 10;
 
         PageBuilder builder = new PageBuilder(ImmutableList.of());
 
         for (int i = 0; i < 100; i++) {
             builder.declarePosition();
 
             .writeLong(builder.getBlockBuilder(0), numberOfBuckets);
             .writeDouble(builder.getBlockBuilder(1), i); // value
             .writeDouble(builder.getBlockBuilder(2), 1); // weight
         }
 
          = builder.build();
     }
 
     @Test
     public void test()
             throws Exception
     {
         Accumulator singleStep = .createAccumulator();
         singleStep.addInput();
         Block expected = getFinalBlock(singleStep);
 
         Accumulator partialStep = .createAccumulator();
         partialStep.addInput();
         Block partialBlock = getIntermediateBlock(partialStep);
 
         Accumulator finalStep = .createAccumulator();
         finalStep.addIntermediate(partialBlock);
         Block actual = getFinalBlock(finalStep);
 
         assertEquals(extractSingleValue(actual), extractSingleValue(expected));
     }
 
     @Test
     public void testMerge()
             throws Exception
     {
         Accumulator singleStep = .createAccumulator();
         singleStep.addInput();
         Block singleStepResult = getFinalBlock(singleStep);
 
         Accumulator partialStep = .createAccumulator();
         partialStep.addInput();
         Block intermediate = getIntermediateBlock(partialStep);
 
         Accumulator finalStep = .createAccumulator();
 
         finalStep.addIntermediate(intermediate);
        finalStep.addIntermediate(intermediate);
        Block actual = getFinalBlock(finalStep);
        Map<StringDoubleexpected = Maps.transformValues(extractSingleValue(singleStepResult), value -> value * 2);
        assertEquals(extractSingleValue(actual), expected);
    }
    @Test
    public void testNull()
            throws Exception
    {
        Accumulator accumulator = .createAccumulator();
        Block result = getFinalBlock(accumulator);
        assertTrue(result.getPositionCount() == 1);
        assertTrue(result.isNull(0));
    }
    private static Map<StringDoubleextractSingleValue(Block block)
            throws IOException
    {
        String json = block.getSlice(0, 0, block.getLength(0)).toStringUtf8();
        return .readValue(jsonMap.class);
    }
New to GrepCode? Check out our FAQ X