Search in sources :

Example 1 with AbstractType

use of io.prestosql.spi.type.AbstractType in project hetu-core by openlookeng.

the class MySqlClient method getColumns.

@SuppressWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
@Override
public Map<String, ColumnHandle> getColumns(ConnectorSession session, String sql, Map<String, Type> types) {
    try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session));
        PreparedStatement statement = connection.prepareStatement(sql)) {
        ResultSetMetaData metaData = statement.getMetaData();
        ImmutableMap.Builder<String, ColumnHandle> columnBuilder = new ImmutableMap.Builder<>();
        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            String columnName = metaData.getColumnLabel(i);
            String typeName = metaData.getColumnTypeName(i);
            int precision = metaData.getPrecision(i);
            int dataType = metaData.getColumnType(i);
            int scale = metaData.getScale(i);
            // extracted from logical plan during pre-processing
            if (dataType == Types.DECIMAL && (precision > MAX_PRECISION || scale < 0)) {
                // Covert MySql Decimal type to Presto type
                Type type = types.get(columnName.toLowerCase(ENGLISH));
                if (type instanceof AbstractType) {
                    TypeSignature signature = type.getTypeSignature();
                    typeName = signature.getBase().toUpperCase(ENGLISH);
                    dataType = JDBCType.valueOf(typeName).getVendorTypeNumber();
                    if (type instanceof DecimalType) {
                        precision = ((DecimalType) type).getPrecision();
                        scale = ((DecimalType) type).getScale();
                    }
                }
            }
            boolean isNullable = metaData.isNullable(i) != ResultSetMetaData.columnNoNulls;
            JdbcTypeHandle typeHandle = new JdbcTypeHandle(dataType, Optional.ofNullable(typeName), precision, scale, Optional.empty());
            Optional<ColumnMapping> columnMapping;
            try {
                columnMapping = toPrestoType(session, connection, typeHandle);
            } catch (UnsupportedOperationException ex) {
                throw new PrestoException(JDBC_UNSUPPORTED_EXPRESSION, format("Data type [%s] is not support", typeHandle.getJdbcTypeName()));
            }
            // skip unsupported column types
            if (columnMapping.isPresent()) {
                Type type = columnMapping.get().getType();
                JdbcColumnHandle handle = new JdbcColumnHandle(columnName, typeHandle, type, isNullable);
                columnBuilder.put(columnName.toLowerCase(ENGLISH), handle);
            } else {
                return Collections.emptyMap();
            }
        }
        return columnBuilder.build();
    } catch (SQLException | PrestoException e) {
        throw new PrestoException(JDBC_QUERY_GENERATOR_FAILURE, String.format("Query generator failed for [%s]", e.getMessage()));
    }
}
Also used : JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) PreparedStatement(java.sql.PreparedStatement) PrestoException(io.prestosql.spi.PrestoException) ImmutableMap(com.google.common.collect.ImmutableMap) ResultSetMetaData(java.sql.ResultSetMetaData) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) AbstractType(io.prestosql.spi.type.AbstractType) JDBCType(java.sql.JDBCType) VarcharType(io.prestosql.spi.type.VarcharType) TypeSignature(io.prestosql.spi.type.TypeSignature) JdbcTypeHandle(io.prestosql.plugin.jdbc.JdbcTypeHandle) AbstractType(io.prestosql.spi.type.AbstractType) DecimalType(io.prestosql.spi.type.DecimalType) ColumnMapping(io.prestosql.plugin.jdbc.ColumnMapping)

Example 2 with AbstractType

use of io.prestosql.spi.type.AbstractType in project hetu-core by openlookeng.

the class OracleClient method getColumns.

