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.assertOperatorEquals;
 import static com.facebook.presto.operator.OperatorAssertion.toPages;
 import static com.facebook.presto.operator.WindowFunctionDefinition.window;
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static com.facebook.presto.testing.MaterializedResult.resultBuilder;
 import static io.airlift.concurrent.Threads.daemonThreadsNamed;
 import static java.util.concurrent.Executors.newCachedThreadPool;
 
 @Test(singleThreaded = true)
 public class TestWindowOperator
 {
     private static final List<WindowFunctionDefinitionROW_NUMBER = ImmutableList.of(
             window(new ReflectionWindowFunctionSupplier<>("row_number", ImmutableList.<Type>of(), RowNumberFunction.class))
     );
 
     private static final List<WindowFunctionDefinitionFIRST_VALUE = ImmutableList.of(
             window(new ReflectionWindowFunctionSupplier<>("first_value", ImmutableList.<Type>of(), VarcharFirstValueFunction.class), 1)
     );
 
     private static final List<WindowFunctionDefinitionLAST_VALUE = ImmutableList.of(
             window(new ReflectionWindowFunctionSupplier<>("last_value", ImmutableList.<Type>of(), VarcharLastValueFunction.class), 1)
     );
 
     private static final List<WindowFunctionDefinitionNTH_VALUE = ImmutableList.of(
             window(new ReflectionWindowFunctionSupplier<>("nth_value", ImmutableList.of(), VarcharNthValueFunction.class), 1, 3)
     );
 
     private static final List<WindowFunctionDefinitionLAG = ImmutableList.of(
             window(new ReflectionWindowFunctionSupplier<>("lag", ImmutableList.of(), VarcharLagFunction.class), 1, 3, 4)
     );
 
     private static final List<WindowFunctionDefinitionLEAD = ImmutableList.of(
             window(new ReflectionWindowFunctionSupplier<>("lead", ImmutableList.of(), VarcharLeadFunction.class), 1, 3, 4)
     );
 
     private ExecutorService executor;
     private DriverContext driverContext;
 
     @BeforeMethod
     public void setUp()
     {
          = newCachedThreadPool(daemonThreadsNamed("test"));
          = new TaskContext(new TaskId("query""stage""task"), )
                 .addPipelineContext(truetrue)
                 .addDriverContext();
     }
 
     @AfterMethod
     public void tearDown()
     {
         .shutdownNow();
    }
    @Test
    public void testRowNumber()
            throws Exception
    {
        List<Pageinput = rowPagesBuilder()
                .row(2, 0.3)
                .row(4, 0.2)
                .row(6, 0.1)
                .pageBreak()
                .row(-1, -0.1)
                .row(5, 0.4)
                .build();
        WindowOperatorFactory operatorFactory = createFactoryUnbounded(
                ImmutableList.of(),
                Ints.asList(1, 0),
                ,
                Ints.asList(),
                Ints.asList(0),
                ImmutableList.copyOf(new SortOrder[] {.}));
        Operator operator = operatorFactory.createOperator();
                .row(-0.1, -1, 1)
                .row(0.3, 2, 2)
                .row(0.2, 4, 3)
                .row(0.4, 5, 4)
                .row(0.1, 6, 5)
                .build();
        assertOperatorEquals(operatorinputexpected);
    }
    @Test
    public void testRowNumberPartition()
            throws Exception
    {
        List<Pageinput = rowPagesBuilder()
                .row("b", -1, -0.1, true)
                .row("a", 2, 0.3, false)
                .row("a", 4, 0.2, true)
                .pageBreak()
                .row("b", 5, 0.4, false)
                .row("a", 6, 0.1, true)
                .build();
        WindowOperatorFactory operatorFactory = createFactoryUnbounded(
                ImmutableList.of(),
                Ints.asList(0, 1, 2, 3),
                ,
                Ints.asList(0),
                Ints.asList(1),
                ImmutableList.copyOf(new SortOrder[] {.}));
        Operator operator = operatorFactory.createOperator();
                .row("a", 2, 0.3, false, 1)
                .row("a", 4, 0.2, true, 2)
                .row("a", 6, 0.1, true, 3)
                .row("b", -1, -0.1, true, 1)
                .row("b", 5, 0.4, false, 2)
                .build();
        assertOperatorEquals(operatorinputexpected);
    }
    @Test
    public void testRowNumberArbitrary()
            throws Exception
    {
        List<Pageinput = rowPagesBuilder()
                .row(1)
                .row(3)
                .row(5)
                .row(7)
                .pageBreak()
                .row(2)
                .row(4)
                .row(6)
                .row(8)
                .build();
        WindowOperatorFactory operatorFactory = createFactoryUnbounded(
                ImmutableList.of(),
                Ints.asList(0),
                ,
                Ints.asList(),
                Ints.asList(),
                ImmutableList.copyOf(new SortOrder[] {}));
        Operator operator = operatorFactory.createOperator();
                .row(1, 1)
                .row(3, 2)
                .row(5, 3)
                .row(7, 4)
                .row(2, 5)
                .row(4, 6)
                .row(6, 7)
                .row(8, 8)
                .build();
        assertOperatorEquals(operatorinputexpected);
    }
    @Test(expectedExceptions = ExceededMemoryLimitException.class, expectedExceptionsMessageRegExp = "Task exceeded max memory size of 10B")
    public void testMemoryLimit()
            throws Exception
    {
        List<Pageinput = rowPagesBuilder()
                .row(1, 0.1)
                .row(2, 0.2)
                .pageBreak()
                .row(-1, -0.1)
                .row(4, 0.4)
                .build();
        DriverContext driverContext = new TaskContext(new TaskId("query""stage""task"), new DataSize(10, .))
                .addPipelineContext(truetrue)
                .addDriverContext();
        WindowOperatorFactory operatorFactory = createFactoryUnbounded(
                ImmutableList.of(),
                Ints.asList(1),
                ,
                Ints.asList(),
                Ints.asList(0),
                ImmutableList.copyOf(new SortOrder[] {.}));
        Operator operator = operatorFactory.createOperator(driverContext);
        toPages(operatorinput);
    }
    @Test
    public void testFirstValuePartition()
            throws Exception
    {
        List<Pageinput = rowPagesBuilder()
                .row("b""A1", 1, true"")
                .row("a""A2", 1, false"")
                .row("a""B1", 2, true"")
                .pageBreak()
                .row("b""C1", 2, false"")
                .row("a""C2", 3, true"")
                .row("c""A3", 1, true"")
                .build();
        WindowOperatorFactory operatorFactory = createFactoryUnbounded(
                ImmutableList.of(),
                Ints.asList(0, 1, 2, 3),
                ,
                Ints.asList(0),
                Ints.asList(2),
                ImmutableList.copyOf(new SortOrder[] {.}));
        Operator operator = operatorFactory.createOperator();
                .row("a""A2", 1, false"A2")
                .row("a""B1", 2, true"A2")
                .row("a""C2", 3, true"A2")
                .row("b""A1", 1, true"A1")
                .row("b""C1", 2, false"A1")
                .row("c""A3", 1, true"A3")
                .build();
        assertOperatorEquals(operatorinputexpected);
    }
    @Test
    public void testLastValuePartition()
            throws Exception
    {
        List<Pageinput = rowPagesBuilder()
                .row("b""A1", 1, true"")
                .row("a""A2", 1, false"")
                .row("a""B1", 2, true"")
                .pageBreak()
                .row("b""C1", 2, false"")
                .row("a""C2", 3, true"")
                .row("c""A3", 1, true"")
                .build();
        WindowOperatorFactory operatorFactory = createFactoryUnbounded(
                ImmutableList.of(),
                Ints.asList(0, 1, 2, 3),
                ,
                Ints.asList(0),
                Ints.asList(2),
                ImmutableList.copyOf(new SortOrder[] {.}));
        Operator operator = operatorFactory.createOperator();
                .row("a""A2", 1, false"C2")
                .row("a""B1", 2, true"C2")
                .row("a""C2", 3, true"C2")
                .row("b""A1", 1, true"C1")
                .row("b""C1", 2, false"C1")
                .row("c""A3", 1, true"A3")
                .build();
        assertOperatorEquals(operatorinputexpected);
    }
    @Test
    public void testNthValuePartition()
            throws Exception
    {
        List<Pageinput = rowPagesBuilder()
                .row("b""A1", 1, 2, true"")
                .row("a""A2", 1, 3, false"")
                .row("a""B1", 2, 2, true"")
                .pageBreak()
                .row("b""C1", 2, 3, false"")
                .row("a""C2", 3, 1, true"")
                .row("c""A3", 1, nulltrue"")
                .build();
        WindowOperatorFactory operatorFactory = createFactoryUnbounded(
                ImmutableList.of(),
                Ints.asList(0, 1, 2, 4),
                ,
                Ints.asList(0),
                Ints.asList(2),
                ImmutableList.copyOf(new SortOrder[] {.}));
        Operator operator = operatorFactory.createOperator();
                .row("a""A2", 1, false"C2")
                .row("a""B1", 2, true"B1")
                .row("a""C2", 3, true"A2")
                .row("b""A1", 1, true"C1")
                .row("b""C1", 2, falsenull)
                .row("c""A3", 1, truenull)
                .build();
        assertOperatorEquals(operatorinputexpected);
    }
    @Test
    public void testLagPartition()
            throws Exception
    {
        List<Pageinput = rowPagesBuilder()
                .row("b""A1", 1, 1, "D"true"")
                .row("a""A2", 1, 2, "D"false"")
                .row("a""B1", 2, 2, "D"true"")
                .pageBreak()
                .row("b""C1", 2, 1, "D"false"")
                .row("a""C2", 3, 2, "D"true"")
                .row("c""A3", 1, 1, "D"true"")
                .build();
        WindowOperatorFactory operatorFactory = createFactoryUnbounded(
                ImmutableList.of(),
                Ints.asList(0, 1, 2, 5),
                ,
                Ints.asList(0),
                Ints.asList(2),
                ImmutableList.copyOf(new SortOrder[] {.}));
        Operator operator = operatorFactory.createOperator();
                .row("a""A2", 1, false"D")
                .row("a""B1", 2, true"D")
                .row("a""C2", 3, true"A2")
                .row("b""A1", 1, true"D")
                .row("b""C1", 2, false"A1")
                .row("c""A3", 1, true"D")
                .build();
        assertOperatorEquals(operatorinputexpected);
    }
    @Test
    public void testLeadPartition()
            throws Exception
    {
        List<Pageinput = rowPagesBuilder()
                .row("b""A1", 1, 1, "D"true"")
                .row("a""A2", 1, 2, "D"false"")
                .row("a""B1", 2, 2, "D"true"")
                .pageBreak()
                .row("b""C1", 2, 1, "D"false"")
                .row("a""C2", 3, 2, "D"true"")
                .row("c""A3", 1, 1, "D"true"")
                .build();
        WindowOperatorFactory operatorFactory = createFactoryUnbounded(
                ImmutableList.of(),
                Ints.asList(0, 1, 2, 5),
                ,
                Ints.asList(0),
                Ints.asList(2),
                ImmutableList.copyOf(new SortOrder[] {.}));
        Operator operator = operatorFactory.createOperator();
                .row("a""A2", 1, false"C2")
                .row("a""B1", 2, true"D")
                .row("a""C2", 3, true"D")
                .row("b""A1", 1, true"C1")
                .row("b""C1", 2, false"D")
                .row("c""A3", 1, true"D")
                .build();
        assertOperatorEquals(operatorinputexpected);
    }
            List<? extends TypesourceTypes,
            List<IntegeroutputChannels,
            List<WindowFunctionDefinitionfunctions,
            List<IntegerpartitionChannels,
            List<IntegersortChannels,
            List<SortOrdersortOrder)
    {
        return new WindowOperatorFactory(
                0,
                sourceTypes,
                outputChannels,
                functions,
                partitionChannels,
                sortChannels,
                sortOrder,
                ..,
                .., Optional.empty(),
                .., Optional.empty(),
                10);
    }
New to GrepCode? Check out our FAQ X