use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class MongoPageSink method getObjectValue.
private Object getObjectValue(Type type, Block block, int position) {
if (block.isNull(position)) {
if (type.equals(OBJECT_ID)) {
return new ObjectId();
}
return null;
}
if (type.equals(OBJECT_ID)) {
return new ObjectId(block.getSlice(position, 0, block.getSliceLength(position)).getBytes());
}
if (type.equals(BooleanType.BOOLEAN)) {
return type.getBoolean(block, position);
}
if (type.equals(BigintType.BIGINT)) {
return type.getLong(block, position);
}
if (type.equals(IntegerType.INTEGER)) {
return (int) type.getLong(block, position);
}
if (type.equals(SmallintType.SMALLINT)) {
return (short) type.getLong(block, position);
}
if (type.equals(TinyintType.TINYINT)) {
return (byte) type.getLong(block, position);
}
if (type.equals(DoubleType.DOUBLE)) {
return type.getDouble(block, position);
}
if (isVarcharType(type)) {
return type.getSlice(block, position).toStringUtf8();
}
if (type.equals(VarbinaryType.VARBINARY)) {
return new Binary(type.getSlice(block, position).getBytes());
}
if (type.equals(DateType.DATE)) {
long days = type.getLong(block, position);
return new Date(TimeUnit.DAYS.toMillis(days));
}
if (type.equals(TimeType.TIME)) {
long millisUtc = type.getLong(block, position);
return new Date(millisUtc);
}
if (type.equals(TimestampType.TIMESTAMP)) {
long millisUtc = type.getLong(block, position);
return new Date(millisUtc);
}
if (type.equals(TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE)) {
long millisUtc = unpackMillisUtc(type.getLong(block, position));
return new Date(millisUtc);
}
if (type instanceof DecimalType) {
// TODO: decimal type might not support yet
// TODO: this code is likely wrong and should switch to Decimals.readBigDecimal()
DecimalType decimalType = (DecimalType) type;
BigInteger unscaledValue;
if (decimalType.isShort()) {
unscaledValue = BigInteger.valueOf(decimalType.getLong(block, position));
} else {
unscaledValue = Decimals.decodeUnscaledValue(decimalType.getSlice(block, position));
}
return new BigDecimal(unscaledValue);
}
if (isArrayType(type)) {
Type elementType = type.getTypeParameters().get(0);
Block arrayBlock = block.getBlock(position);
List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
Object element = getObjectValue(elementType, arrayBlock, i);
list.add(element);
}
return unmodifiableList(list);
}
if (isMapType(type)) {
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
Block mapBlock = block.getBlock(position);
// map type is converted into list of fixed keys document
List<Object> values = new ArrayList<>(mapBlock.getPositionCount() / 2);
for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
Map<String, Object> mapValue = new HashMap<>();
mapValue.put("key", getObjectValue(keyType, mapBlock, i));
mapValue.put("value", getObjectValue(valueType, mapBlock, i + 1));
values.add(mapValue);
}
return unmodifiableList(values);
}
if (isRowType(type)) {
Block rowBlock = block.getBlock(position);
List<Type> fieldTypes = type.getTypeParameters();
if (fieldTypes.size() != rowBlock.getPositionCount()) {
throw new PrestoException(StandardErrorCode.GENERIC_INTERNAL_ERROR, "Expected row value field count does not match type field count");
}
if (isImplicitRowType(type)) {
List<Object> rowValue = new ArrayList<>();
for (int i = 0; i < rowBlock.getPositionCount(); i++) {
Object element = getObjectValue(fieldTypes.get(i), rowBlock, i);
rowValue.add(element);
}
return unmodifiableList(rowValue);
}
Map<String, Object> rowValue = new HashMap<>();
for (int i = 0; i < rowBlock.getPositionCount(); i++) {
rowValue.put(type.getTypeSignature().getParameters().get(i).getNamedTypeSignature().getName().orElse("field" + i), getObjectValue(fieldTypes.get(i), rowBlock, i));
}
return unmodifiableMap(rowValue);
}
throw new PrestoException(NOT_SUPPORTED, "unsupported type: " + type);
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class ParquetTestUtils method getHiveType.
private static String getHiveType(Type type) {
if (type.equals(BOOLEAN) || type.equals(BIGINT) || type.equals(SmallintType.SMALLINT) || type.equals(TinyintType.TINYINT) || type.equals(DOUBLE)) {
return type.getTypeSignature().toString();
}
if (type.equals(INTEGER)) {
return "int";
}
if (type.equals(REAL)) {
return "float";
}
if (type.equals(TIMESTAMP)) {
return "timestamp";
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
int varcharLength = varcharType.getLength();
return "varchar(" + varcharLength + ")";
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return format("decimal(%d,%d)", decimalType.getPrecision(), decimalType.getScale());
}
if (type instanceof ArrayType) {
return "array<" + getHiveType(((ArrayType) type).getElementType()) + ">";
}
if (type instanceof RowType) {
return "struct<" + Joiner.on(",").join(((RowType) type).getFields().stream().map(t -> t.getName().get() + ":" + getHiveType(t.getType())).collect(toList())) + ">";
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class ParquetTestUtils method getObjectInspector.
private static ObjectInspector getObjectInspector(Type type) {
if (type.equals(BOOLEAN)) {
return writableBooleanObjectInspector;
}
if (type.equals(BIGINT)) {
return writableLongObjectInspector;
}
if (type.equals(INTEGER)) {
return writableIntObjectInspector;
}
if (type.equals(SmallintType.SMALLINT)) {
return writableShortObjectInspector;
}
if (type.equals(TinyintType.TINYINT)) {
return writableByteObjectInspector;
}
if (type.equals(DOUBLE)) {
return writableDoubleObjectInspector;
}
if (type.equals(REAL)) {
return writableFloatObjectInspector;
}
if (type.equals(TIMESTAMP)) {
return writableTimestampObjectInspector;
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
int varcharLength = varcharType.getLength();
return getPrimitiveWritableObjectInspector(getVarcharTypeInfo(varcharLength));
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveWritableObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
}
if (type instanceof ArrayType || type instanceof RowType) {
return getJavaObjectInspector(type);
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class SerDeUtils method serializePrimitive.
private static void serializePrimitive(Type type, BlockBuilder builder, Object object, PrimitiveObjectInspector inspector) {
requireNonNull(builder, "parent builder is null");
if (object == null) {
builder.appendNull();
return;
}
switch(inspector.getPrimitiveCategory()) {
case BOOLEAN:
BooleanType.BOOLEAN.writeBoolean(builder, ((BooleanObjectInspector) inspector).get(object));
return;
case BYTE:
TinyintType.TINYINT.writeLong(builder, ((ByteObjectInspector) inspector).get(object));
return;
case SHORT:
SmallintType.SMALLINT.writeLong(builder, ((ShortObjectInspector) inspector).get(object));
return;
case INT:
IntegerType.INTEGER.writeLong(builder, ((IntObjectInspector) inspector).get(object));
return;
case LONG:
BigintType.BIGINT.writeLong(builder, ((LongObjectInspector) inspector).get(object));
return;
case FLOAT:
RealType.REAL.writeLong(builder, floatToRawIntBits(((FloatObjectInspector) inspector).get(object)));
return;
case DOUBLE:
DoubleType.DOUBLE.writeDouble(builder, ((DoubleObjectInspector) inspector).get(object));
return;
case STRING:
type.writeSlice(builder, Slices.utf8Slice(((StringObjectInspector) inspector).getPrimitiveJavaObject(object)));
return;
case VARCHAR:
type.writeSlice(builder, Slices.utf8Slice(((HiveVarcharObjectInspector) inspector).getPrimitiveJavaObject(object).getValue()));
return;
case CHAR:
CharType charType = (CharType) type;
HiveChar hiveChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(object);
type.writeSlice(builder, truncateToLengthAndTrimSpaces(Slices.utf8Slice(hiveChar.getValue()), charType.getLength()));
return;
case DATE:
DateType.DATE.writeLong(builder, formatDateAsLong(object, (DateObjectInspector) inspector));
return;
case TIMESTAMP:
TimestampType.TIMESTAMP.writeLong(builder, formatTimestampAsLong(object, (TimestampObjectInspector) inspector));
return;
case BINARY:
VARBINARY.writeSlice(builder, Slices.wrappedBuffer(((BinaryObjectInspector) inspector).getPrimitiveJavaObject(object)));
return;
case DECIMAL:
DecimalType decimalType = (DecimalType) type;
HiveDecimalWritable hiveDecimal = ((HiveDecimalObjectInspector) inspector).getPrimitiveWritableObject(object);
if (decimalType.isShort()) {
decimalType.writeLong(builder, DecimalUtils.getShortDecimalValue(hiveDecimal, decimalType.getScale()));
} else {
decimalType.writeSlice(builder, DecimalUtils.getLongDecimalValue(hiveDecimal, decimalType.getScale()));
}
return;
}
throw new RuntimeException("Unknown primitive type: " + inspector.getPrimitiveCategory());
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class AggregatedParquetPageSource method writeMinMax.
private void writeMinMax(ParquetMetadata parquetMetadata, int columnIndex, BlockBuilder blockBuilder, Type type, HiveType hiveType, boolean isMin) {
org.apache.parquet.schema.Type parquetType = parquetMetadata.getFileMetaData().getSchema().getType(columnIndex);
if (parquetType instanceof GroupType) {
throw new IllegalArgumentException("Unsupported type : " + parquetType.toString());
}
Object value = null;
for (BlockMetaData blockMetaData : parquetMetadata.getBlocks()) {
Statistics statistics = blockMetaData.getColumns().get(columnIndex).getStatistics();
if (!statistics.hasNonNullValue()) {
throw new UnsupportedOperationException("No min/max found for parquet file. Set session property hive.pushdown_partial_aggregations_into_scan=false and execute query again");
}
if (isMin) {
Object currentValue = statistics.genericGetMin();
if (currentValue != null && (value == null || ((Comparable) currentValue).compareTo(value) < 0)) {
value = currentValue;
}
} else {
Object currentValue = statistics.genericGetMax();
if (currentValue != null && (value == null || ((Comparable) currentValue).compareTo(value) > 0)) {
value = currentValue;
}
}
}
if (type instanceof FixedWidthType) {
completedBytes += ((FixedWidthType) type).getFixedSize();
}
if (value == null) {
blockBuilder.appendNull();
return;
}
PrimitiveType.PrimitiveTypeName parquetTypeName = parquetType.asPrimitiveType().getPrimitiveTypeName();
switch(parquetTypeName) {
case INT32:
{
blockBuilder.writeLong(Long.valueOf((Integer) value));
break;
}
case INT64:
{
blockBuilder.writeLong((Long) value);
break;
}
case INT96:
{
blockBuilder.writeLong(getTimestampMillis(((Binary) value).getBytes(), 0));
break;
}
case FLOAT:
{
blockBuilder.writeLong(floatToRawIntBits((Float) value));
break;
}
case DOUBLE:
{
type.writeDouble(blockBuilder, (Double) value);
break;
}
case FIXED_LEN_BYTE_ARRAY:
{
byte[] valBytes = ((Binary) value).getBytes();
DecimalType decimalType = (DecimalType) hiveType.getType(typeManager);
if (decimalType.isShort()) {
blockBuilder.writeLong(getShortDecimalValue(valBytes));
} else {
BigInteger bigIntValue = new BigInteger(valBytes);
type.writeSlice(blockBuilder, encodeUnscaledValue(bigIntValue));
}
break;
}
case BINARY:
{
Slice slice = Slices.wrappedBuffer(((Binary) value).getBytes());
blockBuilder.writeBytes(slice, 0, slice.length()).closeEntry();
completedBytes += slice.length();
break;
}
case BOOLEAN:
default:
throw new IllegalArgumentException("Unexpected parquet type name: " + parquetTypeName);
}
}
Aggregations