Search in sources :

Example 6 with DecimalType

use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.

the class LongDecimalStatisticsBuilder method addBlock.

@Override
public void addBlock(Type type, Block block) {
    int scale = ((DecimalType) type).getScale();
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (!block.isNull(position)) {
            Slice value = type.getSlice(block, position);
            addValue(new BigDecimal(Decimals.decodeUnscaledValue(value), scale));
        }
    }
}
Also used : Slice(io.airlift.slice.Slice) DecimalType(com.facebook.presto.common.type.DecimalType) BigDecimal(java.math.BigDecimal)

Example 7 with DecimalType

use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.

the class PinotBrokerPageSourceBase method setValue.

protected void setValue(Type type, BlockBuilder blockBuilder, String value) {
    if (blockBuilder == null) {
        return;
    }
    if (value == null) {
        blockBuilder.appendNull();
        return;
    }
    if (!(type instanceof FixedWidthType) && !(type instanceof VarcharType) && !(type instanceof JsonType)) {
        throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "type '" + type + "' not supported");
    }
    if (type instanceof FixedWidthType) {
        completedBytes += ((FixedWidthType) type).getFixedSize();
        if (type instanceof BigintType) {
            type.writeLong(blockBuilder, parseDouble(value).longValue());
        } else if (type instanceof IntegerType) {
            blockBuilder.writeInt(parseDouble(value).intValue());
        } else if (type instanceof TinyintType) {
            blockBuilder.writeByte(parseDouble(value).byteValue());
        } else if (type instanceof SmallintType) {
            blockBuilder.writeShort(parseDouble(value).shortValue());
        } else if (type instanceof BooleanType) {
            type.writeBoolean(blockBuilder, parseBoolean(value));
        } else if (type instanceof DecimalType || type instanceof DoubleType) {
            type.writeDouble(blockBuilder, parseDouble(value));
        } else if (type instanceof TimestampType) {
            type.writeLong(blockBuilder, parseTimestamp(value));
        } else if (type instanceof DateType) {
            type.writeLong(blockBuilder, parseLong(value));
        } else {
            throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "type '" + type + "' not supported");
        }
    } else {
        Slice slice = Slices.utf8Slice(value);
        blockBuilder.writeBytes(slice, 0, slice.length()).closeEntry();
        completedBytes += slice.length();
    }
}
Also used : JsonType(com.facebook.presto.common.type.JsonType) VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) BooleanType(com.facebook.presto.common.type.BooleanType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) DoubleType(com.facebook.presto.common.type.DoubleType) Slice(io.airlift.slice.Slice) DecimalType(com.facebook.presto.common.type.DecimalType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) DateType(com.facebook.presto.common.type.DateType) FixedWidthType(com.facebook.presto.common.type.FixedWidthType)

Example 8 with DecimalType

use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.

the class Row method nativeContainerToOrcValue.

private static Object nativeContainerToOrcValue(Type type, Object nativeValue) {
    if (nativeValue == null) {
        return null;
    }
    if (type instanceof DecimalType) {
        BigInteger unscaledValue;
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            unscaledValue = BigInteger.valueOf((long) nativeValue);
        } else {
            unscaledValue = Decimals.decodeUnscaledValue((Slice) nativeValue);
        }
        return HiveDecimal.create(unscaledValue, decimalType.getScale());
    }
    if (type.getJavaType() == boolean.class) {
        return nativeValue;
    }
    if (type.getJavaType() == long.class) {
        return nativeValue;
    }
    if (type.getJavaType() == double.class) {
        return nativeValue;
    }
    if (type.getJavaType() == Slice.class) {
        Slice slice = (Slice) nativeValue;
        return type instanceof VarcharType ? slice.toStringUtf8() : slice.getBytes();
    }
    if (isArrayType(type)) {
        Block arrayBlock = (Block) nativeValue;
        Type elementType = type.getTypeParameters().get(0);
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
            list.add(nativeContainerToOrcValue(elementType, getNativeContainerValue(elementType, arrayBlock, i)));
        }
        return list;
    }
    if (isMapType(type)) {
        Block mapBlock = (Block) nativeValue;
        Type keyType = type.getTypeParameters().get(0);
        Type valueType = type.getTypeParameters().get(1);
        Map<Object, Object> map = new HashMap<>();
        for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
            Object key = nativeContainerToOrcValue(keyType, getNativeContainerValue(keyType, mapBlock, i));
            Object value = nativeContainerToOrcValue(valueType, getNativeContainerValue(valueType, mapBlock, i + 1));
            map.put(key, value);
        }
        return map;
    }
    throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unimplemented type: " + type);
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PrestoException(com.facebook.presto.spi.PrestoException) DecimalType(com.facebook.presto.common.type.DecimalType) VarcharType(com.facebook.presto.common.type.VarcharType) Types.isArrayType(com.facebook.presto.raptor.util.Types.isArrayType) Types.isMapType(com.facebook.presto.raptor.util.Types.isMapType) Type(com.facebook.presto.common.type.Type) Slice(io.airlift.slice.Slice) DecimalType(com.facebook.presto.common.type.DecimalType) BigInteger(java.math.BigInteger) Block(com.facebook.presto.common.block.Block)

