Search in sources :

Example 1 with JdbcTypeHandle

use of io.trino.plugin.jdbc.JdbcTypeHandle in project trino by trinodb.

the class ClickHouseClient 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> mapping = getForcedMappingToVarchar(typeHandle);
    if (mapping.isPresent()) {
        return mapping;
    }
    ClickHouseColumn column = ClickHouseColumn.of("", jdbcTypeName);
    ClickHouseDataType columnDataType = column.getDataType();
    switch(columnDataType) {
        case UInt8:
            return Optional.of(ColumnMapping.longMapping(SMALLINT, ResultSet::getShort, uInt8WriteFunction()));
        case UInt16:
            return Optional.of(ColumnMapping.longMapping(INTEGER, ResultSet::getInt, uInt16WriteFunction()));
        case UInt32:
            return Optional.of(ColumnMapping.longMapping(BIGINT, ResultSet::getLong, uInt32WriteFunction()));
        case UInt64:
            return Optional.of(ColumnMapping.objectMapping(UINT64_TYPE, longDecimalReadFunction(UINT64_TYPE, UNNECESSARY), uInt64WriteFunction()));
        case IPv4:
            return Optional.of(ipAddressColumnMapping("IPv4StringToNum(?)"));
        case IPv6:
            return Optional.of(ipAddressColumnMapping("IPv6StringToNum(?)"));
        case Enum8:
        case Enum16:
            return Optional.of(ColumnMapping.sliceMapping(createUnboundedVarcharType(), varcharReadFunction(createUnboundedVarcharType()), varcharWriteFunction(), // TODO (https://github.com/trinodb/trino/issues/7100) Currently pushdown would not work and may require a custom bind expression
            DISABLE_PUSHDOWN));
        // FixedString(n)
        case FixedString:
        case String:
            if (isMapStringAsVarchar(session)) {
                return Optional.of(ColumnMapping.sliceMapping(createUnboundedVarcharType(), varcharReadFunction(createUnboundedVarcharType()), varcharWriteFunction(), DISABLE_PUSHDOWN));
            }
            // TODO (https://github.com/trinodb/trino/issues/7100) test & enable predicate pushdown
            return Optional.of(varbinaryColumnMapping());
        case UUID:
            return Optional.of(uuidColumnMapping());
        default:
    }
    switch(typeHandle.getJdbcType()) {
        case Types.TINYINT:
            return Optional.of(tinyintColumnMapping());
        case Types.SMALLINT:
            return Optional.of(smallintColumnMapping());
        case Types.INTEGER:
            return Optional.of(integerColumnMapping());
        case Types.BIGINT:
            return Optional.of(bigintColumnMapping());
        case Types.FLOAT:
        case Types.REAL:
            return Optional.of(ColumnMapping.longMapping(REAL, (resultSet, columnIndex) -> floatToRawIntBits(resultSet.getFloat(columnIndex)), realWriteFunction(), DISABLE_PUSHDOWN));
        case Types.DOUBLE:
            return Optional.of(doubleColumnMapping());
        case Types.DECIMAL:
            int decimalDigits = typeHandle.getRequiredDecimalDigits();
            int precision = typeHandle.getRequiredColumnSize();
            ColumnMapping decimalColumnMapping;
            if (getDecimalRounding(session) == ALLOW_OVERFLOW && precision > Decimals.MAX_PRECISION) {
                int scale = Math.min(decimalDigits, getDecimalDefaultScale(session));
                decimalColumnMapping = decimalColumnMapping(createDecimalType(Decimals.MAX_PRECISION, scale), getDecimalRoundingMode(session));
            } else {
                decimalColumnMapping = decimalColumnMapping(createDecimalType(precision, max(decimalDigits, 0)));
            }
            return Optional.of(new ColumnMapping(decimalColumnMapping.getType(), decimalColumnMapping.getReadFunction(), decimalColumnMapping.getWriteFunction(), // TODO (https://github.com/trinodb/trino/issues/7100) fix, enable and test decimal pushdown
            DISABLE_PUSHDOWN));
        case Types.DATE:
            return Optional.of(dateColumnMappingUsingLocalDate());
        case Types.TIMESTAMP:
            if (columnDataType == ClickHouseDataType.DateTime) {
                verify(typeHandle.getRequiredDecimalDigits() == 0, "Expected 0 as timestamp precision, but got %s", typeHandle.getRequiredDecimalDigits());
                return Optional.of(ColumnMapping.longMapping(TIMESTAMP_SECONDS, timestampReadFunction(TIMESTAMP_SECONDS), timestampSecondsWriteFunction()));
            }
            // TODO (https://github.com/trinodb/trino/issues/10537) Add support for Datetime64 type
            return Optional.of(timestampColumnMappingUsingSqlTimestampWithRounding(TIMESTAMP_MILLIS));
    }
    return Optional.empty();
}
Also used : UNNECESSARY(java.math.RoundingMode.UNNECESSARY) AggregateFunction(io.trino.spi.connector.AggregateFunction) StandardColumnMappings.bigintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.bigintWriteFunction) NANOSECONDS_PER_MICROSECOND(io.trino.spi.type.Timestamps.NANOSECONDS_PER_MICROSECOND) ImplementCount(io.trino.plugin.jdbc.aggregation.ImplementCount) InetAddress(java.net.InetAddress) BigDecimal(java.math.BigDecimal) Slices.wrappedBuffer(io.airlift.slice.Slices.wrappedBuffer) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) ResultSet(java.sql.ResultSet) Map(java.util.Map) StandardColumnMappings.doubleWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.doubleWriteFunction) BigInteger(java.math.BigInteger) DecimalSessionSessionProperties.getDecimalDefaultScale(io.trino.plugin.jdbc.DecimalSessionSessionProperties.getDecimalDefaultScale) ENGLISH(java.util.Locale.ENGLISH) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) MathContext(java.math.MathContext) ImplementAvgFloatingPoint(io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint) LongWriteFunction(io.trino.plugin.jdbc.LongWriteFunction) ImplementCountAll(io.trino.plugin.jdbc.aggregation.ImplementCountAll) PreparedStatement(java.sql.PreparedStatement) SchemaTableName(io.trino.spi.connector.SchemaTableName) UncheckedIOException(java.io.UncheckedIOException) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) StandardColumnMappings.smallintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.smallintWriteFunction) StandardColumnMappings.longDecimalWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.longDecimalWriteFunction) AggregateFunctionRewriter(io.trino.plugin.base.aggregation.AggregateFunctionRewriter) ConnectionFactory(io.trino.plugin.jdbc.ConnectionFactory) JdbcConnectorExpressionRewriterBuilder(io.trino.plugin.jdbc.expression.JdbcConnectorExpressionRewriterBuilder) JdbcTableHandle(io.trino.plugin.jdbc.JdbcTableHandle) DATE(io.trino.spi.type.DateType.DATE) REAL(io.trino.spi.type.RealType.REAL) StandardColumnMappings.doubleColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping) Slice(io.airlift.slice.Slice) StandardColumnMappings.timestampColumnMappingUsingSqlTimestampWithRounding(io.trino.plugin.jdbc.StandardColumnMappings.timestampColumnMappingUsingSqlTimestampWithRounding) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) TIMESTAMP_MILLIS(io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS) LocalDateTime(java.time.LocalDateTime) StandardColumnMappings.booleanWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.booleanWriteFunction) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ALLOW_OVERFLOW(io.trino.plugin.jdbc.DecimalConfig.DecimalMapping.ALLOW_OVERFLOW) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) StandardColumnMappings.bigintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping) Enums(com.google.common.base.Enums) OptionalLong(java.util.OptionalLong) Float.floatToRawIntBits(java.lang.Float.floatToRawIntBits) SQLException(java.sql.SQLException) String.join(java.lang.String.join) UuidType.javaUuidToTrinoUuid(io.trino.spi.type.UuidType.javaUuidToTrinoUuid) StandardColumnMappings.decimalColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping) UuidType.trinoUuidToJavaUuid(io.trino.spi.type.UuidType.trinoUuidToJavaUuid) ColumnHandle(io.trino.spi.connector.ColumnHandle) INVALID_TABLE_PROPERTY(io.trino.spi.StandardErrorCode.INVALID_TABLE_PROPERTY) ConnectorExpressionRewriter(io.trino.plugin.base.expression.ConnectorExpressionRewriter) SAMPLE_BY_PROPERTY(io.trino.plugin.clickhouse.ClickHouseTableProperties.SAMPLE_BY_PROPERTY) Math.floorDiv(java.lang.Math.floorDiv) AggregateFunctionRule(io.trino.plugin.base.aggregation.AggregateFunctionRule) Nullable(javax.annotation.Nullable) TIMESTAMP_SECONDS(io.trino.spi.type.TimestampType.TIMESTAMP_SECONDS) Int128(io.trino.spi.type.Int128) StandardColumnMappings.realWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.realWriteFunction) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) QueryBuilder(io.trino.plugin.jdbc.QueryBuilder) ClickHouseSessionProperties.isMapStringAsVarchar(io.trino.plugin.clickhouse.ClickHouseSessionProperties.isMapStringAsVarchar) StandardColumnMappings.smallintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping) StandardTypes(io.trino.spi.type.StandardTypes) ConnectorSession(io.trino.spi.connector.ConnectorSession) UnknownHostException(java.net.UnknownHostException) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) IdentifierMapping(io.trino.plugin.jdbc.mapping.IdentifierMapping) VarbinaryType(io.trino.spi.type.VarbinaryType) CharType(io.trino.spi.type.CharType) DecimalSessionSessionProperties.getDecimalRoundingMode(io.trino.plugin.jdbc.DecimalSessionSessionProperties.getDecimalRoundingMode) TINYINT(io.trino.spi.type.TinyintType.TINYINT) InetAddresses(com.google.common.net.InetAddresses) StandardColumnMappings.shortDecimalWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.shortDecimalWriteFunction) StandardColumnMappings.varbinaryWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryWriteFunction) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression) WriteMapping(io.trino.plugin.jdbc.WriteMapping) PRIMARY_KEY_PROPERTY(io.trino.plugin.clickhouse.ClickHouseTableProperties.PRIMARY_KEY_PROPERTY) JDBC_ERROR(io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR) System.arraycopy(java.lang.System.arraycopy) BaseJdbcConfig(io.trino.plugin.jdbc.BaseJdbcConfig) Connection(java.sql.Connection) BiFunction(java.util.function.BiFunction) ImplementSum(io.trino.plugin.jdbc.aggregation.ImplementSum) ObjectWriteFunction(io.trino.plugin.jdbc.ObjectWriteFunction) StandardColumnMappings.dateReadFunctionUsingLocalDate(io.trino.plugin.jdbc.StandardColumnMappings.dateReadFunctionUsingLocalDate) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping) INTEGER(io.trino.spi.type.IntegerType.INTEGER) Splitter(com.google.common.base.Splitter) TypeSignature(io.trino.spi.type.TypeSignature) INVALID_ARGUMENTS(io.trino.spi.StandardErrorCode.INVALID_ARGUMENTS) ImmutableSet(com.google.common.collect.ImmutableSet) DecimalSessionSessionProperties.getDecimalRounding(io.trino.plugin.jdbc.DecimalSessionSessionProperties.getDecimalRounding) ImmutableMap(com.google.common.collect.ImmutableMap) ClickHouseColumn(com.clickhouse.client.ClickHouseColumn) MICROSECONDS_PER_SECOND(io.trino.spi.type.Timestamps.MICROSECONDS_PER_SECOND) TrinoException(io.trino.spi.TrinoException) UUID(java.util.UUID) 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) LocalDate(java.time.LocalDate) Decimals(io.trino.spi.type.Decimals) Entry(java.util.Map.Entry) UTC(java.time.ZoneOffset.UTC) Optional(java.util.Optional) Math.max(java.lang.Math.max) StandardColumnMappings.varcharReadFunction(io.trino.plugin.jdbc.StandardColumnMappings.varcharReadFunction) StandardColumnMappings.tinyintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.tinyintColumnMapping) DecimalType(io.trino.spi.type.DecimalType) StandardColumnMappings.integerColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.integerColumnMapping) Types(java.sql.Types) StandardColumnMappings.varbinaryColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping) SliceWriteFunction(io.trino.plugin.jdbc.SliceWriteFunction) Math.floorMod(java.lang.Math.floorMod) ClickHouseDataType(com.clickhouse.client.ClickHouseDataType) StandardColumnMappings.varcharWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.varcharWriteFunction) StandardColumnMappings.tinyintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.tinyintWriteFunction) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) Shorts(com.google.common.primitives.Shorts) Inject(javax.inject.Inject) VarcharType(io.trino.spi.type.VarcharType) PARTITION_BY_PROPERTY(io.trino.plugin.clickhouse.ClickHouseTableProperties.PARTITION_BY_PROPERTY) ImmutableList(com.google.common.collect.ImmutableList) Verify.verify(com.google.common.base.Verify.verify) BaseJdbcClient(io.trino.plugin.jdbc.BaseJdbcClient) Math.toIntExact(java.lang.Math.toIntExact) RemoteTableName(io.trino.plugin.jdbc.RemoteTableName) ImplementMinMax(io.trino.plugin.jdbc.aggregation.ImplementMinMax) ORDER_BY_PROPERTY(io.trino.plugin.clickhouse.ClickHouseTableProperties.ORDER_BY_PROPERTY) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) ENGINE_PROPERTY(io.trino.plugin.clickhouse.ClickHouseTableProperties.ENGINE_PROPERTY) StandardColumnMappings.integerWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.integerWriteFunction) GENERIC_INTERNAL_ERROR(io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) DISABLE_PUSHDOWN(io.trino.plugin.jdbc.PredicatePushdownController.DISABLE_PUSHDOWN) StandardColumnMappings.timestampReadFunction(io.trino.plugin.jdbc.StandardColumnMappings.timestampReadFunction) DateTimeFormatter(java.time.format.DateTimeFormatter) TypeManager(io.trino.spi.type.TypeManager) ClickHouseDataType(com.clickhouse.client.ClickHouseDataType) TrinoException(io.trino.spi.TrinoException) ClickHouseColumn(com.clickhouse.client.ClickHouseColumn) StandardColumnMappings.doubleColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping) StandardColumnMappings.bigintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping) StandardColumnMappings.decimalColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping) StandardColumnMappings.smallintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping) StandardColumnMappings.tinyintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.tinyintColumnMapping) StandardColumnMappings.integerColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.integerColumnMapping) StandardColumnMappings.varbinaryColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping) ImplementAvgFloatingPoint(io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint)

