Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions presto-base-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@
<artifactId>jmxutils</artifactId>
</dependency>

<dependency>
<groupId>com.esri.geometry</groupId>
<artifactId>esri-geometry-api</artifactId>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-geospatial-toolkit</artifactId>
<scope>provided</scope>
</dependency>

<!-- for testing -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.plugin.jdbc;

import com.esri.core.geometry.ogc.OGCGeometry;
import com.facebook.presto.spi.PrestoException;
import io.airlift.slice.Slice;

import static com.esri.core.geometry.ogc.OGCGeometry.fromBinary;
import static com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry;
import static com.facebook.presto.geospatial.serde.EsriGeometrySerde.serialize;
import static com.facebook.presto.geospatial.serde.JtsGeometrySerde.deserialize;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static io.airlift.slice.Slices.utf8Slice;
import static java.util.Objects.requireNonNull;

public class GeometryUtils
{
private GeometryUtils() {}

public static Slice getAsText(Slice input)
{
return utf8Slice(wktFromJtsGeometry(deserialize(input)));
}

public static Slice stGeomFromBinary(Slice input)
{
requireNonNull(input, "input is null");
OGCGeometry geometry;
try {
geometry = fromBinary(input.toByteBuffer().slice());
}
catch (IllegalArgumentException | IndexOutOfBoundsException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Invalid Well-Known Binary (WKB)", e);
}
geometry.setSpatialReference(null);
return serialize(geometry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@
import static com.facebook.presto.common.type.UuidType.UUID;
import static com.facebook.presto.common.type.UuidType.prestoUuidToJavaUuid;
import static com.facebook.presto.common.type.VarbinaryType.VARBINARY;
import static com.facebook.presto.common.type.VarcharType.VARCHAR;
import static com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType;
import static com.facebook.presto.common.type.VarcharType.createVarcharType;
import static com.facebook.presto.plugin.jdbc.GeometryUtils.getAsText;
import static com.facebook.presto.plugin.jdbc.GeometryUtils.stGeomFromBinary;
import static com.facebook.presto.plugin.jdbc.mapping.ReadMapping.createBooleanReadMapping;
import static com.facebook.presto.plugin.jdbc.mapping.ReadMapping.createDoubleReadMapping;
import static com.facebook.presto.plugin.jdbc.mapping.ReadMapping.createLongReadMapping;
Expand Down Expand Up @@ -445,4 +448,9 @@ else if (type instanceof UuidType) {
}
return Optional.empty();
}
public static ReadMapping geometryReadMapping()
{
return createSliceReadMapping(VARCHAR,
(resultSet, columnIndex) -> getAsText(stGeomFromBinary(wrappedBuffer(resultSet.getBytes(columnIndex)))));
}
}
5 changes: 4 additions & 1 deletion presto-docs/src/main/sphinx/connector/postgresql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ The connector maps PostgreSQL types to the corresponding PrestoDB types:
- ``JSON``
* - ``JSONB``
- ``JSON``

* - ``GEOMETRY``
- ``VARCHAR``
* - ``GEOGRAPHY``
- ``VARCHAR``
No other types are supported.

PrestoDB to PostgreSQL type mapping
Expand Down
5 changes: 0 additions & 5 deletions presto-mysql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-geospatial-toolkit</artifactId>
</dependency>

<dependency>
<groupId>com.facebook.drift</groupId>
<artifactId>drift-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package com.facebook.presto.plugin.mysql;

import com.esri.core.geometry.ogc.OGCGeometry;
import com.facebook.presto.common.type.TimestampType;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.common.type.VarcharType;
Expand All @@ -36,7 +35,6 @@
import com.google.common.collect.ImmutableSet;
import com.mysql.cj.jdbc.JdbcStatement;
import com.mysql.jdbc.Driver;
import io.airlift.slice.Slice;

import javax.inject.Inject;

Expand All @@ -52,31 +50,22 @@
import java.util.Optional;
import java.util.Properties;

import static com.esri.core.geometry.ogc.OGCGeometry.fromBinary;
import static com.facebook.presto.common.type.RealType.REAL;
import static com.facebook.presto.common.type.StandardTypes.GEOMETRY;
import static com.facebook.presto.common.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE;
import static com.facebook.presto.common.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE;
import static com.facebook.presto.common.type.VarbinaryType.VARBINARY;
import static com.facebook.presto.common.type.VarcharType.VARCHAR;
import static com.facebook.presto.common.type.Varchars.isVarcharType;
import static com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry;
import static com.facebook.presto.geospatial.serde.EsriGeometrySerde.serialize;
import static com.facebook.presto.geospatial.serde.JtsGeometrySerde.deserialize;
import static com.facebook.presto.plugin.jdbc.DriverConnectionFactory.basicConnectionProperties;
import static com.facebook.presto.plugin.jdbc.JdbcErrorCode.JDBC_ERROR;
import static com.facebook.presto.plugin.jdbc.QueryBuilder.quote;
import static com.facebook.presto.plugin.jdbc.mapping.ReadMapping.createSliceReadMapping;
import static com.facebook.presto.plugin.jdbc.mapping.StandardColumnMappings.geometryReadMapping;
import static com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static io.airlift.slice.Slices.utf8Slice;
import static io.airlift.slice.Slices.wrappedBuffer;
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;

public class MySqlClient
Expand Down Expand Up @@ -253,31 +242,6 @@ public Optional<ReadMapping> toPrestoType(ConnectorSession session, JdbcTypeHand
return super.toPrestoType(session, typeHandle);
}

protected static ReadMapping geometryReadMapping()
{
return createSliceReadMapping(VARCHAR,
(resultSet, columnIndex) -> getAsText(stGeomFromBinary(wrappedBuffer(resultSet.getBytes(columnIndex)))));
}

protected static Slice getAsText(Slice input)
{
return utf8Slice(wktFromJtsGeometry(deserialize(input)));
}

private static Slice stGeomFromBinary(Slice input)
{
requireNonNull(input, "input is null");
OGCGeometry geometry;
try {
geometry = fromBinary(input.toByteBuffer().slice());
}
catch (IllegalArgumentException | IndexOutOfBoundsException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Invalid Well-Known Binary (WKB)", e);
}
geometry.setSpatialReference(null);
return serialize(geometry);
}

@Override
public void createTable(ConnectorSession session, ConnectorTableMetadata tableMetadata)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.sql.SQLException;

import static com.facebook.presto.geospatial.GeoFunctions.stGeomFromBinary;
import static com.facebook.presto.plugin.mysql.MySqlClient.geometryReadMapping;
import static com.facebook.presto.plugin.mysql.MySqlClient.getAsText;
import static com.facebook.presto.plugin.jdbc.GeometryUtils.getAsText;
import static com.facebook.presto.plugin.jdbc.mapping.StandardColumnMappings.geometryReadMapping;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

Expand Down
17 changes: 17 additions & 0 deletions presto-postgresql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,24 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.esri.geometry</groupId>
<artifactId>esri-geometry-api</artifactId>
</dependency>

<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<scope>provided</scope>
</dependency>

<!-- for testing -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-testng-services</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import com.facebook.presto.plugin.jdbc.BaseJdbcClient;
import com.facebook.presto.plugin.jdbc.BaseJdbcConfig;
import com.facebook.presto.plugin.jdbc.DriverConnectionFactory;
import com.facebook.presto.plugin.jdbc.JdbcColumnHandle;
import com.facebook.presto.plugin.jdbc.JdbcConnectorId;
import com.facebook.presto.plugin.jdbc.JdbcIdentity;
import com.facebook.presto.plugin.jdbc.JdbcSplit;
import com.facebook.presto.plugin.jdbc.JdbcTypeHandle;
import com.facebook.presto.plugin.jdbc.QueryBuilder;
import com.facebook.presto.plugin.jdbc.mapping.ReadMapping;
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.ConnectorTableMetadata;
Expand All @@ -48,22 +51,30 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import static com.facebook.presto.common.type.StandardTypes.GEOMETRY;
import static com.facebook.presto.common.type.StandardTypes.SPHERICAL_GEOGRAPHY;
import static com.facebook.presto.common.type.VarbinaryType.VARBINARY;
import static com.facebook.presto.plugin.jdbc.JdbcErrorCode.JDBC_ERROR;
import static com.facebook.presto.plugin.jdbc.QueryBuilder.quote;
import static com.facebook.presto.plugin.jdbc.mapping.ReadMapping.createSliceReadMapping;
import static com.facebook.presto.plugin.jdbc.mapping.StandardColumnMappings.geometryReadMapping;
import static com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
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.collect.ImmutableMap.toImmutableMap;
import static io.airlift.slice.Slices.utf8Slice;
import static io.airlift.slice.Slices.wrappedLongArray;
import static java.lang.Long.reverseBytes;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Locale.ENGLISH;
import static java.util.function.Function.identity;

public class PostgreSqlClient
extends BaseJdbcClient
Expand Down Expand Up @@ -116,16 +127,44 @@ protected String toSqlType(Type type)
return super.toSqlType(type);
}