Example 9 with DecimalType

use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.

the class ParquetSchemaConverter method getPrimitiveType.

private org.apache.parquet.schema.Type getPrimitiveType(Type type, String name, List<String> parent) {
    List<String> fullName = ImmutableList.<String>builder().addAll(parent).add(name).build();
    primitiveTypes.put(fullName, type);
    if (BOOLEAN.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.BOOLEAN, OPTIONAL).named(name);
    }
    if (INTEGER.equals(type) || SMALLINT.equals(type) || TINYINT.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.INT32, OPTIONAL).named(name);
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.getPrecision() <= 9) {
            return Types.optional(PrimitiveType.PrimitiveTypeName.INT32).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
        } else if (decimalType.isShort()) {
            return Types.optional(PrimitiveType.PrimitiveTypeName.INT64).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
        } else {
            return Types.optional(PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY).length(16).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
        }
    }
    if (DATE.equals(type)) {
        return Types.optional(PrimitiveType.PrimitiveTypeName.INT32).as(OriginalType.DATE).named(name);
    }
    if (BIGINT.equals(type) || TIMESTAMP.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.INT64, OPTIONAL).named(name);
    }
    if (DOUBLE.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.DOUBLE, OPTIONAL).named(name);
    }
    if (RealType.REAL.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.FLOAT, OPTIONAL).named(name);
    }
    if (type instanceof VarcharType || type instanceof CharType || type instanceof VarbinaryType) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.BINARY, OPTIONAL).named(name);
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported primitive type: %s", type));
}
Also used : VarbinaryType(com.facebook.presto.common.type.VarbinaryType) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) PrestoException(com.facebook.presto.spi.PrestoException) CharType(com.facebook.presto.common.type.CharType)

Example 10 with DecimalType

use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.

the class H2QueryRunner method rowMapper.

