Search in sources :

Example 1 with JDBC_ERROR

use of io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR 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 JDBC_ERROR

use of io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR in project trino by trinodb.

the class PostgreSqlClient 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;
    }
    switch(jdbcTypeName) {
        case "money":
            return Optional.of(moneyColumnMapping());
        case "uuid":
            return Optional.of(uuidColumnMapping());
        case "jsonb":
        case "json":
            return Optional.of(jsonColumnMapping());
        case "timestamptz":
            // PostgreSQL's "timestamp with time zone" is reported as Types.TIMESTAMP rather than Types.TIMESTAMP_WITH_TIMEZONE
            int decimalDigits = typeHandle.getRequiredDecimalDigits();
            return Optional.of(timestampWithTimeZoneColumnMapping(decimalDigits));
        case "hstore":
            return Optional.of(hstoreColumnMapping(session));
    }
    switch(typeHandle.getJdbcType()) {
        case Types.BIT:
            return Optional.of(booleanColumnMapping());
        case Types.SMALLINT:
            return Optional.of(smallintColumnMapping());
        case Types.INTEGER:
            return Optional.of(integerColumnMapping());
        case Types.BIGINT:
            return Optional.of(bigintColumnMapping());
        case Types.REAL:
            return Optional.of(realColumnMapping());
        case Types.DOUBLE:
            return Optional.of(doubleColumnMapping());
        case Types.NUMERIC:
            {
                int columnSize = typeHandle.getRequiredColumnSize();
                int precision;
                int decimalDigits = typeHandle.getDecimalDigits().orElse(0);
                if (getDecimalRounding(session) == ALLOW_OVERFLOW) {
                    if (columnSize == PRECISION_OF_UNSPECIFIED_DECIMAL) {
                        // decimal type with unspecified scale - up to 131072 digits before the decimal point; up to 16383 digits after the decimal point)
                        return Optional.of(decimalColumnMapping(createDecimalType(Decimals.MAX_PRECISION, getDecimalDefaultScale(session)), getDecimalRoundingMode(session)));
                    }
                    precision = columnSize;
                    if (precision > Decimals.MAX_PRECISION) {
                        int scale = min(decimalDigits, getDecimalDefaultScale(session));
                        return Optional.of(decimalColumnMapping(createDecimalType(Decimals.MAX_PRECISION, scale), getDecimalRoundingMode(session)));
                    }
                }
                // Map decimal(p, -s) (negative scale) to decimal(p+s, 0).
                precision = columnSize + max(-decimalDigits, 0);
                if (columnSize == PRECISION_OF_UNSPECIFIED_DECIMAL || precision > Decimals.MAX_PRECISION) {
                    break;
                }
                return Optional.of(decimalColumnMapping(createDecimalType(precision, max(decimalDigits, 0)), UNNECESSARY));
            }
        case Types.CHAR:
            return Optional.of(charColumnMapping(typeHandle.getRequiredColumnSize()));
        case Types.VARCHAR:
            if (!jdbcTypeName.equals("varchar")) {
                // This can be e.g. an ENUM
                return Optional.of(typedVarcharColumnMapping(jdbcTypeName));
            }
            return Optional.of(varcharColumnMapping(typeHandle.getRequiredColumnSize()));
        case Types.BINARY:
            return Optional.of(varbinaryColumnMapping());
        case Types.DATE:
            return Optional.of(ColumnMapping.longMapping(DATE, (resultSet, index) -> LocalDate.parse(resultSet.getString(index), DATE_FORMATTER).toEpochDay(), dateWriteFunctionUsingLocalDate()));
        case Types.TIME:
            return Optional.of(timeColumnMapping(typeHandle.getRequiredDecimalDigits()));
        case Types.TIMESTAMP:
            TimestampType timestampType = createTimestampType(typeHandle.getRequiredDecimalDigits());
            return Optional.of(ColumnMapping.longMapping(timestampType, timestampReadFunction(timestampType), PostgreSqlClient::shortTimestampWriteFunction));
        case Types.ARRAY:
            Optional<ColumnMapping> columnMapping = arrayToTrinoType(session, connection, typeHandle);
            if (columnMapping.isPresent()) {
                return columnMapping;
            }
            break;
    }
    if (getUnsupportedTypeHandling(session) == CONVERT_TO_VARCHAR) {
        return mapToUnboundedVarchar(typeHandle);
    }
    return Optional.empty();
}
Also used : UNNECESSARY(java.math.RoundingMode.UNNECESSARY) AggregateFunction(io.trino.spi.connector.AggregateFunction) StandardColumnMappings.bigintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.bigintWriteFunction) LongTimestampWithTimeZone(io.trino.spi.type.LongTimestampWithTimeZone) PredicatePushdownController(io.trino.plugin.jdbc.PredicatePushdownController) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) Map(java.util.Map) StandardColumnMappings.fromTrinoTimestamp(io.trino.plugin.jdbc.StandardColumnMappings.fromTrinoTimestamp) DecimalSessionSessionProperties.getDecimalDefaultScale(io.trino.plugin.jdbc.DecimalSessionSessionProperties.getDecimalDefaultScale) BooleanReadFunction(io.trino.plugin.jdbc.BooleanReadFunction) ImplementStddevSamp(io.trino.plugin.jdbc.aggregation.ImplementStddevSamp) Domain(io.trino.spi.predicate.Domain) PreparedStatement(java.sql.PreparedStatement) Collectors.joining(java.util.stream.Collectors.joining) Stream(java.util.stream.Stream) StandardColumnMappings.longDecimalWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.longDecimalWriteFunction) CONVERT_TO_VARCHAR(io.trino.plugin.jdbc.UnsupportedTypeHandling.CONVERT_TO_VARCHAR) JdbcConnectorExpressionRewriterBuilder(io.trino.plugin.jdbc.expression.JdbcConnectorExpressionRewriterBuilder) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) JdbcTableHandle(io.trino.plugin.jdbc.JdbcTableHandle) REAL(io.trino.spi.type.RealType.REAL) ImplementCountDistinct(io.trino.plugin.jdbc.aggregation.ImplementCountDistinct) JoinCondition(io.trino.spi.connector.JoinCondition) LocalDateTime(java.time.LocalDateTime) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) ImplementCovarianceSamp(io.trino.plugin.jdbc.aggregation.ImplementCovarianceSamp) StandardColumnMappings.bigintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping) TimestampType(io.trino.spi.type.TimestampType) ImplementVariancePop(io.trino.plugin.jdbc.aggregation.ImplementVariancePop) OptionalLong(java.util.OptionalLong) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) UuidType.javaUuidToTrinoUuid(io.trino.spi.type.UuidType.javaUuidToTrinoUuid) StandardColumnMappings.decimalColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping) UuidType.trinoUuidToJavaUuid(io.trino.spi.type.UuidType.trinoUuidToJavaUuid) Timestamps.round(io.trino.spi.type.Timestamps.round) ConnectorExpressionRewriter(io.trino.plugin.base.expression.ConnectorExpressionRewriter) Math.floorDiv(java.lang.Math.floorDiv) RewriteComparison(io.trino.plugin.jdbc.expression.RewriteComparison) QueryBuilder(io.trino.plugin.jdbc.QueryBuilder) MapType(io.trino.spi.type.MapType) StandardTypes(io.trino.spi.type.StandardTypes) IOException(java.io.IOException) ImplementAvgDecimal(io.trino.plugin.jdbc.aggregation.ImplementAvgDecimal) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) IdentifierMapping(io.trino.plugin.jdbc.mapping.IdentifierMapping) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) BlockBuilder(io.trino.spi.block.BlockBuilder) StandardColumnMappings.varbinaryWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryWriteFunction) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) WriteMapping(io.trino.plugin.jdbc.WriteMapping) ArrayMapping(io.trino.plugin.postgresql.PostgreSqlConfig.ArrayMapping) Connection(java.sql.Connection) BiFunction(java.util.function.BiFunction) Array(java.sql.Array) ObjectWriteFunction(io.trino.plugin.jdbc.ObjectWriteFunction) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Block(io.trino.spi.block.Block) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping) JdbcMetadataSessionProperties.getDomainCompactionThreshold(io.trino.plugin.jdbc.JdbcMetadataSessionProperties.getDomainCompactionThreshold) IGNORE(io.trino.plugin.jdbc.UnsupportedTypeHandling.IGNORE) INTEGER(io.trino.spi.type.IntegerType.INTEGER) ImplementStddevPop(io.trino.plugin.jdbc.aggregation.ImplementStddevPop) TypeSignature(io.trino.spi.type.TypeSignature) ImmutableSet(com.google.common.collect.ImmutableSet) DecimalSessionSessionProperties.getDecimalRounding(io.trino.plugin.jdbc.DecimalSessionSessionProperties.getDecimalRounding) Timestamp(java.sql.Timestamp) StandardColumnMappings.realColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.realColumnMapping) LongMath(com.google.common.math.LongMath) UUID(java.util.UUID) Math.min(java.lang.Math.min) Instant(java.time.Instant) JdbcTypeHandle(io.trino.plugin.jdbc.JdbcTypeHandle) BIGINT(io.trino.spi.type.BigintType.BIGINT) INVALID_FUNCTION_ARGUMENT(io.trino.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) LocalDate(java.time.LocalDate) StandardColumnMappings.varcharReadFunction(io.trino.plugin.jdbc.StandardColumnMappings.varcharReadFunction) DecimalType(io.trino.spi.type.DecimalType) StandardColumnMappings.varbinaryColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping) TimeType(io.trino.spi.type.TimeType) Math.floorMod(java.lang.Math.floorMod) StandardColumnMappings.varcharWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.varcharWriteFunction) DoubleReadFunction(io.trino.plugin.jdbc.DoubleReadFunction) DISABLED(io.trino.plugin.postgresql.PostgreSqlConfig.ArrayMapping.DISABLED) ImplementCorr(io.trino.plugin.jdbc.aggregation.ImplementCorr) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) MILLISECONDS_PER_SECOND(io.trino.spi.type.Timestamps.MILLISECONDS_PER_SECOND) JsonTypeUtil.jsonParse(io.trino.plugin.base.util.JsonTypeUtil.jsonParse) ImplementMinMax(io.trino.plugin.jdbc.aggregation.ImplementMinMax) ImplementRegrIntercept(io.trino.plugin.jdbc.aggregation.ImplementRegrIntercept) PICOSECONDS_PER_DAY(io.trino.spi.type.Timestamps.PICOSECONDS_PER_DAY) LongTimestamp(io.trino.spi.type.LongTimestamp) StandardColumnMappings.integerWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.integerWriteFunction) StandardColumnMappings.dateWriteFunctionUsingLocalDate(io.trino.plugin.jdbc.StandardColumnMappings.dateWriteFunctionUsingLocalDate) DISABLE_PUSHDOWN(io.trino.plugin.jdbc.PredicatePushdownController.DISABLE_PUSHDOWN) TimestampWithTimeZoneType.createTimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType) DateTimeFormatter(java.time.format.DateTimeFormatter) TypeManager(io.trino.spi.type.TypeManager) ImplementCount(io.trino.plugin.jdbc.aggregation.ImplementCount) CharType.createCharType(io.trino.spi.type.CharType.createCharType) StandardColumnMappings.booleanColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping) ResultSet(java.sql.ResultSet) StandardColumnMappings.doubleWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.doubleWriteFunction) ImplementVarianceSamp(io.trino.plugin.jdbc.aggregation.ImplementVarianceSamp) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling(io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling) PICOSECONDS_PER_MICROSECOND(io.trino.spi.type.Timestamps.PICOSECONDS_PER_MICROSECOND) UTC_KEY(io.trino.spi.type.TimeZoneKey.UTC_KEY) ImplementAvgFloatingPoint(io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint) DateTimeEncoding.packDateTimeWithZone(io.trino.spi.type.DateTimeEncoding.packDateTimeWithZone) LongWriteFunction(io.trino.plugin.jdbc.LongWriteFunction) ImplementCountAll(io.trino.plugin.jdbc.aggregation.ImplementCountAll) SchemaTableName(io.trino.spi.connector.SchemaTableName) TypeInfo(org.postgresql.core.TypeInfo) LongReadFunction(io.trino.plugin.jdbc.LongReadFunction) SingleMapBlock(io.trino.spi.block.SingleMapBlock) StandardColumnMappings.charReadFunction(io.trino.plugin.jdbc.StandardColumnMappings.charReadFunction) StandardColumnMappings.smallintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.smallintWriteFunction) AggregateFunctionRewriter(io.trino.plugin.base.aggregation.AggregateFunctionRewriter) ConnectionFactory(io.trino.plugin.jdbc.ConnectionFactory) DateTimeEncoding.unpackMillisUtc(io.trino.spi.type.DateTimeEncoding.unpackMillisUtc) PostgreSqlSessionProperties.getArrayMapping(io.trino.plugin.postgresql.PostgreSqlSessionProperties.getArrayMapping) DATE(io.trino.spi.type.DateType.DATE) StandardColumnMappings.doubleColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping) JSON(io.trino.spi.type.StandardTypes.JSON) Slice(io.airlift.slice.Slice) StandardColumnMappings.booleanWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.booleanWriteFunction) ALLOW_OVERFLOW(io.trino.plugin.jdbc.DecimalConfig.DecimalMapping.ALLOW_OVERFLOW) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) TypeUtils.arrayDepth(io.trino.plugin.postgresql.TypeUtils.arrayDepth) FULL_PUSHDOWN(io.trino.plugin.jdbc.PredicatePushdownController.FULL_PUSHDOWN) StandardColumnMappings.charWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.charWriteFunction) PreparedQuery(io.trino.plugin.jdbc.PreparedQuery) ColumnHandle(io.trino.spi.connector.ColumnHandle) PostgreSqlSessionProperties.isEnableStringPushdownWithCollate(io.trino.plugin.postgresql.PostgreSqlSessionProperties.isEnableStringPushdownWithCollate) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) AggregateFunctionRule(io.trino.plugin.base.aggregation.AggregateFunctionRule) JsonTypeUtil.toJsonValue(io.trino.plugin.base.util.JsonTypeUtil.toJsonValue) AS_ARRAY(io.trino.plugin.postgresql.PostgreSqlConfig.ArrayMapping.AS_ARRAY) StandardColumnMappings.realWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.realWriteFunction) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) StandardColumnMappings.smallintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping) TypeUtils.getArrayElementPgTypeName(io.trino.plugin.postgresql.TypeUtils.getArrayElementPgTypeName) ConnectorSession(io.trino.spi.connector.ConnectorSession) CharType(io.trino.spi.type.CharType) DecimalSessionSessionProperties.getDecimalRoundingMode(io.trino.plugin.jdbc.DecimalSessionSessionProperties.getDecimalRoundingMode) TINYINT(io.trino.spi.type.TinyintType.TINYINT) StandardColumnMappings.shortDecimalWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.shortDecimalWriteFunction) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression) JDBC_ERROR(io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR) BaseJdbcConfig(io.trino.plugin.jdbc.BaseJdbcConfig) ReadFunction(io.trino.plugin.jdbc.ReadFunction) DatabaseMetaData.columnNoNulls(java.sql.DatabaseMetaData.columnNoNulls) ImplementSum(io.trino.plugin.jdbc.aggregation.ImplementSum) UnsupportedTypeHandling(io.trino.plugin.jdbc.UnsupportedTypeHandling) LocalTime(java.time.LocalTime) ALREADY_EXISTS(io.trino.spi.StandardErrorCode.ALREADY_EXISTS) ImmutableMap(com.google.common.collect.ImmutableMap) TrinoException(io.trino.spi.TrinoException) ArrayType(io.trino.spi.type.ArrayType) ImplementRegrSlope(io.trino.plugin.jdbc.aggregation.ImplementRegrSlope) String.format(java.lang.String.format) JdbcSortItem(io.trino.plugin.jdbc.JdbcSortItem) TypeUtils.getJdbcObjectArray(io.trino.plugin.postgresql.TypeUtils.getJdbcObjectArray) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Decimals(io.trino.spi.type.Decimals) Optional(java.util.Optional) Math.max(java.lang.Math.max) StandardColumnMappings.integerColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.integerColumnMapping) Types(java.sql.Types) SliceWriteFunction(io.trino.plugin.jdbc.SliceWriteFunction) Logger(io.airlift.log.Logger) StandardColumnMappings.tinyintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.tinyintWriteFunction) PICOSECONDS_PER_NANOSECOND(io.trino.spi.type.Timestamps.PICOSECONDS_PER_NANOSECOND) ImplementCovariancePop(io.trino.plugin.jdbc.aggregation.ImplementCovariancePop) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) HashMap(java.util.HashMap) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType) NANOSECONDS_PER_DAY(io.trino.spi.type.Timestamps.NANOSECONDS_PER_DAY) VarcharType(io.trino.spi.type.VarcharType) TypeUtils.toPgTimestamp(io.trino.plugin.postgresql.TypeUtils.toPgTimestamp) SliceReadFunction(io.trino.plugin.jdbc.SliceReadFunction) Verify.verify(com.google.common.base.Verify.verify) Objects.requireNonNull(java.util.Objects.requireNonNull) BaseJdbcClient(io.trino.plugin.jdbc.BaseJdbcClient) JdbcJoinCondition(io.trino.plugin.jdbc.JdbcJoinCondition) AS_JSON(io.trino.plugin.postgresql.PostgreSqlConfig.ArrayMapping.AS_JSON) PgConnection(org.postgresql.jdbc.PgConnection) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) NANOSECONDS_PER_MILLISECOND(io.trino.spi.type.Timestamps.NANOSECONDS_PER_MILLISECOND) ObjectReadFunction(io.trino.plugin.jdbc.ObjectReadFunction) StandardColumnMappings.timestampReadFunction(io.trino.plugin.jdbc.StandardColumnMappings.timestampReadFunction) Collections(java.util.Collections) TrinoException(io.trino.spi.TrinoException) TimestampType(io.trino.spi.type.TimestampType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) StandardColumnMappings.bigintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping) StandardColumnMappings.decimalColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping) StandardColumnMappings.realColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.realColumnMapping) StandardColumnMappings.varbinaryColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping) StandardColumnMappings.booleanColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping) StandardColumnMappings.doubleColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping) StandardColumnMappings.smallintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping) StandardColumnMappings.integerColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.integerColumnMapping) ImplementAvgFloatingPoint(io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint)