@Override
public PreparedStatement buildSql(ConnectorSession session, Connection connection, JdbcSplit split, List<JdbcColumnHandle> columnHandles) throws SQLException
{
Map<String, String> columnExpressions = columnHandles.stream()
.filter(handle -> handle.getJdbcTypeHandle().getJdbcTypeName().equalsIgnoreCase(GEOMETRY))
.map(JdbcColumnHandle::getColumnName)
.collect(toImmutableMap(
identity(),
columnName -> "ST_AsBinary(" + quote(identifierQuote, columnName) + ")"));

return new QueryBuilder(identifierQuote).buildSql(
this,
session,
connection,
split.getCatalogName(),
split.getSchemaName(),
split.getTableName(),
columnHandles,
columnExpressions,
split.getTupleDomain(),
split.getAdditionalPredicate());
}

@Override
public Optional<ReadMapping> toPrestoType(ConnectorSession session, JdbcTypeHandle typeHandle)
{
String typeName = typeHandle.getJdbcTypeName();

if (typeHandle.getJdbcTypeName().equals("jsonb") || typeHandle.getJdbcTypeName().equals("json")) {
return Optional.of(jsonReadMapping());
}

else if (typeHandle.getJdbcTypeName().equals("uuid")) {
return Optional.of(uuidReadMapping());
}
else if (typeName.equalsIgnoreCase(GEOMETRY) || typeName.equalsIgnoreCase(SPHERICAL_GEOGRAPHY)) {
return Optional.of(geometryReadMapping());
}
return super.toPrestoType(session, typeHandle);
}

Expand Down
Loading
Loading