Search in sources :

Example 51 with RowType

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

the class TestParquetPredicateUtils method testParquetTupleDomainStruct.

@Test
public void testParquetTupleDomainStruct() {
    HiveColumnHandle columnHandle = new HiveColumnHandle("my_struct", HiveType.valueOf("struct<a:int,b:int>"), parseTypeSignature(StandardTypes.ROW), 0, REGULAR, Optional.empty(), Optional.empty());
    RowType.Field rowField = new RowType.Field(Optional.of("my_struct"), INTEGER);
    RowType rowType = RowType.from(ImmutableList.of(rowField));
    TupleDomain<HiveColumnHandle> domain = withColumnDomains(ImmutableMap.of(columnHandle, Domain.notNull(rowType)));
    MessageType fileSchema = new MessageType("hive_schema", new GroupType(OPTIONAL, "my_struct", new PrimitiveType(OPTIONAL, INT32, "a"), new PrimitiveType(OPTIONAL, INT32, "b")));
    Map<List<String>, RichColumnDescriptor> descriptorsByPath = getDescriptors(fileSchema, fileSchema);
    TupleDomain<ColumnDescriptor> tupleDomain = getParquetTupleDomain(descriptorsByPath, domain);
    assertTrue(tupleDomain.getDomains().get().isEmpty());
}
Also used : RichColumnDescriptor(com.facebook.presto.parquet.RichColumnDescriptor) RichColumnDescriptor(com.facebook.presto.parquet.RichColumnDescriptor) ColumnDescriptor(org.apache.parquet.column.ColumnDescriptor) RowType(com.facebook.presto.common.type.RowType) GroupType(org.apache.parquet.schema.GroupType) PrimitiveType(org.apache.parquet.schema.PrimitiveType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) HiveColumnHandle(com.facebook.presto.hive.HiveColumnHandle) MessageType(org.apache.parquet.schema.MessageType) Test(org.testng.annotations.Test)

Example 52 with RowType

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

the class TypeCoercer method compatibility.

private TypeCompatibility compatibility(Type fromType, Type toType) {
    Type standardFromType = toStandardType(fromType);
    Type standardToType = toStandardType(toType);
    if (standardFromType.equals(standardToType)) {
        return TypeCompatibility.compatible(toSemanticType(toType, standardToType), true);
    }
    if (standardFromType.equals(UnknownType.UNKNOWN)) {
        return TypeCompatibility.compatible(toSemanticType(toType, standardToType), true);
    }
    if (standardToType.equals(UnknownType.UNKNOWN)) {
        return TypeCompatibility.compatible(toSemanticType(fromType, standardFromType), false);
    }
    String fromTypeBaseName = standardFromType.getTypeSignature().getBase();
    String toTypeBaseName = standardToType.getTypeSignature().getBase();
    if (featuresConfig.isLegacyDateTimestampToVarcharCoercion()) {
        if ((fromTypeBaseName.equals(StandardTypes.DATE) || fromTypeBaseName.equals(StandardTypes.TIMESTAMP)) && toTypeBaseName.equals(StandardTypes.VARCHAR)) {
            return TypeCompatibility.compatible(toSemanticType(toType, standardToType), true);
        }
        if (fromTypeBaseName.equals(StandardTypes.VARCHAR) && (toTypeBaseName.equals(StandardTypes.DATE) || toTypeBaseName.equals(StandardTypes.TIMESTAMP))) {
            return TypeCompatibility.compatible(toSemanticType(fromType, standardFromType), true);
        }
    }
    if (fromTypeBaseName.equals(toTypeBaseName)) {
        if (fromTypeBaseName.equals(StandardTypes.DECIMAL)) {
            Type commonSuperType = getCommonSuperTypeForDecimal((DecimalType) standardFromType, (DecimalType) standardToType);
            return TypeCompatibility.compatible(toSemanticType(toType, commonSuperType), commonSuperType.equals(standardToType));
        }
        if (fromTypeBaseName.equals(StandardTypes.VARCHAR)) {
            Type commonSuperType = getCommonSuperTypeForVarchar((VarcharType) standardFromType, (VarcharType) standardToType);
            return TypeCompatibility.compatible(toSemanticType(toType, commonSuperType), commonSuperType.equals(standardToType));
        }
        if (fromTypeBaseName.equals(StandardTypes.CHAR) && !featuresConfig.isLegacyCharToVarcharCoercion()) {
            Type commonSuperType = getCommonSuperTypeForChar((CharType) standardFromType, (CharType) standardToType);
            return TypeCompatibility.compatible(toSemanticType(toType, commonSuperType), commonSuperType.equals(standardToType));
        }
        if (fromTypeBaseName.equals(StandardTypes.ROW)) {
            return typeCompatibilityForRow((RowType) standardFromType, (RowType) standardToType).toSemanticTypeCompatibility(toType);
        }
        if (isCovariantParametrizedType(standardFromType)) {
            return typeCompatibilityForCovariantParametrizedType(standardFromType, standardToType).toSemanticTypeCompatibility(toType);
        }
        return TypeCompatibility.incompatible();
    }
    Optional<Type> coercedType = coerceTypeBase(standardFromType, standardToType.getTypeSignature().getBase());
    if (coercedType.isPresent()) {
        return compatibility(toSemanticType(toType, coercedType.get()), standardToType);
    }
    coercedType = coerceTypeBase(standardToType, standardFromType.getTypeSignature().getBase());
    if (coercedType.isPresent()) {
        TypeCompatibility typeCompatibility = compatibility(standardFromType, coercedType.get());
        if (!typeCompatibility.isCompatible()) {
            return TypeCompatibility.incompatible();
        }
        return TypeCompatibility.compatible(toSemanticType(toType, typeCompatibility.getCommonSuperType()), false);
    }
    return TypeCompatibility.incompatible();
}
Also used : CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) KHyperLogLogType(com.facebook.presto.type.khyperloglog.KHyperLogLogType) SetDigestType(com.facebook.presto.type.setdigest.SetDigestType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) UnknownType(com.facebook.presto.common.type.UnknownType) Type(com.facebook.presto.common.type.Type) VarcharType(com.facebook.presto.common.type.VarcharType) RowType(com.facebook.presto.common.type.RowType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) RowType(com.facebook.presto.common.type.RowType)

