Search in sources :

Example 11 with MapType

use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.

the class MapAggregationFunction method specialize.

@Override
public InternalAggregationFunction specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    Type keyType = boundVariables.getTypeVariable("K");
    Type valueType = boundVariables.getTypeVariable("V");
    MapType outputType = (MapType) functionAndTypeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
    return generateAggregation(keyType, valueType, outputType);
}
Also used : MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) MapType(io.prestosql.spi.type.MapType)

Example 12 with MapType

use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.

the class MapAggregationFunction method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type keyType, Type valueType, MapType outputType) {
    DynamicClassLoader classLoader = new DynamicClassLoader(MapAggregationFunction.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(keyType, valueType);
    KeyValuePairStateSerializer stateSerializer = new KeyValuePairStateSerializer(outputType);
    Type intermediateType = stateSerializer.getSerializedType();
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(keyType, valueType), INPUT_FUNCTION.bindTo(keyType).bindTo(valueType), COMBINE_FUNCTION, OUTPUT_FUNCTION, ImmutableList.of(new AccumulatorStateDescriptor(KeyValuePairsState.class, stateSerializer, new KeyValuePairsStateFactory(keyType, valueType))), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, true, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) KeyValuePairStateSerializer(io.prestosql.operator.aggregation.state.KeyValuePairStateSerializer) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) KeyValuePairsStateFactory(io.prestosql.operator.aggregation.state.KeyValuePairsStateFactory)

Example 13 with MapType

use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.

the class RelationPlanner method planCrossJoinUnnest.

