Search in sources :

Example 16 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class HiveUtil method parsePartitionValue.

public static NullableValue parsePartitionValue(String partitionName, String value, Type type) {
    verifyPartitionTypeSupported(partitionName, type);
    boolean isNull = HivePartitionKey.HIVE_DEFAULT_DYNAMIC_PARTITION.equals(value);
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (isNull) {
            return NullableValue.asNull(decimalType);
        }
        if (decimalType.isShort()) {
            if (value.isEmpty()) {
                return NullableValue.of(decimalType, 0L);
            }
            return NullableValue.of(decimalType, shortDecimalPartitionKey(value, decimalType, partitionName));
        } else {
            if (value.isEmpty()) {
                return NullableValue.of(decimalType, Decimals.encodeUnscaledValue(BigInteger.ZERO));
            }
            return NullableValue.of(decimalType, longDecimalPartitionKey(value, decimalType, partitionName));
        }
    }
    if (BOOLEAN.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(BOOLEAN);
        }
        if (value.isEmpty()) {
            return NullableValue.of(BOOLEAN, false);
        }
        return NullableValue.of(BOOLEAN, booleanPartitionKey(value, partitionName));
    }
    if (TINYINT.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(TINYINT);
        }
        if (value.isEmpty()) {
            return NullableValue.of(TINYINT, 0L);
        }
        return NullableValue.of(TINYINT, tinyintPartitionKey(value, partitionName));
    }
    if (SMALLINT.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(SMALLINT);
        }
        if (value.isEmpty()) {
            return NullableValue.of(SMALLINT, 0L);
        }
        return NullableValue.of(SMALLINT, smallintPartitionKey(value, partitionName));
    }
    if (INTEGER.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(INTEGER);
        }
        if (value.isEmpty()) {
            return NullableValue.of(INTEGER, 0L);
        }
        return NullableValue.of(INTEGER, integerPartitionKey(value, partitionName));
    }
    if (BIGINT.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(BIGINT);
        }
        if (value.isEmpty()) {
            return NullableValue.of(BIGINT, 0L);
        }
        return NullableValue.of(BIGINT, bigintPartitionKey(value, partitionName));
    }
    if (DATE.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(DATE);
        }
        return NullableValue.of(DATE, datePartitionKey(value, partitionName));
    }
    if (TIMESTAMP.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(TIMESTAMP);
        }
        return NullableValue.of(TIMESTAMP, timestampPartitionKey(value, partitionName));
    }
    if (REAL.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(REAL);
        }
        if (value.isEmpty()) {
            return NullableValue.of(REAL, (long) floatToRawIntBits(0.0f));
        }
        return NullableValue.of(REAL, floatPartitionKey(value, partitionName));
    }
    if (DOUBLE.equals(type)) {
        if (isNull) {
            return NullableValue.asNull(DOUBLE);
        }
        if (value.isEmpty()) {
            return NullableValue.of(DOUBLE, 0.0);
        }
        return NullableValue.of(DOUBLE, doublePartitionKey(value, partitionName));
    }
    if (isVarcharType(type)) {
        if (isNull) {
            return NullableValue.asNull(type);
        }
        return NullableValue.of(type, varcharPartitionKey(value, partitionName, type));
    }
    if (isCharType(type)) {
        if (isNull) {
            return NullableValue.asNull(type);
        }
        return NullableValue.of(type, charPartitionKey(value, partitionName, type));
    }
    throw new VerifyException(format("Unhandled type [%s] for partition: %s", type, partitionName));
}
Also used : VerifyException(com.google.common.base.VerifyException) DecimalType(io.prestosql.spi.type.DecimalType) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType)

Example 17 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class HiveWriteUtils method getJavaObjectInspector.

