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;
 
 
 import java.util.List;
 
 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 org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 
 public class TestGroupByHash
 {
     private static final int MAX_GROUP_ID = 500;
 
     @Test
     public void testGetGroupIds()
             throws Exception
     {
         GroupByHash groupByHash = new GroupByHash(ImmutableList.of(), new int[] { 0 }, Optional.of(1), 100);
         for (int tries = 0; tries < 2; tries++) {
             for (int value = 0; value < value++) {
                 Block block = BlockAssertions.createLongsBlock(value);
                 Block hashBlock = TypeUtils.getHashBlock(ImmutableList.of(), block);
                 Page page = new Page(blockhashBlock);
                 for (int addValuesTries = 0; addValuesTries < 10; addValuesTries++) {
                     GroupByIdBlock groupIds = groupByHash.getGroupIds(page);
                     assertEquals(groupIds.getGroupCount(), tries == 0 ? value + 1 : );
                     assertEquals(groupIds.getPositionCount(), 1);
                     long groupId = groupIds.getGroupId(0);
                     assertEquals(groupIdvalue);
                 }
             }
         }
     }
 
     @Test
     public void testTypes()
             throws Exception
     {
         GroupByHash groupByHash = new GroupByHash(ImmutableList.of(), new int[] { 0 }, Optional.of(1), 100);
         // Additional bigint channel for hash
         assertEquals(groupByHash.getTypes(), ImmutableList.of());
     }
 
     @Test
     public void testAppendTo()
             throws Exception
     {
         Block valuesBlock = BlockAssertions.createStringSequenceBlock(0, 100);
         Block hashBlock = TypeUtils.getHashBlock(ImmutableList.of(), valuesBlock);
         GroupByHash groupByHash = new GroupByHash(ImmutableList.of(), new int[] { 0 }, Optional.of(1), 100);
 
         GroupByIdBlock groupIds = groupByHash.getGroupIds(new Page(valuesBlockhashBlock));
         for (int i = 0; i < groupIds.getPositionCount(); i++) {
             assertEquals(groupIds.getGroupId(i), i);
         }
         assertEquals(groupByHash.getGroupCount(), 100);
 
         PageBuilder pageBuilder = new PageBuilder(groupByHash.getTypes());
         for (int i = 0; i < groupByHash.getGroupCount(); i++) {
             pageBuilder.declarePosition();
             groupByHash.appendValuesTo(ipageBuilder, 0);
         }
         Page page = pageBuilder.build();
         // Ensure that all blocks have the same positionCount
         for (int i = 0; i < groupByHash.getTypes().size(); i++) {
             assertEquals(page.getBlock(i).getPositionCount(), 100);
         }
         assertEquals(page.getPositionCount(), 100);
         BlockAssertions.assertBlockEquals(page.getBlock(0), valuesBlock);
         BlockAssertions.assertBlockEquals(page.getBlock(1), hashBlock);
     }
 
     @Test
     public void testAppendToMultipleTuplesPerGroup()
            throws Exception
    {
        List<Longvalues = new ArrayList<>();
        for (long i = 0; i < 100; i++) {
            values.add(i % 50);
        }
        Block valuesBlock = BlockAssertions.createLongsBlock(values);
        Block hashBlock = TypeUtils.getHashBlock(ImmutableList.of(), valuesBlock);
        GroupByHash groupByHash = new GroupByHash(ImmutableList.of(), new int[] { 0 }, Optional.of(1), 100);
        groupByHash.getGroupIds(new Page(valuesBlockhashBlock));
        assertEquals(groupByHash.getGroupCount(), 50);
        PageBuilder pageBuilder = new PageBuilder(groupByHash.getTypes());
        for (int i = 0; i < groupByHash.getGroupCount(); i++) {
            pageBuilder.declarePosition();
            groupByHash.appendValuesTo(ipageBuilder, 0);
        }
        Page outputPage = pageBuilder.build();
        assertEquals(outputPage.getPositionCount(), 50);
        BlockAssertions.assertBlockEquals(outputPage.getBlock(1), BlockAssertions.createLongSequenceBlock(0, 50));
    }
    @Test
    public void testContains()
            throws Exception
    {
        Block valuesBlock = BlockAssertions.createDoubleSequenceBlock(0, 10);
        Block hashBlock = TypeUtils.getHashBlock(ImmutableList.of(), valuesBlock);
        GroupByHash groupByHash = new GroupByHash(ImmutableList.of(), new int[] { 0 }, Optional.of(1), 100);
        groupByHash.getGroupIds(new Page(valuesBlockhashBlock));
        Block testBlock = BlockAssertions.createDoublesBlock((double) 3);
        Block testHashBlock = TypeUtils.getHashBlock(ImmutableList.of(), testBlock);
        assertTrue(groupByHash.contains(0, new Page(testBlocktestHashBlock)));
        testBlock = BlockAssertions.createDoublesBlock(11.0);
        testHashBlock = TypeUtils.getHashBlock(ImmutableList.of(), testBlock);
        assertFalse(groupByHash.contains(0, new Page(testBlocktestHashBlock)));
    }
    @Test
    public void testContainsMultipleColumns()
            throws Exception
    {
        Block valuesBlock = BlockAssertions.createDoubleSequenceBlock(0, 10);
        Block stringValuesBlock = BlockAssertions.createStringSequenceBlock(0, 10);
        Block hashBlock = TypeUtils.getHashBlock(ImmutableList.of(), valuesBlockstringValuesBlock);
        GroupByHash groupByHash = new GroupByHash(ImmutableList.of(), new int[] { 0, 1 }, Optional.of(2), 100);
        groupByHash.getGroupIds(new Page(valuesBlockstringValuesBlockhashBlock));
        Block testValuesBlock = BlockAssertions.createDoublesBlock((double) 3);
        Block testStringValuesBlock = BlockAssertions.createStringsBlock("3");
        Block testHashBlock = TypeUtils.getHashBlock(ImmutableList.of(), testValuesBlocktestStringValuesBlock);
        assertTrue(groupByHash.contains(0, new Page(testValuesBlocktestStringValuesBlocktestHashBlock)));
    }
    @Test
    public void testForceRehash()
            throws Exception
    {
        // Create a page with positionCount >> expected size of groupByHash
        Block valuesBlock = BlockAssertions.createStringSequenceBlock(0, 100);
        Block hashBlock = TypeUtils.getHashBlock(ImmutableList.of(), valuesBlock);
        // Create group by hash with extremely small size
        GroupByHash groupByHash = new GroupByHash(ImmutableList.of(), new int[] { 0 }, Optional.of(1), 4);
        groupByHash.getGroupIds(new Page(valuesBlockhashBlock));
        // Ensure that all groups are present in group by hash
        for (int i = 0; i < valuesBlock.getPositionCount(); i++) {
            assertTrue(groupByHash.contains(inew Page(valuesBlockhashBlock)));
        }
    }
New to GrepCode? Check out our FAQ X