private RelationPlan planCrossJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node) {
    RelationType unnestOutputDescriptor = analysis.getOutputDescriptor(node);
    // Create symbols for the result of unnesting
    ImmutableList.Builder<Symbol> unnestedSymbolsBuilder = ImmutableList.builder();
    for (Field field : unnestOutputDescriptor.getVisibleFields()) {
        Symbol symbol = planSymbolAllocator.newSymbol(field);
        unnestedSymbolsBuilder.add(symbol);
    }
    ImmutableList<Symbol> unnestedSymbols = unnestedSymbolsBuilder.build();
    // Add a projection for all the unnest arguments
    PlanBuilder planBuilder = initializePlanBuilder(leftPlan);
    planBuilder = planBuilder.appendProjections(node.getExpressions(), planSymbolAllocator, idAllocator);
    TranslationMap translations = planBuilder.getTranslations();
    ProjectNode projectNode = (ProjectNode) planBuilder.getRoot();
    ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
    UnmodifiableIterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
    for (Expression expression : node.getExpressions()) {
        Type type = analysis.getType(expression);
        Symbol inputSymbol = translations.get(expression);
        if (type instanceof ArrayType) {
            Type elementType = ((ArrayType) type).getElementType();
            if (elementType instanceof RowType) {
                ImmutableList.Builder<Symbol> unnestSymbolBuilder = ImmutableList.builder();
                for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
                    unnestSymbolBuilder.add(unnestedSymbolsIterator.next());
                }
                unnestSymbols.put(inputSymbol, unnestSymbolBuilder.build());
            } else {
                unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
            }
        } else if (type instanceof MapType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
        } else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<Symbol> ordinalitySymbol = node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
    checkState(!unnestedSymbolsIterator.hasNext(), "Not all output symbols were matched with input symbols");
    UnnestNode unnestNode = new UnnestNode(idAllocator.getNextId(), projectNode, leftPlan.getFieldMappings(), unnestSymbols.build(), ordinalitySymbol);
    return new RelationPlan(unnestNode, analysis.getScope(joinNode), unnestNode.getOutputSymbols());
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Symbol(io.prestosql.spi.plan.Symbol) RowType(io.prestosql.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(io.prestosql.spi.type.MapType) ArrayType(io.prestosql.spi.type.ArrayType) Field(io.prestosql.sql.analyzer.Field) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) RelationType(io.prestosql.sql.analyzer.RelationType) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) UnnestNode(io.prestosql.sql.planner.plan.UnnestNode) RelationType(io.prestosql.sql.analyzer.RelationType) ProjectNode(io.prestosql.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 14 with MapType

use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.

the class MapParametricType method createType.

@Override
public Type createType(List<String> params) {
    checkArgument(params.size() == 2, "Expected two parameters, got %s", params);
    this.keyType = getType(params.get(0));
    this.valueType = getType(params.get(1));
    try {
        MethodHandle keyNativeEquals = MethodHandles.lookup().findVirtual(MapParametricType.class, "equal", MethodType.methodType(void.class));
        MethodHandle keyNativeHashCode = Reflection.methodHandle(MapParametricType.class, "hash", Block.class, int.class).bindTo(this);
        return new MapType(keyType, valueType, keyNativeEquals, keyNativeEquals, keyNativeHashCode, keyNativeHashCode);
    } catch (NoSuchMethodException | IllegalAccessException e) {
        throw new PrestoException(NOT_SUPPORTED, format("Map type cannot be supported with exception %s", e));
    }
}
Also used : Block(io.prestosql.spi.block.Block) PrestoException(io.prestosql.spi.PrestoException) MapType(io.prestosql.spi.type.MapType) MethodHandle(java.lang.invoke.MethodHandle)

Example 15 with MapType

use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.

the class TestStringFunctions method testSplitToMap.

@Test
public void testSplitToMap() {
    MapType expectedType = mapType(VARCHAR, VARCHAR);
    assertFunction("SPLIT_TO_MAP('', ',', '=')", expectedType, ImmutableMap.of());
    assertFunction("SPLIT_TO_MAP('a=123,b=.4,c=,=d', ',', '=')", expectedType, ImmutableMap.of("a", "123", "b", ".4", "c", "", "", "d"));
    assertFunction("SPLIT_TO_MAP('=', ',', '=')", expectedType, ImmutableMap.of("", ""));
    assertFunction("SPLIT_TO_MAP('key=>value', ',', '=>')", expectedType, ImmutableMap.of("key", "value"));
    assertFunction("SPLIT_TO_MAP('key => value', ',', '=>')", expectedType, ImmutableMap.of("key ", " value"));
    // Test SPLIT_TO_MAP for non-ASCII
    assertFunction("SPLIT_TO_MAP('\u4EA0\u4EFF\u4EA1', '\u4E00', '\u4EFF')", expectedType, ImmutableMap.of("\u4EA0", "\u4EA1"));
    // If corresponding value is not found, then ""(empty string) is its value
    assertFunction("SPLIT_TO_MAP('\u4EC0\u4EFF', '\u4E00', '\u4EFF')", expectedType, ImmutableMap.of("\u4EC0", ""));
    // If corresponding key is not found, then ""(empty string) is its key
    assertFunction("SPLIT_TO_MAP('\u4EFF\u4EC1', '\u4E00', '\u4EFF')", expectedType, ImmutableMap.of("", "\u4EC1"));
    // Entry delimiter and key-value delimiter must not be the same.
    assertInvalidFunction("SPLIT_TO_MAP('', '\u4EFF', '\u4EFF')", "entryDelimiter and keyValueDelimiter must not be the same");
    assertInvalidFunction("SPLIT_TO_MAP('a=123,b=.4,c=', '=', '=')", "entryDelimiter and keyValueDelimiter must not be the same");
    // Duplicate keys are not allowed to exist.
    assertInvalidFunction("SPLIT_TO_MAP('a=123,a=.4', ',', '=')", "Duplicate keys (a) are not allowed");
    assertInvalidFunction("SPLIT_TO_MAP('\u4EA0\u4EFF\u4EA1\u4E00\u4EA0\u4EFF\u4EB1', '\u4E00', '\u4EFF')", "Duplicate keys (\u4EA0) are not allowed");
    // Key-value delimiter must appear exactly once in each entry.
    assertInvalidFunction("SPLIT_TO_MAP('key', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: 'key'");
    assertInvalidFunction("SPLIT_TO_MAP('key==value', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: 'key==value'");
    assertInvalidFunction("SPLIT_TO_MAP('key=va=lue', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: 'key=va=lue'");
    assertCachedInstanceHasBoundedRetainedSize("SPLIT_TO_MAP('a=123,b=.4,c=,=d', ',', '=')");
}
Also used : MapType(io.prestosql.spi.type.MapType) Test(org.testng.annotations.Test)

Aggregations

MapType (io.prestosql.spi.type.MapType)80 Type (io.prestosql.spi.type.Type)39 ArrayType (io.prestosql.spi.type.ArrayType)31 RowType (io.prestosql.spi.type.RowType)27 Test (org.testng.annotations.Test)27 BlockBuilder (io.prestosql.spi.block.BlockBuilder)17 Block (io.prestosql.spi.block.Block)15 Signature (io.prestosql.spi.function.Signature)14 VarcharType (io.prestosql.spi.type.VarcharType)11 List (java.util.List)11 BigintType (io.prestosql.spi.type.BigintType)10 BooleanType (io.prestosql.spi.type.BooleanType)10 DoubleType (io.prestosql.spi.type.DoubleType)10 IntegerType (io.prestosql.spi.type.IntegerType)10 TypeSignature.parseTypeSignature (io.prestosql.spi.type.TypeSignature.parseTypeSignature)10 RealType (io.prestosql.spi.type.RealType)9 SmallintType (io.prestosql.spi.type.SmallintType)9 TimestampType (io.prestosql.spi.type.TimestampType)9 TinyintType (io.prestosql.spi.type.TinyintType)9 VarbinaryType (io.prestosql.spi.type.VarbinaryType)9