Example 3 with JDBC_ERROR

use of io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR in project trino by trinodb.

the class ClickHouseClient method setTableProperties.

@Override
public void setTableProperties(ConnectorSession session, JdbcTableHandle handle, Map<String, Optional<Object>> nullableProperties) {
    // TODO: Support other table properties
    checkArgument(nullableProperties.size() == 1 && nullableProperties.containsKey(SAMPLE_BY_PROPERTY), "Only support setting 'sample_by' property");
    // TODO: Support sampling key removal when we support a newer version of ClickHouse. See https://github.com/ClickHouse/ClickHouse/pull/30180.
    checkArgument(nullableProperties.values().stream().noneMatch(Optional::isEmpty), "Setting a property to null is not supported");
    Map<String, Object> properties = nullableProperties.entrySet().stream().filter(entry -> entry.getValue().isPresent()).collect(toImmutableMap(Entry::getKey, entry -> entry.getValue().orElseThrow()));
    ImmutableList.Builder<String> tableOptions = ImmutableList.builder();
    ClickHouseTableProperties.getSampleBy(properties).ifPresent(value -> tableOptions.add("SAMPLE BY " + value));
    try (Connection connection = connectionFactory.openConnection(session)) {
        String sql = format("ALTER TABLE %s MODIFY %s", quoted(handle.asPlainTable().getRemoteTableName()), join(" ", tableOptions.build()));
        execute(connection, sql);
    } catch (SQLException e) {
        throw new TrinoException(JDBC_ERROR, e);
    }
}
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) SQLException(java.sql.SQLException) ImmutableList(com.google.common.collect.ImmutableList) Connection(java.sql.Connection) TrinoException(io.trino.spi.TrinoException)

