Search in sources :

Example 51 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class TestStringFunctions method testSplitToMultimap.

@Test
public void testSplitToMultimap() {
    MapType expectedType = mapType(VARCHAR, new ArrayType(VARCHAR));
    assertFunction("SPLIT_TO_MULTIMAP('', ',', '=')", expectedType, ImmutableMap.of());
    assertFunction("SPLIT_TO_MULTIMAP('a=123,b=.4,c=,=d', ',', '=')", expectedType, ImmutableMap.of("a", ImmutableList.of("123"), "b", ImmutableList.of(".4"), "c", ImmutableList.of(""), "", ImmutableList.of("d")));
    assertFunction("SPLIT_TO_MULTIMAP('=', ',', '=')", expectedType, ImmutableMap.of("", ImmutableList.of("")));
    // Test multiple values of the same key preserve the order as they appear in input
    assertFunction("SPLIT_TO_MULTIMAP('a=123,a=.4,a=5.67', ',', '=')", expectedType, ImmutableMap.of("a", ImmutableList.of("123", ".4", "5.67")));
    // Test multi-character delimiters
    assertFunction("SPLIT_TO_MULTIMAP('key=>value,key=>value', ',', '=>')", expectedType, ImmutableMap.of("key", ImmutableList.of("value", "value")));
    assertFunction("SPLIT_TO_MULTIMAP('key => value, key => value', ',', '=>')", expectedType, ImmutableMap.of("key ", ImmutableList.of(" value"), " key ", ImmutableList.of(" value")));
    assertFunction("SPLIT_TO_MULTIMAP('key => value, key => value', ', ', '=>')", expectedType, ImmutableMap.of("key ", ImmutableList.of(" value", " value")));
    // Test non-ASCII
    assertFunction("SPLIT_TO_MULTIMAP('\u4EA0\u4EFF\u4EA1', '\u4E00', '\u4EFF')", expectedType, ImmutableMap.of("\u4EA0", ImmutableList.of("\u4EA1")));
    assertFunction("SPLIT_TO_MULTIMAP('\u4EA0\u4EFF\u4EA1\u4E00\u4EA0\u4EFF\u4EB1', '\u4E00', '\u4EFF')", expectedType, ImmutableMap.of("\u4EA0", ImmutableList.of("\u4EA1", "\u4EB1")));
    // Entry delimiter and key-value delimiter must not be the same.
    assertInvalidFunction("SPLIT_TO_MULTIMAP('', '\u4EFF', '\u4EFF')", "entryDelimiter and keyValueDelimiter must not be the same");
    assertInvalidFunction("SPLIT_TO_MULTIMAP('a=123,b=.4,c=', '=', '=')", "entryDelimiter and keyValueDelimiter must not be the same");
    // Key-value delimiter must appear exactly once in each entry.
    assertInvalidFunction("SPLIT_TO_MULTIMAP('key', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: key");
    assertInvalidFunction("SPLIT_TO_MULTIMAP('key==value', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: key==value");
    assertInvalidFunction("SPLIT_TO_MULTIMAP('key=va=lue', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: key=va=lue");
    assertCachedInstanceHasBoundedRetainedSize("SPLIT_TO_MULTIMAP('a=123,b=.4,c=,=d', ',', '=')");
}
Also used : ArrayType(io.trino.spi.type.ArrayType) MapType(io.trino.spi.type.MapType) Test(org.testng.annotations.Test)

Example 52 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class MapTransformValuesFunction method specialize.

@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
    MapType inputMapType = (MapType) boundSignature.getArgumentType(0);
    Type inputValueType = inputMapType.getValueType();
    MapType outputMapType = (MapType) boundSignature.getReturnType();
    Type keyType = outputMapType.getKeyType();
    Type outputValueType = outputMapType.getValueType();
    return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, ImmutableList.of(NEVER_NULL, FUNCTION), ImmutableList.of(BinaryFunctionInterface.class), generateTransform(keyType, inputValueType, outputValueType, outputMapType), Optional.of(STATE_FACTORY.bindTo(outputMapType)));
}
Also used : SqlTypeBytecodeExpression.constantType(io.trino.sql.gen.SqlTypeBytecodeExpression.constantType) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) Type(io.trino.spi.type.Type) TypeSignature.functionType(io.trino.spi.type.TypeSignature.functionType) MapType(io.trino.spi.type.MapType) BinaryFunctionInterface(io.trino.sql.gen.lambda.BinaryFunctionInterface) MapType(io.trino.spi.type.MapType)

Example 53 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class MapZipWithFunction method specialize.

