Search in sources :

Example 1 with UnsupportedTypeHandling

use of io.trino.plugin.jdbc.UnsupportedTypeHandling in project trino by trinodb.

the class PostgreSqlClient method getColumns.

@Override
public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHandle tableHandle) {
    if (tableHandle.getColumns().isPresent()) {
        return tableHandle.getColumns().get();
    }
    checkArgument(tableHandle.isNamedRelation(), "Cannot get columns for %s", tableHandle);
    SchemaTableName schemaTableName = tableHandle.getRequiredNamedRelation().getSchemaTableName();
    try (Connection connection = connectionFactory.openConnection(session)) {
        Map<String, Integer> arrayColumnDimensions = ImmutableMap.of();
        if (getArrayMapping(session) == AS_ARRAY) {
            arrayColumnDimensions = getArrayColumnDimensions(connection, tableHandle);
        }
        try (ResultSet resultSet = getColumns(tableHandle, connection.getMetaData())) {
            int allColumns = 0;
            List<JdbcColumnHandle> columns = new ArrayList<>();
            while (resultSet.next()) {
                allColumns++;
                String columnName = resultSet.getString("COLUMN_NAME");
                JdbcTypeHandle typeHandle = new JdbcTypeHandle(getInteger(resultSet, "DATA_TYPE").orElseThrow(() -> new IllegalStateException("DATA_TYPE is null")), Optional.of(resultSet.getString("TYPE_NAME")), getInteger(resultSet, "COLUMN_SIZE"), getInteger(resultSet, "DECIMAL_DIGITS"), Optional.ofNullable(arrayColumnDimensions.get(columnName)), Optional.empty());
                Optional<ColumnMapping> columnMapping = toColumnMapping(session, connection, typeHandle);
                log.debug("Mapping data type of '%s' column '%s': %s mapped to %s", schemaTableName, columnName, typeHandle, columnMapping);
                // skip unsupported column types
                if (columnMapping.isPresent()) {
                    boolean nullable = (resultSet.getInt("NULLABLE") != columnNoNulls);
                    Optional<String> comment = Optional.ofNullable(resultSet.getString("REMARKS"));
                    columns.add(JdbcColumnHandle.builder().setColumnName(columnName).setJdbcTypeHandle(typeHandle).setColumnType(columnMapping.get().getType()).setNullable(nullable).setComment(comment).build());
                }
                if (columnMapping.isEmpty()) {
                    UnsupportedTypeHandling unsupportedTypeHandling = getUnsupportedTypeHandling(session);
                    verify(unsupportedTypeHandling == IGNORE, "Unsupported type handling is set to %s, but toColumnMapping() returned empty for %s", unsupportedTypeHandling, typeHandle);
                }
            }
            if (columns.isEmpty()) {
                // A table may have no supported columns. In rare cases a table might have no columns at all.
                throw new TableNotFoundException(tableHandle.getSchemaTableName(), format("Table '%s' has no supported columns (all %s columns are not supported)", tableHandle.getSchemaTableName(), allColumns));
            }
            return ImmutableList.copyOf(columns);
        }
    } catch (SQLException e) {
        throw new TrinoException(JDBC_ERROR, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PgConnection(org.postgresql.jdbc.PgConnection) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) ArrayList(java.util.ArrayList) SchemaTableName(io.trino.spi.connector.SchemaTableName) ImplementAvgFloatingPoint(io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) JdbcTypeHandle(io.trino.plugin.jdbc.JdbcTypeHandle) ResultSet(java.sql.ResultSet) TrinoException(io.trino.spi.TrinoException) TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling(io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling) UnsupportedTypeHandling(io.trino.plugin.jdbc.UnsupportedTypeHandling) StandardColumnMappings.bigintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping) StandardColumnMappings.decimalColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping) ColumnMapping(io.trino.plugin.jdbc.ColumnMapping) StandardColumnMappings.realColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.realColumnMapping) StandardColumnMappings.varbinaryColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping) StandardColumnMappings.booleanColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping) StandardColumnMappings.doubleColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping) StandardColumnMappings.smallintColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping) StandardColumnMappings.integerColumnMapping(io.trino.plugin.jdbc.StandardColumnMappings.integerColumnMapping)

Aggregations

ColumnMapping (io.trino.plugin.jdbc.ColumnMapping)1 JdbcColumnHandle (io.trino.plugin.jdbc.JdbcColumnHandle)1 JdbcTypeHandle (io.trino.plugin.jdbc.JdbcTypeHandle)1 StandardColumnMappings.bigintColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping)1 StandardColumnMappings.booleanColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping)1 StandardColumnMappings.decimalColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping)1 StandardColumnMappings.doubleColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping)1 StandardColumnMappings.integerColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.integerColumnMapping)1 StandardColumnMappings.realColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.realColumnMapping)1 StandardColumnMappings.smallintColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping)1 StandardColumnMappings.varbinaryColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping)1 TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling (io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling)1 UnsupportedTypeHandling (io.trino.plugin.jdbc.UnsupportedTypeHandling)1 ImplementAvgFloatingPoint (io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint)1 TrinoException (io.trino.spi.TrinoException)1 SchemaTableName (io.trino.spi.connector.SchemaTableName)1 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1