Example 53 with RowType

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

the class JsonUtil method canCastFromJson.

public static boolean canCastFromJson(Type type) {
    TypeSignature signature = type.getTypeSignature();
    String baseType = signature.getBase();
    if (signature.isEnum()) {
        return true;
    }
    if (baseType.equals(StandardTypes.BOOLEAN) || baseType.equals(StandardTypes.TINYINT) || baseType.equals(StandardTypes.SMALLINT) || baseType.equals(StandardTypes.INTEGER) || baseType.equals(StandardTypes.BIGINT) || baseType.equals(StandardTypes.REAL) || baseType.equals(StandardTypes.DOUBLE) || baseType.equals(StandardTypes.VARCHAR) || baseType.equals(StandardTypes.DECIMAL) || baseType.equals(StandardTypes.JSON)) {
        return true;
    }
    if (type instanceof ArrayType) {
        return canCastFromJson(((ArrayType) type).getElementType());
    }
    if (type instanceof MapType) {
        return isValidJsonObjectKeyType(((MapType) type).getKeyType()) && canCastFromJson(((MapType) type).getValueType());
    }
    if (type instanceof RowType) {
        return type.getTypeParameters().stream().allMatch(JsonUtil::canCastFromJson);
    }
    return false;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) TypeSignature(com.facebook.presto.common.type.TypeSignature) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType)

Example 54 with RowType

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

the class TestSingleRowBlockWriter method testSizeInBytes.

