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);
}
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()));
}
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();
}
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();
}
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);
}
Aggregations