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;
 
 
 import java.util.List;
 
 import static com.facebook.presto.SessionTestUtils.TEST_SESSION;
 import static com.facebook.presto.operator.OperatorAssertion.toMaterializedResult;
 import static com.facebook.presto.spi.block.SortOrder.ASC_NULLS_FIRST;
 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;
 
 {
     private static final PagesIndexPageSorter sorter = new PagesIndexPageSorter();
 
     @Test
     public void testPageSorter()
             throws Exception
     {
         List<Typetypes = ImmutableList.of();
         List<TypesortTypes = ImmutableList.of();
         List<IntegersortChannels = Ints.asList(0);
         List<SortOrdersortOrders = ImmutableList.of();
 
         List<PageinputPages = RowPagesBuilder.rowPagesBuilder(types)
                 .row(2, 1.1, "d")
                 .row(1, 2.2, "c")
                 .pageBreak()
                 .row(-2, 2.2, "b")
                 .row(-12, 2.2, "a")
                 .build();
 
         List<PageexpectedPages = RowPagesBuilder.rowPagesBuilder(types)
                 .row(-12, 2.2, "a")
                 .row(-2, 2.2, "b")
                 .pageBreak()
                 .row(1, 2.2, "c")
                 .row(2, 1.1, "d")
                 .build();
 
         assertSorted(inputPagesexpectedPagestypessortTypessortChannelssortOrders, 100);
     }
 
     @Test
     public void testPageSorterMultipleChannels()
             throws Exception
     {
         List<Typetypes = ImmutableList.of();
         List<TypesortTypes = ImmutableList.copyOf(types);
         List<IntegersortChannels = Ints.asList(0, 1, 2);
         List<SortOrdersortOrders = Collections.nCopies(sortChannels.size(), );
 
         List<PageinputPages = RowPagesBuilder.rowPagesBuilder(types)
                 .row(2, 1.1, "d")
                 .row(1, 2.2, "c")
                 .pageBreak()
                 .row(1, 2.2, "b")
                 .row(1, 2.2, "a")
                 .pageBreak()
                 .row(1, 2.2, null)
                 .row(1, null"z")
                 .row(1, nullnull)
                 .build();
 
         List<PageexpectedPages = RowPagesBuilder.rowPagesBuilder(types)
                 .row(1, nullnull)
                 .row(1, null"z")
                 .row(1, 2.2, null)
                 .row(1, 2.2, "a")
                 .row(1, 2.2, "b")
                 .row(1, 2.2, "c")
                 .row(2, 1.1, "d")
                 .build();
         assertSorted(inputPagesexpectedPagestypessortTypessortChannelssortOrders, 100);
     }
    @Test
    public void testPageSorterSorted()
            throws Exception
    {
        List<Typetypes = ImmutableList.of();
        List<TypesortTypes = ImmutableList.of();
        List<IntegersortChannels = Ints.asList(0);
        List<SortOrdersortOrders = ImmutableList.of();
        List<PageinputPages = RowPagesBuilder.rowPagesBuilder(types)
                .row(-12, 2.2, "a")
                .row(-2, 2.2, "b")
                .pageBreak()
                .row(1, 2.2, "d")
                .row(2, 1.1, "c")
                .build();
        List<PageexpectedPages = RowPagesBuilder.rowPagesBuilder(types)
                .row(-12, 2.2, "a")
                .row(-2, 2.2, "b")
                .row(1, 2.2, "d")
                .row(2, 1.1, "c")
                .build();
        assertSorted(inputPagesexpectedPagestypessortTypessortChannelssortOrders, 100);
    }
    @Test
    public void testPageSorterForceExpansion()
            throws Exception
    {
        List<Typetypes = ImmutableList.of();
        List<TypesortTypes = ImmutableList.of();
        List<IntegersortChannels = Ints.asList(0);
        List<SortOrdersortOrders = ImmutableList.of();
        List<PageinputPages = RowPagesBuilder.rowPagesBuilder(types)
                .row(2, 1.1, "c")
                .row(1, 2.2, "d")
                .pageBreak()
                .row(-2, 2.2, "b")
                .row(-12, 2.2, "a")
                .build();
        List<PageexpectedPages = RowPagesBuilder.rowPagesBuilder(types)
                .row(-12, 2.2, "a")
                .row(-2, 2.2, "b")
                .pageBreak()
                .row(1, 2.2, "d")
                .row(2, 1.1, "c")
                .build();
        assertSorted(inputPagesexpectedPagestypessortTypessortChannelssortOrders, 2);
    }
    private static void assertSorted(List<PageinputPagesList<PageexpectedPagesList<TypetypesList<TypesortTypesList<IntegersortChannelsList<SortOrdersortOrdersint expectedPositions)
    {
        long[] sortedAddresses = .sort(typesinputPagessortTypessortChannelssortOrdersexpectedPositions);
        List<PageoutputPages = createOutputPages(typesinputPagessortedAddresses);
        MaterializedResult expected = toMaterializedResult(typesexpectedPages);
        MaterializedResult actual = toMaterializedResult(typesoutputPages);
        assertEquals(actual.getMaterializedRows(), expected.getMaterializedRows());
    }
    private static List<PagecreateOutputPages(List<TypetypesList<PageinputPageslong[] sortedAddresses)
    {
        PageBuilder pageBuilder = new PageBuilder(types);
        pageBuilder.reset();
        for (long address : sortedAddresses) {
            int index = .decodePageIndex(address);
            int position = .decodePositionIndex(address);
            Page page = inputPages.get(index);
            for (int i = 0; i < types.size(); i++) {
                Type type = types.get(i);
                type.appendTo(page.getBlock(i), positionpageBuilder.getBlockBuilder(i));
            }
            pageBuilder.declarePosition();
        }
        return ImmutableList.of(pageBuilder.build());
    }
New to GrepCode? Check out our FAQ X