@Test
public void testSizeInBytes() {
    MapType mapType = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestSingleRowBlockWriter.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestSingleRowBlockWriter.class, "throwUnsupportedOperation"));
    ArrayType arrayType = new ArrayType(DOUBLE);
    RowType.Field rowField = new RowType.Field(Optional.of("my_struct"), INTEGER);
    RowType rowType = RowType.from(ImmutableList.of(rowField));
    List<Type> fieldTypes = ImmutableList.of(REAL, mapType, arrayType, rowType);
    RowBlockBuilder rowBlockBuilder = new RowBlockBuilder(fieldTypes, null, 1);
    for (int i = 0; i < 100; i++) {
        SingleRowBlockWriter singleRowBlockWriter = rowBlockBuilder.beginBlockEntry();
        int expectedSize = 0;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        // Write real (4byte value + 1 byte for null)
        REAL.writeLong(singleRowBlockWriter, i);
        expectedSize += 5;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        // Write Map<Bigint, Varchar>
        BlockBuilder mapWriter = singleRowBlockWriter.beginBlockEntry();
        // Opening entry, does not account for size.
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        // Each entry is of 28 bytes, with 6 byte value.
        BIGINT.writeLong(mapWriter, i * 2);
        VARCHAR.writeSlice(mapWriter, utf8Slice("Value1"));
        expectedSize += 28;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        // Another entry with 28 bytes.
        BIGINT.writeLong(mapWriter, i * 2 + 1);
        VARCHAR.writeSlice(mapWriter, utf8Slice("Value2"));
        expectedSize += 28;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        // closing the entry increases by 5 (offset + null)
        singleRowBlockWriter.closeEntry();
        expectedSize += 5;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        // Write array entry.
        BlockBuilder arrayWriter = singleRowBlockWriter.beginBlockEntry();
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        // Each entry is 9 bytes ( 8 bytes for double , 1 byte for null)
        DOUBLE.writeDouble(arrayWriter, i * 3);
        expectedSize += 9;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        DOUBLE.writeDouble(arrayWriter, i * 3 + 1);
        expectedSize += 9;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        singleRowBlockWriter.closeEntry();
        // closing the entry increases by 5 (offset + null)
        expectedSize += 5;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        // Write row type.
        BlockBuilder rowWriter = singleRowBlockWriter.beginBlockEntry();
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        rowWriter.appendNull();
        expectedSize += 5;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        singleRowBlockWriter.closeEntry();
        expectedSize += 5;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        rowBlockBuilder.closeEntry();
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        rowBlockBuilder.appendNull();
    }
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType) Test(org.testng.annotations.Test)

Example 55 with RowType

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

the class TestSingleRowBlockWriter method testSizeInBytesForNulls.

@Test
public void testSizeInBytesForNulls() {
    MapType mapType = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestSingleRowBlockWriter.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestSingleRowBlockWriter.class, "throwUnsupportedOperation"));
    ArrayType arrayType = new ArrayType(DOUBLE);
    RowType.Field rowField = new RowType.Field(Optional.of("my_struct"), INTEGER);
    RowType rowType = RowType.from(ImmutableList.of(rowField));
    List<Type> fieldTypes = ImmutableList.of(REAL, mapType, arrayType, rowType);
    RowBlockBuilder rowBlockBuilder = new RowBlockBuilder(fieldTypes, null, 1);
    for (int i = 0; i < 100; i++) {
        SingleRowBlockWriter singleRowBlockWriter = rowBlockBuilder.beginBlockEntry();
        int expectedSize = 0;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize);
        // Write real (4byte value + 1 byte for null)
        singleRowBlockWriter.appendNull();
        expectedSize += 5;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        // Write Map<Bigint, Varchar>
        singleRowBlockWriter.appendNull();
        expectedSize += 5;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        // Write array entry.
        singleRowBlockWriter.appendNull();
        expectedSize += 5;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        // Write row type.
        singleRowBlockWriter.appendNull();
        expectedSize += 5;
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        rowBlockBuilder.closeEntry();
        assertEquals(singleRowBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
        rowBlockBuilder.appendNull();
    }
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType) Test(org.testng.annotations.Test)

Aggregations

RowType (com.facebook.presto.common.type.RowType)61 ArrayType (com.facebook.presto.common.type.ArrayType)37 Type (com.facebook.presto.common.type.Type)32 MapType (com.facebook.presto.common.type.MapType)28 ImmutableList (com.google.common.collect.ImmutableList)19 ArrayList (java.util.ArrayList)18 DecimalType (com.facebook.presto.common.type.DecimalType)16 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)15 Test (org.testng.annotations.Test)15 List (java.util.List)14 VarcharType (com.facebook.presto.common.type.VarcharType)12 Block (com.facebook.presto.common.block.Block)11 CharType (com.facebook.presto.common.type.CharType)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 PrestoException (com.facebook.presto.spi.PrestoException)8 Map (java.util.Map)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)6 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)6 TimestampType (com.facebook.presto.common.type.TimestampType)5