use of com.facebook.presto.spi.type.DecimalType in project presto by prestodb.
the class DecimalStreamReader method readBlock.
@Override
public Block readBlock(Type type) throws IOException {
DecimalType decimalType = (DecimalType) type;
if (!rowGroupOpen) {
openRowGroup();
}
seekToOffset();
allocateVectors();
BlockBuilder builder = decimalType.createBlockBuilder(new BlockBuilderStatus(), nextBatchSize);
if (presentStream == null) {
if (decimalStream == null) {
throw new OrcCorruptionException("Value is not null but decimal stream is not present");
}
if (scaleStream == null) {
throw new OrcCorruptionException("Value is not null but scale stream is not present");
}
Arrays.fill(nullVector, false);
scaleStream.nextLongVector(nextBatchSize, scaleVector);
if (decimalType.isShort()) {
decimalStream.nextShortDecimalVector(nextBatchSize, builder, decimalType, scaleVector);
} else {
decimalStream.nextLongDecimalVector(nextBatchSize, builder, decimalType, scaleVector);
}
} else {
int nullValues = presentStream.getUnsetBits(nextBatchSize, nullVector);
if (nullValues != nextBatchSize) {
if (decimalStream == null) {
throw new OrcCorruptionException("Value is not null but decimal stream is not present");
}
if (scaleStream == null) {
throw new OrcCorruptionException("Value is not null but scale stream is not present");
}
scaleStream.nextLongVector(nextBatchSize, scaleVector, nullVector);
if (decimalType.isShort()) {
decimalStream.nextShortDecimalVector(nextBatchSize, builder, decimalType, scaleVector, nullVector);
} else {
decimalStream.nextLongDecimalVector(nextBatchSize, builder, decimalType, scaleVector, nullVector);
}
} else {
for (int i = 0; i < nextBatchSize; i++) {
builder.appendNull();
}
}
}
readOffset = 0;
nextBatchSize = 0;
return builder.build();
}
use of com.facebook.presto.spi.type.DecimalType 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.DecimalType in project carbondata by apache.
the class CarbondataRecordCursor method getLong.
@Override
public long getLong(int field) {
Object obj = getFieldValue(field);
Long timeStr = 0L;
if (obj instanceof Integer) {
timeStr = ((Integer) obj).longValue();
} else if (obj instanceof Long) {
timeStr = (Long) obj;
} else {
timeStr = Math.round(Double.parseDouble(obj.toString()));
}
Type actual = getType(field);
if (actual instanceof TimestampType) {
return new Timestamp(timeStr).getTime() / 1000;
} else if (isShortDecimal(actual)) {
return shortDecimalPartitionKey(obj.toString(), (DecimalType) actual, columnHandles.get(field).getColumnName());
}
// suppose the
return timeStr;
}
use of com.facebook.presto.spi.type.DecimalType in project carbondata by apache.
the class DecimalSliceStreamReader method readBlock.
/**
* Create Block for DecimalType
* @param type
* @return
* @throws IOException
*/
public Block readBlock(Type type) throws IOException {
int numberOfRows = 0;
BlockBuilder builder = null;
if (isVectorReader) {
numberOfRows = batchSize;
builder = type.createBlockBuilder(new BlockBuilderStatus(), numberOfRows);
if (columnVector != null) {
if (columnVector.anyNullsSet()) {
handleNullInVector(type, numberOfRows, builder);
} else {
if (isShortDecimal(type)) {
populateShortDecimalVector(type, numberOfRows, builder);
} else {
populateLongDecimalVector(type, numberOfRows, builder);
}
}
}
} else {
if (streamData != null) {
numberOfRows = streamData.length;
builder = type.createBlockBuilder(new BlockBuilderStatus(), numberOfRows);
for (int i = 0; i < numberOfRows; i++) {
Slice slice = getSlice(streamData[i], type);
if (isShortDecimal(type)) {
type.writeLong(builder, parseLong((DecimalType) type, slice, 0, slice.length()));
} else {
type.writeSlice(builder, parseSlice((DecimalType) type, slice, 0, slice.length()));
}
}
}
}
if (builder == null) {
return null;
}
return builder.build();
}
use of com.facebook.presto.spi.type.DecimalType in project carbondata by apache.
the class DecimalSliceStreamReader method handleNullInVector.
private void handleNullInVector(Type type, int numberOfRows, BlockBuilder builder) {
for (int i = 0; i < numberOfRows; i++) {
if (columnVector.isNullAt(i)) {
builder.appendNull();
} else {
if (isShortDecimal(type)) {
BigDecimal decimalValue = (BigDecimal) columnVector.getData(i);
long rescaledDecimal = Decimals.rescale(decimalValue.unscaledValue().longValue(), decimalValue.scale(), ((DecimalType) type).getScale());
type.writeLong(builder, rescaledDecimal);
} else {
Slice slice = getSlice(columnVector.getData(i), type);
type.writeSlice(builder, parseSlice((DecimalType) type, slice, 0, slice.length()));
}
}
}
}
Aggregations