Search in sources :

Example 41 with CharType

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

the class MySqlClient 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 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 == DATE) {
        return WriteMapping.longMapping("date", dateWriteFunctionUsingLocalDate());
    }
    if (type instanceof TimeType) {
        TimeType timeType = (TimeType) type;
        if (timeType.getPrecision() <= MAX_SUPPORTED_DATE_TIME_PRECISION) {
            return WriteMapping.longMapping(format("time(%s)", timeType.getPrecision()), timeWriteFunction(timeType.getPrecision()));
        }
        return WriteMapping.longMapping(format("time(%s)", MAX_SUPPORTED_DATE_TIME_PRECISION), timeWriteFunction(MAX_SUPPORTED_DATE_TIME_PRECISION));
    }
    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 TimestampType) {
        TimestampType timestampType = (TimestampType) type;
        if (timestampType.getPrecision() <= MAX_SUPPORTED_DATE_TIME_PRECISION) {
            verify(timestampType.getPrecision() <= TimestampType.MAX_SHORT_PRECISION);
            return WriteMapping.longMapping(format("datetime(%s)", timestampType.getPrecision()), timestampWriteFunction(timestampType));
        }
        return WriteMapping.objectMapping(format("datetime(%s)", MAX_SUPPORTED_DATE_TIME_PRECISION), longTimestampWriteFunction(timestampType, MAX_SUPPORTED_DATE_TIME_PRECISION));
    }
    if (VARBINARY.equals(type)) {
        return WriteMapping.sliceMapping("mediumblob", varbinaryWriteFunction());
    }
    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() <= 255) {
            dataType = "tinytext";
        } else if (varcharType.getBoundedLength() <= 65535) {
            dataType = "text";
        } else if (varcharType.getBoundedLength() <= 16777215) {
            dataType = "mediumtext";
        } else {
            dataType = "longtext";
        }
        return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
    }
    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) TrinoException(io.trino.spi.TrinoException) TimestampType(io.trino.spi.type.TimestampType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) CharType(io.trino.spi.type.CharType) TimeType(io.trino.spi.type.TimeType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType)

Example 42 with CharType

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

the class MongoPageSink method getObjectValue.

