Search in sources :

Example 1 with SortOrder

use of com.facebook.presto.common.block.SortOrder in project presto by prestodb.

the class TestShardCompactor method testShardCompactorSorted.

@Test(dataProvider = "useOptimizedOrcWriter")
public void testShardCompactorSorted(boolean useOptimizedOrcWriter) throws Exception {
    StorageManager storageManager = createOrcStorageManager(dbi, temporary, MAX_SHARD_ROWS);
    List<Type> columnTypes = ImmutableList.of(BIGINT, createVarcharType(20), DATE, TIMESTAMP, DOUBLE);
    List<Long> columnIds = ImmutableList.of(3L, 7L, 2L, 1L, 5L);
    List<Long> sortColumnIds = ImmutableList.of(1L, 2L, 3L, 5L, 7L);
    List<SortOrder> sortOrders = nCopies(sortColumnIds.size(), ASC_NULLS_FIRST);
    List<Integer> sortIndexes = sortColumnIds.stream().map(columnIds::indexOf).collect(toList());
    List<ShardInfo> inputShards = createSortedShards(storageManager, columnIds, columnTypes, sortIndexes, sortOrders, 2);
    assertEquals(inputShards.size(), 2);
    long totalRows = inputShards.stream().mapToLong(ShardInfo::getRowCount).sum();
    long expectedOutputShards = computeExpectedOutputShards(totalRows);
    Map<UUID, Optional<UUID>> inputUuids = new HashMap<>();
    for (ShardInfo shardInfo : inputShards) {
        inputUuids.put(shardInfo.getShardUuid(), Optional.empty());
    }
    long transactionId = 1;
    ShardCompactor compactor = new ShardCompactor(storageManager, READER_ATTRIBUTES);
    List<ShardInfo> outputShards = compactor.compactSorted(transactionId, false, OptionalInt.empty(), inputUuids, getColumnInfo(columnIds, columnTypes), sortColumnIds, sortOrders);
    List<UUID> outputUuids = outputShards.stream().map(ShardInfo::getShardUuid).collect(toList());
    assertEquals(outputShards.size(), expectedOutputShards);
    assertShardEqualsSorted(storageManager, inputUuids.keySet(), outputUuids, columnIds, columnTypes, sortIndexes, sortOrders);
}
Also used : Optional(java.util.Optional) HashMap(java.util.HashMap) StorageManager(com.facebook.presto.raptor.storage.StorageManager) TestOrcStorageManager.createOrcStorageManager(com.facebook.presto.raptor.storage.TestOrcStorageManager.createOrcStorageManager) SortOrder(com.facebook.presto.common.block.SortOrder) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) Type(com.facebook.presto.common.type.Type) UUID(java.util.UUID) ShardInfo(com.facebook.presto.raptor.metadata.ShardInfo) Test(org.testng.annotations.Test)

Example 2 with SortOrder

use of com.facebook.presto.common.block.SortOrder in project presto by prestodb.

the class TestMergingPageIterator method testMerging.

