Search in sources :

Example 26 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class MapParametricType method createType.

public Type createType(FunctionAndTypeManager functionAndTypeManager, List<TypeParameter> parameters) {
    checkArgument(parameters.size() == 2, "Expected two parameters, got %s", parameters);
    TypeParameter firstParameter = parameters.get(0);
    TypeParameter secondParameter = parameters.get(1);
    checkArgument(firstParameter.getKind() == ParameterKind.TYPE && secondParameter.getKind() == ParameterKind.TYPE, "Expected key and type to be types, got %s", parameters);
    Type keyType = firstParameter.getType();
    Type valueType = secondParameter.getType();
    MethodHandle keyNativeEquals = functionAndTypeManager.getJavaScalarFunctionImplementation(functionAndTypeManager.resolveOperator(OperatorType.EQUAL, fromTypes(keyType, keyType))).getMethodHandle();
    MethodHandle keyBlockEquals = compose(keyNativeEquals, nativeValueGetter(keyType), nativeValueGetter(keyType));
    MethodHandle keyNativeHashCode = functionAndTypeManager.getJavaScalarFunctionImplementation(functionAndTypeManager.resolveOperator(OperatorType.HASH_CODE, fromTypes(keyType))).getMethodHandle();
    MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(keyType));
    return new MapType(keyType, valueType, keyBlockEquals, keyBlockHashCode);
}
Also used : TypeParameter(com.facebook.presto.common.type.TypeParameter) MapType(com.facebook.presto.common.type.MapType) OperatorType(com.facebook.presto.common.function.OperatorType) ParametricType(com.facebook.presto.common.type.ParametricType) Type(com.facebook.presto.common.type.Type) MapType(com.facebook.presto.common.type.MapType) MethodHandle(java.lang.invoke.MethodHandle)

Example 27 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class BlockInputDecoders method createForMap.

private static BlockInputDecoder createForMap(MapObjectInspector inspector, MapType mapType) {
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    ObjectInspector keyInspector = inspector.getMapKeyObjectInspector();
    ObjectInspector valueInspector = inspector.getMapValueObjectInspector();
    BlockInputDecoder keyDecoder = createBlockInputDecoder(keyInspector, keyType);
    BlockInputDecoder valueDecoder = createBlockInputDecoder(valueInspector, valueType);
    return (b, i) -> {
        if (b.isNull(i)) {
            return null;
        }
        SingleMapBlock single = (SingleMapBlock) b.getBlock(i);
        int positions = single.getPositionCount();
        HashMap<Object, Object> result = new HashMap<>();
        for (int j = 0; j < positions; j += 2) {
            Object key = keyDecoder.decode(single, j);
            Object value = valueDecoder.decode(single, j + 1);
            if (key != null) {
                result.put(key, value);
            }
        }
        return result;
    };
}
Also used : VoidObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.VoidObjectInspector) JavaHiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveDecimalObjectInspector) Text(org.apache.hadoop.io.Text) DateWritable(org.apache.hadoop.hive.serde2.io.DateWritable) StandardStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) LongWritable(org.apache.hadoop.io.LongWritable) DateTimeUtils.createDate(com.facebook.presto.hive.functions.type.DateTimeUtils.createDate) BooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) HiveTypes.createHiveChar(com.facebook.presto.hive.functions.type.HiveTypes.createHiveChar) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) IntWritable(org.apache.hadoop.io.IntWritable) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) Timestamp(java.sql.Timestamp) DecimalUtils.readHiveDecimal(com.facebook.presto.hive.functions.type.DecimalUtils.readHiveDecimal) Streams(com.google.common.collect.Streams) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector) Collectors(java.util.stream.Collectors) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) JavaTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaTimestampObjectInspector) List(java.util.List) BooleanWritable(org.apache.hadoop.io.BooleanWritable) DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) StructField(org.apache.hadoop.hive.serde2.objectinspector.StructField) HiveTypes.createHiveVarChar(com.facebook.presto.hive.functions.type.HiveTypes.createHiveVarChar) JavaHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveCharObjectInspector) ByteWritable(org.apache.hadoop.hive.serde2.io.ByteWritable) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) JavaHiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveVarcharObjectInspector) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) HashMap(java.util.HashMap) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) HiveFunctionErrorCode.unsupportedType(com.facebook.presto.hive.functions.HiveFunctionErrorCode.unsupportedType) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) ArrayList(java.util.ArrayList) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ArrayType(com.facebook.presto.common.type.ArrayType) BytesWritable(org.apache.hadoop.io.BytesWritable) TimestampWritable(org.apache.hadoop.hive.serde2.io.TimestampWritable) LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) Type(com.facebook.presto.common.type.Type) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) JavaDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaDateObjectInspector) Date(java.sql.Date) SettableStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector) TimeUnit(java.util.concurrent.TimeUnit) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector) DoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) Block(com.facebook.presto.common.block.Block) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) FloatWritable(org.apache.hadoop.io.FloatWritable) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) HiveFunctionErrorCode.unsupportedType(com.facebook.presto.hive.functions.HiveFunctionErrorCode.unsupportedType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) VoidObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.VoidObjectInspector) JavaHiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveDecimalObjectInspector) StandardStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) BooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector) ShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) JavaTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaTimestampObjectInspector) DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) JavaHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveCharObjectInspector) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) JavaHiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveVarcharObjectInspector) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) JavaDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaDateObjectInspector) SettableStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector) DoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) HashMap(java.util.HashMap) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock)

