use of com.facebook.presto.spi.type.FixedWidthType in project presto by prestodb.
the class TypeJsonUtils method stackRepresentationToObjectHelper.
private static Object stackRepresentationToObjectHelper(ConnectorSession session, JsonParser parser, Type type) throws IOException {
// cast('[null]', array(json)) should be casted to a single item array containing a json document "null" instead of sql null.
if (type instanceof JsonType) {
return OBJECT_MAPPER.writeValueAsString(parser.readValueAsTree());
}
if (parser.getCurrentToken() == JsonToken.VALUE_NULL) {
return null;
}
if (type instanceof ArrayType) {
List<Object> list = new ArrayList<>();
checkState(parser.getCurrentToken() == JsonToken.START_ARRAY, "Expected a json array");
while (parser.nextToken() != JsonToken.END_ARRAY) {
list.add(stackRepresentationToObjectHelper(session, parser, ((ArrayType) type).getElementType()));
}
return Collections.unmodifiableList(list);
}
if (type instanceof MapType) {
Map<Object, Object> map = new LinkedHashMap<>();
checkState(parser.getCurrentToken() == JsonToken.START_OBJECT, "Expected a json object");
while (parser.nextValue() != JsonToken.END_OBJECT) {
Object key = mapKeyToObject(session, parser.getCurrentName(), ((MapType) type).getKeyType());
Object value = stackRepresentationToObjectHelper(session, parser, ((MapType) type).getValueType());
map.put(key, value);
}
return Collections.unmodifiableMap(map);
}
if (type instanceof RowType) {
List<Object> list = new ArrayList<>();
checkState(parser.getCurrentToken() == JsonToken.START_ARRAY, "Expected a json array");
int field = 0;
RowType rowType = (RowType) type;
while (parser.nextValue() != JsonToken.END_ARRAY) {
checkArgument(field < rowType.getFields().size(), "Unexpected field for type %s", type);
Object value = stackRepresentationToObjectHelper(session, parser, rowType.getFields().get(field).getType());
list.add(value);
field++;
}
checkArgument(field == rowType.getFields().size(), "Expected %s fields for type %s", rowType.getFields().size(), type);
return Collections.unmodifiableList(list);
}
Slice sliceValue = null;
if (type.getJavaType() == Slice.class) {
sliceValue = Slices.utf8Slice(parser.getValueAsString());
}
BlockBuilder blockBuilder;
if (type instanceof FixedWidthType) {
blockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), 1);
} else {
blockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), 1, requireNonNull(sliceValue, "sliceValue is null").length());
}
if (type instanceof DecimalType) {
return getSqlDecimal((DecimalType) type, parser.getDecimalValue());
} else if (type.getJavaType() == boolean.class) {
type.writeBoolean(blockBuilder, parser.getBooleanValue());
} else if (type.getJavaType() == long.class) {
type.writeLong(blockBuilder, parser.getLongValue());
} else if (type.getJavaType() == double.class) {
type.writeDouble(blockBuilder, getDoubleValue(parser));
} else if (type.getJavaType() == Slice.class) {
type.writeSlice(blockBuilder, requireNonNull(sliceValue, "sliceValue is null"));
}
return type.getObjectValue(session, blockBuilder.build(), 0);
}
use of com.facebook.presto.spi.type.FixedWidthType in project presto by prestodb.
the class BlackHolePageSourceProvider method createZeroBlock.
private Block createZeroBlock(Type type, int rowsCount, Slice constantSlice) {
checkArgument(isSupportedType(type), "Unsupported type [%s]", type);
Slice slice;
// do not exceed varchar limit
if (isVarcharType(type)) {
slice = constantSlice.slice(0, Math.min(((VarcharType) type).getLength(), constantSlice.length()));
} else if (isLongDecimal(type)) {
slice = encodeScaledValue(ZERO);
} else {
slice = constantSlice;
}
BlockBuilder builder;
if (type instanceof FixedWidthType) {
builder = type.createBlockBuilder(new BlockBuilderStatus(), rowsCount);
} else {
builder = type.createBlockBuilder(new BlockBuilderStatus(), rowsCount, slice.length());
}
for (int i = 0; i < rowsCount; i++) {
Class<?> javaType = type.getJavaType();
if (javaType == boolean.class) {
type.writeBoolean(builder, false);
} else if (javaType == long.class) {
type.writeLong(builder, 0);
} else if (javaType == double.class) {
type.writeDouble(builder, 0.0);
} else if (javaType == Slice.class) {
requireNonNull(slice, "slice is null");
type.writeSlice(builder, slice, 0, slice.length());
} else {
throw new UnsupportedOperationException("Unknown javaType: " + javaType.getName());
}
}
return builder.build();
}
Aggregations