public static ObjectInspector getJavaObjectInspector(Type type) {
    if (type.equals(BooleanType.BOOLEAN)) {
        return javaBooleanObjectInspector;
    }
    if (type.equals(BigintType.BIGINT)) {
        return javaLongObjectInspector;
    }
    if (type.equals(IntegerType.INTEGER)) {
        return javaIntObjectInspector;
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return javaShortObjectInspector;
    }
    if (type.equals(TinyintType.TINYINT)) {
        return javaByteObjectInspector;
    }
    if (type.equals(RealType.REAL)) {
        return javaFloatObjectInspector;
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return javaDoubleObjectInspector;
    }
    if (type instanceof VarcharType) {
        return writableStringObjectInspector;
    }
    if (type instanceof CharType) {
        return writableHiveCharObjectInspector;
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return javaByteArrayObjectInspector;
    }
    if (type.equals(DateType.DATE)) {
        return javaDateObjectInspector;
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return javaTimestampObjectInspector;
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (isArrayType(type)) {
        return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
    }
    if (isMapType(type)) {
        ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
        ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
        return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
    }
    if (isRowType(type)) {
        return ObjectInspectorFactory.getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName().get()).collect(toList()), type.getTypeParameters().stream().map(HiveWriteUtils::getJavaObjectInspector).collect(toList()));
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) 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) PrimitiveObjectInspectorFactory.writableTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableTimestampObjectInspector) 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) PrimitiveObjectInspectorFactory.writableFloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableFloatObjectInspector) PrimitiveObjectInspectorFactory.javaShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaShortObjectInspector) PrimitiveObjectInspectorFactory.writableBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableBooleanObjectInspector) PrimitiveObjectInspectorFactory.writableBinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableBinaryObjectInspector) PrimitiveObjectInspectorFactory.writableDoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDoubleObjectInspector) PrimitiveObjectInspectorFactory.writableLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableLongObjectInspector) PrimitiveObjectInspectorFactory.writableByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableByteObjectInspector) PrimitiveObjectInspectorFactory.writableDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDateObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector) PrimitiveObjectInspectorFactory.writableHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableHiveCharObjectInspector) PrimitiveObjectInspectorFactory.writableShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableShortObjectInspector) PrimitiveObjectInspectorFactory.javaBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaBooleanObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector) PrimitiveObjectInspectorFactory.writableStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableStringObjectInspector) PrimitiveObjectInspectorFactory.writableIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableIntObjectInspector) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) CharType(io.prestosql.spi.type.CharType)

Example 18 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class HiveCoercer method createCoercer.

static HiveCoercer createCoercer(TypeManager typeManager, HiveType fromHiveType, HiveType toHiveType) {
    Type fromType = typeManager.getType(fromHiveType.getTypeSignature());
    Type toType = typeManager.getType(toHiveType.getTypeSignature());
    if (toType instanceof VarcharType && fromType instanceof VarcharType) {
        return new VarcharToVarcharCoercer((VarcharType) fromType, (VarcharType) toType);
    }
    if (toType instanceof VarcharType && (fromHiveType.equals(HIVE_BYTE) || fromHiveType.equals(HIVE_SHORT) || fromHiveType.equals(HIVE_INT) || fromHiveType.equals(HIVE_LONG))) {
        return new IntegerNumberToVarcharCoercer<>(fromType, (VarcharType) toType);
    }
    if (fromType instanceof VarcharType && (toHiveType.equals(HIVE_BYTE) || toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG))) {
        return new VarcharToIntegerNumberCoercer<>((VarcharType) fromType, toType);
    }
    if (fromHiveType.equals(HIVE_BYTE) && toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer<>(fromType, toType);
    }
    if (fromHiveType.equals(HIVE_SHORT) && toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer<>(fromType, toType);
    }
    if (fromHiveType.equals(HIVE_INT) && toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer<>(fromType, toType);
    }
    if (fromHiveType.equals(HIVE_FLOAT) && toHiveType.equals(HIVE_DOUBLE)) {
        return new FloatToDoubleCoercer();
    }
    if (fromHiveType.equals(HIVE_DOUBLE) && toHiveType.equals(HIVE_FLOAT)) {
        return new DoubleToFloatCoercer();
    }
    if (fromType instanceof DecimalType && toType instanceof DecimalType) {
        return createDecimalToDecimalCoercer((DecimalType) fromType, (DecimalType) toType);
    }
    if (fromType instanceof DecimalType && toType == DOUBLE) {
        return createDecimalToDoubleCoercer((DecimalType) fromType);
    }
    if (fromType instanceof DecimalType && toType == REAL) {
        return createDecimalToRealCoercer((DecimalType) fromType);
    }
    if (fromType == DOUBLE && toType instanceof DecimalType) {
        return createDoubleToDecimalCoercer((DecimalType) toType);
    }
    if (fromType == REAL && toType instanceof DecimalType) {
        return createRealToDecimalCoercer((DecimalType) toType);
    }
    if (isArrayType(fromType) && isArrayType(toType)) {
        return new ListCoercer(typeManager, fromHiveType, toHiveType);
    }
    if (isMapType(fromType) && isMapType(toType)) {
        return new MapCoercer(typeManager, fromHiveType, toHiveType);
    }
    if (isRowType(fromType) && isRowType(toType)) {
        return new StructCoercer(typeManager, fromHiveType, toHiveType);
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported coercion from %s to %s", fromHiveType, toHiveType));
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) PrestoException(io.prestosql.spi.PrestoException) HiveUtil.isRowType(io.prestosql.plugin.hive.HiveUtil.isRowType) DecimalType(io.prestosql.spi.type.DecimalType) MapType(io.prestosql.spi.type.MapType) HiveUtil.isArrayType(io.prestosql.plugin.hive.HiveUtil.isArrayType) Type(io.prestosql.spi.type.Type) HiveType(io.prestosql.plugin.hive.HiveType) HiveUtil.isMapType(io.prestosql.plugin.hive.HiveUtil.isMapType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType)

