public static BigDecimal rescale(BigDecimal value, DecimalType type) { value = value.setScale(type.getScale(), UNNECESSARY); if (value.precision() > type.getPrecision()) { throw new IllegalArgumentException("decimal precision larger than column precision"); } return value; }
private BigDecimal parseBigDecimal(Slice slice, int offset, int length) { checkArgument(length < buffer.length); for (int i = 0; i < length; i++) { buffer[i] = (char) slice.getByte(offset + i); } BigDecimal decimal = new BigDecimal(buffer, 0, length); checkState(decimal.scale() <= type.getScale(), "Read decimal value scale larger than column scale"); decimal = decimal.setScale(type.getScale(), HALF_UP); checkState(decimal.precision() <= type.getPrecision(), "Read decimal precision larger than column precision"); return decimal; } }
private static Optional<PrimitiveColumnReader> createDecimalColumnReader(RichColumnDescriptor descriptor) { Optional<Type> type = createDecimalType(descriptor); if (type.isPresent()) { DecimalType decimalType = (DecimalType) type.get(); return Optional.of(DecimalColumnReaderFactory.createReader(descriptor, decimalType.getPrecision(), decimalType.getScale())); } return Optional.empty(); }
private static Type getCommonSuperTypeForDecimal(DecimalType firstType, DecimalType secondType) { int targetScale = Math.max(firstType.getScale(), secondType.getScale()); int targetPrecision = Math.max(firstType.getPrecision() - firstType.getScale(), secondType.getPrecision() - secondType.getScale()) + targetScale; //we allow potential loss of precision here. Overflow checking is done in operators. targetPrecision = Math.min(38, targetPrecision); return createDecimalType(targetPrecision, targetScale); }
private static BigDecimal decimalPartitionKey(String value, DecimalType type, String name) { try { if (value.endsWith(BIG_DECIMAL_POSTFIX)) { value = value.substring(0, value.length() - BIG_DECIMAL_POSTFIX.length()); } BigDecimal decimal = new BigDecimal(value); decimal = decimal.setScale(type.getScale(), ROUND_UNNECESSARY); if (decimal.precision() > type.getPrecision()) { throw new PrestoException(HIVE_INVALID_PARTITION_VALUE, format("Invalid partition value '%s' for %s partition key: %s", value, type.toString(), name)); } return decimal; } catch (NumberFormatException e) { throw new PrestoException(HIVE_INVALID_PARTITION_VALUE, format("Invalid partition value '%s' for %s partition key: %s", value, type.toString(), name)); } }
private String createColumn(Type type, int position) { String column = format("s._%d", position + 1); if (type.equals(BIGINT) || type.equals(INTEGER) || type.equals(SMALLINT) || type.equals(TINYINT)) { return formatPredicate(column, "INT"); } if (type.equals(BOOLEAN)) { return formatPredicate(column, "BOOL"); } if (type.equals(DATE)) { return formatPredicate(column, "TIMESTAMP"); } if (type instanceof DecimalType) { DecimalType decimalType = (DecimalType) type; return formatPredicate(column, format("DECIMAL(%s,%s)", decimalType.getPrecision(), decimalType.getScale())); } return column; }
public static BigDecimal readBigDecimal(DecimalType type, Block block, int position) { BigInteger unscaledValue = type.isShort() ? BigInteger.valueOf(type.getLong(block, position)) : decodeUnscaledValue(type.getSlice(block, position)); return new BigDecimal(unscaledValue, type.getScale(), new MathContext(type.getPrecision())); }
return ((DecimalType) type).getPrecision();
private SqlDecimal randomDecimal(DecimalType type) { int maxBits = (int) (Math.log(Math.pow(10, type.getPrecision())) / Math.log(2)); BigInteger bigInteger = new BigInteger(maxBits, random); if (bigInteger.equals(ZERO)) { bigInteger = ONE; } if (random.nextBoolean()) { bigInteger = bigInteger.negate(); } return new SqlDecimal(bigInteger, type.getPrecision(), type.getScale()); } }
@Override public void append(JsonParser parser, BlockBuilder blockBuilder) throws IOException { Long result = currentTokenAsShortDecimal(parser, type.getPrecision(), type.getScale()); if (result == null) { blockBuilder.appendNull(); } else { type.writeLong(blockBuilder, result); } } }
@Override public void append(JsonParser parser, BlockBuilder blockBuilder) throws IOException { Slice result = currentTokenAsLongDecimal(parser, type.getPrecision(), type.getScale()); if (result == null) { blockBuilder.appendNull(); } else { type.writeSlice(blockBuilder, result); } } }
|| (!sourceDecimal.isShort() && !resultDecimal.isShort()); boolean sameScale = sourceDecimal.getScale() == resultDecimal.getScale(); boolean sourcePrecisionIsLessOrEqualToResultPrecision = sourceDecimal.getPrecision() <= resultDecimal.getPrecision(); return sameDecimalSubtype && sameScale && sourcePrecisionIsLessOrEqualToResultPrecision;
return getPrimitiveWritableObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
protected String toSqlType(Type type) { if (isVarcharType(type)) { VarcharType varcharType = (VarcharType) type; if (varcharType.isUnbounded()) { return "varchar"; } return "varchar(" + varcharType.getLengthSafe() + ")"; } if (type instanceof CharType) { if (((CharType) type).getLength() == CharType.MAX_LENGTH) { return "char"; } return "char(" + ((CharType) type).getLength() + ")"; } if (type instanceof DecimalType) { return format("decimal(%s, %s)", ((DecimalType) type).getPrecision(), ((DecimalType) type).getScale()); } String sqlType = SQL_TYPES.get(type); if (sqlType != null) { return sqlType; } throw new PrestoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName()); }
private void setTypeAttributes(ColumnMetadata columnMetadata, ColumnSchema.ColumnSchemaBuilder builder) { if (columnMetadata.getType() instanceof DecimalType) { DecimalType type = (DecimalType) columnMetadata.getType(); ColumnTypeAttributes attributes = new ColumnTypeAttributes.ColumnTypeAttributesBuilder() .precision(type.getPrecision()) .scale(type.getScale()).build(); builder.typeAttributes(attributes); } }
return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
protected static SqlDecimal decimal(String decimalString) { DecimalParseResult parseResult = Decimals.parseIncludeLeadingZerosInPrecision(decimalString); BigInteger unscaledValue; if (parseResult.getType().isShort()) { unscaledValue = BigInteger.valueOf((Long) parseResult.getObject()); } else { unscaledValue = Decimals.decodeUnscaledValue((Slice) parseResult.getObject()); } return new SqlDecimal(unscaledValue, parseResult.getType().getPrecision(), parseResult.getType().getScale()); }
private static SqlScalarFunction castFunctionToDecimalFromBuilder(TypeSignature from, boolean nullableResult, String... methodNames) { Signature signature = Signature.builder() .kind(SCALAR) .operatorType(CAST) .argumentTypes(from) .returnType(parseTypeSignature("decimal(precision,scale)", ImmutableSet.of("precision", "scale"))) .build(); return SqlScalarFunction.builder(DecimalCasts.class) .signature(signature) .deterministic(true) .choice(choice -> choice .nullableResult(nullableResult) .implementation(methodsGroup -> methodsGroup .methods(methodNames) .withExtraParameters((context) -> { DecimalType resultType = (DecimalType) context.getReturnType(); Number tenToScale; if (isShortDecimal(resultType)) { tenToScale = longTenToNth(resultType.getScale()); } else { tenToScale = bigIntegerTenToNth(resultType.getScale()); } return ImmutableList.of(resultType.getPrecision(), resultType.getScale(), tenToScale); }))).build(); }
return new SqlDecimal((BigInteger) fieldFromCursor, decimalType.getPrecision(), decimalType.getScale());