@VisibleForTesting static double convertPartitionValueToDouble(Type type, Object value) { if (type.equals(BIGINT) || type.equals(INTEGER) || type.equals(SMALLINT) || type.equals(TINYINT)) { return (Long) value; } if (type.equals(DOUBLE)) { return (Double) value; } if (type.equals(REAL)) { return intBitsToFloat(((Long) value).intValue()); } if (type instanceof DecimalType) { DecimalType decimalType = (DecimalType) type; if (isShortDecimal(decimalType)) { return parseDouble(Decimals.toString((Long) value, decimalType.getScale())); } if (isLongDecimal(decimalType)) { return parseDouble(Decimals.toString((Slice) value, decimalType.getScale())); } throw new IllegalArgumentException("Unexpected decimal type: " + decimalType); } if (type.equals(DATE)) { return (Long) value; } throw new IllegalArgumentException("Unexpected type: " + type); }
private static double toDouble(Object value, Type type) { if (value instanceof String && type.equals(DATE)) { return LocalDate.parse((CharSequence) value).toEpochDay(); } if (type.equals(BIGINT) || type.equals(INTEGER) || type.equals(DATE)) { return ((Number) value).doubleValue(); } if (type instanceof DecimalType) { DecimalType decimalType = (DecimalType) type; if (isShortDecimal(decimalType)) { return parseDouble(Decimals.toString(((Number) value).longValue(), decimalType.getScale())); } if (isLongDecimal(decimalType)) { return parseDouble(Decimals.toString((Slice) value, decimalType.getScale())); } throw new IllegalArgumentException("Unexpected decimal type: " + decimalType); } if (type.equals(DOUBLE)) { return ((Number) value).doubleValue(); } throw new IllegalArgumentException("unsupported column type " + type); } }
@Override public void encodeValueInto(Block block, int position, SliceOutput output) { if (isShortDecimal(type)) { writeLong(output, type.getLong(block, position)); } else { writeSlice(output, block, position); } }
@Override public void decodeValueInto(BlockBuilder builder, Slice slice, int offset, int length) { if (isShortDecimal(type)) { type.writeLong(builder, parseLong(slice, offset)); } else { type.writeSlice(builder, parseSlice(slice, offset)); } }
@Override public void decodeValueInto(int depth, BlockBuilder builder, Slice slice, int offset, int length) { if (isShortDecimal(type)) { type.writeLong(builder, parseLong(slice, offset, length)); } else { type.writeSlice(builder, parseSlice(slice, offset, length)); } }
prefilledValue = timestampPartitionKey(columnValue, hiveStorageTimeZone, name); else if (isShortDecimal(type)) { prefilledValue = shortDecimalPartitionKey(columnValue, (DecimalType) type, name);
case StandardTypes.DECIMAL: DecimalType decimalType = (DecimalType) type; if (isShortDecimal(decimalType)) { return (block, position) -> Decimals.toString(decimalType.getLong(block, position), decimalType.getScale());
private void encodeValue(Block block, int position, SliceOutput output) { if (isShortDecimal(type)) { output.writeBytes(utf8Slice(Decimals.toString(type.getLong(block, position), type.getScale()))); } else { output.writeBytes(utf8Slice(Decimals.toString(type.getSlice(block, position), type.getScale()))); } }
if (isShortDecimal(type)) { string = Decimals.toString((long) object, ((DecimalType) type).getScale());
blocks[i] = BlockAssertions.createTimestampSequenceBlock(initialValue, initialValue + length); else if (isShortDecimal(type)) { blocks[i] = BlockAssertions.createShortDecimalSequenceBlock(initialValue, initialValue + length, (DecimalType) type);
return new DoubleJsonGeneratorWriter(); case StandardTypes.DECIMAL: if (isShortDecimal(type)) { return new ShortDecimalJsonGeneratorWriter((DecimalType) type);
@Override public Block decodeColumn(ColumnData columnData) { int size = columnData.rowCount(); BlockBuilder builder = type.createBlockBuilder(null, size); Slice slice = columnData.getSlice(); for (int i = 0; i < size; i++) { int offset = columnData.getOffset(i); int length = columnData.getLength(i); if (length == 0) { builder.appendNull(); } else if (isShortDecimal(type)) { type.writeLong(builder, parseLong(slice, offset)); } else { type.writeSlice(builder, parseSlice(slice, offset)); } } return builder.build(); }
private static SqlScalarFunction castFunctionFromDecimalTo(TypeSignature to, String... methodNames) { Signature signature = Signature.builder() .kind(SCALAR) .operatorType(CAST) .argumentTypes(parseTypeSignature("decimal(precision,scale)", ImmutableSet.of("precision", "scale"))) .returnType(to) .build(); return SqlScalarFunction.builder(DecimalCasts.class) .signature(signature) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods(methodNames) .withExtraParameters((context) -> { long precision = context.getLiteral("precision"); long scale = context.getLiteral("scale"); Number tenToScale; if (isShortDecimal(context.getParameterTypes().get(0))) { tenToScale = longTenToNth(intScale(scale)); } else { tenToScale = bigIntegerTenToNth(intScale(scale)); } return ImmutableList.of(precision, scale, tenToScale); }))) .build(); }
longs[columnIndex] = timestampPartitionKey(columnValue, hiveStorageTimeZone, name); else if (isShortDecimal(type)) { longs[columnIndex] = shortDecimalPartitionKey(columnValue, (DecimalType) type, name);
@Override public Block decodeColumn(ColumnData columnData) { int size = columnData.rowCount(); BlockBuilder builder = type.createBlockBuilder(null, size); Slice slice = columnData.getSlice(); for (int i = 0; i < size; i++) { int offset = columnData.getOffset(i); int length = columnData.getLength(i); if (length == 0 || nullSequence.equals(0, nullSequence.length(), slice, offset, length)) { builder.appendNull(); } else if (isShortDecimal(type)) { type.writeLong(builder, parseLong(slice, offset, length)); } else { type.writeSlice(builder, parseSlice(slice, offset, length)); } } return builder.build(); }
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 DoubleBlockBuilderAppender(); case StandardTypes.DECIMAL: if (isShortDecimal(type)) { return new ShortDecimalBlockBuilderAppender((DecimalType) type);
type.writeLong(blockBuilder, ((Number) value).longValue()); else if (Decimals.isShortDecimal(type)) { type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue());
else if (isShortDecimal(type) && columnStatistics.getDecimalStatistics() != null) { return createDomain(type, hasNullValue, columnStatistics.getDecimalStatistics(), value -> rescale(value, (DecimalType) type).unscaledValue().longValue());
type.writeLong(blockBuilder, ((Number) value).longValue()); else if (Decimals.isShortDecimal(type)) { type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue());