Search in sources :

Example 11 with MapType

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());
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowType(com.facebook.presto.common.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Field(com.facebook.presto.sql.analyzer.Field) TypeUtils.isEnumType(com.facebook.presto.common.type.TypeUtils.isEnumType) ArrayType(com.facebook.presto.common.type.ArrayType) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) ExpressionTreeUtils.isEqualComparisonExpression(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.isEqualComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) UnnestNode(com.facebook.presto.sql.planner.plan.UnnestNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RelationType(com.facebook.presto.sql.analyzer.RelationType) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 12 with MapType

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);
}
Also used : MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)

Example 13 with MapType

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());
}
Also used : MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 14 with MapType

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);
}
Also used : MapUnionSumStateFactory(com.facebook.presto.operator.aggregation.state.MapUnionSumStateFactory) HashMap(java.util.HashMap) MapUnionSumState(com.facebook.presto.operator.aggregation.state.MapUnionSumState) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) MapBlock(com.facebook.presto.common.block.MapBlock) BlockAssertions.createMapBlock(com.facebook.presto.block.BlockAssertions.createMapBlock) Block(com.facebook.presto.common.block.Block) MapBlock(com.facebook.presto.common.block.MapBlock) BlockAssertions.createMapBlock(com.facebook.presto.block.BlockAssertions.createMapBlock) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType) MapType(com.facebook.presto.common.type.MapType) Test(org.testng.annotations.Test)

Example 15 with MapType

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));
    }
}
Also used : IntStream(java.util.stream.IntStream) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) MapType(com.facebook.presto.common.type.MapType) IntUnaryOperator(java.util.function.IntUnaryOperator) SingleTypedHistogram(com.facebook.presto.operator.aggregation.histogram.SingleTypedHistogram) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) StructuralTestUtil.mapType(com.facebook.presto.util.StructuralTestUtil.mapType) Block(com.facebook.presto.common.block.Block) TypedHistogram(com.facebook.presto.operator.aggregation.histogram.TypedHistogram) SingleTypedHistogram(com.facebook.presto.operator.aggregation.histogram.SingleTypedHistogram) TypedHistogram(com.facebook.presto.operator.aggregation.histogram.TypedHistogram) SingleTypedHistogram(com.facebook.presto.operator.aggregation.histogram.SingleTypedHistogram) Block(com.facebook.presto.common.block.Block) MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Test(org.testng.annotations.Test)

Aggregations

MapType (com.facebook.presto.common.type.MapType)92 Type (com.facebook.presto.common.type.Type)49 ArrayType (com.facebook.presto.common.type.ArrayType)40 Test (org.testng.annotations.Test)32 RowType (com.facebook.presto.common.type.RowType)30 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)24 Block (com.facebook.presto.common.block.Block)21 HashMap (java.util.HashMap)12 DecimalType (com.facebook.presto.common.type.DecimalType)11 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 Map (java.util.Map)11 VarcharType (com.facebook.presto.common.type.VarcharType)9 MethodHandle (java.lang.invoke.MethodHandle)9 ArrayList (java.util.ArrayList)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)7 PrestoException (com.facebook.presto.spi.PrestoException)7 OperatorType (com.facebook.presto.common.function.OperatorType)6 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)5