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', ',', '=')");
}
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)));
}
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)));
}
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);
}
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);
}
Aggregations