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.operator.PageAssertions.assertPageEquals;
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
 import static io.airlift.testing.Assertions.assertEqualsIgnoreOrder;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 
 public final class OperatorAssertion
 {
     private OperatorAssertion()
     {
     }
 
     public static List<PageappendSampleWeight(List<Pageinputint sampleWeight)
     {
         return input.stream()
                 .map(page -> {
                     BlockBuilder builder = .createBlockBuilder(new BlockBuilderStatus(), page.getPositionCount());
                     for (int i = 0; i < page.getPositionCount(); i++) {
                         .writeLong(buildersampleWeight);
                     }
                     Block[] blocks = new Block[page.getChannelCount() + 1];
                     System.arraycopy(page.getBlocks(), 0, blocks, 0, page.getChannelCount());
                     blocks[blocks.length - 1] = builder.build();
                     return new Page(blocks);
                 })
                 .collect(toImmutableList());
     }
 
     public static List<PagetoPages(Operator operatorIterator<Pageinput)
     {
         ImmutableList.Builder<PageoutputPages = ImmutableList.builder();
 
         boolean finishing = false;
 
         for (int loops = 0; !operator.isFinished() && loops < 10_000; loops++) {
             if (operator.needsInput()) {
                 if (input.hasNext()) {
                     Page inputPage = input.next();
                     operator.addInput(inputPage);
                 }
                 else if (!finishing) {
                     operator.finish();
                     finishing = true;
                 }
             }
 
             Page outputPage = operator.getOutput();
             if (outputPage != null) {
                 outputPages.add(outputPage);
             }
         }
 
         assertFalse(operator.needsInput());
         assertTrue(operator.isBlocked().isDone());
         assertTrue(operator.isFinished());
 
         return outputPages.build();
     }
 
     public static List<PagetoPages(Operator operatorList<Pageinput)
     {
         // verify initial state
         assertEquals(operator.isFinished(), false);
         assertEquals(operator.needsInput(), true);
         assertEquals(operator.getOutput(), null);
 
         return toPages(operatorinput.iterator());
     }
    public static List<PagetoPages(Operator operator)
    {
        // operator does not have input so should never require input
        assertEquals(operator.needsInput(), false);
        ImmutableList.Builder<PageoutputPages = ImmutableList.builder();
        addRemainingOutputPages(operatoroutputPages);
        return outputPages.build();
    }
    private static void addRemainingOutputPages(Operator operatorImmutableList.Builder<PageoutputPages)
    {
        // pull remaining output pages
        while (!operator.isFinished()) {
            // at this point the operator should not need more input
            assertEquals(operator.needsInput(), false);
            Page outputPage = operator.getOutput();
            if (outputPage != null) {
                outputPages.add(outputPage);
            }
        }
        // verify final state
        assertEquals(operator.isFinished(), true);
        assertEquals(operator.needsInput(), false);
        assertEquals(operator.getOutput(), null);
    }
    public static MaterializedResult toMaterializedResult(Session sessionList<TypetypesList<Pagepages)
    {
        // materialize pages
        MaterializedResult.Builder resultBuilder = MaterializedResult.resultBuilder(sessiontypes);
        for (Page outputPage : pages) {
            resultBuilder.page(outputPage);
        }
        return resultBuilder.build();
    }
    public static void assertOperatorEquals(Operator operatorList<Pageexpected)
    {
        List<Pageactual = toPages(operator);
        assertEquals(actual.size(), expected.size());
        for (int i = 0; i < actual.size(); i++) {
            assertPageEquals(operator.getTypes(), actual.get(i), expected.get(i));
        }
    }
    public static void assertOperatorEquals(Operator operatorList<PageinputList<Pageexpected)
    {
        List<Pageactual = toPages(operatorinput);
        assertEquals(actual.size(), expected.size());
        for (int i = 0; i < actual.size(); i++) {
            assertPageEquals(operator.getTypes(), actual.get(i), expected.get(i));
        }
    }
    public static void assertOperatorEquals(Operator operatorMaterializedResult expected)
    {
        List<Pagepages = toPages(operator);
        MaterializedResult actual = toMaterializedResult(operator.getOperatorContext().getSession(), operator.getTypes(), pages);
        assertEquals(actualexpected);
    }
    public static void assertOperatorEquals(Operator operatorList<PageinputMaterializedResult expected)
    {
        assertOperatorEquals(operatorinputexpectedfalse, ImmutableList.<Integer>of());
    }
    public static void assertOperatorEquals(Operator operatorList<PageinputMaterializedResult expectedboolean hashEnabledList<IntegerhashChannels)
    {
        List<Pagepages = toPages(operatorinput);
        MaterializedResult actual;
        if (hashEnabled && !hashChannels.isEmpty()) {
            // Drop the hashChannel for all pages
            List<PageactualPages = dropChannel(pageshashChannels);
            List<TypeexpectedTypes = without(operator.getTypes(), hashChannels);
            actual = toMaterializedResult(operator.getOperatorContext().getSession(), expectedTypesactualPages);
        }
        else {
            actual = toMaterializedResult(operator.getOperatorContext().getSession(), operator.getTypes(), pages);
        }
        assertEquals(actualexpected);
    }
    public static void assertOperatorEqualsIgnoreOrder(Operator operatorList<PageinputMaterializedResult expected)
    {
        assertOperatorEqualsIgnoreOrder(operatorinputexpectedfalse, Optional.empty());
    }
    public static void assertOperatorEqualsIgnoreOrder(Operator operatorList<PageinputMaterializedResult expectedboolean hashEnabledOptional<IntegerhashChannel)
    {
        List<Pagepages = toPages(operatorinput);
        MaterializedResult actual;
        if (hashEnabled && hashChannel.isPresent()) {
            // Drop the hashChannel for all pages
            List<PageactualPages = dropChannel(pages, ImmutableList.of(hashChannel.get()));
            List<TypeexpectedTypes = without(operator.getTypes(), ImmutableList.of(hashChannel.get()));
            actual = toMaterializedResult(operator.getOperatorContext().getSession(), expectedTypesactualPages);
        }
        else {
            actual = toMaterializedResult(operator.getOperatorContext().getSession(), operator.getTypes(), pages);
        }
    }
    static <T> List<T> without(List<T> typesList<Integerchannels)
    {
        types = new ArrayList<>(types);
        int removed = 0;
        for (int hashChannel : channels) {
            types.remove(hashChannel - removed);
            removed++;
        }
        return ImmutableList.copyOf(types);
    }
    static List<PagedropChannel(List<PagepagesList<Integerchannels)
    {
        List<PageactualPages = new ArrayList<>();
        for (Page page : pages) {
            int channel = 0;
            Block[] blocks = new Block[page.getChannelCount() - channels.size()];
            for (int i = 0; i < page.getChannelCount(); i++) {
                if (channels.contains(i)) {
                    continue;
                }
                blocks[channel++] = page.getBlock(i);
            }
            actualPages.add(new Page(blocks));
        }
        return actualPages;
    }
New to GrepCode? Check out our FAQ X