Search in sources :

Example 46 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class TypeUtils method trinoNativeToJdbcObject.

private static Object trinoNativeToJdbcObject(ConnectorSession session, Type type, Object object) {
    if (object == null) {
        return null;
    }
    if (DOUBLE.equals(type) || BOOLEAN.equals(type) || BIGINT.equals(type)) {
        return object;
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            BigInteger unscaledValue = BigInteger.valueOf((long) object);
            return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
        }
        BigInteger unscaledValue = ((Int128) object).toBigInteger();
        return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
    }
    if (REAL.equals(type)) {
        return intBitsToFloat(toIntExact((long) object));
    }
    if (TINYINT.equals(type)) {
        return SignedBytes.checkedCast((long) object);
    }
    if (SMALLINT.equals(type)) {
        return Shorts.checkedCast((long) object);
    }
    if (INTEGER.equals(type)) {
        return toIntExact((long) object);
    }
    if (DATE.equals(type)) {
        // convert to midnight in default time zone
        long millis = DAYS.toMillis((long) object);
        return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis));
    }
    if (type instanceof VarcharType || type instanceof CharType) {
        return ((Slice) object).toStringUtf8();
    }
    if (type instanceof ArrayType) {
        // process subarray of multi-dimensional array
        return getJdbcObjectArray(session, ((ArrayType) type).getElementType(), (Block) object);
    }
    throw new TrinoException(NOT_SUPPORTED, "Unsupported type: " + type);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) VarcharType(io.trino.spi.type.VarcharType) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalType(io.trino.spi.type.DecimalType) BigInteger(java.math.BigInteger) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType) Int128(io.trino.spi.type.Int128) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Date(java.sql.Date)

Example 47 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class PhoenixClient method toWriteMapping.

@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
    if (type == BOOLEAN) {
        return WriteMapping.booleanMapping("boolean", booleanWriteFunction());
    }
    if (type == TINYINT) {
        return WriteMapping.longMapping("tinyint", tinyintWriteFunction());
    }
    if (type == SMALLINT) {
        return WriteMapping.longMapping("smallint", smallintWriteFunction());
    }
    if (type == INTEGER) {
        return WriteMapping.longMapping("integer", integerWriteFunction());
    }
    if (type == BIGINT) {
        return WriteMapping.longMapping("bigint", bigintWriteFunction());
    }
    if (type == REAL) {
        return WriteMapping.longMapping("float", realWriteFunction());
    }
    if (type == DOUBLE) {
        return WriteMapping.doubleMapping("double", 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) {
        return WriteMapping.sliceMapping("char(" + ((CharType) type).getLength() + ")", charWriteFunction());
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        String dataType;
        if (varcharType.isUnbounded()) {
            dataType = "varchar";
        } else {
            dataType = "varchar(" + varcharType.getBoundedLength() + ")";
        }
        return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
    }
    if (type instanceof VarbinaryType) {
        return WriteMapping.sliceMapping("varbinary", varbinaryWriteFunction());
    }
    if (type == DATE) {
        return WriteMapping.longMapping("date", dateWriteFunctionUsingString());
    }
    if (TIME.equals(type)) {
        return WriteMapping.longMapping("time", timeWriteFunctionUsingSqlTime());
    }
    // Phoenix doesn't support _WITH_TIME_ZONE
    if (TIME_WITH_TIME_ZONE.equals(type) || TIMESTAMP_TZ_MILLIS.equals(type)) {
        throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
    }
    if (type instanceof ArrayType) {
        Type elementType = ((ArrayType) type).getElementType();
        String elementDataType = toWriteMapping(session, elementType).getDataType().toUpperCase(ENGLISH);
        String elementWriteName = getArrayElementPhoenixTypeName(session, this, elementType);
        return WriteMapping.objectMapping(elementDataType + " ARRAY", arrayWriteFunction(session, elementType, elementWriteName));
    }
    throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
Also used : ArrayType(io.trino.spi.type.ArrayType) BloomType(org.apache.hadoop.hbase.regionserver.BloomType) JDBCType(java.sql.JDBCType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) VarbinaryType(io.trino.spi.type.VarbinaryType) CharType(io.trino.spi.type.CharType) ArrayType(io.trino.spi.type.ArrayType) PDataType(org.apache.phoenix.schema.types.PDataType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) VarbinaryType(io.trino.spi.type.VarbinaryType) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) DecimalType(io.trino.spi.type.DecimalType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType)

Example 48 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class SingleStoreClient method toWriteMapping.

