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());
}
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();
}
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;
}
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();
}
}
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();
}
}
Aggregations