private Object getObjectValue(Type type, Block block, int position) {
    if (block.isNull(position)) {
        if (type.equals(OBJECT_ID)) {
            return new ObjectId();
        }
        return null;
    }
    if (type.equals(OBJECT_ID)) {
        return new ObjectId(block.getSlice(position, 0, block.getSliceLength(position)).getBytes());
    }
    if (type.equals(BooleanType.BOOLEAN)) {
        return type.getBoolean(block, position);
    }
    if (type.equals(BigintType.BIGINT)) {
        return type.getLong(block, position);
    }
    if (type.equals(IntegerType.INTEGER)) {
        return toIntExact(type.getLong(block, position));
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return Shorts.checkedCast(type.getLong(block, position));
    }
    if (type.equals(TinyintType.TINYINT)) {
        return SignedBytes.checkedCast(type.getLong(block, position));
    }
    if (type.equals(RealType.REAL)) {
        return intBitsToFloat(toIntExact(type.getLong(block, position)));
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return type.getDouble(block, position);
    }
    if (type instanceof VarcharType) {
        return type.getSlice(block, position).toStringUtf8();
    }
    if (type instanceof CharType) {
        return padSpaces(type.getSlice(block, position), ((CharType) type)).toStringUtf8();
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return new Binary(type.getSlice(block, position).getBytes());
    }
    if (type.equals(DateType.DATE)) {
        long days = type.getLong(block, position);
        return new Date(TimeUnit.DAYS.toMillis(days));
    }
    if (type.equals(TimeType.TIME)) {
        long picos = type.getLong(block, position);
        return new Date(roundDiv(picos, PICOSECONDS_PER_MILLISECOND));
    }
    if (type.equals(TIMESTAMP_MILLIS)) {
        long millisUtc = floorDiv(type.getLong(block, position), MICROSECONDS_PER_MILLISECOND);
        return new Date(millisUtc);
    }
    if (type.equals(TimestampWithTimeZoneType.TIMESTAMP_TZ_MILLIS)) {
        long millisUtc = unpackMillisUtc(type.getLong(block, position));
        return new Date(millisUtc);
    }
    if (type instanceof DecimalType) {
        return readBigDecimal((DecimalType) type, block, position);
    }
    if (isJsonType(type)) {
        String json = type.getSlice(block, position).toStringUtf8();
        try {
            return Document.parse(json);
        } catch (BsonInvalidOperationException e) {
            throw new TrinoException(NOT_SUPPORTED, "Can't convert json to MongoDB Document: " + json, e);
        }
    }
    if (isArrayType(type)) {
        Type elementType = type.getTypeParameters().get(0);
        Block arrayBlock = block.getObject(position, Block.class);
        List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
        for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
            Object element = getObjectValue(elementType, arrayBlock, i);
            list.add(element);
        }
        return unmodifiableList(list);
    }
    if (isMapType(type)) {
        Type keyType = type.getTypeParameters().get(0);
        Type valueType = type.getTypeParameters().get(1);
        Block mapBlock = block.getObject(position, Block.class);
        // map type is converted into list of fixed keys document
        List<Object> values = new ArrayList<>(mapBlock.getPositionCount() / 2);
        for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
            Map<String, Object> mapValue = new HashMap<>();
            mapValue.put("key", getObjectValue(keyType, mapBlock, i));
            mapValue.put("value", getObjectValue(valueType, mapBlock, i + 1));
            values.add(mapValue);
        }
        return unmodifiableList(values);
    }
    if (isRowType(type)) {
        Block rowBlock = block.getObject(position, Block.class);
        List<Type> fieldTypes = type.getTypeParameters();
        if (fieldTypes.size() != rowBlock.getPositionCount()) {
            throw new TrinoException(StandardErrorCode.GENERIC_INTERNAL_ERROR, "Expected row value field count does not match type field count");
        }
        if (isImplicitRowType(type)) {
            List<Object> rowValue = new ArrayList<>();
            for (int i = 0; i < rowBlock.getPositionCount(); i++) {
                Object element = getObjectValue(fieldTypes.get(i), rowBlock, i);
                rowValue.add(element);
            }
            return unmodifiableList(rowValue);
        }
        Map<String, Object> rowValue = new HashMap<>();
        for (int i = 0; i < rowBlock.getPositionCount(); i++) {
            rowValue.put(type.getTypeSignature().getParameters().get(i).getNamedTypeSignature().getName().orElse("field" + i), getObjectValue(fieldTypes.get(i), rowBlock, i));
        }
        return unmodifiableMap(rowValue);
    }
    throw new TrinoException(NOT_SUPPORTED, "unsupported type: " + type);
}
Also used : BsonInvalidOperationException(org.bson.BsonInvalidOperationException) ObjectId(org.bson.types.ObjectId) VarcharType(io.trino.spi.type.VarcharType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date) DateType(io.trino.spi.type.DateType) BooleanType(io.trino.spi.type.BooleanType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) SmallintType(io.trino.spi.type.SmallintType) TypeUtils.isMapType(io.trino.plugin.mongodb.TypeUtils.isMapType) DecimalType(io.trino.spi.type.DecimalType) TypeUtils.isArrayType(io.trino.plugin.mongodb.TypeUtils.isArrayType) TimeType(io.trino.spi.type.TimeType) DoubleType(io.trino.spi.type.DoubleType) Type(io.trino.spi.type.Type) BigintType(io.trino.spi.type.BigintType) VarcharType(io.trino.spi.type.VarcharType) TypeUtils.isJsonType(io.trino.plugin.mongodb.TypeUtils.isJsonType) TinyintType(io.trino.spi.type.TinyintType) IntegerType(io.trino.spi.type.IntegerType) VarbinaryType(io.trino.spi.type.VarbinaryType) CharType(io.trino.spi.type.CharType) RealType(io.trino.spi.type.RealType) TypeUtils.isRowType(io.trino.plugin.mongodb.TypeUtils.isRowType) DecimalType(io.trino.spi.type.DecimalType) TrinoException(io.trino.spi.TrinoException) Block(io.trino.spi.block.Block) CharType(io.trino.spi.type.CharType) Binary(org.bson.types.Binary)

Example 43 with CharType

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

the class OracleClient method toWriteMapping.

