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));
}
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);
}
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));
}
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));
}
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));
}
Aggregations