@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
    if (type == BOOLEAN) {
        return WriteMapping.booleanMapping("boolean", booleanWriteFunction());
    }
    if (type == TINYINT) {
        return WriteMapping.longMapping("tinyint", tinyintWriteFunction());
    }
    if (type == SMALLINT) {
        return WriteMapping.longMapping("smallint", smallintWriteFunction());
    }
    if (type == INTEGER) {
        return WriteMapping.longMapping("integer", integerWriteFunction());
    }
    if (type == BIGINT) {
        return WriteMapping.longMapping("bigint", bigintWriteFunction());
    }
    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 (REAL.equals(type)) {
        return WriteMapping.longMapping("float", realWriteFunction());
    }
    if (type == DOUBLE) {
        return WriteMapping.doubleMapping("double precision", doubleWriteFunction());
    }
    if (type instanceof CharType) {
        return WriteMapping.sliceMapping("char(" + ((CharType) type).getLength() + ")", charWriteFunction());
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        String dataType;
        if (varcharType.isUnbounded()) {
            dataType = "longtext";
        } else if (varcharType.getBoundedLength() <= SINGLESTORE_VARCHAR_MAX_LENGTH) {
            dataType = "varchar(" + varcharType.getBoundedLength() + ")";
        } else if (varcharType.getBoundedLength() <= SINGLESTORE_TEXT_MAX_LENGTH) {
            dataType = "text";
        } else if (varcharType.getBoundedLength() <= SINGLESTORE_MEDIUMTEXT_MAX_LENGTH) {
            dataType = "mediumtext";
        } else {
            dataType = "longtext";
        }
        return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
    }
    if (VARBINARY.equals(type)) {
        return WriteMapping.sliceMapping("longblob", varbinaryWriteFunction());
    }
    if (type == DATE) {
        return WriteMapping.longMapping("date", dateWriteFunction());
    }
    if (type instanceof TimeType) {
        TimeType timeType = (TimeType) type;
        checkArgument(timeType.getPrecision() <= SINGLESTORE_DATE_TIME_MAX_PRECISION, "The max time precision in SingleStore is 6");
        if (timeType.getPrecision() == 0) {
            return WriteMapping.longMapping("time", timeWriteFunction(0));
        }
        return WriteMapping.longMapping("time(6)", timeWriteFunction(6));
    }
    // TODO implement TIME type
    if (type instanceof TimestampType) {
        TimestampType timestampType = (TimestampType) type;
        checkArgument(timestampType.getPrecision() <= SINGLESTORE_DATE_TIME_MAX_PRECISION, "The max timestamp precision in SingleStore is 6");
        if (timestampType.getPrecision() == 0) {
            return WriteMapping.longMapping("datetime", timestampWriteFunction(timestampType));
        }
        return WriteMapping.longMapping(format("datetime(%s)", SINGLESTORE_DATE_TIME_MAX_PRECISION), timestampWriteFunction(TIMESTAMP_MICROS));
    }
    if (type.equals(jsonType)) {
        return WriteMapping.sliceMapping("json", varcharWriteFunction());
    }
    throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
Also used : VarcharType(io.trino.spi.type.VarcharType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) DecimalType(io.trino.spi.type.DecimalType) TimestampType(io.trino.spi.type.TimestampType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType) TimeType(io.trino.spi.type.TimeType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType)

Example 49 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class SqlServerClient method toWriteMapping.