Example 2 with JdbcTypeHandle

use of io.trino.plugin.jdbc.JdbcTypeHandle in project trino by trinodb.

the class BaseImplementAvgBigint method rewrite.

@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
    Variable argument = captures.get(this.argument);
    verify(aggregateFunction.getOutputType() == DOUBLE);
    return Optional.of(new JdbcExpression(format(getRewriteFormatExpression(), context.rewriteExpression(argument).orElseThrow()), new JdbcTypeHandle(Types.DOUBLE, Optional.of("double"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())));
}
Also used : JdbcTypeHandle(io.trino.plugin.jdbc.JdbcTypeHandle) Variable(io.trino.spi.expression.Variable) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression)

Example 3 with JdbcTypeHandle

use of io.trino.plugin.jdbc.JdbcTypeHandle in project trino by trinodb.

the class PhoenixClient method getArrayElementTypeHandle.

private JdbcTypeHandle getArrayElementTypeHandle(JdbcTypeHandle arrayTypeHandle) {
    String arrayTypeName = arrayTypeHandle.getJdbcTypeName().orElseThrow(() -> new TrinoException(PHOENIX_METADATA_ERROR, "Type name is missing for jdbc type: " + JDBCType.valueOf(arrayTypeHandle.getJdbcType())));
    checkArgument(arrayTypeName.endsWith(" ARRAY"), "array type must end with ' ARRAY'");
    arrayTypeName = arrayTypeName.substring(0, arrayTypeName.length() - " ARRAY".length());
    verify(arrayTypeHandle.getCaseSensitivity().isEmpty(), "Case sensitivity not supported");
    return new JdbcTypeHandle(PDataType.fromSqlTypeName(arrayTypeName).getSqlType(), Optional.of(arrayTypeName), arrayTypeHandle.getColumnSize(), arrayTypeHandle.getDecimalDigits(), arrayTypeHandle.getArrayDimensions(), Optional.empty());
}
Also used : JdbcTypeHandle(io.trino.plugin.jdbc.JdbcTypeHandle) TrinoException(io.trino.spi.TrinoException)