@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
    MapType outputMapType = (MapType) boundSignature.getReturnType();
    Type keyType = outputMapType.getKeyType();
    Type inputValueType1 = ((MapType) boundSignature.getArgumentType(0)).getValueType();
    Type inputValueType2 = ((MapType) boundSignature.getArgumentType(1)).getValueType();
    return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, ImmutableList.of(NEVER_NULL, NEVER_NULL, FUNCTION), ImmutableList.of(MapZipWithLambda.class), METHOD_HANDLE.bindTo(keyType).bindTo(inputValueType1).bindTo(inputValueType2).bindTo(outputMapType), Optional.of(STATE_FACTORY.bindTo(outputMapType)));
}
Also used : Type(io.trino.spi.type.Type) TypeSignature.functionType(io.trino.spi.type.TypeSignature.functionType) MapType(io.trino.spi.type.MapType) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) MapType(io.trino.spi.type.MapType)

Example 54 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class MapSubscriptOperator method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies) {
    MapType mapType = (MapType) boundSignature.getArgumentType(0);
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    MethodHandle methodHandle;
    if (keyType.getJavaType() == boolean.class) {
        methodHandle = METHOD_HANDLE_BOOLEAN;
    } else if (keyType.getJavaType() == long.class) {
        methodHandle = METHOD_HANDLE_LONG;
    } else if (keyType.getJavaType() == double.class) {
        methodHandle = METHOD_HANDLE_DOUBLE;
    } else {
        methodHandle = METHOD_HANDLE_OBJECT;
    }
    MissingKeyExceptionFactory missingKeyExceptionFactory = new MissingKeyExceptionFactory(functionDependencies, keyType);
    methodHandle = methodHandle.bindTo(missingKeyExceptionFactory).bindTo(keyType).bindTo(valueType);
    methodHandle = methodHandle.asType(methodHandle.type().changeReturnType(Primitives.wrap(valueType.getJavaType())));
    return new ChoicesScalarFunctionImplementation(boundSignature, NULLABLE_RETURN, ImmutableList.of(NEVER_NULL, NEVER_NULL), methodHandle);
}
Also used : Type(io.trino.spi.type.Type) MapType(io.trino.spi.type.MapType) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) MapType(io.trino.spi.type.MapType) MethodHandle(java.lang.invoke.MethodHandle)

Example 55 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class MapBlockEncoding method readBlock.

@Override
public Block readBlock(BlockEncodingSerde blockEncodingSerde, SliceInput sliceInput) {
    MapType mapType = (MapType) blockEncodingSerde.readType(sliceInput);
    Block keyBlock = blockEncodingSerde.readBlock(sliceInput);
    Block valueBlock = blockEncodingSerde.readBlock(sliceInput);
    int hashTableLength = sliceInput.readInt();
    int[] hashTable = null;
    if (hashTableLength >= 0) {
        hashTable = new int[hashTableLength];
        sliceInput.readBytes(wrappedIntArray(hashTable));
    }
    if (keyBlock.getPositionCount() != valueBlock.getPositionCount()) {
        throw new IllegalArgumentException(format("Deserialized MapBlock violates invariants: key %s, value %s", keyBlock.getPositionCount(), valueBlock.getPositionCount()));
    }
    if (hashTable != null && keyBlock.getPositionCount() * HASH_MULTIPLIER != hashTable.length) {
        throw new IllegalArgumentException(format("Deserialized MapBlock violates invariants: expected hashtable size %s, actual hashtable size %s", keyBlock.getPositionCount() * HASH_MULTIPLIER, hashTable.length));
    }
    int positionCount = sliceInput.readInt();
    int[] offsets = new int[positionCount + 1];
    sliceInput.readBytes(wrappedIntArray(offsets));
    Optional<boolean[]> mapIsNull = EncoderUtil.decodeNullBits(sliceInput, positionCount);
    MapHashTables hashTables = new MapHashTables(mapType, Optional.ofNullable(hashTable));
    return createMapBlockInternal(mapType, 0, positionCount, mapIsNull, offsets, keyBlock, valueBlock, hashTables);
}
Also used : MapType(io.trino.spi.type.MapType)

Aggregations

MapType (io.trino.spi.type.MapType)85 Type (io.trino.spi.type.Type)45 ArrayType (io.trino.spi.type.ArrayType)42 RowType (io.trino.spi.type.RowType)38 BlockBuilder (io.trino.spi.block.BlockBuilder)28 VarcharType (io.trino.spi.type.VarcharType)26 Block (io.trino.spi.block.Block)17 DecimalType (io.trino.spi.type.DecimalType)17 Map (java.util.Map)17 Test (org.testng.annotations.Test)17 ImmutableList (com.google.common.collect.ImmutableList)16 List (java.util.List)14 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)13 CharType (io.trino.spi.type.CharType)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 TypeOperators (io.trino.spi.type.TypeOperators)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 VarbinaryType (io.trino.spi.type.VarbinaryType)8 Collectors.toList (java.util.stream.Collectors.toList)8