use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class RelationPlanner method planCrossJoinUnnest.
private RelationPlan planCrossJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node) {
RelationType unnestOutputDescriptor = analysis.getOutputDescriptor(node);
// Create variables for the result of unnesting
ImmutableList.Builder<VariableReferenceExpression> unnestedVariablesBuilder = ImmutableList.builder();
for (Field field : unnestOutputDescriptor.getVisibleFields()) {
VariableReferenceExpression variable = variableAllocator.newVariable(field);
unnestedVariablesBuilder.add(variable);
}
ImmutableList<VariableReferenceExpression> unnestedVariables = unnestedVariablesBuilder.build();
// Add a projection for all the unnest arguments
PlanBuilder planBuilder = initializePlanBuilder(leftPlan);
planBuilder = planBuilder.appendProjections(node.getExpressions(), variableAllocator, idAllocator);
TranslationMap translations = planBuilder.getTranslations();
ProjectNode projectNode = (ProjectNode) planBuilder.getRoot();
ImmutableMap.Builder<VariableReferenceExpression, List<VariableReferenceExpression>> unnestVariables = ImmutableMap.builder();
UnmodifiableIterator<VariableReferenceExpression> unnestedVariablesIterator = unnestedVariables.iterator();
for (Expression expression : node.getExpressions()) {
Type type = analysis.getType(expression);
VariableReferenceExpression inputVariable = new VariableReferenceExpression(getSourceLocation(expression), translations.get(expression).getName(), type);
if (type instanceof ArrayType) {
Type elementType = ((ArrayType) type).getElementType();
if (!SystemSessionProperties.isLegacyUnnest(session) && elementType instanceof RowType) {
ImmutableList.Builder<VariableReferenceExpression> unnestVariableBuilder = ImmutableList.builder();
for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
unnestVariableBuilder.add(unnestedVariablesIterator.next());
}
unnestVariables.put(inputVariable, unnestVariableBuilder.build());
} else {
unnestVariables.put(inputVariable, ImmutableList.of(unnestedVariablesIterator.next()));
}
} else if (type instanceof MapType) {
unnestVariables.put(inputVariable, ImmutableList.of(unnestedVariablesIterator.next(), unnestedVariablesIterator.next()));
} else {
throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
}
}
Optional<VariableReferenceExpression> ordinalityVariable = node.isWithOrdinality() ? Optional.of(unnestedVariablesIterator.next()) : Optional.empty();
checkState(!unnestedVariablesIterator.hasNext(), "Not all output variables were matched with input variables");
UnnestNode unnestNode = new UnnestNode(getSourceLocation(node), idAllocator.getNextId(), projectNode, leftPlan.getFieldMappings(), unnestVariables.build(), ordinalityVariable);
return new RelationPlan(unnestNode, analysis.getScope(joinNode), unnestNode.getOutputVariables());
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class TestMapOperators method assertMapHashOperator.
private void assertMapHashOperator(String inputString, Type keyType, Type valueType, List<Object> elements) {
checkArgument(elements.size() % 2 == 0, "the size of elements should be even number");
MapType mapType = mapType(keyType, valueType);
BlockBuilder mapArrayBuilder = mapType.createBlockBuilder(null, 1);
BlockBuilder singleMapWriter = mapArrayBuilder.beginBlockEntry();
for (int i = 0; i < elements.size(); i += 2) {
appendToBlockBuilder(keyType, elements.get(i), singleMapWriter);
appendToBlockBuilder(valueType, elements.get(i + 1), singleMapWriter);
}
mapArrayBuilder.closeEntry();
long hashResult = mapType.hash(mapArrayBuilder.build(), 0);
assertOperator(HASH_CODE, inputString, BIGINT, hashResult);
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class TestMapAggAggregation method testDoubleMapMap.
@Test
public void testDoubleMapMap() {
MapType innerMapType = mapType(VARCHAR, VARCHAR);
InternalAggregationFunction aggFunc = getAggregation(DOUBLE, innerMapType);
BlockBuilder builder = innerMapType.createBlockBuilder(null, 3);
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("a", "b")));
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("c", "d")));
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("e", "f")));
assertAggregation(aggFunc, ImmutableMap.of(1.0, ImmutableMap.of("a", "b"), 2.0, ImmutableMap.of("c", "d"), 3.0, ImmutableMap.of("e", "f")), createDoublesBlock(1.0, 2.0, 3.0), builder.build());
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class TestMapUnionSumResult method testAddToKeySet.
@Test
public void testAddToKeySet() {
MapType mapType = createMapType(BIGINT, BIGINT);
Map<Long, Long> map = new HashMap<>();
map.put(1L, 1L);
MapBlock mapBlock = (MapBlock) createMapBlock(mapType, map);
Block singleMapBlock = mapBlock.getBlock(0);
MapUnionSumState mapUnionSumState = new MapUnionSumStateFactory(BIGINT, BIGINT).createSingleState();
MapUnionSumResult mapUnionSumResult = MapUnionSumResult.create(BIGINT, BIGINT, mapUnionSumState.getAdder(), singleMapBlock);
TypedSet typedSet = new TypedSet(BIGINT, 1, "TEST");
Block block = createLongsBlock(-1);
typedSet.add(block, 0);
assertEquals(typedSet.size(), 1);
assertEquals(mapUnionSumResult.size(), 1);
mapUnionSumResult.unionSum(mapUnionSumResult).addKeyToSet(typedSet, 0);
assertEquals(typedSet.size(), 2);
assertEquals(mapUnionSumResult.size(), 1);
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class TestTypedHistogram method testMassive.
@Test
public void testMassive() {
BlockBuilder inputBlockBuilder = BIGINT.createBlockBuilder(null, 5000);
TypedHistogram typedHistogram = new SingleTypedHistogram(BIGINT, 1000);
IntStream.range(1, 2000).flatMap(i -> IntStream.iterate(i, IntUnaryOperator.identity()).limit(i)).forEach(j -> BIGINT.writeLong(inputBlockBuilder, j));
Block inputBlock = inputBlockBuilder.build();
for (int i = 0; i < inputBlock.getPositionCount(); i++) {
typedHistogram.add(i, inputBlock, 1);
}
MapType mapType = mapType(BIGINT, BIGINT);
BlockBuilder out = mapType.createBlockBuilder(null, 1);
typedHistogram.serialize(out);
Block outputBlock = mapType.getObject(out, 0);
for (int i = 0; i < outputBlock.getPositionCount(); i += 2) {
assertEquals(BIGINT.getLong(outputBlock, i + 1), BIGINT.getLong(outputBlock, i));
}
}
Aggregations