@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
    if (type == BOOLEAN) {
        return WriteMapping.booleanMapping("bit", booleanWriteFunction());
    }
    if (type == TINYINT) {
        return WriteMapping.longMapping("tinyint", tinyintWriteFunction());
    }
    if (type == SMALLINT) {
        return WriteMapping.longMapping("smallint", smallintWriteFunction());
    }
    if (type == INTEGER) {
        return WriteMapping.longMapping("integer", integerWriteFunction());
    }
    if (type == BIGINT) {
        return WriteMapping.longMapping("bigint", bigintWriteFunction());
    }
    if (type == REAL) {
        return WriteMapping.longMapping("real", realWriteFunction());
    }
    if (type == DOUBLE) {
        return WriteMapping.doubleMapping("double precision", 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 VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        String dataType;
        if (varcharType.isUnbounded() || varcharType.getBoundedLength() > 4000) {
            dataType = "nvarchar(max)";
        } else {
            dataType = "nvarchar(" + varcharType.getBoundedLength() + ")";
        }
        return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        String dataType;
        if (charType.getLength() > 4000) {
            dataType = "nvarchar(max)";
        } else {
            dataType = "nchar(" + charType.getLength() + ")";
        }
        return WriteMapping.sliceMapping(dataType, charWriteFunction());
    }
    if (type instanceof VarbinaryType) {
        return WriteMapping.sliceMapping("varbinary(max)", varbinaryWriteFunction());
    }
    if (type == DATE) {
        return WriteMapping.longMapping("date", sqlServerDateWriteFunction());
    }
    if (type instanceof TimeType) {
        TimeType timeType = (TimeType) type;
        int precision = min(timeType.getPrecision(), MAX_SUPPORTED_TEMPORAL_PRECISION);
        String dataType = format("time(%d)", precision);
        return WriteMapping.longMapping(dataType, sqlServerTimeWriteFunction(precision));
    }
    if (type instanceof TimestampType) {
        TimestampType timestampType = (TimestampType) type;
        int precision = min(timestampType.getPrecision(), MAX_SUPPORTED_TEMPORAL_PRECISION);
        String dataType = format("datetime2(%d)", precision);
        if (timestampType.getPrecision() <= MAX_SHORT_PRECISION) {
            return WriteMapping.longMapping(dataType, timestampWriteFunction(timestampType));
        }
        return WriteMapping.objectMapping(dataType, longTimestampWriteFunction(timestampType, precision));
    }
    throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
Also used : VarbinaryType(io.trino.spi.type.VarbinaryType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) DecimalType(io.trino.spi.type.DecimalType) TimestampType(io.trino.spi.type.TimestampType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType) ImplementAvgFloatingPoint(io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint) TimeType(io.trino.spi.type.TimeType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType)

Example 50 with CharType

use of io.trino.spi.type.CharType in project trino by trinodb.

the class TypeUtils method prestoNativeToJdbcObject.

private static Object prestoNativeToJdbcObject(ConnectorSession session, Type prestoType, Object prestoNative) throws SQLException {
    if (prestoNative == null) {
        return null;
    }
    if (DOUBLE.equals(prestoType) || BOOLEAN.equals(prestoType) || BIGINT.equals(prestoType)) {
        return prestoNative;
    }
    if (prestoType instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) prestoType;
        if (decimalType.isShort()) {
            BigInteger unscaledValue = BigInteger.valueOf((long) prestoNative);
            return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
        }
        BigInteger unscaledValue = ((Int128) prestoNative).toBigInteger();
        return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
    }
    if (REAL.equals(prestoType)) {
        return intBitsToFloat(toIntExact((long) prestoNative));
    }
    if (TINYINT.equals(prestoType)) {
        return SignedBytes.checkedCast((long) prestoNative);
    }
    if (SMALLINT.equals(prestoType)) {
        return Shorts.checkedCast((long) prestoNative);
    }
    if (INTEGER.equals(prestoType)) {
        return toIntExact((long) prestoNative);
    }
    if (DATE.equals(prestoType)) {
        // convert to midnight in default time zone
        long millis = DAYS.toMillis((long) prestoNative);
        return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis));
    }
    if (prestoType instanceof TimestampType && ((TimestampType) prestoType).isShort()) {
        return toPgTimestamp(fromTrinoTimestamp((long) prestoNative));
    }
    if (prestoType instanceof TimestampWithTimeZoneType) {
        // PostgreSQL does not store zone, only the point in time
        int precision = ((TimestampWithTimeZoneType) prestoType).getPrecision();
        if (precision <= TimestampWithTimeZoneType.MAX_SHORT_PRECISION) {
            long millisUtc = unpackMillisUtc((long) prestoNative);
            return new Timestamp(millisUtc);
        } else {
            LongTimestampWithTimeZone value = (LongTimestampWithTimeZone) prestoNative;
            long epochSeconds = floorDiv(value.getEpochMillis(), MILLISECONDS_PER_SECOND);
            long nanosOfSecond = floorMod(value.getEpochMillis(), MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND + value.getPicosOfMilli() / PICOSECONDS_PER_NANOSECOND;
            return OffsetDateTime.ofInstant(Instant.ofEpochSecond(epochSeconds, nanosOfSecond), UTC_KEY.getZoneId());
        }
    }
    if (prestoType instanceof VarcharType || prestoType instanceof CharType) {
        return ((Slice) prestoNative).toStringUtf8();
    }
    if (prestoType instanceof ArrayType) {
        // process subarray of multi-dimensional array
        return getJdbcObjectArray(session, ((ArrayType) prestoType).getElementType(), (Block) prestoNative);
    }
    throw new TrinoException(NOT_SUPPORTED, "Unsupported type: " + prestoType);
}
Also used : VarcharType(io.trino.spi.type.VarcharType) StandardColumnMappings.fromTrinoTimestamp(io.trino.plugin.jdbc.StandardColumnMappings.fromTrinoTimestamp) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Date(java.sql.Date) ArrayType(io.trino.spi.type.ArrayType) Slice(io.airlift.slice.Slice) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) DecimalType(io.trino.spi.type.DecimalType) BigInteger(java.math.BigInteger) TimestampType(io.trino.spi.type.TimestampType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType) LongTimestampWithTimeZone(io.trino.spi.type.LongTimestampWithTimeZone) Int128(io.trino.spi.type.Int128)

Aggregations

CharType (io.trino.spi.type.CharType)50 VarcharType (io.trino.spi.type.VarcharType)45 DecimalType (io.trino.spi.type.DecimalType)39 TrinoException (io.trino.spi.TrinoException)28 ArrayType (io.trino.spi.type.ArrayType)20 TimestampType (io.trino.spi.type.TimestampType)20 Type (io.trino.spi.type.Type)19 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)16 Slice (io.airlift.slice.Slice)13 TimeType (io.trino.spi.type.TimeType)12 VarbinaryType (io.trino.spi.type.VarbinaryType)12 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)12 MapType (io.trino.spi.type.MapType)11 RowType (io.trino.spi.type.RowType)11 ImmutableList (com.google.common.collect.ImmutableList)10 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)10 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)9 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8