Example 4 with JdbcTypeHandle

use of io.trino.plugin.jdbc.JdbcTypeHandle in project trino by trinodb.

the class PhoenixClient method toColumnMapping.

@Override
public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle) {
    Optional<ColumnMapping> mapping = getForcedMappingToVarchar(typeHandle);
    if (mapping.isPresent()) {
        return mapping;
    }
    switch(typeHandle.getJdbcType()) {
        case Types.BOOLEAN:
            return Optional.of(booleanColumnMapping());
        case Types.TINYINT:
            return Optional.of(tinyintColumnMapping());
        case Types.SMALLINT:
            return Optional.of(smallintColumnMapping());
        case Types.INTEGER:
            return Optional.of(integerColumnMapping());
        case Types.BIGINT:
            return Optional.of(bigintColumnMapping());
        case Types.FLOAT:
            return Optional.of(realColumnMapping());
        case Types.DOUBLE:
            return Optional.of(doubleColumnMapping());
        case Types.DECIMAL:
            Optional<Integer> columnSize = typeHandle.getColumnSize();
            int precision = columnSize.orElse(DEFAULT_PRECISION);
            int decimalDigits = typeHandle.getDecimalDigits().orElse(DEFAULT_SCALE);
            if (getDecimalRounding(session) == ALLOW_OVERFLOW) {
                if (columnSize.isEmpty()) {
                    return Optional.of(decimalColumnMapping(createDecimalType(Decimals.MAX_PRECISION, getDecimalDefaultScale(session)), getDecimalRoundingMode(session)));
                }
            }
            // TODO does phoenix support negative scale?
            // Map decimal(p, -s) (negative scale) to decimal(p+s, 0).
            precision = precision + max(-decimalDigits, 0);
            if (precision > Decimals.MAX_PRECISION) {
                break;
            }
            return Optional.of(decimalColumnMapping(createDecimalType(precision, max(decimalDigits, 0)), UNNECESSARY));
        case Types.CHAR:
            return Optional.of(defaultCharColumnMapping(typeHandle.getRequiredColumnSize(), true));
        case VARCHAR:
        case NVARCHAR:
        case LONGVARCHAR:
        case LONGNVARCHAR:
            if (typeHandle.getColumnSize().isEmpty()) {
                return Optional.of(varcharColumnMapping(createUnboundedVarcharType(), true));
            }
            return Optional.of(defaultVarcharColumnMapping(typeHandle.getRequiredColumnSize(), true));
        case Types.BINARY:
        case Types.VARBINARY:
            return Optional.of(varbinaryColumnMapping());
        case Types.DATE:
            return Optional.of(ColumnMapping.longMapping(DATE, dateReadFunction(), dateWriteFunctionUsingString()));
        // TODO add support for TIMESTAMP after Phoenix adds support for LocalDateTime
        case TIMESTAMP:
        case TIME_WITH_TIMEZONE:
        case TIMESTAMP_WITH_TIMEZONE:
            if (getUnsupportedTypeHandling(session) == CONVERT_TO_VARCHAR) {
                return mapToUnboundedVarchar(typeHandle);
            }
            return Optional.empty();
        case ARRAY:
            JdbcTypeHandle elementTypeHandle = getArrayElementTypeHandle(typeHandle);
            if (elementTypeHandle.getJdbcType() == Types.VARBINARY) {
                return Optional.empty();
            }
            return toColumnMapping(session, connection, elementTypeHandle).map(elementMapping -> {
                ArrayType trinoArrayType = new ArrayType(elementMapping.getType());
                String jdbcTypeName = elementTypeHandle.getJdbcTypeName().orElseThrow(() -> new TrinoException(PHOENIX_METADATA_ERROR, "Type name is missing for jdbc type: " + JDBCType.valueOf(elementTypeHandle.getJdbcType())));
                // TODO (https://github.com/trinodb/trino/issues/11132) Enable predicate pushdown on ARRAY(CHAR) type in Phoenix
                PredicatePushdownController pushdownController = elementTypeHandle.getJdbcType() == Types.CHAR ? DISABLE_PUSHDOWN : FULL_PUSHDOWN;
                return arrayColumnMapping(session, trinoArrayType, jdbcTypeName, pushdownController);
            });
    }
    if (getUnsupportedTypeHandling(session) == CONVERT_TO_VARCHAR) {
        return mapToUnboundedVarchar(typeHandle);
    }
    return Optional.empty();
}
Also used : ArrayType(io.trino.spi.type.ArrayType) JdbcTypeHandle(io.trino.plugin.jdbc.JdbcTypeHandle) TrinoException(io.trino.spi.TrinoException) PredicatePushdownController(io.trino.plugin.jdbc.PredicatePushdownController) StandardColumnMappings.varcharColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.varcharColumnMapping) StandardColumnMappings.booleanColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping) StandardColumnMappings.defaultVarcharColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.defaultVarcharColumnMapping) StandardColumnMappings.doubleColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping) StandardColumnMappings.bigintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping) StandardColumnMappings.defaultCharColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.defaultCharColumnMapping) StandardColumnMappings.decimalColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping) StandardColumnMappings.smallintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping) StandardColumnMappings.realColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.realColumnMapping) StandardColumnMappings.tinyintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.tinyintColumnMapping) StandardColumnMappings.integerColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.integerColumnMapping) StandardColumnMappings.varbinaryColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping)