@Test
public void testMerging() {
    List<Type> types = ImmutableList.of(INTEGER, INTEGER);
    List<Integer> sortIndexes = ImmutableList.of(1);
    List<SortOrder> sortOrders = ImmutableList.of(SortOrder.ASC_NULLS_FIRST);
    List<List<Page>> pageLists = new ArrayList<>();
    PageBuilder pageBuilder = new PageBuilder(types);
    for (int i = 0; i < 10; i++) {
        Iterator<Integer> values = IntStream.range(0, 1000).map(ignored -> ThreadLocalRandom.current().nextInt(100_000)).mapToObj(n -> ((n % 100) == 0) ? null : n).sorted(nullsFirst(naturalOrder())).iterator();
        List<Page> pages = new ArrayList<>();
        for (int j = 0; j < 10; j++) {
            for (int k = 0; k < 100; k++) {
                Integer n = values.next();
                pageBuilder.declarePosition();
                if (n == null) {
                    pageBuilder.getBlockBuilder(0).appendNull();
                    pageBuilder.getBlockBuilder(1).appendNull();
                } else {
                    INTEGER.writeLong(pageBuilder.getBlockBuilder(0), n);
                    INTEGER.writeLong(pageBuilder.getBlockBuilder(1), n * 22L);
                }
            }
            pages.add(pageBuilder.build());
            pageBuilder.reset();
        }
        pageLists.add(pages);
        assertFalse(values.hasNext());
    }
    List<Iterator<Page>> pages = pageLists.stream().map(List::iterator).collect(toList());
    Iterator<Page> iterator = new MergingPageIterator(pages, types, sortIndexes, sortOrders);
    List<Long> values = new ArrayList<>();
    while (iterator.hasNext()) {
        Page page = iterator.next();
        for (int i = 0; i < page.getPositionCount(); i++) {
            if (page.getBlock(0).isNull(i)) {
                assertTrue(page.getBlock(1).isNull(i));
                values.add(null);
            } else {
                long x = INTEGER.getLong(page.getBlock(0), i);
                long y = INTEGER.getLong(page.getBlock(1), i);
                assertEquals(y, x * 22);
                values.add(x);
            }
        }
    }
    assertThat(values).isSortedAccordingTo(nullsFirst(naturalOrder()));
}
Also used : IntStream(java.util.stream.IntStream) Page(com.facebook.presto.common.Page) Comparator.nullsFirst(java.util.Comparator.nullsFirst) Iterator(java.util.Iterator) Comparator.naturalOrder(java.util.Comparator.naturalOrder) SortOrder(com.facebook.presto.common.block.SortOrder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) PageBuilder(com.facebook.presto.common.PageBuilder) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ImmutableList(com.google.common.collect.ImmutableList) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assert.assertTrue(org.testng.Assert.assertTrue) Assert.assertFalse(org.testng.Assert.assertFalse) Type(com.facebook.presto.common.type.Type) ArrayList(java.util.ArrayList) SortOrder(com.facebook.presto.common.block.SortOrder) Page(com.facebook.presto.common.Page) PageBuilder(com.facebook.presto.common.PageBuilder) Type(com.facebook.presto.common.type.Type) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ImmutableList(com.google.common.collect.ImmutableList) Test(org.testng.annotations.Test)

Example 3 with SortOrder

use of com.facebook.presto.common.block.SortOrder in project presto by prestodb.

the class OrderingCompiler method generatePageIndexCompareTo.

