use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class PartitionTransforms method getColumnTransform.
public static ColumnTransform getColumnTransform(PartitionField field, Type type) {
String transform = field.transform().toString();
if (transform.equals("identity")) {
return new ColumnTransform(type, Function.identity());
}
Matcher matcher = BUCKET_PATTERN.matcher(transform);
if (matcher.matches()) {
int count = parseInt(matcher.group(1));
if (type.equals(INTEGER)) {
return new ColumnTransform(INTEGER, block -> bucketInteger(block, count));
}
if (type.equals(BIGINT)) {
return new ColumnTransform(INTEGER, block -> bucketBigint(block, count));
}
if (isShortDecimal(type)) {
DecimalType decimal = (DecimalType) type;
return new ColumnTransform(INTEGER, block -> bucketShortDecimal(decimal, block, count));
}
if (isLongDecimal(type)) {
DecimalType decimal = (DecimalType) type;
return new ColumnTransform(INTEGER, block -> bucketLongDecimal(decimal, block, count));
}
if (type.equals(DATE)) {
return new ColumnTransform(INTEGER, block -> bucketDate(block, count));
}
if (type instanceof VarcharType) {
return new ColumnTransform(INTEGER, block -> bucketVarchar(block, count));
}
if (type.equals(VARBINARY)) {
return new ColumnTransform(INTEGER, block -> bucketVarbinary(block, count));
}
throw new UnsupportedOperationException("Unsupported type for 'bucket': " + field);
}
matcher = TRUNCATE_PATTERN.matcher(transform);
if (matcher.matches()) {
int width = parseInt(matcher.group(1));
if (type.equals(INTEGER)) {
return new ColumnTransform(INTEGER, block -> truncateInteger(block, width));
}
if (type.equals(BIGINT)) {
return new ColumnTransform(BIGINT, block -> truncateBigint(block, width));
}
if (isShortDecimal(type)) {
DecimalType decimal = (DecimalType) type;
return new ColumnTransform(type, block -> truncateShortDecimal(decimal, block, width));
}
if (isLongDecimal(type)) {
DecimalType decimal = (DecimalType) type;
return new ColumnTransform(type, block -> truncateLongDecimal(decimal, block, width));
}
if (type instanceof VarcharType) {
return new ColumnTransform(VARCHAR, block -> truncateVarchar(block, width));
}
if (type.equals(VARBINARY)) {
return new ColumnTransform(VARBINARY, block -> truncateVarbinary(block, width));
}
throw new UnsupportedOperationException("Unsupported type for 'truncate': " + field);
}
throw new UnsupportedOperationException("Unsupported partition transform: " + field);
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class DecimalAverageAggregation method generateAggregation.
private static InternalAggregationFunction generateAggregation(Type type) {
checkArgument(type instanceof DecimalType, "type must be Decimal");
DynamicClassLoader classLoader = new DynamicClassLoader(DecimalAverageAggregation.class.getClassLoader());
List<Type> inputTypes = ImmutableList.of(type);
MethodHandle inputFunction;
MethodHandle outputFunction;
Class<? extends AccumulatorState> stateInterface = LongDecimalWithOverflowAndLongState.class;
AccumulatorStateSerializer<?> stateSerializer = new LongDecimalWithOverflowAndLongStateSerializer();
if (((DecimalType) type).isShort()) {
inputFunction = SHORT_DECIMAL_INPUT_FUNCTION;
outputFunction = SHORT_DECIMAL_OUTPUT_FUNCTION;
} else {
inputFunction = LONG_DECIMAL_INPUT_FUNCTION;
outputFunction = LONG_DECIMAL_OUTPUT_FUNCTION;
}
outputFunction = outputFunction.bindTo(type);
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(type), inputFunction, COMBINE_FUNCTION, outputFunction, ImmutableList.of(new AccumulatorStateDescriptor(stateInterface, stateSerializer, new LongDecimalWithOverflowAndLongStateFactory())), type);
Type intermediateType = stateSerializer.getSerializedType();
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), type, true, false, factory);
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class DecimalSumAggregation method generateAggregation.
private static InternalAggregationFunction generateAggregation(Type inputType, Type outputType) {
checkArgument(inputType instanceof DecimalType, "type must be Decimal");
DynamicClassLoader classLoader = new DynamicClassLoader(DecimalSumAggregation.class.getClassLoader());
List<Type> inputTypes = ImmutableList.of(inputType);
MethodHandle inputFunction;
Class<? extends AccumulatorState> stateInterface = LongDecimalWithOverflowState.class;
AccumulatorStateSerializer<?> stateSerializer = new LongDecimalWithOverflowStateSerializer();
if (((DecimalType) inputType).isShort()) {
inputFunction = SHORT_DECIMAL_INPUT_FUNCTION;
} else {
inputFunction = LONG_DECIMAL_INPUT_FUNCTION;
}
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(inputType), inputFunction, COMBINE_FUNCTION, LONG_DECIMAL_OUTPUT_FUNCTION, ImmutableList.of(new AccumulatorStateDescriptor(stateInterface, stateSerializer, new LongDecimalWithOverflowStateFactory())), outputType);
Type intermediateType = stateSerializer.getSerializedType();
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, false, factory);
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class BlockAssertions method createLongDecimalsBlock.
public static Block createLongDecimalsBlock(Iterable<String> values) {
DecimalType longDecimalType = DecimalType.createDecimalType(MAX_SHORT_PRECISION + 1);
BlockBuilder builder = longDecimalType.createBlockBuilder(null, 100);
for (String value : values) {
if (value == null) {
builder.appendNull();
} else {
writeBigDecimal(longDecimalType, builder, new BigDecimal(value));
}
}
return builder.build();
}
use of com.facebook.presto.common.type.DecimalType in project presto by prestodb.
the class OrcTester method getJavaObjectInspector.
private static ObjectInspector getJavaObjectInspector(Type type) {
if (type.equals(BOOLEAN)) {
return javaBooleanObjectInspector;
}
if (type.equals(BIGINT)) {
return javaLongObjectInspector;
}
if (type.equals(INTEGER)) {
return javaIntObjectInspector;
}
if (type.equals(SMALLINT)) {
return javaShortObjectInspector;
}
if (type.equals(TINYINT)) {
return javaByteObjectInspector;
}
if (type.equals(REAL)) {
return javaFloatObjectInspector;
}
if (type.equals(DOUBLE)) {
return javaDoubleObjectInspector;
}
if (type instanceof VarcharType) {
return javaStringObjectInspector;
}
if (type instanceof CharType) {
int charLength = ((CharType) type).getLength();
return new JavaHiveCharObjectInspector(getCharTypeInfo(charLength));
}
if (type instanceof VarbinaryType) {
return javaByteArrayObjectInspector;
}
if (type.equals(DATE)) {
return javaDateObjectInspector;
}
if (type.equals(TIMESTAMP)) {
return javaTimestampObjectInspector;
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
}
if (type.getTypeSignature().getBase().equals(StandardTypes.ARRAY)) {
return getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
}
if (type.getTypeSignature().getBase().equals(StandardTypes.MAP)) {
ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
return getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
}
if (type.getTypeSignature().getBase().equals(StandardTypes.ROW)) {
return getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName().get()).collect(toList()), type.getTypeParameters().stream().map(OrcTester::getJavaObjectInspector).collect(toList()));
}
throw new IllegalArgumentException("unsupported type: " + type);
}
Aggregations