Search in sources :

Example 11 with JdbcTypeHandle

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

the class BasePostgreSqlClient method getArrayElementTypeHandle.

protected JdbcTypeHandle getArrayElementTypeHandle(Connection connection, JdbcTypeHandle arrayTypeHandle) {
    String jdbcTypeName = arrayTypeHandle.getJdbcTypeName().orElseThrow(() -> new PrestoException(JDBC_ERROR, "Type name is missing: " + arrayTypeHandle));
    try {
        TypeInfo typeInfo = connection.unwrap(PgConnection.class).getTypeInfo();
        int pgElementOid = typeInfo.getPGArrayElement(typeInfo.getPGType(jdbcTypeName));
        return new JdbcTypeHandle(typeInfo.getSQLType(pgElementOid), Optional.of(typeInfo.getPGType(pgElementOid)), arrayTypeHandle.getColumnSize(), arrayTypeHandle.getDecimalDigits(), arrayTypeHandle.getArrayDimensions());
    } catch (SQLException e) {
        throw new PrestoException(JDBC_ERROR, e);
    }
}
Also used : JdbcTypeHandle(io.prestosql.plugin.jdbc.JdbcTypeHandle) SQLException(java.sql.SQLException) PgConnection(org.postgresql.jdbc.PgConnection) PrestoException(io.prestosql.spi.PrestoException) TypeInfo(org.postgresql.core.TypeInfo)

Example 12 with JdbcTypeHandle

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

Example 13 with JdbcTypeHandle

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

the class PostgreSqlClient method toPrestoType.

@Override
public Optional<ColumnMapping> toPrestoType(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle) {
    String jdbcTypeName = typeHandle.getJdbcTypeName().orElseThrow(() -> new PrestoException(JDBC_ERROR, "Type name is missing: " + typeHandle));
    switch(jdbcTypeName) {
        case "uuid":
            return Optional.of(uuidColumnMapping());
        case "jsonb":
        case "json":
            return Optional.of(jsonColumnMapping());
        case "timestamptz":
            // PostgreSQL's "timestamp with time zone" is reported as Types.TIMESTAMP rather than Types.TIMESTAMP_WITH_TIMEZONE
            return Optional.of(timestampWithTimeZoneColumnMapping());
        default:
            break;
    }
    if (typeHandle.getJdbcType() == Types.VARCHAR && !jdbcTypeName.equals("varchar")) {
        // This can be e.g. an ENUM
        return Optional.of(typedVarcharColumnMapping(jdbcTypeName));
    }
    if (typeHandle.getJdbcType() == Types.TIMESTAMP) {
        return Optional.of(timestampColumnMapping());
    }
    if (typeHandle.getJdbcType() == Types.ARRAY && supportArrays) {
        if (!typeHandle.getArrayDimensions().isPresent()) {
            return Optional.empty();
        }
        JdbcTypeHandle elementTypeHandle = getArrayElementTypeHandle(connection, typeHandle);
        String elementTypeName = typeHandle.getJdbcTypeName().orElseThrow(() -> new PrestoException(JDBC_ERROR, "Element type name is missing: " + elementTypeHandle));
        if (elementTypeHandle.getJdbcType() == Types.VARBINARY) {
            // https://github.com/pgjdbc/pgjdbc/pull/1184
            return Optional.empty();
        }
        return toPrestoType(session, connection, elementTypeHandle).map(elementMapping -> {
            ArrayType prestoArrayType = new ArrayType(elementMapping.getType());
            int arrayDimensions = typeHandle.getArrayDimensions().get();
            for (int i = 1; i < arrayDimensions; i++) {
                prestoArrayType = new ArrayType(prestoArrayType);
            }
            return arrayColumnMapping(session, prestoArrayType, elementTypeName);
        });
    }
    return super.toPrestoType(session, connection, typeHandle);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) JdbcTypeHandle(io.prestosql.plugin.jdbc.JdbcTypeHandle) PrestoException(io.prestosql.spi.PrestoException)

Aggregations

JdbcTypeHandle (io.prestosql.plugin.jdbc.JdbcTypeHandle)13 PrestoException (io.prestosql.spi.PrestoException)12 SQLException (java.sql.SQLException)9 ColumnMapping (io.prestosql.plugin.jdbc.ColumnMapping)7 JdbcColumnHandle (io.prestosql.plugin.jdbc.JdbcColumnHandle)7 Connection (java.sql.Connection)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 PgConnection (org.postgresql.jdbc.PgConnection)5 ArrayType (io.prestosql.spi.type.ArrayType)4 VarcharType (io.prestosql.spi.type.VarcharType)4 SuppressFBWarnings (io.prestosql.spi.SuppressFBWarnings)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