@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
@Override
public Map<String, ColumnHandle> getColumns(ConnectorSession session, String sql, Map<String, Type> types) {
    try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session));
        PreparedStatement statement = connection.prepareStatement(sql)) {
        ResultSetMetaData metadata = statement.getMetaData();
        ImmutableMap.Builder<String, ColumnHandle> builder = new ImmutableMap.Builder<>();
        for (int i = 1; i <= metadata.getColumnCount(); i++) {
            String columnName = metadata.getColumnLabel(i);
            String typeName = metadata.getColumnTypeName(i);
            int precision = metadata.getPrecision(i);
            int dataType = metadata.getColumnType(i);
            int scale = metadata.getScale(i);
            boolean isNullAble = metadata.isNullable(i) != ResultSetMetaData.columnNoNulls;
            // the type of that column as integer.
            if ((dataType == Types.DECIMAL || NUMBER_DATA_TYPE_NAME.equalsIgnoreCase(typeName)) && (precision == 0 || scale < 0)) {
                // Covert Oracle NUMBER(scale) type to Presto types
                String loweredColumnName = columnName.toLowerCase(ENGLISH);
                Type hetuType = types.get(loweredColumnName);
                if (hetuType instanceof AbstractType) {
                    TypeSignature signature = hetuType.getTypeSignature();
                    typeName = signature.getBase().toUpperCase(ENGLISH);
                    dataType = JDBCType.valueOf(typeName).getVendorTypeNumber();
                    if (hetuType instanceof DecimalType && this.roundingMode != RoundingMode.UNNECESSARY) {
                        precision = ((DecimalType) hetuType).getPrecision();
                        scale = ((DecimalType) hetuType).getScale();
                    }
                }
            }
            JdbcTypeHandle typeHandle = new JdbcTypeHandle(dataType, Optional.ofNullable(typeName), precision, scale, Optional.empty());
            Optional<ColumnMapping> columnMapping;
            try {
                columnMapping = toPrestoType(session, connection, typeHandle);
            } catch (UnsupportedOperationException ex) {
                // User configured to fail the query if the data type is not supported
                return Collections.emptyMap();
            }
            // skip unsupported column types
            if (columnMapping.isPresent()) {
                Type type = columnMapping.get().getType();
                JdbcColumnHandle handle = new JdbcColumnHandle(columnName, typeHandle, type, isNullAble);
                builder.put(columnName.toLowerCase(ENGLISH), handle);
            }
        }
        return builder.build();
    } catch (SQLException | PrestoException e) {
        // Returning empty map will indicate that something wrong and let the Presto to execute the query as usual.
        return Collections.emptyMap();
    }
}
Also used : ColumnHandle(io.prestosql.spi.connector.ColumnHandle) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) PreparedStatement(java.sql.PreparedStatement) PrestoException(io.prestosql.spi.PrestoException) ImmutableMap(com.google.common.collect.ImmutableMap) ResultSetMetaData(java.sql.ResultSetMetaData) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) AbstractType(io.prestosql.spi.type.AbstractType) CharType(io.prestosql.spi.type.CharType) JDBCType(java.sql.JDBCType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) TypeSignature(io.prestosql.spi.type.TypeSignature) JdbcTypeHandle(io.prestosql.plugin.jdbc.JdbcTypeHandle) AbstractType(io.prestosql.spi.type.AbstractType) DecimalType(io.prestosql.spi.type.DecimalType) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) StandardColumnMappings.tinyintColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.tinyintColumnMapping) StandardColumnMappings.smallintColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.smallintColumnMapping) StandardColumnMappings.bigintColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.bigintColumnMapping) StandardColumnMappings.varbinaryColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping) StandardColumnMappings.realColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.realColumnMapping) StandardColumnMappings.doubleColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.doubleColumnMapping) StandardColumnMappings.varcharColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.varcharColumnMapping) ColumnMapping(io.prestosql.plugin.jdbc.ColumnMapping) StandardColumnMappings.integerColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.integerColumnMapping) SuppressFBWarnings(io.prestosql.spi.SuppressFBWarnings)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)2 ColumnMapping (io.prestosql.plugin.jdbc.ColumnMapping)2 JdbcColumnHandle (io.prestosql.plugin.jdbc.JdbcColumnHandle)2 JdbcTypeHandle (io.prestosql.plugin.jdbc.JdbcTypeHandle)2 PrestoException (io.prestosql.spi.PrestoException)2 ColumnHandle (io.prestosql.spi.connector.ColumnHandle)2 AbstractType (io.prestosql.spi.type.AbstractType)2 DecimalType (io.prestosql.spi.type.DecimalType)2 Type (io.prestosql.spi.type.Type)2 TypeSignature (io.prestosql.spi.type.TypeSignature)2 VarcharType (io.prestosql.spi.type.VarcharType)2 Connection (java.sql.Connection)2 JDBCType (java.sql.JDBCType)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 SQLException (java.sql.SQLException)2 StandardColumnMappings.bigintColumnMapping (io.prestosql.plugin.jdbc.StandardColumnMappings.bigintColumnMapping)1 StandardColumnMappings.doubleColumnMapping (io.prestosql.plugin.jdbc.StandardColumnMappings.doubleColumnMapping)1 StandardColumnMappings.integerColumnMapping (io.prestosql.plugin.jdbc.StandardColumnMappings.integerColumnMapping)1 StandardColumnMappings.realColumnMapping (io.prestosql.plugin.jdbc.StandardColumnMappings.realColumnMapping)1