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