Search in sources :

Example 6 with ColumnMapping

use of io.prestosql.plugin.jdbc.ColumnMapping in project hetu-core by openlookeng.

the class HanaClient 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<>();
        Map<String, Type> nodeMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
        nodeMap.putAll(types);
        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);
            // use hetu's precision and scale define from hetu plan tree node's output column type.
            if (dataType == Types.DECIMAL) {
                Type type = nodeMap.get(columnName);
                if (type instanceof DecimalType) {
                    DecimalType decimalType = (DecimalType) type;
                    precision = decimalType.getPrecision();
                    scale = decimalType.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) {
                // 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);
            } else {
                return Collections.emptyMap();
            }
        }
        return builder.build();
    } catch (SQLException e1) {
        // No need to raise an error.
        // This method is used inside applySubQuery method to extract the column types from a sub-query.
        // Returning empty map will indicate that something wrong and let the Hetu to execute the query as usual.
        logger.warn("in hana push down, hana data source error msg[%s] rewrite sql[%s]", e1.getMessage(), sql);
        return Collections.emptyMap();
    } catch (PrestoException e2) {
        // No need to raise an error.
        // This method is used inside applySubQuery method to extract the column types from a sub-query.
        // Returning empty map will indicate that something wrong and let the Hetu to execute the query as usual.
        logger.warn("in hana push down, hana connector error msg[%s] rewrite sql[%s]", e2.getMessage(), sql);
        return Collections.emptyMap();
    }
}
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) TreeMap(java.util.TreeMap) ImmutableMap(com.google.common.collect.ImmutableMap) ResultSetMetaData(java.sql.ResultSetMetaData) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) JdbcTypeHandle(io.prestosql.plugin.jdbc.JdbcTypeHandle) DecimalType(io.prestosql.spi.type.DecimalType) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) ColumnMapping(io.prestosql.plugin.jdbc.ColumnMapping) SuppressFBWarnings(io.prestosql.spi.SuppressFBWarnings)

Example 7 with ColumnMapping

use of io.prestosql.plugin.jdbc.ColumnMapping in project hetu-core by openlookeng.

the class GreenPlumSqlClient method getColumns.

@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);
            // need to deal with different data type here
            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) {
                // 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);
            } else {
                return Collections.emptyMap();
            }
        }
        return builder.build();
    } catch (SQLException | PrestoException e) {
        // No need to raise an error.
        // This method is used inside applySubQuery method to extract the column types from a sub-query.
        // Returning empty map will indicate that something wrong and let the Hetu to execute the query as usual.
        logger.warn("in greenplum push down, greenplum data source error msg[%s] rewrite sql[%s]", e.getMessage(), sql);
        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) PgConnection(org.postgresql.jdbc.PgConnection) 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) JdbcTypeHandle(io.prestosql.plugin.jdbc.JdbcTypeHandle) TinyintType(io.prestosql.spi.type.TinyintType) VarcharType(io.prestosql.spi.type.VarcharType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) StandardColumnMappings.timestampColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.timestampColumnMapping) ColumnMapping(io.prestosql.plugin.jdbc.ColumnMapping)

Example 8 with ColumnMapping

use of io.prestosql.plugin.jdbc.ColumnMapping in project hetu-core by openlookeng.

the class BasePostgreSqlClient method getColumns.

@Override
public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHandle tableHandle) {
    try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session))) {
        Map<String, Integer> arrayColumnDimensions = getArrayColumnDimensions(connection, tableHandle);
        try (ResultSet resultSet = optimizedGetColumns(connection, tableHandle)) {
            List<JdbcColumnHandle> columns = new ArrayList<>();
            while (resultSet.next()) {
                String columnName = resultSet.getString("COLUMN_NAME");
                JdbcTypeHandle typeHandle = new JdbcTypeHandle(resultSet.getInt("DATA_TYPE"), Optional.of(resultSet.getString("TYPE_NAME")), resultSet.getInt("COLUMN_SIZE"), resultSet.getInt("DECIMAL_DIGITS"), Optional.ofNullable(arrayColumnDimensions.get(columnName)));
                Optional<ColumnMapping> columnMapping = toPrestoType(session, connection, typeHandle);
                // skip unsupported column types
                if (columnMapping.isPresent()) {
                    boolean nullable = (resultSet.getInt("NULLABLE") != columnNoNulls);
                    columns.add(new JdbcColumnHandle(columnName, typeHandle, columnMapping.get().getType(), nullable));
                }
            }
            if (columns.isEmpty()) {
                // In rare cases a table might have no columns.
                throw new TableNotFoundException(tableHandle.getSchemaTableName());
            }
            return ImmutableList.copyOf(columns);
        }
    } catch (SQLException e) {
        throw new PrestoException(JDBC_ERROR, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PgConnection(org.postgresql.jdbc.PgConnection) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) ArrayList(java.util.ArrayList) PrestoException(io.prestosql.spi.PrestoException) TableNotFoundException(io.prestosql.spi.connector.TableNotFoundException) JdbcTypeHandle(io.prestosql.plugin.jdbc.JdbcTypeHandle) ResultSet(java.sql.ResultSet) ColumnMapping(io.prestosql.plugin.jdbc.ColumnMapping)

Aggregations

ColumnMapping (io.prestosql.plugin.jdbc.ColumnMapping)8 JdbcColumnHandle (io.prestosql.plugin.jdbc.JdbcColumnHandle)7 JdbcTypeHandle (io.prestosql.plugin.jdbc.JdbcTypeHandle)7 PrestoException (io.prestosql.spi.PrestoException)7 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)7 ImmutableMap (com.google.common.collect.ImmutableMap)5 ColumnHandle (io.prestosql.spi.connector.ColumnHandle)5 DecimalType (io.prestosql.spi.type.DecimalType)5 Type (io.prestosql.spi.type.Type)5 PreparedStatement (java.sql.PreparedStatement)5 ResultSetMetaData (java.sql.ResultSetMetaData)5 SuppressFBWarnings (io.prestosql.spi.SuppressFBWarnings)3 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)3 VarcharType (io.prestosql.spi.type.VarcharType)3 PgConnection (org.postgresql.jdbc.PgConnection)3 StandardColumnMappings.bigintColumnMapping (io.prestosql.plugin.jdbc.StandardColumnMappings.bigintColumnMapping)2 StandardColumnMappings.integerColumnMapping (io.prestosql.plugin.jdbc.StandardColumnMappings.integerColumnMapping)2 StandardColumnMappings.smallintColumnMapping (io.prestosql.plugin.jdbc.StandardColumnMappings.smallintColumnMapping)2 StandardColumnMappings.timestampColumnMapping (io.prestosql.plugin.jdbc.StandardColumnMappings.timestampColumnMapping)2