Example 4 with JDBC_ERROR

use of io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR in project trino by trinodb.

the class MySqlClient 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;
    }
    switch(jdbcTypeName.toLowerCase(ENGLISH)) {
        case "tinyint unsigned":
            return Optional.of(smallintColumnMapping());
        case "smallint unsigned":
            return Optional.of(integerColumnMapping());
        case "int unsigned":
            return Optional.of(bigintColumnMapping());
        case "bigint unsigned":
            return Optional.of(decimalColumnMapping(createDecimalType(20)));
        case "json":
            return Optional.of(jsonColumnMapping());
    }
    switch(typeHandle.getJdbcType()) {
        case Types.BIT:
            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.REAL:
            // attempts to treat them as exact in comparisons may lead to problems
            return Optional.of(ColumnMapping.longMapping(REAL, (resultSet, columnIndex) -> floatToRawIntBits(resultSet.getFloat(columnIndex)), realWriteFunction(), DISABLE_PUSHDOWN));
        case Types.DOUBLE:
            return Optional.of(doubleColumnMapping());
        case Types.NUMERIC:
        case Types.DECIMAL:
            int decimalDigits = typeHandle.getDecimalDigits().orElseThrow(() -> new IllegalStateException("decimal digits not present"));
            int precision = typeHandle.getRequiredColumnSize();
            if (getDecimalRounding(session) == ALLOW_OVERFLOW && precision > Decimals.MAX_PRECISION) {
                int scale = min(decimalDigits, getDecimalDefaultScale(session));
                return Optional.of(decimalColumnMapping(createDecimalType(Decimals.MAX_PRECISION, scale), getDecimalRoundingMode(session)));
            }
            // TODO does mysql 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))));
        case Types.CHAR:
            return Optional.of(defaultCharColumnMapping(typeHandle.getRequiredColumnSize(), false));
        // TODO not all these type constants are necessarily used by the JDBC driver
        case Types.VARCHAR:
        case Types.NVARCHAR:
        case Types.LONGVARCHAR:
        case Types.LONGNVARCHAR:
            return Optional.of(defaultVarcharColumnMapping(typeHandle.getRequiredColumnSize(), false));
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            return Optional.of(ColumnMapping.sliceMapping(VARBINARY, varbinaryReadFunction(), varbinaryWriteFunction(), FULL_PUSHDOWN));
        case Types.DATE:
            return Optional.of(dateColumnMappingUsingLocalDate());
        case Types.TIME:
            TimeType timeType = createTimeType(getTimePrecision(typeHandle.getRequiredColumnSize()));
            return Optional.of(timeColumnMapping(timeType));
        case Types.TIMESTAMP:
            TimestampType timestampType = createTimestampType(getTimestampPrecision(typeHandle.getRequiredColumnSize()));
            return Optional.of(timestampColumnMapping(timestampType));
    }
    if (getUnsupportedTypeHandling(session) == CONVERT_TO_VARCHAR) {
        return mapToUnboundedVarchar(typeHandle);
    }
    return Optional.empty();
}
Also used : AggregateFunction(io.trino.spi.connector.AggregateFunction) StandardColumnMappings.bigintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.bigintWriteFunction) ImplementCount(io.trino.plugin.jdbc.aggregation.ImplementCount) JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) StandardColumnMappings.booleanColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping) StandardColumnMappings.defaultVarcharColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.defaultVarcharColumnMapping) ResultSet(java.sql.ResultSet) Map(java.util.Map) StandardColumnMappings.doubleWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.doubleWriteFunction) ImplementVarianceSamp(io.trino.plugin.jdbc.aggregation.ImplementVarianceSamp) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalSessionSessionProperties.getDecimalDefaultScale(io.trino.plugin.jdbc.DecimalSessionSessionProperties.getDecimalDefaultScale) ENGLISH(java.util.Locale.ENGLISH) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling(io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling) ImplementStddevSamp(io.trino.plugin.jdbc.aggregation.ImplementStddevSamp) ImplementAvgFloatingPoint(io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint) ImplementCountAll(io.trino.plugin.jdbc.aggregation.ImplementCountAll) StandardColumnMappings.longTimestampWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.longTimestampWriteFunction) PreparedStatement(java.sql.PreparedStatement) SchemaTableName(io.trino.spi.connector.SchemaTableName) Collectors.joining(java.util.stream.Collectors.joining) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) Stream(java.util.stream.Stream) 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) StandardColumnMappings.timeColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.timeColumnMapping) CONVERT_TO_VARCHAR(io.trino.plugin.jdbc.UnsupportedTypeHandling.CONVERT_TO_VARCHAR) JdbcConnectorExpressionRewriterBuilder(io.trino.plugin.jdbc.expression.JdbcConnectorExpressionRewriterBuilder) StandardColumnMappings.dateColumnMappingUsingLocalDate(io.trino.plugin.jdbc.StandardColumnMappings.dateColumnMappingUsingLocalDate) JdbcTableHandle(io.trino.plugin.jdbc.JdbcTableHandle) DATE(io.trino.spi.type.DateType.DATE) REAL(io.trino.spi.type.RealType.REAL) SQL_STATE_ER_TABLE_EXISTS_ERROR(com.mysql.cj.exceptions.MysqlErrorNumbers.SQL_STATE_ER_TABLE_EXISTS_ERROR) JoinCondition(io.trino.spi.connector.JoinCondition) StandardColumnMappings.doubleColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping) StandardColumnMappings.booleanWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.booleanWriteFunction) ALLOW_OVERFLOW(io.trino.plugin.jdbc.DecimalConfig.DecimalMapping.ALLOW_OVERFLOW) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) DatabaseMetaData(java.sql.DatabaseMetaData) StandardColumnMappings.bigintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping) StandardColumnMappings.timeWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.timeWriteFunction) TimestampType(io.trino.spi.type.TimestampType) StandardColumnMappings.defaultCharColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.defaultCharColumnMapping) ImplementVariancePop(io.trino.plugin.jdbc.aggregation.ImplementVariancePop) Float.floatToRawIntBits(java.lang.Float.floatToRawIntBits) SQLException(java.sql.SQLException) TIMESTAMP_TZ_MILLIS(io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MILLIS) String.join(java.lang.String.join) FULL_PUSHDOWN(io.trino.plugin.jdbc.PredicatePushdownController.FULL_PUSHDOWN) StandardColumnMappings.charWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.charWriteFunction) PreparedQuery(io.trino.plugin.jdbc.PreparedQuery) StandardColumnMappings.decimalColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping) ColumnHandle(io.trino.spi.connector.ColumnHandle) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) ConnectorExpressionRewriter(io.trino.plugin.base.expression.ConnectorExpressionRewriter) AggregateFunctionRule(io.trino.plugin.base.aggregation.AggregateFunctionRule) StandardColumnMappings.realWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.realWriteFunction) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) QueryBuilder(io.trino.plugin.jdbc.QueryBuilder) StandardColumnMappings.varbinaryReadFunction(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryReadFunction) StandardColumnMappings.smallintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping) StandardTypes(io.trino.spi.type.StandardTypes) ConnectorSession(io.trino.spi.connector.ConnectorSession) ImplementAvgDecimal(io.trino.plugin.jdbc.aggregation.ImplementAvgDecimal) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) IdentifierMapping(io.trino.plugin.jdbc.mapping.IdentifierMapping) Strings.emptyToNull(com.google.common.base.Strings.emptyToNull) CharType(io.trino.spi.type.CharType) DecimalSessionSessionProperties.getDecimalRoundingMode(io.trino.plugin.jdbc.DecimalSessionSessionProperties.getDecimalRoundingMode) StandardColumnMappings.timestampColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.timestampColumnMapping) TINYINT(io.trino.spi.type.TinyintType.TINYINT) 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) JDBC_ERROR(io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR) BaseJdbcConfig(io.trino.plugin.jdbc.BaseJdbcConfig) Connection(java.sql.Connection) BiFunction(java.util.function.BiFunction) ImplementSum(io.trino.plugin.jdbc.aggregation.ImplementSum) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping) ALREADY_EXISTS(io.trino.spi.StandardErrorCode.ALREADY_EXISTS) INTEGER(io.trino.spi.type.IntegerType.INTEGER) ImplementStddevPop(io.trino.plugin.jdbc.aggregation.ImplementStddevPop) TypeSignature(io.trino.spi.type.TypeSignature) ImmutableSet(com.google.common.collect.ImmutableSet) DecimalSessionSessionProperties.getDecimalRounding(io.trino.plugin.jdbc.DecimalSessionSessionProperties.getDecimalRounding) Collection(java.util.Collection) TrinoException(io.trino.spi.TrinoException) Math.min(java.lang.Math.min) String.format(java.lang.String.format) JdbcSortItem(io.trino.plugin.jdbc.JdbcSortItem) List(java.util.List) JdbcTypeHandle(io.trino.plugin.jdbc.JdbcTypeHandle) BIGINT(io.trino.spi.type.BigintType.BIGINT) Decimals(io.trino.spi.type.Decimals) Optional(java.util.Optional) Math.max(java.lang.Math.max) 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.timestampWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.timestampWriteFunction) TimeType(io.trino.spi.type.TimeType) StandardColumnMappings.varcharWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.varcharWriteFunction) StandardColumnMappings.tinyintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.tinyintWriteFunction) Type(io.trino.spi.type.Type) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType) Inject(javax.inject.Inject) VarcharType(io.trino.spi.type.VarcharType) TIME_WITH_TIME_ZONE(io.trino.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE) SQL_STATE_SYNTAX_ERROR(com.mysql.cj.exceptions.MysqlErrorNumbers.SQL_STATE_SYNTAX_ERROR) JoinStatistics(io.trino.spi.connector.JoinStatistics) JoinType(io.trino.spi.connector.JoinType) Verify.verify(com.google.common.base.Verify.verify) JsonTypeUtil.jsonParse(io.trino.plugin.base.util.JsonTypeUtil.jsonParse) BaseJdbcClient(io.trino.plugin.jdbc.BaseJdbcClient) RemoteTableName(io.trino.plugin.jdbc.RemoteTableName) JdbcJoinCondition(io.trino.plugin.jdbc.JdbcJoinCondition) ImplementMinMax(io.trino.plugin.jdbc.aggregation.ImplementMinMax) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) StandardColumnMappings.integerWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.integerWriteFunction) StandardColumnMappings.dateWriteFunctionUsingLocalDate(io.trino.plugin.jdbc.StandardColumnMappings.dateWriteFunctionUsingLocalDate) DISABLE_PUSHDOWN(io.trino.plugin.jdbc.PredicatePushdownController.DISABLE_PUSHDOWN) TypeManager(io.trino.spi.type.TypeManager) TrinoException(io.trino.spi.TrinoException) TimestampType(io.trino.spi.type.TimestampType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) StandardColumnMappings.booleanColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping) StandardColumnMappings.defaultVarcharColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.defaultVarcharColumnMapping) StandardColumnMappings.timeColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.timeColumnMapping) 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) StandardColumnMappings.timestampColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.timestampColumnMapping) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping) StandardColumnMappings.tinyintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.tinyintColumnMapping) StandardColumnMappings.integerColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.integerColumnMapping) ImplementAvgFloatingPoint(io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint) TimeType(io.trino.spi.type.TimeType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType)