Example 28 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class MapUnionSumAggregation 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(MAP, ImmutableList.of(TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
    return generateAggregation(keyType, valueType, outputType);
}
Also used : MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) MapType(com.facebook.presto.common.type.MapType)

Example 29 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

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"));
    assertFunction("SPLIT_TO_MAP('a:1;b:2;a:3', ';', ':', (k, v1, v2) -> v1)", expectedType, ImmutableMap.of("a", "1", "b", "2"));
    assertFunction("SPLIT_TO_MAP('a:1;b:2;a:3', ';', ':', (k, v1, v2) -> CONCAT(v1, v2))", expectedType, ImmutableMap.of("a", "13", "b", "2"));
    // 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. Specifying a lambda to resolve conflicts can avoid this error");
    assertInvalidFunction("SPLIT_TO_MAP('\u4EA0\u4EFF\u4EA1\u4E00\u4EA0\u4EFF\u4EB1', '\u4E00', '\u4EFF')", "Duplicate keys (\u4EA0) are not allowed. Specifying a lambda to resolve conflicts can avoid this error");
    // 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(com.facebook.presto.common.type.MapType) Test(org.testng.annotations.Test)

Example 30 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class TestArraySqlFunctions method testArrayFrequencyVarchar.

@Test
public void testArrayFrequencyVarchar() {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    MapType type = new MapType(VARCHAR, INTEGER, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"));
    TypeSignature typeSignature = TypeSignature.parseTypeSignature(type.getDisplayName());
    assertFunction("array_frequency(cast(null as array(varchar)))", functionAndTypeManager.getType(typeSignature), null);
    assertFunction("array_frequency(cast(array[] as array(varchar)))", functionAndTypeManager.getType(typeSignature), ImmutableMap.of());
    assertFunction("array_frequency(array[cast(null as varchar), cast(null as varchar), cast(null as varchar)])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of());
    assertFunction("array_frequency(array[varchar 'z', cast(null as varchar)])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("z", 1));
    assertFunction("array_frequency(array[varchar 'a', cast(null as varchar), varchar 'b', cast(null as varchar), cast(null as varchar) ])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("a", 1, "b", 1));
    assertFunction("array_frequency(array[varchar 'a', varchar 'b', varchar 'a', varchar 'a', varchar 'a'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("a", 4, "b", 1));
    assertFunction("array_frequency(array[varchar 'a', varchar 'b', varchar 'a', varchar 'b', varchar 'c'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("a", 2, "b", 2, "c", 1));
    assertFunction("array_frequency(array[varchar 'y', varchar 'p'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("p", 1, "y", 1));
    assertFunction("array_frequency(array[varchar 'a', varchar 'a', varchar 'p'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("p", 1, "a", 2));
    assertFunction("array_frequency(array[varchar 'z'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("z", 1));
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TestRowType(com.facebook.presto.common.type.TestRowType) MapType(com.facebook.presto.common.type.MapType) 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