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.RowPagesBuilder.rowPagesBuilder;
 import static com.facebook.presto.SessionTestUtils.TEST_SESSION;
 import static com.facebook.presto.operator.OperatorAssertion.toMaterializedResult;
 import static com.facebook.presto.operator.OperatorAssertion.toPages;
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 import static com.facebook.presto.testing.MaterializedResult.resultBuilder;
 import static com.facebook.presto.testing.TestingTaskContext.createTaskContext;
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
 import static io.airlift.concurrent.Threads.daemonThreadsNamed;
 import static io.airlift.testing.Assertions.assertEqualsIgnoreOrder;
 import static java.util.concurrent.Executors.newCachedThreadPool;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
 
 public class TestRowNumberOperator
 {
     private ExecutorService executor;
 
     @BeforeClass
     public void setUp()
     {
          = newCachedThreadPool(daemonThreadsNamed("test-%s"));
     }
 
     @AfterClass
     public void tearDown()
     {
         .shutdownNow();
     }
 
     @DataProvider(name = "hashEnabledValues")
     public static Object[][] hashEnabledValuesProvider()
     {
         return new Object[][] { { true }, { false } };
     }
 
     private DriverContext getDriverContext()
     {
         return createTaskContext()
                 .addPipelineContext(truetrue)
                 .addDriverContext();
     }
 
     @Test
     public void testRowNumberUnPartitioned()
             throws Exception
     {
         DriverContext driverContext = getDriverContext();
         List<Pageinput = rowPagesBuilder()
                 .row(1, 0.3)
                 .row(2, 0.2)
                 .row(3, 0.1)
                 .row(3, 0.19)
                 .pageBreak()
                 .row(1, 0.4)
                 .pageBreak()
                 .row(1, 0.5)
                 .row(1, 0.6)
                 .row(2, 0.7)
                 .row(2, 0.8)
                 .row(2, 0.9)
                .build();
                0,
                ImmutableList.of(),
                Ints.asList(1, 0),
                Ints.asList(),
                ImmutableList.<Type>of(),
                Optional.empty(),
                Optional.empty(),
                10);
        Operator operator = operatorFactory.createOperator(driverContext);
        MaterializedResult expectedResult = resultBuilder(driverContext.getSession(), )
                .row(0.3, 1)
                .row(0.4, 1)
                .row(0.5, 1)
                .row(0.6, 1)
                .row(0.2, 2)
                .row(0.7, 2)
                .row(0.8, 2)
                .row(0.9, 2)
                .row(0.1, 3)
                .row(0.19, 3)
                .build();
        List<Pagepages = toPages(operatorinput);
        Block rowNumberColumn = getRowNumberColumn(pages);
        assertEquals(rowNumberColumn.getPositionCount(), 10);
        pages = stripRowNumberColumn(pages);
        MaterializedResult actual = toMaterializedResult(operator.getOperatorContext().getSession(), ImmutableList.<Type>of(), pages);
        assertEqualsIgnoreOrder(actual.getMaterializedRows(), expectedResult.getMaterializedRows());
    }
    @Test(dataProvider = "hashEnabledValues")
    public void testRowNumberPartitioned(boolean hashEnabled)
            throws Exception
    {
        DriverContext driverContext = getDriverContext();
        RowPagesBuilder rowPagesBuilder = rowPagesBuilder(hashEnabled, Ints.asList(0), );
        List<Pageinput = rowPagesBuilder
                .row(1, 0.3)
                .row(2, 0.2)
                .row(3, 0.1)
                .row(3, 0.19)
                .pageBreak()
                .row(1, 0.4)
                .pageBreak()
                .row(1, 0.5)
                .row(1, 0.6)
                .row(2, 0.7)
                .row(2, 0.8)
                .row(2, 0.9)
                .build();
                0,
                ImmutableList.of(),
                Ints.asList(1, 0),
                Ints.asList(0),
                ImmutableList.of(),
                Optional.of(10),
                rowPagesBuilder.getHashChannel(),
                10);
        Operator operator = operatorFactory.createOperator(driverContext);
        MaterializedResult expectedPartition1 = resultBuilder(driverContext.getSession(), )
                .row(0.3, 1)
                .row(0.4, 1)
                .row(0.5, 1)
                .row(0.6, 1)
                .build();
        MaterializedResult expectedPartition2 = resultBuilder(driverContext.getSession(), )
                .row(0.2, 2)
                .row(0.7, 2)
                .row(0.8, 2)
                .row(0.9, 2)
                .build();
        MaterializedResult expectedPartition3 = resultBuilder(driverContext.getSession(), )
                .row(0.1, 3)
                .row(0.19, 3)
                .build();
        List<Pagepages = toPages(operatorinput);
        Block rowNumberColumn = getRowNumberColumn(pages);
        assertEquals(rowNumberColumn.getPositionCount(), 10);
        pages = stripRowNumberColumn(pages);
        MaterializedResult actual = toMaterializedResult(operator.getOperatorContext().getSession(), ImmutableList.<Type>of(), pages);
        ImmutableSet<?> actualSet = ImmutableSet.copyOf(actual.getMaterializedRows());
        ImmutableSet<?> expectedPartition1Set = ImmutableSet.copyOf(expectedPartition1.getMaterializedRows());
        ImmutableSet<?> expectedPartition2Set = ImmutableSet.copyOf(expectedPartition2.getMaterializedRows());
        ImmutableSet<?> expectedPartition3Set = ImmutableSet.copyOf(expectedPartition3.getMaterializedRows());
        assertEquals(Sets.intersection(expectedPartition1SetactualSet).size(), 4);
        assertEquals(Sets.intersection(expectedPartition2SetactualSet).size(), 4);
        assertEquals(Sets.intersection(expectedPartition3SetactualSet).size(), 2);
    }
    @Test(dataProvider = "hashEnabledValues")
    public void testRowNumberPartitionedLimit(boolean hashEnabled)
            throws Exception
    {
        DriverContext driverContext = getDriverContext();
        RowPagesBuilder rowPagesBuilder = rowPagesBuilder(hashEnabled, Ints.asList(0), );
        List<Pageinput = rowPagesBuilder
                .row(1, 0.3)
                .row(2, 0.2)
                .row(3, 0.1)
                .row(3, 0.19)
                .pageBreak()
                .row(1, 0.4)
                .pageBreak()
                .row(1, 0.5)
                .row(1, 0.6)
                .row(2, 0.7)
                .row(2, 0.8)
                .row(2, 0.9)
                .build();
                0,
                ImmutableList.of(),
                Ints.asList(1, 0),
                Ints.asList(0),
                ImmutableList.of(),
                Optional.of(3),
                Optional.empty(),
                10);
        Operator operator = operatorFactory.createOperator(driverContext);
        MaterializedResult expectedPartition1 = resultBuilder(driverContext.getSession(), )
                .row(0.3, 1)
                .row(0.4, 1)
                .row(0.5, 1)
                .row(0.6, 1)
                .build();
        MaterializedResult expectedPartition2 = resultBuilder(driverContext.getSession(), )
                .row(0.2, 2)
                .row(0.7, 2)
                .row(0.8, 2)
                .row(0.9, 2)
                .build();
        MaterializedResult expectedPartition3 = resultBuilder(driverContext.getSession(), )
                .row(0.1, 3)
                .row(0.19, 3)
                .build();
        List<Pagepages = toPages(operatorinput);
        Block rowNumberColumn = getRowNumberColumn(pages);
        assertEquals(rowNumberColumn.getPositionCount(), 8);
        // Check that all row numbers generated are <= 3
        for (int i = 0; i < rowNumberColumn.getPositionCount(); i++) {
            assertTrue(rowNumberColumn.getLong(i, 0) <= 3);
        }
        pages = stripRowNumberColumn(pages);
        MaterializedResult actual = toMaterializedResult(operator.getOperatorContext().getSession(), ImmutableList.<Type>of(), pages);
        ImmutableSet<?> actualSet = ImmutableSet.copyOf(actual.getMaterializedRows());
        ImmutableSet<?> expectedPartition1Set = ImmutableSet.copyOf(expectedPartition1.getMaterializedRows());
        ImmutableSet<?> expectedPartition2Set = ImmutableSet.copyOf(expectedPartition2.getMaterializedRows());
        ImmutableSet<?> expectedPartition3Set = ImmutableSet.copyOf(expectedPartition3.getMaterializedRows());
        assertEquals(Sets.intersection(expectedPartition1SetactualSet).size(), 3);
        assertEquals(Sets.intersection(expectedPartition2SetactualSet).size(), 3);
        assertEquals(Sets.intersection(expectedPartition3SetactualSet).size(), 2);
    }
    @Test
    public void testRowNumberUnPartitionedLimit()
            throws Exception
    {
        DriverContext driverContext = getDriverContext();
        List<Pageinput = rowPagesBuilder()
                .row(1, 0.3)
                .row(2, 0.2)
                .row(3, 0.1)
                .row(3, 0.19)
                .pageBreak()
                .row(1, 0.4)
                .pageBreak()
                .row(1, 0.5)
                .row(1, 0.6)
                .row(2, 0.7)
                .row(2, 0.8)
                .row(2, 0.9)
                .build();
                0,
                ImmutableList.of(),
                Ints.asList(1, 0),
                Ints.asList(),
                ImmutableList.<Type>of(),
                Optional.of(3),
                Optional.empty(),
                10);
        Operator operator = operatorFactory.createOperator(driverContext);
        MaterializedResult expectedRows = resultBuilder(driverContext.getSession(), )
                .row(0.3, 1)
                .row(0.2, 2)
                .row(0.1, 3)
                .row(0.19, 3)
                .row(0.4, 1)
                .row(0.5, 1)
                .row(0.6, 1)
                .row(0.7, 2)
                .row(0.8, 2)
                .row(0.9, 2)
                .build();
        List<Pagepages = toPages(operatorinput);
        Block rowNumberColumn = getRowNumberColumn(pages);
        assertEquals(rowNumberColumn.getPositionCount(), 3);
        pages = stripRowNumberColumn(pages);
        MaterializedResult actual = toMaterializedResult(operator.getOperatorContext().getSession(), ImmutableList.<Type>of(), pages);
        assertEquals(actual.getMaterializedRows().size(), 3);
        ImmutableSet<?> actualSet = ImmutableSet.copyOf(actual.getMaterializedRows());
        ImmutableSet<?> expectedRowsSet = ImmutableSet.copyOf(expectedRows.getMaterializedRows());
        assertEquals(Sets.intersection(expectedRowsSetactualSet).size(), 3);
    }
    private static Block getRowNumberColumn(List<Pagepages)
    {
        BlockBuilder builder = .createBlockBuilder(new BlockBuilderStatus(), pages.size() * 100);
        for (Page page : pages) {
            int rowNumberChannel = page.getChannelCount() - 1;
            for (int i = 0; i < page.getPositionCount(); i++) {
                .writeLong(builderpage.getBlock(rowNumberChannel).getLong(i, 0));
            }
        }
        return builder.build();
    }
    private static List<PagestripRowNumberColumn(List<Pageinput)
    {
        return input.stream()
                .map(page -> {
                    Block[] blocks = Arrays.copyOf(page.getBlocks(), page.getChannelCount() - 1);
                    return new Page(blocks);
                })
                .collect(toImmutableList());
    }
New to GrepCode? Check out our FAQ X