Example 19 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class FormatFunction method valueConverter.

private static BiFunction<ConnectorSession, Block, Object> valueConverter(FunctionAndTypeManager functionAndTypeManager, Type type, int position) {
    if (type.equals(UNKNOWN)) {
        return (session, block) -> null;
    }
    if (type.equals(BOOLEAN)) {
        return (session, block) -> type.getBoolean(block, position);
    }
    if (type.equals(TINYINT) || type.equals(SMALLINT) || type.equals(INTEGER) || type.equals(BIGINT)) {
        return (session, block) -> type.getLong(block, position);
    }
    if (type.equals(REAL)) {
        return (session, block) -> intBitsToFloat(toIntExact(type.getLong(block, position)));
    }
    if (type.equals(DOUBLE)) {
        return (session, block) -> type.getDouble(block, position);
    }
    if (type.equals(DATE)) {
        return (session, block) -> LocalDate.ofEpochDay(type.getLong(block, position));
    }
    if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) {
        return (session, block) -> toZonedDateTime(type.getLong(block, position));
    }
    if (type.equals(TIMESTAMP)) {
        return (session, block) -> toLocalDateTime(type.getLong(block, position));
    }
    if (type.equals(TIME)) {
        return (session, block) -> toLocalTime(session, type.getLong(block, position));
    }
    // TODO: support TIME WITH TIME ZONE by making SqlTimeWithTimeZone implement TemporalAccessor
    if (type.equals(JSON)) {
        FunctionHandle functionHandle = functionAndTypeManager.resolveFunction(Optional.empty(), QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "json_format"), fromTypes(JSON));
        MethodHandle handle = functionAndTypeManager.getBuiltInScalarFunctionImplementation(functionHandle).getMethodHandle();
        return (session, block) -> convertToString(handle, type.getSlice(block, position));
    }
    if (isShortDecimal(type)) {
        int scale = ((DecimalType) type).getScale();
        return (session, block) -> BigDecimal.valueOf(type.getLong(block, position), scale);
    }
    if (isLongDecimal(type)) {
        int scale = ((DecimalType) type).getScale();
        return (session, block) -> new BigDecimal(decodeUnscaledValue(type.getSlice(block, position)), scale);
    }
    if (isVarcharType(type) || isCharType(type)) {
        return (session, block) -> type.getSlice(block, position).toStringUtf8();
    }
    BiFunction<ConnectorSession, Block, Object> function;
    if (type.getJavaType() == long.class) {
        function = (session, block) -> type.getLong(block, position);
    } else if (type.getJavaType() == double.class) {
        function = (session, block) -> type.getDouble(block, position);
    } else if (type.getJavaType() == boolean.class) {
        function = (session, block) -> type.getBoolean(block, position);
    } else if (type.getJavaType() == Slice.class) {
        function = (session, block) -> type.getSlice(block, position);
    } else {
        function = (session, block) -> type.getObject(block, position);
    }
    MethodHandle handle = castToVarchar(functionAndTypeManager, type);
    if ((handle == null) || (handle.type().parameterCount() != 1)) {
        throw new PrestoException(NOT_SUPPORTED, "Type not supported for formatting: " + type.getDisplayName());
    }
    return (session, block) -> convertToString(handle, function.apply(session, block));
}
Also used : DateTimeEncoding.unpackZoneKey(io.prestosql.spi.type.DateTimeEncoding.unpackZoneKey) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) SCALAR(io.prestosql.spi.function.FunctionKind.SCALAR) OperatorNotFoundException(io.prestosql.metadata.OperatorNotFoundException) INVALID_FUNCTION_ARGUMENT(io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) DecimalType(io.prestosql.spi.type.DecimalType) BoundVariables(io.prestosql.metadata.BoundVariables) DEFAULT_NAMESPACE(io.prestosql.spi.connector.CatalogSchemaName.DEFAULT_NAMESPACE) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) BigDecimal(java.math.BigDecimal) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) LocalTime(java.time.LocalTime) IllegalFormatException(java.util.IllegalFormatException) BOOLEAN(io.prestosql.spi.type.BooleanType.BOOLEAN) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) JSON(io.prestosql.type.JsonType.JSON) Type(io.prestosql.spi.type.Type) ZoneOffset(java.time.ZoneOffset) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) PrestoException(io.prestosql.spi.PrestoException) DateTimeEncoding.unpackMillisUtc(io.prestosql.spi.type.DateTimeEncoding.unpackMillisUtc) CastType(io.prestosql.metadata.CastType) SqlScalarFunction(io.prestosql.metadata.SqlScalarFunction) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TIME(io.prestosql.spi.type.TimeType.TIME) Reflection.methodHandle(io.prestosql.spi.util.Reflection.methodHandle) Decimals.isLongDecimal(io.prestosql.spi.type.Decimals.isLongDecimal) TIMESTAMP(io.prestosql.spi.type.TimestampType.TIMESTAMP) TINYINT(io.prestosql.spi.type.TinyintType.TINYINT) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Instant(java.time.Instant) ZoneId(java.time.ZoneId) String.format(java.lang.String.format) FunctionHandle(io.prestosql.spi.function.FunctionHandle) Decimals.isShortDecimal(io.prestosql.spi.type.Decimals.isShortDecimal) List(java.util.List) LocalDate(java.time.LocalDate) ArgumentProperty.valueTypeArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty) Optional(java.util.Optional) NOT_SUPPORTED(io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED) TIMESTAMP_WITH_TIME_ZONE(io.prestosql.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) Signature.withVariadicBound(io.prestosql.spi.function.Signature.withVariadicBound) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) UNKNOWN(io.prestosql.spi.type.UnknownType.UNKNOWN) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) MethodHandle(java.lang.invoke.MethodHandle) Slice(io.airlift.slice.Slice) LocalDateTime(java.time.LocalDateTime) INTEGER(io.prestosql.spi.type.IntegerType.INTEGER) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) VARCHAR(io.prestosql.spi.type.VarcharType.VARCHAR) Failures.internalError(io.prestosql.util.Failures.internalError) ImmutableList(com.google.common.collect.ImmutableList) RETURN_NULL_ON_NULL(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.NullConvention.RETURN_NULL_ON_NULL) DOUBLE(io.prestosql.spi.type.DoubleType.DOUBLE) DATE(io.prestosql.spi.type.DateType.DATE) REAL(io.prestosql.spi.type.RealType.REAL) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.prestosql.spi.function.Signature) Block(io.prestosql.spi.block.Block) Streams.mapWithIndex(com.google.common.collect.Streams.mapWithIndex) TypeSignatureProvider.fromTypes(io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypes) Decimals.decodeUnscaledValue(io.prestosql.spi.type.Decimals.decodeUnscaledValue) SMALLINT(io.prestosql.spi.type.SmallintType.SMALLINT) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) DecimalType(io.prestosql.spi.type.DecimalType) Block(io.prestosql.spi.block.Block) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) PrestoException(io.prestosql.spi.PrestoException) FunctionHandle(io.prestosql.spi.function.FunctionHandle) BigDecimal(java.math.BigDecimal) MethodHandle(java.lang.invoke.MethodHandle)

