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.block.BlockAssertions.createDoublesBlock;
 import static com.facebook.presto.block.BlockAssertions.createLongsBlock;
 import static com.facebook.presto.operator.aggregation.AggregationTestUtils.assertAggregation;
 import static com.facebook.presto.operator.aggregation.ApproximateDoublePercentileAggregations.DOUBLE_APPROXIMATE_PERCENTILE_AGGREGATION;
 import static com.facebook.presto.operator.aggregation.ApproximateDoublePercentileAggregations.DOUBLE_APPROXIMATE_PERCENTILE_WEIGHTED_AGGREGATION;
 import static com.facebook.presto.operator.aggregation.ApproximateLongPercentileAggregations.LONG_APPROXIMATE_PERCENTILE_AGGREGATION;
 import static com.facebook.presto.operator.aggregation.ApproximateLongPercentileAggregations.LONG_APPROXIMATE_PERCENTILE_WEIGHTED_AGGREGATION;
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 
 {
     private static final Block EMPTY_DOUBLE_BLOCK = .createBlockBuilder(new BlockBuilderStatus(), 0).build();
     private static final Block EMPTY_LONG_BLOCK = .createBlockBuilder(new BlockBuilderStatus(), 0).build();
 
     @Test
     public void testLongPartialStep()
             throws Exception
     {
         // regular approx_percentile
         assertAggregation(
                 ,
                 1.0,
                 null,
                 createPage(
                         new Long[] {null},
                         0.5),
                 createPage(
                         new Long[] {null},
                         0.5));
 
                 1.0,
                 1L,
                 createPage(
                         new Long[] {null},
                         0.5),
                 createPage(
                         new Long[] {1L},
                         0.5));
 
         assertAggregation(
                 ,
                 1.0,
                 2L,
                 createPage(
                         new Long[] {null},
                         0.5),
                 createPage(
                         new Long[] {1L, 2L, 3L},
                         0.5));
 
         assertAggregation(
                 ,
                 1.0,
                 2L,
                 createPage(
                         new Long[] {1L},
                         0.5),
                 createPage(
                         new Long[] {2L, 3L},
                         0.5));
 
         assertAggregation(
                 ,
                 1.0,
                 3L,
                 createPage(
                         new Long[] {1L, null, 2L, 2L, null, 2L, 2L, null},
                         0.5),
                 createPage(
                         new Long[] {2L, 2L, null, 3L, 3L, null, 3L, null, 3L, 4L, 5L, 6L, 7L},
                         0.5));
 
         // weighted approx_percentile
         assertAggregation(
                1.0,
                null,
                createPage(
                        new Long[] {null},
                        new Long[] {1L},
                        0.5),
                createPage(
                        new Long[] {null},
                        new Long[] {1L},
                        0.5));
        assertAggregation(
                1.0,
                1L,
                createPage(
                        new Long[] {null},
                        new Long[] {1L},
                        0.5),
                createPage(
                        new Long[] {1L},
                        new Long[] {1L},
                        0.5));
        assertAggregation(
                1.0,
                2L,
                createPage(
                        new Long[] {null},
                        new Long[] {1L}, 0.5),
                createPage(
                        new Long[] {1L, 2L, 3L},
                        new Long[] {1L, 1L, 1L},
                        0.5));
        assertAggregation(
                1.0,
                2L,
                createPage(
                        new Long[] {1L},
                        new Long[] {1L},
                        0.5),
                createPage(
                        new Long[] {2L, 3L},
                        new Long[] {1L, 1L},
                        0.5));
        assertAggregation(
                1.0,
                3L,
                createPage(
                        new Long[] {1L, null, 2L, null, 2L, null},
                        new Long[] {1L, 1L, 2L, 1L, 2L, 1L},
                        0.5),
                createPage(
                        new Long[] {2L, null, 3L, null, 3L, null, 3L, 4L, 5L, 6L, 7L},
                        new Long[] {2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L},
                        0.5));
    }
    @Test
    public void testDoublePartialStep()
            throws Exception
    {
        // regular approx_percentile
        assertAggregation(
                ,
                1.0,
                null,
                createPage(
                        new Double[] {null},
                        0.5),
                createPage(
                        new Double[] {null},
                        0.5));
        assertAggregation(
                ,
                1.0,
                1.0,
                createPage(
                        new Double[] {null},
                        0.5),
                createPage(
                        new Double[] {1.0},
                        0.5));
        assertAggregation(
                ,
                1.0,
                2.0,
                createPage(
                        new Double[] {null},
                        0.5),
                createPage(
                        new Double[] {1.0, 2.0, 3.0},
                        0.5));
        assertAggregation(
                ,
                1.0,
                2.0,
                createPage(
                        new Double[] {1.0},
                        0.5),
                createPage(
                        new Double[] {2.0, 3.0},
                        0.5));
        assertAggregation(
                ,
                1.0,
                3.0,
                createPage(
                        new Double[] {1.0, null, 2.0, 2.0, null, 2.0, 2.0, null},
                        0.5),
                createPage(
                        new Double[] {2.0, 2.0, null, 3.0, 3.0, null, 3.0, null, 3.0, 4.0, 5.0, 6.0, 7.0},
                        0.5));
        // weighted approx_percentile
        assertAggregation(
                1.0,
                null,
                createPage(
                        new Double[] {null},
                        new Long[] {1L},
                        0.5),
                createPage(
                        new Double[] {null},
                        new Long[] {1L},
                        0.5));
        assertAggregation(
                1.0,
                1.0,
                createPage(
                        new Double[] {null},
                        new Long[] {1L},
                        0.5),
                createPage(
                        new Double[] {1.0},
                        new Long[] {1L},
                        0.5));
        assertAggregation(
                1.0,
                2.0,
                createPage(
                        new Double[] {null},
                        new Long[] {1L},
                        0.5),
                createPage(
                        new Double[] {1.0, 2.0, 3.0},
                        new Long[] {1L, 1L, 1L},
                        0.5));
        assertAggregation(
                1.0,
                2.0,
                createPage(
                        new Double[] {1.0},
                        new Long[] {1L},
                        0.5),
                createPage(
                        new Double[] {2.0, 3.0},
                        new Long[] {1L, 1L},
                        0.5));
        assertAggregation(
                1.0,
                3.0,
                createPage(
                        new Double[] {1.0, null, 2.0, null, 2.0, null},
                        new Long[] {1L, 1L, 2L, 1L, 2L, 1L},
                        0.5),
                createPage(
                        new Double[] {2.0, null, 3.0, null, 3.0, null, 3.0, 4.0, 5.0, 6.0, 7.0},
                        new Long[] {2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L},
                        0.5));
    }
    private static Page createPage(Double[] valuesdouble percentile)
    {
        Block valuesBlock;
        Block percentilesBlock;
        if (values.length == 0) {
            valuesBlock = ;
            percentilesBlock = ;
        }
        else {
            valuesBlock = createDoublesBlock(values);
            int positionCount = values.length;
            percentilesBlock = createRLEBlock(percentilepositionCount);
        }
        return new Page(valuesBlockpercentilesBlock);
    }
    private static Page createPage(Long[] valuesdouble percentile)
    {
        Block valuesBlock;
        Block percentilesBlock;
        if (values.length == 0) {
            valuesBlock = ;
            percentilesBlock = ;
        }
        else {
            valuesBlock = createLongsBlock(values);
            percentilesBlock = createRLEBlock(percentilevalues.length);
        }
        return new Page(valuesBlockpercentilesBlock);
    }
    private static Page createPage(Long[] valuesLong[] weightsdouble percentile)
    {
        Preconditions.checkArgument(values.length == weights.length"values.length must match weights.length");
        Block valuesBlock;
        Block weightsBlock;
        Block percentilesBlock;
        if (values.length == 0) {
            valuesBlock = ;
            weightsBlock = ;
            percentilesBlock = ;
        }
        else {
            valuesBlock = createLongsBlock(values);
            weightsBlock = createLongsBlock(weights);
            percentilesBlock = createRLEBlock(percentilevalues.length);
        }
        return new Page(valuesBlockweightsBlockpercentilesBlock);
    }
    private static Page createPage(Double[] valuesLong[] weightsdouble percentile)
    {
        Preconditions.checkArgument(values.length == weights.length"values.length must match weights.length");
        Block valuesBlock;
        Block weightsBlock;
        Block percentilesBlock;
        if (values.length == 0) {
            valuesBlock = ;
            weightsBlock = ;
            percentilesBlock = ;
        }
        else {
            valuesBlock = createDoublesBlock(values);
            weightsBlock = createLongsBlock(weights);
            percentilesBlock = createRLEBlock(percentilevalues.length);
        }
        return new Page(valuesBlockweightsBlockpercentilesBlock);
    }
    private static RunLengthEncodedBlock createRLEBlock(double percentileint positionCount)
    {
        BlockBuilder blockBuilder = .createBlockBuilder(new BlockBuilderStatus(), 1);
        .writeDouble(blockBuilderpercentile);
        return new RunLengthEncodedBlock(blockBuilder.build(), positionCount);
    }
New to GrepCode? Check out our FAQ X