Example 5 with JdbcTypeHandle

use of io.trino.plugin.jdbc.JdbcTypeHandle in project trino by trinodb.

the class PhoenixClient method toColumnMapping.

@Override
public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle) {
    Optional<ColumnMapping> mapping = getForcedMappingToVarchar(typeHandle);
    if (mapping.isPresent()) {
        return mapping;
    }
    switch(typeHandle.getJdbcType()) {
        case Types.BOOLEAN:
            return Optional.of(booleanColumnMapping());
        case Types.TINYINT:
            return Optional.of(tinyintColumnMapping());
        case Types.SMALLINT:
            return Optional.of(smallintColumnMapping());
        case Types.INTEGER:
            return Optional.of(integerColumnMapping());
        case Types.BIGINT:
            return Optional.of(bigintColumnMapping());
        case Types.FLOAT:
            return Optional.of(realColumnMapping());
        case Types.DOUBLE:
            return Optional.of(doubleColumnMapping());
        case Types.DECIMAL:
            Optional<Integer> columnSize = typeHandle.getColumnSize();
            int precision = columnSize.orElse(DEFAULT_PRECISION);
            int decimalDigits = typeHandle.getDecimalDigits().orElse(DEFAULT_SCALE);
            if (getDecimalRounding(session) == ALLOW_OVERFLOW) {
                if (columnSize.isEmpty()) {
                    return Optional.of(decimalColumnMapping(createDecimalType(Decimals.MAX_PRECISION, getDecimalDefaultScale(session)), getDecimalRoundingMode(session)));
                }
            }
            // TODO does phoenix support negative scale?
            // Map decimal(p, -s) (negative scale) to decimal(p+s, 0).
            precision = precision + max(-decimalDigits, 0);
            if (precision > Decimals.MAX_PRECISION) {
                break;
            }
            return Optional.of(decimalColumnMapping(createDecimalType(precision, max(decimalDigits, 0)), UNNECESSARY));
        case Types.CHAR:
            return Optional.of(defaultCharColumnMapping(typeHandle.getRequiredColumnSize(), true));
        case VARCHAR:
        case NVARCHAR:
        case LONGVARCHAR:
        case LONGNVARCHAR:
            if (typeHandle.getColumnSize().isEmpty()) {
                return Optional.of(varcharColumnMapping(createUnboundedVarcharType(), true));
            }
            return Optional.of(defaultVarcharColumnMapping(typeHandle.getRequiredColumnSize(), true));
        case Types.BINARY:
        case Types.VARBINARY:
            return Optional.of(varbinaryColumnMapping());
        case Types.DATE:
            return Optional.of(ColumnMapping.longMapping(DATE, dateReadFunction(), dateWriteFunctionUsingString()));
        // TODO add support for TIMESTAMP after Phoenix adds support for LocalDateTime
        case TIMESTAMP:
        case TIME_WITH_TIMEZONE:
        case TIMESTAMP_WITH_TIMEZONE:
            if (getUnsupportedTypeHandling(session) == CONVERT_TO_VARCHAR) {
                return mapToUnboundedVarchar(typeHandle);
            }
            return Optional.empty();
        case ARRAY:
            JdbcTypeHandle elementTypeHandle = getArrayElementTypeHandle(typeHandle);
            if (elementTypeHandle.getJdbcType() == Types.VARBINARY) {
                return Optional.empty();
            }
            return toColumnMapping(session, connection, elementTypeHandle).map(elementMapping -> {
                ArrayType trinoArrayType = new ArrayType(elementMapping.getType());
                String jdbcTypeName = elementTypeHandle.getJdbcTypeName().orElseThrow(() -> new TrinoException(PHOENIX_METADATA_ERROR, "Type name is missing for jdbc type: " + JDBCType.valueOf(elementTypeHandle.getJdbcType())));
                // TODO (https://github.com/trinodb/trino/issues/11132) Enable predicate pushdown on ARRAY(CHAR) type in Phoenix
                PredicatePushdownController pushdown = elementTypeHandle.getJdbcType() == Types.CHAR ? DISABLE_PUSHDOWN : FULL_PUSHDOWN;
                return arrayColumnMapping(session, trinoArrayType, jdbcTypeName, pushdown);
            });
    }
    if (getUnsupportedTypeHandling(session) == CONVERT_TO_VARCHAR) {
        return mapToUnboundedVarchar(typeHandle);
    }
    return Optional.empty();
}
Also used : ArrayType(io.trino.spi.type.ArrayType) JdbcTypeHandle(io.trino.plugin.jdbc.JdbcTypeHandle) TrinoException(io.trino.spi.TrinoException) PredicatePushdownController(io.trino.plugin.jdbc.PredicatePushdownController) StandardColumnMappings.varcharColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.varcharColumnMapping) StandardColumnMappings.booleanColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping) StandardColumnMappings.defaultVarcharColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.defaultVarcharColumnMapping) StandardColumnMappings.doubleColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping) StandardColumnMappings.bigintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping) StandardColumnMappings.defaultCharColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.defaultCharColumnMapping) StandardColumnMappings.decimalColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping) StandardColumnMappings.smallintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping) StandardColumnMappings.realColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.realColumnMapping) StandardColumnMappings.tinyintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.tinyintColumnMapping) StandardColumnMappings.integerColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.integerColumnMapping) StandardColumnMappings.varbinaryColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping)

