Search in sources :

Example 1 with SortOrder

use of io.trino.spi.connector.SortOrder in project trino by trinodb.

the class OrderingCompiler method generatePageIndexCompareTo.

private void generatePageIndexCompareTo(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> sortTypes, List<Integer> sortChannels, List<SortOrder> sortOrders) {
    Parameter pagesIndex = arg("pagesIndex", PagesIndex.class);
    Parameter leftPosition = arg("leftPosition", int.class);
    Parameter rightPosition = arg("rightPosition", int.class);
    MethodDefinition compareToMethod = classDefinition.declareMethod(a(PUBLIC), "compareTo", type(int.class), pagesIndex, leftPosition, rightPosition);
    Scope scope = compareToMethod.getScope();
    Variable valueAddresses = scope.declareVariable(LongArrayList.class, "valueAddresses");
    compareToMethod.getBody().comment("LongArrayList valueAddresses = pagesIndex.valueAddresses").append(valueAddresses.set(pagesIndex.invoke("getValueAddresses", LongArrayList.class)));
    Variable leftPageAddress = scope.declareVariable(long.class, "leftPageAddress");
    compareToMethod.getBody().comment("long leftPageAddress = valueAddresses.getLong(leftPosition)").append(leftPageAddress.set(valueAddresses.invoke("getLong", long.class, leftPosition)));
    Variable leftBlockIndex = scope.declareVariable(int.class, "leftBlockIndex");
    compareToMethod.getBody().comment("int leftBlockIndex = decodeSliceIndex(leftPageAddress)").append(leftBlockIndex.set(invokeStatic(SyntheticAddress.class, "decodeSliceIndex", int.class, leftPageAddress)));
    Variable leftBlockPosition = scope.declareVariable(int.class, "leftBlockPosition");
    compareToMethod.getBody().comment("int leftBlockPosition = decodePosition(leftPageAddress)").append(leftBlockPosition.set(invokeStatic(SyntheticAddress.class, "decodePosition", int.class, leftPageAddress)));
    Variable rightPageAddress = scope.declareVariable(long.class, "rightPageAddress");
    compareToMethod.getBody().comment("long rightPageAddress = valueAddresses.getLong(rightPosition);").append(rightPageAddress.set(valueAddresses.invoke("getLong", long.class, rightPosition)));
    Variable rightBlockIndex = scope.declareVariable(int.class, "rightBlockIndex");
    compareToMethod.getBody().comment("int rightBlockIndex = decodeSliceIndex(rightPageAddress)").append(rightBlockIndex.set(invokeStatic(SyntheticAddress.class, "decodeSliceIndex", int.class, rightPageAddress)));
    Variable rightBlockPosition = scope.declareVariable(int.class, "rightBlockPosition");
    compareToMethod.getBody().comment("int rightBlockPosition = decodePosition(rightPageAddress)").append(rightBlockPosition.set(invokeStatic(SyntheticAddress.class, "decodePosition", int.class, rightPageAddress)));
    for (int i = 0; i < sortChannels.size(); i++) {
        int sortChannel = sortChannels.get(i);
        SortOrder sortOrder = sortOrders.get(i);
        Type sortType = sortTypes.get(i);
        MethodHandle compareBlockValue = getBlockPositionOrderingOperator(sortOrder, sortType);
        BytecodeBlock block = new BytecodeBlock().setDescription("compare channel " + sortChannel + " " + sortOrder);
        BytecodeExpression leftBlock = pagesIndex.invoke("getChannel", ObjectArrayList.class, constantInt(sortChannel)).invoke("get", Object.class, leftBlockIndex).cast(Block.class);
        BytecodeExpression rightBlock = pagesIndex.invoke("getChannel", ObjectArrayList.class, constantInt(sortChannel)).invoke("get", Object.class, rightBlockIndex).cast(Block.class);
        block.append(invokeDynamic(BOOTSTRAP_METHOD, ImmutableList.of(callSiteBinder.bind(compareBlockValue).getBindingId()), "compareBlockValue", compareBlockValue.type(), leftBlock, leftBlockPosition, rightBlock, rightBlockPosition));
        LabelNode equal = new LabelNode("equal");
        block.comment("if (compare != 0) return compare").dup().ifZeroGoto(equal).retInt().visitLabel(equal).pop(int.class);
        compareToMethod.getBody().append(block);
    }
    // values are equal
    compareToMethod.getBody().push(0).retInt();
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) Type(io.trino.spi.type.Type) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter) SortOrder(io.trino.spi.connector.SortOrder) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with SortOrder

use of io.trino.spi.connector.SortOrder in project trino by trinodb.