@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
    if (type instanceof VarcharType) {
        String dataType;
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded() || varcharType.getBoundedLength() > ORACLE_VARCHAR2_MAX_CHARS) {
            dataType = "nclob";
        } else {
            dataType = "varchar2(" + varcharType.getBoundedLength() + " CHAR)";
        }
        return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
    }
    if (type instanceof CharType) {
        String dataType;
        if (((CharType) type).getLength() > ORACLE_CHAR_MAX_CHARS) {
            dataType = "nclob";
        } else {
            dataType = "char(" + ((CharType) type).getLength() + " CHAR)";
        }
        return WriteMapping.sliceMapping(dataType, charWriteFunction());
    }
    if (type instanceof DecimalType) {
        String dataType = format("number(%s, %s)", ((DecimalType) type).getPrecision(), ((DecimalType) type).getScale());
        if (((DecimalType) type).isShort()) {
            return WriteMapping.longMapping(dataType, shortDecimalWriteFunction((DecimalType) type));
        }
        return WriteMapping.objectMapping(dataType, longDecimalWriteFunction((DecimalType) type));
    }
    if (type.equals(TIMESTAMP_SECONDS)) {
        // Oracle date stores year, month, day, hour, minute, seconds, but not second fraction
        return WriteMapping.longMapping("date", trinoTimestampToOracleDateWriteFunction());
    }
    if (type.equals(TIMESTAMP_MILLIS)) {
        return WriteMapping.longMapping("timestamp(3)", trinoTimestampToOracleTimestampWriteFunction());
    }
    WriteMapping writeMapping = WRITE_MAPPINGS.get(type);
    if (writeMapping != null) {
        return writeMapping;
    }
    throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
Also used : 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) TrinoException(io.trino.spi.TrinoException) CharType.createCharType(io.trino.spi.type.CharType.createCharType) CharType(io.trino.spi.type.CharType) WriteMapping(io.trino.plugin.jdbc.WriteMapping)

Example 44 with CharType

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

the class OracleClient method toColumnMapping.

