use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class PrometheusRecordCursor method writeObject.
private static void writeObject(BlockBuilder builder, Type type, Object obj) {
if (type instanceof ArrayType) {
Type elementType = ((ArrayType) type).getElementType();
BlockBuilder arrayBuilder = builder.beginBlockEntry();
for (Object item : (List<?>) obj) {
writeObject(arrayBuilder, elementType, item);
}
builder.closeEntry();
} else if (type instanceof MapType) {
MapType mapType = (MapType) type;
BlockBuilder mapBlockBuilder = builder.beginBlockEntry();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) obj).entrySet()) {
writeObject(mapBlockBuilder, mapType.getKeyType(), entry.getKey());
writeObject(mapBlockBuilder, mapType.getValueType(), entry.getValue());
}
builder.closeEntry();
} else {
if (BOOLEAN.equals(type) || TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type) || DOUBLE.equals(type) || type instanceof VarcharType) {
TypeUtils.writeNativeValue(type, builder, obj);
}
}
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class PrometheusRecordCursor method getBlockFromMap.
static Block getBlockFromMap(Type mapType, Map<?, ?> map) {
// on functions like COUNT() the Type won't be a MapType
if (!(mapType instanceof MapType)) {
return null;
}
Type keyType = mapType.getTypeParameters().get(0);
Type valueType = mapType.getTypeParameters().get(1);
BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, 1);
BlockBuilder builder = mapBlockBuilder.beginBlockEntry();
for (Map.Entry<?, ?> entry : map.entrySet()) {
writeObject(builder, keyType, entry.getKey());
writeObject(builder, valueType, entry.getValue());
}
mapBlockBuilder.closeEntry();
return (Block) mapType.getObject(mapBlockBuilder, 0);
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class PrometheusRecordCursor method getMapFromBlock.
static Map<Object, Object> getMapFromBlock(Type type, Block block) {
MapType mapType = (MapType) type;
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
Map<Object, Object> map = new HashMap<>(block.getPositionCount() / 2);
for (int i = 0; i < block.getPositionCount(); i += 2) {
map.put(readObject(keyType, block, i), readObject(valueType, block, i + 1));
}
return map;
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class QueryRewriter method getColumnTypeRewrite.
private Optional<Type> getColumnTypeRewrite(Type type) {
if (type.equals(DATE) || type.equals(TIME)) {
return Optional.of(TIMESTAMP);
}
if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) {
return Optional.of(VARCHAR);
}
if (type.equals(UNKNOWN)) {
return Optional.of(BIGINT);
}
if (type instanceof DecimalType) {
return Optional.of(DOUBLE);
}
if (type instanceof ArrayType) {
return getColumnTypeRewrite(((ArrayType) type).getElementType()).map(ArrayType::new);
}
if (type instanceof MapType) {
Type keyType = ((MapType) type).getKeyType();
Type valueType = ((MapType) type).getValueType();
Optional<Type> keyTypeRewrite = getColumnTypeRewrite(keyType);
Optional<Type> valueTypeRewrite = getColumnTypeRewrite(valueType);
if (keyTypeRewrite.isPresent() || valueTypeRewrite.isPresent()) {
return Optional.of(typeManager.getType(new TypeSignature(MAP, TypeSignatureParameter.of(keyTypeRewrite.orElse(keyType).getTypeSignature()), TypeSignatureParameter.of(valueTypeRewrite.orElse(valueType).getTypeSignature()))));
}
return Optional.empty();
}
if (type instanceof RowType) {
List<Field> fields = ((RowType) type).getFields();
List<Field> fieldsRewrite = new ArrayList<>();
boolean rewrite = false;
for (Field field : fields) {
Optional<Type> fieldTypeRewrite = getColumnTypeRewrite(field.getType());
rewrite = rewrite || fieldTypeRewrite.isPresent();
fieldsRewrite.add(new Field(field.getName(), fieldTypeRewrite.orElse(field.getType())));
}
return rewrite ? Optional.of(RowType.from(fieldsRewrite)) : Optional.empty();
}
return Optional.empty();
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class StructuredColumnMismatchResolver method resolveResultMismatch.
@Override
public Optional<String> resolveResultMismatch(DataMatchResult matchResult, QueryBundle control) {
checkArgument(!matchResult.isMatched(), "Expect not matched");
if (matchResult.getMatchType() != COLUMN_MISMATCH) {
return Optional.empty();
}
for (ColumnMatchResult<?> mismatchedColumn : matchResult.getMismatchedColumns()) {
Type columnType = mismatchedColumn.getColumn().getType();
if (columnType instanceof ArrayType) {
ArrayColumnChecksum controlChecksum = (ArrayColumnChecksum) mismatchedColumn.getControlChecksum();
ArrayColumnChecksum testChecksum = (ArrayColumnChecksum) mismatchedColumn.getTestChecksum();
if (!containsFloatingPointType(((ArrayType) columnType).getElementType()) || !isCardinalityMatched(controlChecksum, testChecksum)) {
return Optional.empty();
}
} else if (columnType instanceof MapType) {
MapColumnChecksum controlChecksum = (MapColumnChecksum) mismatchedColumn.getControlChecksum();
MapColumnChecksum testChecksum = (MapColumnChecksum) mismatchedColumn.getTestChecksum();
if (!isCardinalityMatched(controlChecksum, testChecksum)) {
return Optional.empty();
}
boolean keyContainsFloatingPoint = containsFloatingPointType(((MapType) columnType).getKeyType());
boolean valueContainsFloatingPoint = containsFloatingPointType(((MapType) columnType).getValueType());
if (!keyContainsFloatingPoint && !valueContainsFloatingPoint) {
return Optional.empty();
}
if (!keyContainsFloatingPoint && !Objects.equals(controlChecksum.getKeysChecksum(), testChecksum.getKeysChecksum())) {
return Optional.empty();
}
if (!valueContainsFloatingPoint && !Objects.equals(controlChecksum.getValuesChecksum(), testChecksum.getValuesChecksum())) {
return Optional.empty();
}
} else {
return Optional.empty();
}
}
return Optional.of("Structured columns auto-resolved");
}
Aggregations