Skip to content

Commit

Permalink
Use proper APIs when getting array element type in PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentpoon authored and findepi committed Apr 26, 2019
1 parent 69a6bf9 commit 05a812b
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import io.prestosql.spi.type.TypeSignature;
import io.prestosql.spi.type.VarcharType;
import org.postgresql.Driver;
import org.postgresql.core.TypeInfo;
import org.postgresql.jdbc.PgConnection;
import org.postgresql.util.PGobject;

Expand All @@ -70,7 +71,6 @@

import static com.fasterxml.jackson.core.JsonFactory.Feature.CANONICALIZE_FIELD_NAMES;
import static com.fasterxml.jackson.databind.SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS;
import static com.google.common.base.Preconditions.checkArgument;
import static io.airlift.slice.Slices.utf8Slice;
import static io.prestosql.plugin.jdbc.ColumnMapping.DISABLE_PUSHDOWN;
import static io.prestosql.plugin.jdbc.JdbcErrorCode.JDBC_ERROR;
Expand Down Expand Up @@ -326,14 +326,12 @@ private JdbcTypeHandle getArrayElementTypeHandle(ConnectorSession session, JdbcT
{
String jdbcTypeName = arrayTypeHandle.getJdbcTypeName()
.orElseThrow(() -> new PrestoException(JDBC_ERROR, "Type name is missing: " + arrayTypeHandle));
// PostgreSql array type names are the base element type prepended with "_"
checkArgument(jdbcTypeName.startsWith("_"), "array type must start with '_'");
String elementPgTypeName = jdbcTypeName.substring(1);
try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session))) {
int elementJdbcType = connection.unwrap(PgConnection.class).getTypeInfo().getSQLType(elementPgTypeName);
TypeInfo typeInfo = connection.unwrap(PgConnection.class).getTypeInfo();
int pgElementOid = typeInfo.getPGArrayElement(typeInfo.getPGType(jdbcTypeName));
return new JdbcTypeHandle(
elementJdbcType,
Optional.of(elementPgTypeName),
typeInfo.getSQLType(pgElementOid),
Optional.of(typeInfo.getPGType(pgElementOid)),
arrayTypeHandle.getColumnSize(),
arrayTypeHandle.getDecimalDigits(),
arrayTypeHandle.getArrayDimensions());
Expand Down

0 comments on commit 05a812b

Please sign in to comment.