the class TestWindowOperator method testMultipleOutputPages.

@Test(dataProvider = "spillEnabled")
public void testMultipleOutputPages(boolean spillEnabled, boolean revokeMemoryWhenAddingPages, long memoryLimit) {
    // make operator produce multiple pages during finish phase
    int numberOfRows = 80_000;
    List<Page> input = rowPagesBuilder(BIGINT, DOUBLE).addSequencePage(numberOfRows, 0, 0).build();
    WindowOperatorFactory operatorFactory = createFactoryUnbounded(ImmutableList.of(BIGINT, DOUBLE), Ints.asList(1, 0), ROW_NUMBER, Ints.asList(), Ints.asList(0), ImmutableList.copyOf(new SortOrder[] { SortOrder.DESC_NULLS_FIRST }), spillEnabled);
    DriverContext driverContext = createDriverContext(memoryLimit);
    MaterializedResult.Builder expectedBuilder = resultBuilder(driverContext.getSession(), DOUBLE, BIGINT, BIGINT);
    for (int i = 0; i < numberOfRows; ++i) {
        expectedBuilder.row((double) numberOfRows - i - 1, (long) numberOfRows - i - 1, (long) i + 1);
    }
    MaterializedResult expected = expectedBuilder.build();
    List<Page> pages = toPages(operatorFactory, driverContext, input, revokeMemoryWhenAddingPages);
    assertGreaterThan(pages.size(), 1, "Expected more than one output page");
    MaterializedResult actual = toMaterializedResult(driverContext.getSession(), expected.getTypes(), pages);
    assertEquals(actual.getMaterializedRows(), expected.getMaterializedRows());
    assertTrue(spillEnabled == (spillerFactory.getSpillsCount() > 0), format("Spill state mismatch. Expected spill: %s, spill count: %s", spillEnabled, spillerFactory.getSpillsCount()));
}
Also used : WindowOperatorFactory(io.trino.operator.WindowOperator.WindowOperatorFactory) SortOrder(io.trino.spi.connector.SortOrder) Page(io.trino.spi.Page) MaterializedResult(io.trino.testing.MaterializedResult) OperatorAssertion.toMaterializedResult(io.trino.operator.OperatorAssertion.toMaterializedResult) Test(org.testng.annotations.Test)

Example 3 with SortOrder

use of io.trino.spi.connector.SortOrder in project trino by trinodb.

the class TestWindowOperator method testLastValuePartition.

@Test(dataProvider = "spillEnabled")
public void testLastValuePartition(boolean spillEnabled, boolean revokeMemoryWhenAddingPages, long memoryLimit) {
    List<Page> input = rowPagesBuilder(VARCHAR, VARCHAR, BIGINT, BOOLEAN, VARCHAR).row("b", "A1", 1L, true, "").row("a", "A2", 1L, false, "").row("a", "B1", 2L, true, "").pageBreak().row("b", "C1", 2L, false, "").row("a", "C2", 3L, true, "").row("c", "A3", 1L, true, "").build();
    DriverContext driverContext = createDriverContext(memoryLimit);
    WindowOperatorFactory operatorFactory = createFactoryUnbounded(ImmutableList.of(VARCHAR, VARCHAR, BIGINT, BOOLEAN, VARCHAR), Ints.asList(0, 1, 2, 3), LAST_VALUE, Ints.asList(0), Ints.asList(2), ImmutableList.copyOf(new SortOrder[] { SortOrder.ASC_NULLS_LAST }), spillEnabled);
    MaterializedResult expected = resultBuilder(driverContext.getSession(), VARCHAR, VARCHAR, BIGINT, BOOLEAN, VARCHAR).row("a", "A2", 1L, false, "C2").row("a", "B1", 2L, true, "C2").row("a", "C2", 3L, true, "C2").row("b", "A1", 1L, true, "C1").row("b", "C1", 2L, false, "C1").row("c", "A3", 1L, true, "A3").build();
    assertOperatorEquals(operatorFactory, driverContext, input, expected, revokeMemoryWhenAddingPages);
}
Also used : WindowOperatorFactory(io.trino.operator.WindowOperator.WindowOperatorFactory) SortOrder(io.trino.spi.connector.SortOrder) Page(io.trino.spi.Page) MaterializedResult(io.trino.testing.MaterializedResult) OperatorAssertion.toMaterializedResult(io.trino.operator.OperatorAssertion.toMaterializedResult) Test(org.testng.annotations.Test)

Example 4 with SortOrder

use of io.trino.spi.connector.SortOrder in project trino by trinodb.