Example 20 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class TestIonSqlQueryBuilder method testDecimalColumns.

@Test
public void testDecimalColumns() {
    TypeManager manager = this.typeManager;
    IonSqlQueryBuilder queryBuilder = new IonSqlQueryBuilder(manager);
    List<HiveColumnHandle> columns = ImmutableList.of(new HiveColumnHandle("quantity", HiveType.valueOf("decimal(20,0)"), parseTypeSignature(DECIMAL), 0, REGULAR, Optional.empty()), new HiveColumnHandle("extendedprice", HiveType.valueOf("decimal(20,2)"), parseTypeSignature(DECIMAL), 1, REGULAR, Optional.empty()), new HiveColumnHandle("discount", HiveType.valueOf("decimal(10,2)"), parseTypeSignature(DECIMAL), 2, REGULAR, Optional.empty()));
    DecimalType decimalType = DecimalType.createDecimalType(10, 2);
    TupleDomain<HiveColumnHandle> tupleDomain = withColumnDomains(ImmutableMap.of(columns.get(0), Domain.create(ofRanges(Range.lessThan(DecimalType.createDecimalType(20, 0), HiveTestUtils.longDecimal("50"))), false), columns.get(1), Domain.create(ofRanges(Range.equal(HiveType.valueOf("decimal(20,2)").getType(manager), HiveTestUtils.longDecimal("0.05"))), false), columns.get(2), Domain.create(ofRanges(Range.range(decimalType, HiveTestUtils.shortDecimal("0.0"), true, HiveTestUtils.shortDecimal("0.02"), true)), false)));
    assertEquals("SELECT s._1, s._2, s._3 FROM S3Object s WHERE ((case s._1 when '' then null else CAST(s._1 AS DECIMAL(20,0)) end < 50)) AND " + "(case s._2 when '' then null else CAST(s._2 AS DECIMAL(20,2)) end = 0.05) AND ((case s._3 when '' then null else CAST(s._3 AS DECIMAL(10,2)) " + "end >= 0.00 AND case s._3 when '' then null else CAST(s._3 AS DECIMAL(10,2)) end <= 0.02))", queryBuilder.buildSql(columns, tupleDomain));
}
Also used : InternalTypeManager(io.prestosql.type.InternalTypeManager) TypeManager(io.prestosql.spi.type.TypeManager) DecimalType(io.prestosql.spi.type.DecimalType) Test(org.testng.annotations.Test)

Aggregations

DecimalType (io.prestosql.spi.type.DecimalType)90 VarcharType (io.prestosql.spi.type.VarcharType)58 CharType (io.prestosql.spi.type.CharType)45 Type (io.prestosql.spi.type.Type)43 PrestoException (io.prestosql.spi.PrestoException)30 Slice (io.airlift.slice.Slice)22 BigDecimal (java.math.BigDecimal)21 BigInteger (java.math.BigInteger)18 Block (io.prestosql.spi.block.Block)15 ArrayType (io.prestosql.spi.type.ArrayType)15 TimestampType (io.prestosql.spi.type.TimestampType)15 VarbinaryType (io.prestosql.spi.type.VarbinaryType)15 ArrayList (java.util.ArrayList)14 DateType (io.prestosql.spi.type.DateType)13 DoubleType (io.prestosql.spi.type.DoubleType)13 RealType (io.prestosql.spi.type.RealType)13 RowType (io.prestosql.spi.type.RowType)12 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)11 BigintType (io.prestosql.spi.type.BigintType)11 BooleanType (io.prestosql.spi.type.BooleanType)11