|
21 | 21 | import com.facebook.presto.plugin.jdbc.BaseJdbcClient; |
22 | 22 | import com.facebook.presto.plugin.jdbc.BaseJdbcConfig; |
23 | 23 | import com.facebook.presto.plugin.jdbc.DriverConnectionFactory; |
| 24 | +import com.facebook.presto.plugin.jdbc.JdbcColumnHandle; |
24 | 25 | import com.facebook.presto.plugin.jdbc.JdbcConnectorId; |
25 | 26 | import com.facebook.presto.plugin.jdbc.JdbcIdentity; |
| 27 | +import com.facebook.presto.plugin.jdbc.JdbcSplit; |
26 | 28 | import com.facebook.presto.plugin.jdbc.JdbcTypeHandle; |
| 29 | +import com.facebook.presto.plugin.jdbc.QueryBuilder; |
27 | 30 | import com.facebook.presto.plugin.jdbc.ReadMapping; |
28 | 31 | import com.facebook.presto.spi.ConnectorSession; |
29 | 32 | import com.facebook.presto.spi.ConnectorTableMetadata; |
|
48 | 51 | import java.sql.PreparedStatement; |
49 | 52 | import java.sql.ResultSet; |
50 | 53 | import java.sql.SQLException; |
| 54 | +import java.util.List; |
| 55 | +import java.util.Map; |
51 | 56 | import java.util.Optional; |
52 | 57 | import java.util.UUID; |
53 | 58 |
|
| 59 | +import static com.facebook.presto.common.type.StandardTypes.GEOMETRY; |
| 60 | +import static com.facebook.presto.common.type.StandardTypes.SPHERICAL_GEOGRAPHY; |
54 | 61 | import static com.facebook.presto.common.type.VarbinaryType.VARBINARY; |
55 | 62 | import static com.facebook.presto.plugin.jdbc.JdbcErrorCode.JDBC_ERROR; |
| 63 | +import static com.facebook.presto.plugin.jdbc.QueryBuilder.quote; |
| 64 | +import static com.facebook.presto.plugin.jdbc.StandardReadMappings.geometryReadMapping; |
56 | 65 | import static com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS; |
57 | 66 | import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT; |
58 | 67 | import static com.fasterxml.jackson.core.JsonFactory.Feature.CANONICALIZE_FIELD_NAMES; |
59 | 68 | import static com.fasterxml.jackson.databind.SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS; |
| 69 | +import static com.google.common.collect.ImmutableMap.toImmutableMap; |
60 | 70 | import static io.airlift.slice.Slices.utf8Slice; |
61 | 71 | import static io.airlift.slice.Slices.wrappedLongArray; |
62 | 72 | import static java.lang.Long.reverseBytes; |
63 | 73 | import static java.lang.String.format; |
64 | 74 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 75 | +import static java.util.function.Function.identity; |
65 | 76 |
|
66 | 77 | public class PostgreSqlClient |
67 | 78 | extends BaseJdbcClient |
@@ -114,16 +125,44 @@ protected String toSqlType(Type type) |
114 | 125 | return super.toSqlType(type); |
115 | 126 | } |
116 | 127 |
|
| 128 | + @Override |
| 129 | + public PreparedStatement buildSql(ConnectorSession session, Connection connection, JdbcSplit split, List<JdbcColumnHandle> columnHandles) throws SQLException |
| 130 | + { |
| 131 | + Map<String, String> columnExpressions = columnHandles.stream() |
| 132 | + .filter(handle -> handle.getJdbcTypeHandle().getJdbcTypeName().equalsIgnoreCase(GEOMETRY)) |
| 133 | + .map(JdbcColumnHandle::getColumnName) |
| 134 | + .collect(toImmutableMap( |
| 135 | + identity(), |
| 136 | + columnName -> "ST_AsBinary(" + quote(identifierQuote, columnName) + ")")); |
| 137 | + |
| 138 | + return new QueryBuilder(identifierQuote).buildSql( |
| 139 | + this, |
| 140 | + session, |
| 141 | + connection, |
| 142 | + split.getCatalogName(), |
| 143 | + split.getSchemaName(), |
| 144 | + split.getTableName(), |
| 145 | + columnHandles, |
| 146 | + columnExpressions, |
| 147 | + split.getTupleDomain(), |
| 148 | + split.getAdditionalPredicate()); |
| 149 | + } |
| 150 | + |
117 | 151 | @Override |
118 | 152 | public Optional<ReadMapping> toPrestoType(ConnectorSession session, JdbcTypeHandle typeHandle) |
119 | 153 | { |
| 154 | + String typeName = typeHandle.getJdbcTypeName(); |
| 155 | + |
120 | 156 | if (typeHandle.getJdbcTypeName().equals("jsonb") || typeHandle.getJdbcTypeName().equals("json")) { |
121 | 157 | return Optional.of(jsonColumnMapping()); |
122 | 158 | } |
123 | 159 |
|
124 | 160 | else if (typeHandle.getJdbcTypeName().equals("uuid")) { |
125 | 161 | return Optional.of(uuidColumnMapping()); |
126 | 162 | } |
| 163 | + else if (typeName.equalsIgnoreCase(GEOMETRY) || typeName.equalsIgnoreCase(SPHERICAL_GEOGRAPHY)) { |
| 164 | + return Optional.of(geometryReadMapping()); |
| 165 | + } |
127 | 166 | return super.toPrestoType(session, typeHandle); |
128 | 167 | } |
129 | 168 |
|
|
0 commit comments