private static 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(AdaptiveLongBigArray.class, "valueAddresses");
    compareToMethod.getBody().comment("AdaptiveLongBigArray valueAddresses = pagesIndex.valueAddresses").append(valueAddresses.set(pagesIndex.invoke("getValueAddresses", AdaptiveLongBigArray.class)));
    Variable leftPageAddress = scope.declareVariable(long.class, "leftPageAddress");
    compareToMethod.getBody().comment("long leftPageAddress = valueAddresses.get(leftPosition)").append(leftPageAddress.set(valueAddresses.invoke("get", 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.get(rightPosition);").append(rightPageAddress.set(valueAddresses.invoke("get", 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);
        BytecodeBlock block = new BytecodeBlock().setDescription("compare channel " + sortChannel + " " + sortOrder);
        Type sortType = sortTypes.get(i);
        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(getStatic(SortOrder.class, sortOrder.name()).invoke("compareBlockValue", int.class, ImmutableList.of(Type.class, Block.class, int.class, Block.class, int.class), constantType(callSiteBinder, sortType), 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(com.facebook.presto.bytecode.instruction.LabelNode) Type(com.facebook.presto.common.type.Type) SqlTypeBytecodeExpression.constantType(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression.constantType) Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) SortOrder(com.facebook.presto.common.block.SortOrder) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 4 with SortOrder

use of com.facebook.presto.common.block.SortOrder in project presto by prestodb.

the class OrderingCompiler method generateMergeSortCompareTo.

private void generateMergeSortCompareTo(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> types, List<Integer> sortChannels, List<SortOrder> sortOrders) {
    Parameter leftPage = arg("leftPage", Page.class);
    Parameter leftPosition = arg("leftPosition", int.class);
    Parameter rightPage = arg("rightPage", Page.class);
    Parameter rightPosition = arg("rightPosition", int.class);
    MethodDefinition compareToMethod = classDefinition.declareMethod(a(PUBLIC), "compareTo", type(int.class), leftPage, leftPosition, rightPage, rightPosition);
    for (int i = 0; i < sortChannels.size(); i++) {
        int sortChannel = sortChannels.get(i);
        SortOrder sortOrder = sortOrders.get(i);
        BytecodeBlock block = new BytecodeBlock().setDescription("compare channel " + sortChannel + " " + sortOrder);
        Type sortType = types.get(sortChannel);
        BytecodeExpression leftBlock = leftPage.invoke("getBlock", Block.class, constantInt(sortChannel));
        BytecodeExpression rightBlock = rightPage.invoke("getBlock", Block.class, constantInt(sortChannel));
        block.append(getStatic(SortOrder.class, sortOrder.name()).invoke("compareBlockValue", int.class, ImmutableList.of(Type.class, Block.class, int.class, Block.class, int.class), constantType(callSiteBinder, sortType), leftBlock, leftPosition, rightBlock, rightPosition));
        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(com.facebook.presto.bytecode.instruction.LabelNode) Type(com.facebook.presto.common.type.Type) SqlTypeBytecodeExpression.constantType(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression.constantType) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) SortOrder(com.facebook.presto.common.block.SortOrder) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 5 with SortOrder

use of com.facebook.presto.common.block.SortOrder in project presto by prestodb.

the class TestVerifyNoOriginalExpression method testAggregation.

@Test
public void testAggregation() {
    ImmutableList<VariableReferenceExpression> groupingKeys = ImmutableList.of(VARIABLE_REFERENCE_EXPRESSION);
    int groupingSetCount = 1;
    ImmutableMap<VariableReferenceExpression, SortOrder> orderings = ImmutableMap.of(VARIABLE_REFERENCE_EXPRESSION, SortOrder.ASC_NULLS_FIRST);
    OrderingScheme orderingScheme = new OrderingScheme(groupingKeys.stream().map(variable -> new Ordering(variable, orderings.get(variable))).collect(toImmutableList()));
    ImmutableMap<VariableReferenceExpression, AggregationNode.Aggregation> aggregations = ImmutableMap.of(VARIABLE_REFERENCE_EXPRESSION, new AggregationNode.Aggregation(comparisonCallExpression, Optional.of(comparisonCallExpression), Optional.of(orderingScheme), false, Optional.of(new VariableReferenceExpression(Optional.empty(), "orderkey", BIGINT))));
    ImmutableSet<Integer> globalGroupingSets = ImmutableSet.of(1);
    AggregationNode.GroupingSetDescriptor groupingSets = new AggregationNode.GroupingSetDescriptor(groupingKeys, groupingSetCount, globalGroupingSets);
    ImmutableList<VariableReferenceExpression> preGroupedVariables = ImmutableList.of();
    Optional<VariableReferenceExpression> hashVariable = Optional.of(VARIABLE_REFERENCE_EXPRESSION);
    Optional<VariableReferenceExpression> groupIdVariable = Optional.of(VARIABLE_REFERENCE_EXPRESSION);
    AggregationNode aggregationNode = new AggregationNode(Optional.empty(), new PlanNodeId("1"), valuesNode, aggregations, groupingSets, preGroupedVariables, AggregationNode.Step.SINGLE, hashVariable, groupIdVariable);
    testValidation(aggregationNode);
}
Also used : OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) SortOrder(com.facebook.presto.common.block.SortOrder) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Ordering(com.facebook.presto.spi.plan.Ordering) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Aggregations

SortOrder (com.facebook.presto.common.block.SortOrder)24 Test (org.testng.annotations.Test)14 Page (com.facebook.presto.common.Page)12 WindowOperatorFactory (com.facebook.presto.operator.WindowOperator.WindowOperatorFactory)11 OperatorAssertion.toMaterializedResult (com.facebook.presto.operator.OperatorAssertion.toMaterializedResult)10 MaterializedResult (com.facebook.presto.testing.MaterializedResult)10 Type (com.facebook.presto.common.type.Type)8 OrderingScheme (com.facebook.presto.spi.plan.OrderingScheme)6 Ordering (com.facebook.presto.spi.plan.Ordering)5 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)5 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 NotSupportedException (com.facebook.presto.common.NotSupportedException)3 Block (com.facebook.presto.common.block.Block)3 PrestoException (com.facebook.presto.spi.PrestoException)3 HashMap (java.util.HashMap)3 List (java.util.List)3 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)2 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)2 Parameter (com.facebook.presto.bytecode.Parameter)2