use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class TestIonSqlQueryBuilder method testDecimalColumns.
@Test
public void testDecimalColumns() {
TypeManager typeManager = TESTING_TYPE_MANAGER;
IonSqlQueryBuilder queryBuilder = new IonSqlQueryBuilder(typeManager);
List<HiveColumnHandle> columns = ImmutableList.of(createBaseColumn("quantity", 0, HiveType.valueOf("decimal(20,0)"), DecimalType.createDecimalType(), REGULAR, Optional.empty()), createBaseColumn("extendedprice", 1, HiveType.valueOf("decimal(20,2)"), DecimalType.createDecimalType(), REGULAR, Optional.empty()), createBaseColumn("discount", 2, HiveType.valueOf("decimal(10,2)"), DecimalType.createDecimalType(), 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), longDecimal("50"))), false), columns.get(1), Domain.create(ofRanges(Range.equal(HiveType.valueOf("decimal(20,2)").getType(typeManager), longDecimal("0.05"))), false), columns.get(2), Domain.create(ofRanges(Range.range(decimalType, shortDecimal("0.0"), true, 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));
}
use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class RcFileTester 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.equals(VARBINARY)) {
return javaByteArrayObjectInspector;
}
if (type.equals(DATE)) {
return javaDateObjectInspector;
}
if (type.equals(TIMESTAMP_MILLIS)) {
return javaTimestampObjectInspector;
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
}
if (type instanceof ArrayType) {
return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
}
if (type instanceof MapType) {
ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
}
if (type instanceof RowType) {
return getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName().get()).collect(toList()), type.getTypeParameters().stream().map(RcFileTester::getJavaObjectInspector).collect(toList()));
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class ClickHouseClient method toWriteMapping.
@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
if (type == BOOLEAN) {
// ClickHouse is no separate type for boolean values. Use UInt8 type, restricted to the values 0 or 1.
return WriteMapping.booleanMapping("UInt8", booleanWriteFunction());
}
if (type == TINYINT) {
return WriteMapping.longMapping("Int8", tinyintWriteFunction());
}
if (type == SMALLINT) {
return WriteMapping.longMapping("Int16", smallintWriteFunction());
}
if (type == INTEGER) {
return WriteMapping.longMapping("Int32", integerWriteFunction());
}
if (type == BIGINT) {
return WriteMapping.longMapping("Int64", bigintWriteFunction());
}
if (type == REAL) {
return WriteMapping.longMapping("Float32", realWriteFunction());
}
if (type == DOUBLE) {
return WriteMapping.doubleMapping("Float64", doubleWriteFunction());
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
String dataType = format("Decimal(%s, %s)", decimalType.getPrecision(), decimalType.getScale());
if (decimalType.isShort()) {
return WriteMapping.longMapping(dataType, shortDecimalWriteFunction(decimalType));
}
return WriteMapping.objectMapping(dataType, longDecimalWriteFunction(decimalType));
}
if (type instanceof CharType || type instanceof VarcharType) {
// The String type replaces the types VARCHAR, BLOB, CLOB, and others from other DBMSs.
return WriteMapping.sliceMapping("String", varcharWriteFunction());
}
if (type instanceof VarbinaryType) {
// Strings of an arbitrary length. The length is not limited
return WriteMapping.sliceMapping("String", varbinaryWriteFunction());
}
if (type == DATE) {
// TODO (https://github.com/trinodb/trino/issues/10055) Deny unsupported dates to prevent inserting wrong values. 2106-02-07 is max value in version 20.8
return WriteMapping.longMapping("Date", dateWriteFunctionUsingLocalDate());
}
if (type == TIMESTAMP_SECONDS) {
return WriteMapping.longMapping("DateTime", timestampSecondsWriteFunction());
}
if (type.equals(uuidType)) {
return WriteMapping.sliceMapping("UUID", uuidWriteFunction());
}
throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type);
}
use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class TypeCoercion method compatibility.
private TypeCompatibility compatibility(Type fromType, Type toType) {
if (fromType.equals(toType)) {
return TypeCompatibility.compatible(toType, true);
}
if (fromType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(toType, true);
}
if (toType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(fromType, false);
}
String fromTypeBaseName = fromType.getBaseName();
String toTypeBaseName = toType.getBaseName();
if (fromTypeBaseName.equals(toTypeBaseName)) {
if (fromTypeBaseName.equals(StandardTypes.DECIMAL)) {
Type commonSuperType = getCommonSuperTypeForDecimal((DecimalType) fromType, (DecimalType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.VARCHAR)) {
Type commonSuperType = getCommonSuperTypeForVarchar((VarcharType) fromType, (VarcharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.CHAR)) {
Type commonSuperType = getCommonSuperTypeForChar((CharType) fromType, (CharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.ROW)) {
return typeCompatibilityForRow((RowType) fromType, (RowType) toType);
}
if (fromTypeBaseName.equals(StandardTypes.TIMESTAMP)) {
Type commonSuperType = createTimestampType(Math.max(((TimestampType) fromType).getPrecision(), ((TimestampType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)) {
Type commonSuperType = createTimestampWithTimeZoneType(Math.max(((TimestampWithTimeZoneType) fromType).getPrecision(), ((TimestampWithTimeZoneType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIME)) {
Type commonSuperType = createTimeType(Math.max(((TimeType) fromType).getPrecision(), ((TimeType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIME_WITH_TIME_ZONE)) {
Type commonSuperType = createTimeWithTimeZoneType(Math.max(((TimeWithTimeZoneType) fromType).getPrecision(), ((TimeWithTimeZoneType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (isCovariantParametrizedType(fromType)) {
return typeCompatibilityForCovariantParametrizedType(fromType, toType);
}
return TypeCompatibility.incompatible();
}
Optional<Type> coercedType = coerceTypeBase(fromType, toType.getBaseName());
if (coercedType.isPresent()) {
return compatibility(coercedType.get(), toType);
}
coercedType = coerceTypeBase(toType, fromType.getBaseName());
if (coercedType.isPresent()) {
TypeCompatibility typeCompatibility = compatibility(fromType, coercedType.get());
if (!typeCompatibility.isCompatible()) {
return TypeCompatibility.incompatible();
}
return TypeCompatibility.compatible(typeCompatibility.getCommonSuperType(), false);
}
return TypeCompatibility.incompatible();
}
use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class LongDecimalColumnReader method readValue.
@Override
protected void readValue(BlockBuilder blockBuilder, Type trinoType) {
if (!(trinoType instanceof DecimalType)) {
throw new ParquetDecodingException(format("Unsupported Trino column type (%s) for Parquet column (%s)", trinoType, columnDescriptor));
}
DecimalType trinoDecimalType = (DecimalType) trinoType;
Binary binary = valuesReader.readBytes();
Int128 value = Int128.fromBigEndian(binary.getBytes());
if (trinoDecimalType.isShort()) {
trinoType.writeLong(blockBuilder, longToShortCast(value, parquetDecimalType.getPrecision(), parquetDecimalType.getScale(), trinoDecimalType.getPrecision(), trinoDecimalType.getScale()));
} else {
trinoType.writeObject(blockBuilder, longToLongCast(value, parquetDecimalType.getPrecision(), parquetDecimalType.getScale(), trinoDecimalType.getPrecision(), trinoDecimalType.getScale()));
}
}
Aggregations