use of com.facebook.presto.spi.type.SqlDecimal in project presto by prestodb.
the class TypeJsonUtils method appendToBlockBuilder.
@VisibleForTesting
public static void appendToBlockBuilder(Type type, Object element, BlockBuilder blockBuilder) {
Class<?> javaType = type.getJavaType();
if (element == null) {
blockBuilder.appendNull();
} else if (type.getTypeSignature().getBase().equals(StandardTypes.ARRAY) && element instanceof Iterable<?>) {
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
for (Object subElement : (Iterable<?>) element) {
appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);
}
blockBuilder.closeEntry();
} else if (type.getTypeSignature().getBase().equals(StandardTypes.ROW) && element instanceof Iterable<?>) {
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
int field = 0;
for (Object subElement : (Iterable<?>) element) {
appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);
field++;
}
blockBuilder.closeEntry();
} else if (type.getTypeSignature().getBase().equals(StandardTypes.MAP) && element instanceof Map<?, ?>) {
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) element).entrySet()) {
appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
}
blockBuilder.closeEntry();
} else if (javaType == boolean.class) {
type.writeBoolean(blockBuilder, (Boolean) element);
} else if (javaType == long.class) {
if (element instanceof SqlDecimal) {
type.writeLong(blockBuilder, ((SqlDecimal) element).getUnscaledValue().longValue());
} else if (REAL.equals(type)) {
type.writeLong(blockBuilder, floatToRawIntBits(((Number) element).floatValue()));
} else {
type.writeLong(blockBuilder, ((Number) element).longValue());
}
} else if (javaType == double.class) {
type.writeDouble(blockBuilder, ((Number) element).doubleValue());
} else if (javaType == Slice.class) {
if (element instanceof String) {
type.writeSlice(blockBuilder, Slices.utf8Slice(element.toString()));
} else if (element instanceof byte[]) {
type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) element));
} else if (element instanceof SqlDecimal) {
type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) element).getUnscaledValue()));
} else {
type.writeSlice(blockBuilder, (Slice) element);
}
} else {
type.writeObject(blockBuilder, element);
}
}
use of com.facebook.presto.spi.type.SqlDecimal in project presto by prestodb.
the class MaterializedResult method convertToJdbcTypes.
private static MaterializedRow convertToJdbcTypes(MaterializedRow prestoRow) {
List<Object> jdbcValues = new ArrayList<>();
for (int field = 0; field < prestoRow.getFieldCount(); field++) {
Object prestoValue = prestoRow.getField(field);
Object jdbcValue;
if (prestoValue instanceof SqlDate) {
int days = ((SqlDate) prestoValue).getDays();
jdbcValue = new Date(TimeUnit.DAYS.toMillis(days));
} else if (prestoValue instanceof SqlTime) {
jdbcValue = new Time(((SqlTime) prestoValue).getMillisUtc());
} else if (prestoValue instanceof SqlTimeWithTimeZone) {
jdbcValue = new Time(((SqlTimeWithTimeZone) prestoValue).getMillisUtc());
} else if (prestoValue instanceof SqlTimestamp) {
jdbcValue = new Timestamp(((SqlTimestamp) prestoValue).getMillisUtc());
} else if (prestoValue instanceof SqlTimestampWithTimeZone) {
jdbcValue = new Timestamp(((SqlTimestampWithTimeZone) prestoValue).getMillisUtc());
} else if (prestoValue instanceof SqlDecimal) {
jdbcValue = ((SqlDecimal) prestoValue).toBigDecimal();
} else {
jdbcValue = prestoValue;
}
jdbcValues.add(jdbcValue);
}
return new MaterializedRow(prestoRow.getPrecision(), jdbcValues);
}
Aggregations