the class TestWindowOperator method testNthValuePartition.

@Test(dataProvider = "spillEnabled")
public void testNthValuePartition(boolean spillEnabled, boolean revokeMemoryWhenAddingPages, long memoryLimit) {
    List<Page> input = rowPagesBuilder(VARCHAR, VARCHAR, BIGINT, BIGINT, BOOLEAN, VARCHAR).row("b", "A1", 1L, 2L, true, "").row("a", "A2", 1L, 3L, false, "").row("a", "B1", 2L, 2L, true, "").pageBreak().row("b", "C1", 2L, 3L, false, "").row("a", "C2", 3L, 1L, true, "").row("c", "A3", 1L, null, true, "").build();
    WindowOperatorFactory operatorFactory = createFactoryUnbounded(ImmutableList.of(VARCHAR, VARCHAR, BIGINT, BIGINT, BOOLEAN, VARCHAR), Ints.asList(0, 1, 2, 4), NTH_VALUE, Ints.asList(0), Ints.asList(2), ImmutableList.copyOf(new SortOrder[] { SortOrder.ASC_NULLS_LAST }), spillEnabled);
    DriverContext driverContext = createDriverContext(memoryLimit);
    MaterializedResult expected = resultBuilder(driverContext.getSession(), VARCHAR, VARCHAR, BIGINT, BOOLEAN, VARCHAR).row("a", "A2", 1L, false, "C2").row("a", "B1", 2L, true, "B1").row("a", "C2", 3L, true, "A2").row("b", "A1", 1L, true, "C1").row("b", "C1", 2L, false, null).row("c", "A3", 1L, true, null).build();
    assertOperatorEquals(operatorFactory, driverContext, input, expected, revokeMemoryWhenAddingPages);
}
Also used : WindowOperatorFactory(io.trino.operator.WindowOperator.WindowOperatorFactory) SortOrder(io.trino.spi.connector.SortOrder) Page(io.trino.spi.Page) MaterializedResult(io.trino.testing.MaterializedResult) OperatorAssertion.toMaterializedResult(io.trino.operator.OperatorAssertion.toMaterializedResult) Test(org.testng.annotations.Test)

Example 5 with SortOrder

use of io.trino.spi.connector.SortOrder in project trino by trinodb.

the class TestWindowOperator method testRowNumberArbitraryWithSpill.

@Test
public void testRowNumberArbitraryWithSpill() {
    List<Page> input = rowPagesBuilder(BIGINT).row(1L).row(3L).row(5L).row(7L).pageBreak().row(2L).row(4L).row(6L).row(8L).build();
    WindowOperatorFactory operatorFactory = createFactoryUnbounded(ImmutableList.of(BIGINT), Ints.asList(0), ROW_NUMBER, Ints.asList(), Ints.asList(), ImmutableList.copyOf(new SortOrder[] {}), true);
    DriverContext driverContext = createDriverContext();
    MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT, BIGINT).row(1L, 1L).row(2L, 2L).row(3L, 3L).row(4L, 4L).row(5L, 5L).row(6L, 6L).row(7L, 7L).row(8L, 8L).build();
    assertOperatorEquals(operatorFactory, driverContext, input, expected);
}
Also used : WindowOperatorFactory(io.trino.operator.WindowOperator.WindowOperatorFactory) SortOrder(io.trino.spi.connector.SortOrder) Page(io.trino.spi.Page) MaterializedResult(io.trino.testing.MaterializedResult) OperatorAssertion.toMaterializedResult(io.trino.operator.OperatorAssertion.toMaterializedResult) Test(org.testng.annotations.Test)

Aggregations

SortOrder (io.trino.spi.connector.SortOrder)20 Page (io.trino.spi.Page)14 Test (org.testng.annotations.Test)13 WindowOperatorFactory (io.trino.operator.WindowOperator.WindowOperatorFactory)12 OperatorAssertion.toMaterializedResult (io.trino.operator.OperatorAssertion.toMaterializedResult)11 MaterializedResult (io.trino.testing.MaterializedResult)11 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 Type (io.trino.spi.type.Type)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)2 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)2 MethodDefinition (io.airlift.bytecode.MethodDefinition)2 Parameter (io.airlift.bytecode.Parameter)2 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)2 LabelNode (io.airlift.bytecode.instruction.LabelNode)2 SelectExpression (io.trino.sql.analyzer.Analysis.SelectExpression)2 OrderingScheme.sortItemToSortOrder (io.trino.sql.planner.OrderingScheme.sortItemToSortOrder)2 Symbol (io.trino.sql.planner.Symbol)2 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)2