Search in sources :

Example 16 with TimeType

use of io.trino.spi.type.TimeType 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)

Aggregations

TimeType (io.trino.spi.type.TimeType)16 VarcharType (io.trino.spi.type.VarcharType)14 CharType (io.trino.spi.type.CharType)13 TimestampType (io.trino.spi.type.TimestampType)13 DecimalType (io.trino.spi.type.DecimalType)12 Type (io.trino.spi.type.Type)11 TrinoException (io.trino.spi.TrinoException)10 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)7 TimeType.createTimeType (io.trino.spi.type.TimeType.createTimeType)7 List (java.util.List)7 TimestampType.createTimestampType (io.trino.spi.type.TimestampType.createTimestampType)6 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)4 ImmutableList (com.google.common.collect.ImmutableList)3 ColumnMapping (io.trino.plugin.jdbc.ColumnMapping)3 StandardColumnMappings.bigintColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping)3 StandardColumnMappings.booleanColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping)3 StandardColumnMappings.decimalColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping)3 StandardColumnMappings.defaultCharColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.defaultCharColumnMapping)3 StandardColumnMappings.defaultVarcharColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.defaultVarcharColumnMapping)3 StandardColumnMappings.doubleColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping)3