private static RowMapper<MaterializedRow> rowMapper(List<? extends Type> types) {
    return new RowMapper<MaterializedRow>() {

        private Object getValue(Type type, ResultSet resultSet, int position) throws SQLException {
            if (BOOLEAN.equals(type)) {
                boolean booleanValue = resultSet.getBoolean(position);
                return resultSet.wasNull() ? null : booleanValue;
            } else if (TINYINT.equals(type)) {
                byte byteValue = resultSet.getByte(position);
                return resultSet.wasNull() ? null : byteValue;
            } else if (SMALLINT.equals(type)) {
                short shortValue = resultSet.getShort(position);
                return resultSet.wasNull() ? null : shortValue;
            } else if (INTEGER.equals(type)) {
                int intValue = resultSet.getInt(position);
                return resultSet.wasNull() ? null : intValue;
            } else if (BIGINT.equals(type)) {
                long longValue = resultSet.getLong(position);
                return resultSet.wasNull() ? null : longValue;
            } else if (REAL.equals(type)) {
                float floatValue = resultSet.getFloat(position);
                return resultSet.wasNull() ? null : floatValue;
            } else if (DOUBLE.equals(type)) {
                double doubleValue = resultSet.getDouble(position);
                return resultSet.wasNull() ? null : doubleValue;
            } else if (isVarcharType(type)) {
                String stringValue = resultSet.getString(position);
                return resultSet.wasNull() ? null : stringValue;
            } else if (isCharType(type)) {
                String stringValue = resultSet.getString(position);
                return resultSet.wasNull() ? null : padEnd(stringValue, ((CharType) type).getLength(), ' ');
            } else if (VARBINARY.equals(type)) {
                byte[] binary = resultSet.getBytes(position);
                return resultSet.wasNull() ? null : binary;
            } else if (DATE.equals(type)) {
                // resultSet.getDate(i) doesn't work if JVM's zone skipped day being retrieved (e.g. 2011-12-30 and Pacific/Apia zone)
                LocalDate dateValue = resultSet.getObject(position, LocalDate.class);
                return resultSet.wasNull() ? null : dateValue;
            } else if (TIME.equals(type)) {
                // resultSet.getTime(i) doesn't work if JVM's zone had forward offset change during 1970-01-01 (e.g. America/Hermosillo zone)
                LocalTime timeValue = resultSet.getObject(position, LocalTime.class);
                return resultSet.wasNull() ? null : timeValue;
            } else if (TIME_WITH_TIME_ZONE.equals(type)) {
                throw new UnsupportedOperationException("H2 does not support TIME WITH TIME ZONE");
            } else if (TIMESTAMP.equals(type)) {
                // resultSet.getTimestamp(i) doesn't work if JVM's zone had forward offset at the date/time being retrieved
                LocalDateTime timestampValue;
                try {
                    timestampValue = resultSet.getObject(position, LocalDateTime.class);
                } catch (SQLException first) {
                    // H2 cannot convert DATE to LocalDateTime in their JDBC driver (even though it can convert to java.sql.Timestamp), we need to do this manually
                    try {
                        timestampValue = Optional.ofNullable(resultSet.getObject(position, LocalDate.class)).map(LocalDate::atStartOfDay).orElse(null);
                    } catch (RuntimeException e) {
                        first.addSuppressed(e);
                        throw first;
                    }
                }
                return resultSet.wasNull() ? null : timestampValue;
            } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
                // This means H2 is unsuitable for testing TIMESTAMP WITH TIME ZONE-bearing queries. Those need to be tested manually.
                throw new UnsupportedOperationException();
            } else if (UNKNOWN.equals(type)) {
                Object objectValue = resultSet.getObject(position);
                checkState(resultSet.wasNull(), "Expected a null value, but got %s", objectValue);
                return null;
            } else if (type instanceof DecimalType) {
                DecimalType decimalType = (DecimalType) type;
                BigDecimal decimalValue = resultSet.getBigDecimal(position);
                return resultSet.wasNull() ? null : decimalValue.setScale(decimalType.getScale(), BigDecimal.ROUND_HALF_UP).round(new MathContext(decimalType.getPrecision()));
            } else if (type instanceof ArrayType) {
                Array array = resultSet.getArray(position);
                return resultSet.wasNull() ? null : newArrayList(mapArrayValues(((ArrayType) type), (Object[]) array.getArray()));
            } else if (type instanceof RowType) {
                Array array = resultSet.getArray(position);
                return resultSet.wasNull() ? null : newArrayList(mapRowValues((RowType) type, (Object[]) array.getArray()));
            } else if (type instanceof TypeWithName) {
                return getValue(((TypeWithName) type).getType(), resultSet, position);
            } else {
                throw new AssertionError("unhandled type: " + type);
            }
        }

        @Override
        public MaterializedRow map(ResultSet resultSet, StatementContext context) throws SQLException {
            int count = resultSet.getMetaData().getColumnCount();
            checkArgument(types.size() == count, "expected types count (%s) does not match actual column count (%s)", types.size(), count);
            List<Object> row = new ArrayList<>(count);
            for (int i = 1; i <= count; i++) {
                row.add(getValue(types.get(i - 1), resultSet, i));
            }
            return new MaterializedRow(MaterializedResult.DEFAULT_PRECISION, row);
        }
    };
}
Also used : LocalDateTime(java.time.LocalDateTime) SQLException(java.sql.SQLException) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) RowType(com.facebook.presto.common.type.RowType) LocalDate(java.time.LocalDate) StatementContext(org.jdbi.v3.core.statement.StatementContext) ArrayType(com.facebook.presto.common.type.ArrayType) ResultSet(java.sql.ResultSet) RowMapper(org.jdbi.v3.core.mapper.RowMapper) TypeWithName(com.facebook.presto.common.type.TypeWithName) LocalTime(java.time.LocalTime) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Array(java.sql.Array) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) CharType(com.facebook.presto.common.type.CharType) MaterializedRow(com.facebook.presto.testing.MaterializedRow)

Aggregations

DecimalType (com.facebook.presto.common.type.DecimalType)55 VarcharType (com.facebook.presto.common.type.VarcharType)28 Type (com.facebook.presto.common.type.Type)23 CharType (com.facebook.presto.common.type.CharType)20 Block (com.facebook.presto.common.block.Block)15 ArrayType (com.facebook.presto.common.type.ArrayType)15 RowType (com.facebook.presto.common.type.RowType)14 Slice (io.airlift.slice.Slice)14 ArrayList (java.util.ArrayList)13 PrestoException (com.facebook.presto.spi.PrestoException)12 TimestampType (com.facebook.presto.common.type.TimestampType)11 BigDecimal (java.math.BigDecimal)11 MapType (com.facebook.presto.common.type.MapType)10 Chars.isCharType (com.facebook.presto.common.type.Chars.isCharType)9 DateType (com.facebook.presto.common.type.DateType)9 IntegerType (com.facebook.presto.common.type.IntegerType)9 VarbinaryType (com.facebook.presto.common.type.VarbinaryType)9 ImmutableList (com.google.common.collect.ImmutableList)9 BigintType (com.facebook.presto.common.type.BigintType)8 BooleanType (com.facebook.presto.common.type.BooleanType)8