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);
}
}
Aggregations