Search in sources :

Example 1 with JdbcColumnHandle

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

the class BasePostgreSqlClient 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 2 with JdbcColumnHandle

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

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

the class GreenPlumSqlClient 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 = getColumns(tableHandle, connection.getMetaData())) {
            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) StandardColumnMappings.timestampColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.timestampColumnMapping) ColumnMapping(io.prestosql.plugin.jdbc.ColumnMapping)

Example 4 with JdbcColumnHandle

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

the class TestHanaClient method testDropColumn.

/**
 * testDropColumn
 */
@Test
public void testDropColumn() {
    String schemaName = getNameByUpperCaseIdentifiers(database.getSchema());
    // get actual table example_*
    String tableName = getNameByUpperCaseIdentifiers(database.getActualTable("example"));
    List<String> expectedColumns = Arrays.asList("text", "text_short", "value");
    List<String> actualColumns = getActualColumns(schemaName, tableName);
    assertEquals(actualColumns, expectedColumns);
    JdbcIdentity identity = JdbcIdentity.from(SESSION);
    SchemaTableName schemaTableName = new SchemaTableName(schemaName, tableName);
    JdbcTableHandle tableHandle = hanaClient.getTableHandle(identity, schemaTableName).get();
    List<JdbcColumnHandle> columns = hanaClient.getColumns(SESSION, tableHandle);
    JdbcColumnHandle columnHandle = null;
    for (JdbcColumnHandle column : columns) {
        if ("value".equalsIgnoreCase(column.getColumnName())) {
            columnHandle = column;
            break;
        }
    }
    hanaClient.dropColumn(identity, tableHandle, columnHandle);
    expectedColumns = Arrays.asList("text", "text_short");
    actualColumns = getActualColumns(schemaName, tableName);
    assertEquals(actualColumns, expectedColumns);
}
Also used : JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) JdbcTableHandle(io.prestosql.plugin.jdbc.JdbcTableHandle) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) JdbcIdentity(io.prestosql.plugin.jdbc.JdbcIdentity) Test(org.testng.annotations.Test)

Example 5 with JdbcColumnHandle

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

the class ClickHouseQueryBuilder method buildSql.

@Override
public PreparedStatement buildSql(JdbcClient client, ConnectorSession session, Connection connection, String catalog, String schema, String table, List<JdbcColumnHandle> columns, TupleDomain<ColumnHandle> tupleDomain, Optional<String> additionalPredicate, Function<String, String> sqlFunction) throws SQLException {
    StringBuilder sql = new StringBuilder();
    String columnNames = columns.stream().map(JdbcColumnHandle::getColumnName).map(this::quote).collect(joining(", "));
    sql.append("SELECT ");
    sql.append(columnNames);
    if (columns.isEmpty()) {
        sql.append("null");
    }
    sql.append(" FROM ");
    if (!isNullOrEmpty(schema)) {
        sql.append(quote(schema)).append('.');
    }
    if (isPushSubQueryDown) {
        sql.append("(").append(table).append(") pushdown");
    } else {
        sql.append(quote(table));
    }
    List<TypeAndValue> accumulator = new ArrayList<>();
    List<String> clauses = toConjuncts(client, session, connection, columns, tupleDomain, accumulator);
    if (additionalPredicate.isPresent()) {
        clauses = ImmutableList.<String>builder().addAll(clauses).add(additionalPredicate.get()).build();
    }
    if (!clauses.isEmpty()) {
        sql.append(" WHERE ").append(Joiner.on(" AND ").join(clauses));
    }
    String query = sqlFunction.apply(sql.toString());
    PreparedStatement statement = client.getPreparedStatement(connection, query);
    for (int i = 0; i < accumulator.size(); i++) {
        TypeAndValue typeAndValue = accumulator.get(i);
        int parameterIndex = i + 1;
        Type type = typeAndValue.getType();
        WriteFunction writeFunction = client.toPrestoType(session, connection, typeAndValue.getTypeHandle()).orElseThrow(() -> new VerifyException(format("Unsupported type %s with handle %s", type, typeAndValue.getTypeHandle()))).getWriteFunction();
        Class<?> javaType = type.getJavaType();
        Object value = typeAndValue.getValue();
        if (javaType == boolean.class) {
            ((BooleanWriteFunction) writeFunction).set(statement, parameterIndex, (boolean) value);
        } else if (javaType == long.class) {
            ((LongWriteFunction) writeFunction).set(statement, parameterIndex, (long) value);
        } else if (javaType == double.class) {
            ((DoubleWriteFunction) writeFunction).set(statement, parameterIndex, (double) value);
        } else if (javaType == Slice.class) {
            ((SliceWriteFunction) writeFunction).set(statement, parameterIndex, (Slice) value);
        } else if (javaType == Block.class) {
            ((BlockWriteFunction) writeFunction).set(statement, parameterIndex, (Block) value);
        } else {
            throw new VerifyException(format("Unexpected type %s with java type %s", type, javaType.getName()));
        }
    }
    return statement;
}
Also used : JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) ArrayList(java.util.ArrayList) DoubleWriteFunction(io.prestosql.plugin.jdbc.DoubleWriteFunction) PreparedStatement(java.sql.PreparedStatement) BooleanWriteFunction(io.prestosql.plugin.jdbc.BooleanWriteFunction) Type(io.prestosql.spi.type.Type) RealType(io.prestosql.spi.type.RealType) ArrayType(io.prestosql.spi.type.ArrayType) TimestampType(io.prestosql.spi.type.TimestampType) BigintType(io.prestosql.spi.type.BigintType) CharType(io.prestosql.spi.type.CharType) DoubleType(io.prestosql.spi.type.DoubleType) TimestampWithTimeZoneType(io.prestosql.spi.type.TimestampWithTimeZoneType) SmallintType(io.prestosql.spi.type.SmallintType) TimeWithTimeZoneType(io.prestosql.spi.type.TimeWithTimeZoneType) IntegerType(io.prestosql.spi.type.IntegerType) TimeType(io.prestosql.spi.type.TimeType) TinyintType(io.prestosql.spi.type.TinyintType) DateType(io.prestosql.spi.type.DateType) BooleanType(io.prestosql.spi.type.BooleanType) VarcharType(io.prestosql.spi.type.VarcharType) VerifyException(com.google.common.base.VerifyException) SliceWriteFunction(io.prestosql.plugin.jdbc.SliceWriteFunction) BlockWriteFunction(io.prestosql.plugin.jdbc.BlockWriteFunction) LongWriteFunction(io.prestosql.plugin.jdbc.LongWriteFunction) DoubleWriteFunction(io.prestosql.plugin.jdbc.DoubleWriteFunction) WriteFunction(io.prestosql.plugin.jdbc.WriteFunction) BooleanWriteFunction(io.prestosql.plugin.jdbc.BooleanWriteFunction) Slice(io.airlift.slice.Slice) BlockWriteFunction(io.prestosql.plugin.jdbc.BlockWriteFunction)

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