Search in sources :

Example 31 with DecimalType

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);
}
Also used : Matcher(java.util.regex.Matcher) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint)

Example 32 with DecimalType

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);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) LongDecimalWithOverflowAndLongStateFactory(com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongStateFactory) LongDecimalWithOverflowAndLongState(com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongState) DecimalType(com.facebook.presto.common.type.DecimalType) Type(com.facebook.presto.common.type.Type) DecimalType(com.facebook.presto.common.type.DecimalType) LongDecimalWithOverflowAndLongStateSerializer(com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongStateSerializer) MethodHandle(java.lang.invoke.MethodHandle)

Example 33 with DecimalType

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);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) LongDecimalWithOverflowState(com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowState) DecimalType(com.facebook.presto.common.type.DecimalType) Type(com.facebook.presto.common.type.Type) LongDecimalWithOverflowStateFactory(com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowStateFactory) DecimalType(com.facebook.presto.common.type.DecimalType) LongDecimalWithOverflowStateSerializer(com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowStateSerializer) MethodHandle(java.lang.invoke.MethodHandle)

Example 34 with DecimalType

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();
}
Also used : DecimalType(com.facebook.presto.common.type.DecimalType) BigDecimal(java.math.BigDecimal) Decimals.writeBigDecimal(com.facebook.presto.common.type.Decimals.writeBigDecimal) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)

Example 35 with DecimalType

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);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) JavaHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveCharObjectInspector) PrimitiveObjectInspectorFactory.javaByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteObjectInspector) PrimitiveObjectInspectorFactory.javaLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaLongObjectInspector) PrimitiveObjectInspectorFactory.javaTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaTimestampObjectInspector) PrimitiveObjectInspectorFactory.javaDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDateObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector) PrimitiveObjectInspectorFactory.javaFloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaFloatObjectInspector) PrimitiveObjectInspectorFactory.javaDoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDoubleObjectInspector) PrimitiveObjectInspectorFactory.javaIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector) JavaHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveCharObjectInspector) PrimitiveObjectInspectorFactory.javaShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaShortObjectInspector) ObjectInspectorFactory.getStandardStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardStructObjectInspector) SettableStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) PrimitiveObjectInspectorFactory.javaBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaBooleanObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector) ObjectInspectorFactory.getStandardMapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardMapObjectInspector) ObjectInspectorFactory.getStandardListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardListObjectInspector) PrimitiveObjectInspectorFactory.javaStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType)

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