Aggregations

JdbcTypeHandle (io.trino.plugin.jdbc.JdbcTypeHandle)14 TrinoException (io.trino.spi.TrinoException)12 ColumnMapping (io.trino.plugin.jdbc.ColumnMapping)9 JdbcColumnHandle (io.trino.plugin.jdbc.JdbcColumnHandle)7 SQLException (java.sql.SQLException)7 StandardColumnMappings.bigintColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping)6 StandardColumnMappings.decimalColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping)6 StandardColumnMappings.doubleColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping)6 StandardColumnMappings.integerColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.integerColumnMapping)6 StandardColumnMappings.smallintColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping)6 StandardColumnMappings.varbinaryColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping)6 SchemaTableName (io.trino.spi.connector.SchemaTableName)6 Verify.verify (com.google.common.base.Verify.verify)5 ImmutableSet (com.google.common.collect.ImmutableSet)5 BaseJdbcClient (io.trino.plugin.jdbc.BaseJdbcClient)5 BaseJdbcConfig (io.trino.plugin.jdbc.BaseJdbcConfig)5 ConnectionFactory (io.trino.plugin.jdbc.ConnectionFactory)5 JDBC_ERROR (io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR)5 JdbcTableHandle (io.trino.plugin.jdbc.JdbcTableHandle)5 StandardColumnMappings.booleanColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping)5