Example 5 with JDBC_ERROR

use of io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR 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)

Aggregations

Verify.verify (com.google.common.base.Verify.verify)6 ImmutableSet (com.google.common.collect.ImmutableSet)6 BaseJdbcClient (io.trino.plugin.jdbc.BaseJdbcClient)6 BaseJdbcConfig (io.trino.plugin.jdbc.BaseJdbcConfig)6 ColumnMapping (io.trino.plugin.jdbc.ColumnMapping)6 ConnectionFactory (io.trino.plugin.jdbc.ConnectionFactory)6 JdbcColumnHandle (io.trino.plugin.jdbc.JdbcColumnHandle)6 JDBC_ERROR (io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR)6 JdbcTableHandle (io.trino.plugin.jdbc.JdbcTableHandle)6 JdbcTypeHandle (io.trino.plugin.jdbc.JdbcTypeHandle)6 DISABLE_PUSHDOWN (io.trino.plugin.jdbc.PredicatePushdownController.DISABLE_PUSHDOWN)6 QueryBuilder (io.trino.plugin.jdbc.QueryBuilder)6 StandardColumnMappings.bigintWriteFunction (io.trino.plugin.jdbc.StandardColumnMappings.bigintWriteFunction)6 StandardColumnMappings.integerWriteFunction (io.trino.plugin.jdbc.StandardColumnMappings.integerWriteFunction)6 StandardColumnMappings.longDecimalWriteFunction (io.trino.plugin.jdbc.StandardColumnMappings.longDecimalWriteFunction)6 StandardColumnMappings.shortDecimalWriteFunction (io.trino.plugin.jdbc.StandardColumnMappings.shortDecimalWriteFunction)6 StandardColumnMappings.smallintWriteFunction (io.trino.plugin.jdbc.StandardColumnMappings.smallintWriteFunction)6 StandardColumnMappings.tinyintWriteFunction (io.trino.plugin.jdbc.StandardColumnMappings.tinyintWriteFunction)4 StandardColumnMappings.varbinaryWriteFunction (io.trino.plugin.jdbc.StandardColumnMappings.varbinaryWriteFunction)4 StandardColumnMappings.varcharWriteFunction (io.trino.plugin.jdbc.StandardColumnMappings.varcharWriteFunction)4