@Override
public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle) {
    String jdbcTypeName = typeHandle.getJdbcTypeName().orElseThrow(() -> new TrinoException(JDBC_ERROR, "Type name is missing: " + typeHandle));
    Optional<ColumnMapping> mappingToVarchar = getForcedMappingToVarchar(typeHandle);
    if (mappingToVarchar.isPresent()) {
        return mappingToVarchar;
    }
    if (jdbcTypeName.equalsIgnoreCase("date")) {
        return Optional.of(ColumnMapping.longMapping(TIMESTAMP_SECONDS, oracleTimestampReadFunction(), trinoTimestampToOracleDateWriteFunction(), FULL_PUSHDOWN));
    }
    switch(typeHandle.getJdbcType()) {
        case Types.SMALLINT:
            return Optional.of(ColumnMapping.longMapping(SMALLINT, ResultSet::getShort, smallintWriteFunction(), FULL_PUSHDOWN));
        case OracleTypes.BINARY_FLOAT:
            return Optional.of(ColumnMapping.longMapping(REAL, (resultSet, columnIndex) -> floatToRawIntBits(resultSet.getFloat(columnIndex)), oracleRealWriteFunction(), FULL_PUSHDOWN));
        case OracleTypes.BINARY_DOUBLE:
        case OracleTypes.FLOAT:
            return Optional.of(ColumnMapping.doubleMapping(DOUBLE, ResultSet::getDouble, oracleDoubleWriteFunction(), FULL_PUSHDOWN));
        case OracleTypes.NUMBER:
            int actualPrecision = typeHandle.getRequiredColumnSize();
            int decimalDigits = typeHandle.getRequiredDecimalDigits();
            // Map negative scale to decimal(p+s, 0).
            int precision = actualPrecision + max(-decimalDigits, 0);
            int scale = max(decimalDigits, 0);
            Optional<Integer> numberDefaultScale = getNumberDefaultScale(session);
            RoundingMode roundingMode = getNumberRoundingMode(session);
            if (precision < scale) {
                if (roundingMode == RoundingMode.UNNECESSARY) {
                    break;
                }
                scale = min(Decimals.MAX_PRECISION, scale);
                precision = scale;
            } else if (numberDefaultScale.isPresent() && precision == PRECISION_OF_UNSPECIFIED_NUMBER) {
                precision = Decimals.MAX_PRECISION;
                scale = numberDefaultScale.get();
            } else if (precision > Decimals.MAX_PRECISION || actualPrecision <= 0) {
                break;
            }
            DecimalType decimalType = createDecimalType(precision, scale);
            // JDBC driver can return BigDecimal with lower scale than column's scale when there are trailing zeroes
            if (decimalType.isShort()) {
                return Optional.of(ColumnMapping.longMapping(decimalType, shortDecimalReadFunction(decimalType, roundingMode), shortDecimalWriteFunction(decimalType), FULL_PUSHDOWN));
            }
            return Optional.of(ColumnMapping.objectMapping(decimalType, longDecimalReadFunction(decimalType, roundingMode), longDecimalWriteFunction(decimalType), FULL_PUSHDOWN));
        case OracleTypes.CHAR:
        case OracleTypes.NCHAR:
            CharType charType = createCharType(typeHandle.getRequiredColumnSize());
            return Optional.of(ColumnMapping.sliceMapping(charType, charReadFunction(charType), oracleCharWriteFunction(), FULL_PUSHDOWN));
        case OracleTypes.VARCHAR:
        case OracleTypes.NVARCHAR:
            return Optional.of(ColumnMapping.sliceMapping(createVarcharType(typeHandle.getRequiredColumnSize()), (varcharResultSet, varcharColumnIndex) -> utf8Slice(varcharResultSet.getString(varcharColumnIndex)), varcharWriteFunction(), FULL_PUSHDOWN));
        case OracleTypes.CLOB:
        case OracleTypes.NCLOB:
            return Optional.of(ColumnMapping.sliceMapping(createUnboundedVarcharType(), (resultSet, columnIndex) -> utf8Slice(resultSet.getString(columnIndex)), varcharWriteFunction(), DISABLE_PUSHDOWN));
        // Oracle's RAW(n)
        case OracleTypes.VARBINARY:
        case OracleTypes.BLOB:
            return Optional.of(ColumnMapping.sliceMapping(VARBINARY, (resultSet, columnIndex) -> wrappedBuffer(resultSet.getBytes(columnIndex)), varbinaryWriteFunction(), DISABLE_PUSHDOWN));
        case OracleTypes.TIMESTAMP:
            return Optional.of(ColumnMapping.longMapping(TIMESTAMP_MILLIS, oracleTimestampReadFunction(), trinoTimestampToOracleTimestampWriteFunction(), FULL_PUSHDOWN));
        case OracleTypes.TIMESTAMPTZ:
            return Optional.of(oracleTimestampWithTimeZoneColumnMapping());
    }
    if (getUnsupportedTypeHandling(session) == CONVERT_TO_VARCHAR) {
        return mapToUnboundedVarchar(typeHandle);
    }
    return Optional.empty();
}
Also used : ZonedDateTime(java.time.ZonedDateTime) StandardColumnMappings.bigintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.bigintWriteFunction) NANOSECONDS_PER_MICROSECOND(io.trino.spi.type.Timestamps.NANOSECONDS_PER_MICROSECOND) CharType.createCharType(io.trino.spi.type.CharType.createCharType) SecureRandom(java.security.SecureRandom) Slices.wrappedBuffer(io.airlift.slice.Slices.wrappedBuffer) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) ResultSet(java.sql.ResultSet) Map(java.util.Map) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) ZoneOffset(java.time.ZoneOffset) ENGLISH(java.util.Locale.ENGLISH) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling(io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling) DateTimeEncoding.packDateTimeWithZone(io.trino.spi.type.DateTimeEncoding.packDateTimeWithZone) LongWriteFunction(io.trino.plugin.jdbc.LongWriteFunction) Set(java.util.Set) PreparedStatement(java.sql.PreparedStatement) SchemaTableName(io.trino.spi.connector.SchemaTableName) ZoneId(java.time.ZoneId) LongReadFunction(io.trino.plugin.jdbc.LongReadFunction) StandardColumnMappings.charReadFunction(io.trino.plugin.jdbc.StandardColumnMappings.charReadFunction) StandardColumnMappings.smallintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.smallintWriteFunction) StandardColumnMappings.longDecimalWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.longDecimalWriteFunction) ConnectionFactory(io.trino.plugin.jdbc.ConnectionFactory) CONVERT_TO_VARCHAR(io.trino.plugin.jdbc.UnsupportedTypeHandling.CONVERT_TO_VARCHAR) DoubleWriteFunction(io.trino.plugin.jdbc.DoubleWriteFunction) DateTimeEncoding.unpackMillisUtc(io.trino.spi.type.DateTimeEncoding.unpackMillisUtc) JdbcTableHandle(io.trino.plugin.jdbc.JdbcTableHandle) DATE(io.trino.spi.type.DateType.DATE) REAL(io.trino.spi.type.RealType.REAL) JoinCondition(io.trino.spi.connector.JoinCondition) TIMESTAMP_MILLIS(io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS) LocalDateTime(java.time.LocalDateTime) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) Float.floatToRawIntBits(java.lang.Float.floatToRawIntBits) SQLException(java.sql.SQLException) TIMESTAMP_TZ_MILLIS(io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MILLIS) FULL_PUSHDOWN(io.trino.plugin.jdbc.PredicatePushdownController.FULL_PUSHDOWN) StandardColumnMappings.charWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.charWriteFunction) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) Math.floorDiv(java.lang.Math.floorDiv) TIMESTAMP_SECONDS(io.trino.spi.type.TimestampType.TIMESTAMP_SECONDS) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) QueryBuilder(io.trino.plugin.jdbc.QueryBuilder) ConnectorSession(io.trino.spi.connector.ConnectorSession) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) IdentifierMapping(io.trino.plugin.jdbc.mapping.IdentifierMapping) CharType(io.trino.spi.type.CharType) TINYINT(io.trino.spi.type.TinyintType.TINYINT) StandardColumnMappings.shortDecimalWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.shortDecimalWriteFunction) StandardColumnMappings.varbinaryWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryWriteFunction) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) WriteMapping(io.trino.plugin.jdbc.WriteMapping) JDBC_ERROR(io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR) BaseJdbcConfig(io.trino.plugin.jdbc.BaseJdbcConfig) Connection(java.sql.Connection) MICROSECONDS_PER_MILLISECOND(io.trino.spi.type.Timestamps.MICROSECONDS_PER_MILLISECOND) OracleSessionProperties.getNumberDefaultScale(io.trino.plugin.oracle.OracleSessionProperties.getNumberDefaultScale) Math.abs(java.lang.Math.abs) DateTimeEncoding.unpackZoneKey(io.trino.spi.type.DateTimeEncoding.unpackZoneKey) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping) INTEGER(io.trino.spi.type.IntegerType.INTEGER) RoundingMode(java.math.RoundingMode) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) MICROSECONDS_PER_SECOND(io.trino.spi.type.Timestamps.MICROSECONDS_PER_SECOND) TrinoException(io.trino.spi.TrinoException) Math.min(java.lang.Math.min) Instant(java.time.Instant) String.format(java.lang.String.format) List(java.util.List) JdbcTypeHandle(io.trino.plugin.jdbc.JdbcTypeHandle) StandardColumnMappings.longDecimalReadFunction(io.trino.plugin.jdbc.StandardColumnMappings.longDecimalReadFunction) BIGINT(io.trino.spi.type.BigintType.BIGINT) Decimals(io.trino.spi.type.Decimals) Optional(java.util.Optional) Math.max(java.lang.Math.max) DecimalType(io.trino.spi.type.DecimalType) OracleTypes(oracle.jdbc.OracleTypes) MAX_RADIX(java.lang.Character.MAX_RADIX) Types(java.sql.Types) SliceWriteFunction(io.trino.plugin.jdbc.SliceWriteFunction) Math.floorMod(java.lang.Math.floorMod) StandardColumnMappings.varcharWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.varcharWriteFunction) StandardColumnMappings.tinyintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.tinyintWriteFunction) Type(io.trino.spi.type.Type) OracleSessionProperties.getNumberRoundingMode(io.trino.plugin.oracle.OracleSessionProperties.getNumberRoundingMode) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) Inject(javax.inject.Inject) VarcharType(io.trino.spi.type.VarcharType) OraclePreparedStatement(oracle.jdbc.OraclePreparedStatement) ImmutableList(com.google.common.collect.ImmutableList) Verify.verify(com.google.common.base.Verify.verify) Objects.requireNonNull(java.util.Objects.requireNonNull) DAYS(java.util.concurrent.TimeUnit.DAYS) BaseJdbcClient(io.trino.plugin.jdbc.BaseJdbcClient) Math.toIntExact(java.lang.Math.toIntExact) JdbcJoinCondition(io.trino.plugin.jdbc.JdbcJoinCondition) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) StandardColumnMappings.integerWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.integerWriteFunction) DISABLE_PUSHDOWN(io.trino.plugin.jdbc.PredicatePushdownController.DISABLE_PUSHDOWN) DateTimeFormatter(java.time.format.DateTimeFormatter) StandardColumnMappings.shortDecimalReadFunction(io.trino.plugin.jdbc.StandardColumnMappings.shortDecimalReadFunction) RoundingMode(java.math.RoundingMode) OracleSessionProperties.getNumberRoundingMode(io.trino.plugin.oracle.OracleSessionProperties.getNumberRoundingMode) TrinoException(io.trino.spi.TrinoException) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) DecimalType(io.trino.spi.type.DecimalType) CharType.createCharType(io.trino.spi.type.CharType.createCharType) CharType(io.trino.spi.type.CharType) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping)

Example 45 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)

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