Search in sources :

Example 11 with JdbcColumnHandle

use of io.prestosql.plugin.jdbc.JdbcColumnHandle 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)

Example 12 with JdbcColumnHandle

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

the class OracleClient method getColumnNameMap.

private Map<String, String> getColumnNameMap(ConnectorSession session, JdbcTableHandle tableHandle) {
    // <columnName in lower case, columnName in datasource>
    HashMap<String, String> columnNameMap = new HashMap<>();
    List<JdbcColumnHandle> columnList = getColumns(session, tableHandle);
    for (JdbcColumnHandle columnHandle : columnList) {
        String columnName = columnHandle.getColumnName();
        columnNameMap.put(columnName.toLowerCase(ENGLISH), columnName);
    }
    return columnNameMap;
}
Also used : HashMap(java.util.HashMap) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle)

Example 13 with JdbcColumnHandle

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

the class KylinClient method getColumns.

@Override
public Map<String, ColumnHandle> getColumns(ConnectorSession session, String sql, Map<String, Type> types) {
    log.debug("begin getColumns : values are session %s, sql %s, types %s", session, sql, types);
    try {
        SqlParser parser = SqlParser.create(sql, frameworkConfig.getParserConfig());
        SqlNode sqlNode = parser.parseStmt();
        SqlSelect select = (SqlSelect) sqlNode;
        SqlNodeList nodeList = select.getSelectList();
        ImmutableMap.Builder<String, ColumnHandle> columnBuilder = new ImmutableMap.Builder<>();
        String columnName = "";
        JdbcColumnHandle columnHandle = null;
        for (SqlNode node : nodeList.getList()) {
            if (SqlKind.IDENTIFIER == node.getKind()) {
                SqlIdentifier sqlBasicCall = (SqlIdentifier) node;
                columnName = sqlBasicCall.getSimple();
            } else {
                SqlBasicCall sqlBasicCall = (SqlBasicCall) node;
                columnName = sqlBasicCall.operands[1].toString();
            }
            Type type = types.get(columnName.toLowerCase(Locale.ENGLISH));
            if (type instanceof BigintType) {
                columnHandle = new JdbcColumnHandle(columnName, KylinJdbcTypeHandle.JDBC_BIGINT, BigintType.BIGINT, true);
            } else if (type instanceof IntegerType) {
                columnHandle = new JdbcColumnHandle(columnName, KylinJdbcTypeHandle.JDBC_INTEGER, IntegerType.INTEGER, true);
            } else if (type instanceof DoubleType) {
                columnHandle = new JdbcColumnHandle(columnName, KylinJdbcTypeHandle.JDBC_DOUBLE, DoubleType.DOUBLE, true);
            } else if (type instanceof VarcharType) {
                columnHandle = new JdbcColumnHandle(columnName, KylinJdbcTypeHandle.JDBC_VARCHAR, VarcharType.VARCHAR, true);
            } else {
                // todo default VarcharType
                columnHandle = new JdbcColumnHandle(columnName, KylinJdbcTypeHandle.JDBC_VARCHAR, VarcharType.VARCHAR, true);
            }
            if (columnHandle != null) {
                columnBuilder.put(columnName.toLowerCase(ENGLISH), columnHandle);
            }
        }
        return columnBuilder.build();
    } catch (Exception e) {
        log.debug("There is a problem %s", e.getLocalizedMessage());
        log.debug("Error message: " + e.getStackTrace());
        // Returning empty map will indicate that something wrong and let the Presto to execute the query as usual.
        return Collections.emptyMap();
    }
}
Also used : JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) VarcharType(io.prestosql.spi.type.VarcharType) QueryBuilder(io.prestosql.plugin.jdbc.QueryBuilder) SqlParser(org.apache.calcite.sql.parser.SqlParser) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) ImmutableMap(com.google.common.collect.ImmutableMap) BigintType(io.prestosql.spi.type.BigintType) PrestoException(io.prestosql.spi.PrestoException) SQLException(java.sql.SQLException) IntegerType(io.prestosql.spi.type.IntegerType) Type(io.prestosql.spi.type.Type) BigintType(io.prestosql.spi.type.BigintType) DoubleType(io.prestosql.spi.type.DoubleType) IntegerType(io.prestosql.spi.type.IntegerType) VarcharType(io.prestosql.spi.type.VarcharType) SqlSelect(org.apache.calcite.sql.SqlSelect) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) DoubleType(io.prestosql.spi.type.DoubleType) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 14 with JdbcColumnHandle

use of io.prestosql.plugin.jdbc.JdbcColumnHandle 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 15 with JdbcColumnHandle

use of io.prestosql.plugin.jdbc.JdbcColumnHandle 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)

Aggregations

JdbcColumnHandle (io.prestosql.plugin.jdbc.JdbcColumnHandle)17 JdbcTypeHandle (io.prestosql.plugin.jdbc.JdbcTypeHandle)8 PrestoException (io.prestosql.spi.PrestoException)8 Type (io.prestosql.spi.type.Type)8 SQLException (java.sql.SQLException)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 ColumnMapping (io.prestosql.plugin.jdbc.ColumnMapping)7 ColumnHandle (io.prestosql.spi.connector.ColumnHandle)7 Connection (java.sql.Connection)7 VarcharType (io.prestosql.spi.type.VarcharType)6 PreparedStatement (java.sql.PreparedStatement)6 JdbcTableHandle (io.prestosql.plugin.jdbc.JdbcTableHandle)5 SchemaTableName (io.prestosql.spi.connector.SchemaTableName)5 DecimalType (io.prestosql.spi.type.DecimalType)5 JdbcIdentity (io.prestosql.plugin.jdbc.JdbcIdentity)4 ResultSetMetaData (java.sql.ResultSetMetaData)4 Test (org.testng.annotations.Test)4 SuppressFBWarnings (io.prestosql.spi.SuppressFBWarnings)3 BigintType (io.prestosql.spi.type.BigintType)3 CharType (io.prestosql.spi.type.CharType)3