Search in sources :

Example 26 with TimestampType

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

the class AccumuloClient method validateColumns.

private static void validateColumns(ConnectorTableMetadata meta) {
    // Check all the column types, and throw an exception if the types of a map are complex
    // While it is a rare case, this is not supported by the Accumulo connector
    ImmutableSet.Builder<String> columnNameBuilder = ImmutableSet.builder();
    for (ColumnMetadata column : meta.getColumns()) {
        if (Types.isMapType(column.getType())) {
            if (Types.isMapType(Types.getKeyType(column.getType())) || Types.isMapType(Types.getValueType(column.getType())) || Types.isArrayType(Types.getKeyType(column.getType())) || Types.isArrayType(Types.getValueType(column.getType()))) {
                throw new TrinoException(INVALID_TABLE_PROPERTY, "Key/value types of a MAP column must be plain types");
            }
        }
        if (column.getType() instanceof TimestampType && ((TimestampType) column.getType()).getPrecision() != 3) {
            throw new TrinoException(NOT_SUPPORTED, format("%s type not supported", column.getType()));
        }
        columnNameBuilder.add(column.getName().toLowerCase(Locale.ENGLISH));
    }
    // Validate the columns are distinct
    if (columnNameBuilder.build().size() != meta.getColumns().size()) {
        throw new TrinoException(INVALID_TABLE_PROPERTY, "Duplicate column names are not supported");
    }
    Optional<Map<String, Pair<String, String>>> columnMapping = AccumuloTableProperties.getColumnMapping(meta.getProperties());
    if (columnMapping.isPresent()) {
        // Validate there are no duplicates in the column mapping
        long distinctMappings = columnMapping.get().values().stream().distinct().count();
        if (distinctMappings != columnMapping.get().size()) {
            throw new TrinoException(INVALID_TABLE_PROPERTY, "Duplicate column family/qualifier pair detected in column mapping, check the value of " + AccumuloTableProperties.COLUMN_MAPPING);
        }
        // Validate no column is mapped to the reserved entry
        String reservedRowIdColumn = AccumuloPageSink.ROW_ID_COLUMN.toString();
        if (columnMapping.get().values().stream().filter(pair -> pair.getKey().equals(reservedRowIdColumn) && pair.getValue().equals(reservedRowIdColumn)).count() > 0) {
            throw new TrinoException(INVALID_TABLE_PROPERTY, format("Column familiy/qualifier mapping of %s:%s is reserved", reservedRowIdColumn, reservedRowIdColumn));
        }
    } else if (AccumuloTableProperties.isExternal(meta.getProperties())) {
        // But column generation is for internal tables only
        throw new TrinoException(INVALID_TABLE_PROPERTY, "Column generation for external tables is not supported, must specify " + AccumuloTableProperties.COLUMN_MAPPING);
    }
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) TrinoException(io.trino.spi.TrinoException) TimestampType(io.trino.spi.type.TimestampType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 27 with TimestampType

use of io.trino.spi.type.TimestampType 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 28 with TimestampType

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

the class MySqlClient method toWriteMapping.

@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
    if (type == BOOLEAN) {
        return WriteMapping.booleanMapping("boolean", booleanWriteFunction());
    }
    if (type == TINYINT) {
        return WriteMapping.longMapping("tinyint", tinyintWriteFunction());
    }
    if (type == SMALLINT) {
        return WriteMapping.longMapping("smallint", smallintWriteFunction());
    }
    if (type == INTEGER) {
        return WriteMapping.longMapping("integer", integerWriteFunction());
    }
    if (type == BIGINT) {
        return WriteMapping.longMapping("bigint", bigintWriteFunction());
    }
    if (type == REAL) {
        return WriteMapping.longMapping("float", realWriteFunction());
    }
    if (type == DOUBLE) {
        return WriteMapping.doubleMapping("double precision", doubleWriteFunction());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        String dataType = format("decimal(%s, %s)", decimalType.getPrecision(), decimalType.getScale());
        if (decimalType.isShort()) {
            return WriteMapping.longMapping(dataType, shortDecimalWriteFunction(decimalType));
        }
        return WriteMapping.objectMapping(dataType, longDecimalWriteFunction(decimalType));
    }
    if (type == DATE) {
        return WriteMapping.longMapping("date", dateWriteFunctionUsingLocalDate());
    }
    if (type instanceof TimeType) {
        TimeType timeType = (TimeType) type;
        if (timeType.getPrecision() <= MAX_SUPPORTED_DATE_TIME_PRECISION) {
            return WriteMapping.longMapping(format("time(%s)", timeType.getPrecision()), timeWriteFunction(timeType.getPrecision()));
        }
        return WriteMapping.longMapping(format("time(%s)", MAX_SUPPORTED_DATE_TIME_PRECISION), timeWriteFunction(MAX_SUPPORTED_DATE_TIME_PRECISION));
    }
    if (TIME_WITH_TIME_ZONE.equals(type) || TIMESTAMP_TZ_MILLIS.equals(type)) {
        throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
    }
    if (type instanceof TimestampType) {
        TimestampType timestampType = (TimestampType) type;
        if (timestampType.getPrecision() <= MAX_SUPPORTED_DATE_TIME_PRECISION) {
            verify(timestampType.getPrecision() <= TimestampType.MAX_SHORT_PRECISION);
            return WriteMapping.longMapping(format("datetime(%s)", timestampType.getPrecision()), timestampWriteFunction(timestampType));
        }
        return WriteMapping.objectMapping(format("datetime(%s)", MAX_SUPPORTED_DATE_TIME_PRECISION), longTimestampWriteFunction(timestampType, MAX_SUPPORTED_DATE_TIME_PRECISION));
    }
    if (VARBINARY.equals(type)) {
        return WriteMapping.sliceMapping("mediumblob", varbinaryWriteFunction());
    }
    if (type instanceof CharType) {
        return WriteMapping.sliceMapping("char(" + ((CharType) type).getLength() + ")", charWriteFunction());
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        String dataType;
        if (varcharType.isUnbounded()) {
            dataType = "longtext";
        } else if (varcharType.getBoundedLength() <= 255) {
            dataType = "tinytext";
        } else if (varcharType.getBoundedLength() <= 65535) {
            dataType = "text";
        } else if (varcharType.getBoundedLength() <= 16777215) {
            dataType = "mediumtext";
        } else {
            dataType = "longtext";
        }
        return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
    }
    if (type.equals(jsonType)) {
        return WriteMapping.sliceMapping("json", varcharWriteFunction());
    }
    throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
Also used : VarcharType(io.trino.spi.type.VarcharType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) DecimalType(io.trino.spi.type.DecimalType) TrinoException(io.trino.spi.TrinoException) TimestampType(io.trino.spi.type.TimestampType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) CharType(io.trino.spi.type.CharType) TimeType(io.trino.spi.type.TimeType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType)

Example 29 with TimestampType

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

the class SingleStoreClient 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;
    }
    Optional<ColumnMapping> unsignedMapping = getUnsignedMapping(typeHandle);
    if (unsignedMapping.isPresent()) {
        return unsignedMapping;
    }
    if (jdbcTypeName.equalsIgnoreCase("json")) {
        return Optional.of(jsonColumnMapping());
    }
    switch(typeHandle.getJdbcType()) {
        case Types.BIT:
        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.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.CHAR:
        case // TODO it it is dummy copied from StandardColumnMappings, verify if it is proper mapping
        Types.NCHAR:
            return Optional.of(defaultCharColumnMapping(typeHandle.getRequiredColumnSize(), false));
        case Types.VARCHAR:
        case Types.LONGVARCHAR:
            return Optional.of(defaultVarcharColumnMapping(typeHandle.getRequiredColumnSize(), false));
        case Types.DECIMAL:
            int precision = typeHandle.getRequiredColumnSize();
            int decimalDigits = typeHandle.getRequiredDecimalDigits();
            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)));
            }
            if (precision > Decimals.MAX_PRECISION) {
                break;
            }
            return Optional.of(decimalColumnMapping(createDecimalType(precision, max(decimalDigits, 0))));
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            return Optional.of(varbinaryColumnMapping());
        case Types.DATE:
            return Optional.of(ColumnMapping.longMapping(DATE, dateReadFunctionUsingLocalDate(), dateWriteFunction()));
        case Types.TIME:
            TimeType timeType = createTimeType(getTimePrecision(typeHandle.getRequiredColumnSize()));
            return Optional.of(ColumnMapping.longMapping(timeType, singleStoreTimeReadFunction(timeType), timeWriteFunction(timeType.getPrecision())));
        case Types.TIMESTAMP:
            // TODO (https://github.com/trinodb/trino/issues/5450) Fix DST handling
            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) 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) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalSessionSessionProperties.getDecimalDefaultScale(io.trino.plugin.jdbc.DecimalSessionSessionProperties.getDecimalDefaultScale) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling(io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling) LongWriteFunction(io.trino.plugin.jdbc.LongWriteFunction) SchemaTableName(io.trino.spi.connector.SchemaTableName) Collectors.joining(java.util.stream.Collectors.joining) LongReadFunction(io.trino.plugin.jdbc.LongReadFunction) DateTimeParseException(java.time.format.DateTimeParseException) Stream(java.util.stream.Stream) 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) 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) 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) 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) Float.floatToRawIntBits(java.lang.Float.floatToRawIntBits) SQLException(java.sql.SQLException) StandardColumnMappings.charWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.charWriteFunction) PreparedQuery(io.trino.plugin.jdbc.PreparedQuery) StandardColumnMappings.decimalColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping) Timestamps.round(io.trino.spi.type.Timestamps.round) ColumnHandle(io.trino.spi.connector.ColumnHandle) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) StandardColumnMappings.realWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.realWriteFunction) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) QueryBuilder(io.trino.plugin.jdbc.QueryBuilder) StandardColumnMappings.smallintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping) StandardTypes(io.trino.spi.type.StandardTypes) ConnectorSession(io.trino.spi.connector.ConnectorSession) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) IdentifierMapping(io.trino.plugin.jdbc.mapping.IdentifierMapping) TIMESTAMP_MICROS(io.trino.spi.type.TimestampType.TIMESTAMP_MICROS) 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) 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) StandardColumnMappings.dateReadFunctionUsingLocalDate(io.trino.plugin.jdbc.StandardColumnMappings.dateReadFunctionUsingLocalDate) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping) LocalTime(java.time.LocalTime) INTEGER(io.trino.spi.type.IntegerType.INTEGER) 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) LocalDate(java.time.LocalDate) Decimals(io.trino.spi.type.Decimals) Optional(java.util.Optional) Math.max(java.lang.Math.max) Pattern(java.util.regex.Pattern) ISO_LOCAL_TIME(java.time.format.DateTimeFormatter.ISO_LOCAL_TIME) 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) StandardColumnMappings.varbinaryColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping) TimeType(io.trino.spi.type.TimeType) StandardColumnMappings.varcharWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.varcharWriteFunction) StandardColumnMappings.tinyintWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.tinyintWriteFunction) PICOSECONDS_PER_NANOSECOND(io.trino.spi.type.Timestamps.PICOSECONDS_PER_NANOSECOND) Type(io.trino.spi.type.Type) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType) NANOSECONDS_PER_DAY(io.trino.spi.type.Timestamps.NANOSECONDS_PER_DAY) Inject(javax.inject.Inject) VarcharType(io.trino.spi.type.VarcharType) JoinStatistics(io.trino.spi.connector.JoinStatistics) JoinType(io.trino.spi.connector.JoinType) Verify.verify(com.google.common.base.Verify.verify) Objects.requireNonNull(java.util.Objects.requireNonNull) JsonTypeUtil.jsonParse(io.trino.plugin.base.util.JsonTypeUtil.jsonParse) BaseJdbcClient(io.trino.plugin.jdbc.BaseJdbcClient) JdbcJoinCondition(io.trino.plugin.jdbc.JdbcJoinCondition) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) PICOSECONDS_PER_DAY(io.trino.spi.type.Timestamps.PICOSECONDS_PER_DAY) StandardColumnMappings.integerWriteFunction(io.trino.plugin.jdbc.StandardColumnMappings.integerWriteFunction) DISABLE_PUSHDOWN(io.trino.plugin.jdbc.PredicatePushdownController.DISABLE_PUSHDOWN) DateTimeFormatter(java.time.format.DateTimeFormatter) 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.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) StandardColumnMappings.varbinaryColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping) TimeType(io.trino.spi.type.TimeType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType)

Example 30 with TimestampType

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

the class SingleStoreClient method toWriteMapping.

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

Aggregations

TimestampType (io.trino.spi.type.TimestampType)32 DecimalType (io.trino.spi.type.DecimalType)24 VarcharType (io.trino.spi.type.VarcharType)24 CharType (io.trino.spi.type.CharType)23 TrinoException (io.trino.spi.TrinoException)16 TimeType (io.trino.spi.type.TimeType)14 Type (io.trino.spi.type.Type)14 TimestampType.createTimestampType (io.trino.spi.type.TimestampType.createTimestampType)12 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)10 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)10 ArrayType (io.trino.spi.type.ArrayType)8 List (java.util.List)8 ImmutableList (com.google.common.collect.ImmutableList)7 BIGINT (io.trino.spi.type.BigintType.BIGINT)6 BOOLEAN (io.trino.spi.type.BooleanType.BOOLEAN)6 DATE (io.trino.spi.type.DateType.DATE)6 DOUBLE (io.trino.spi.type.DoubleType.DOUBLE)6 INTEGER (io.trino.spi.type.IntegerType.INTEGER)6 REAL (io.trino.spi.type.RealType.REAL)6 SMALLINT (io.trino.spi.type.SmallintType.SMALLINT)6