Search in sources :

Example 51 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class GeoFunctions method toSphericalGeography.

@Description("Converts a Geometry object to a SphericalGeography object")
@ScalarFunction("to_spherical_geography")
@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME)
public static Slice toSphericalGeography(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    // "every point in input is in range" <=> "the envelope of input is in range"
    Envelope envelope = deserializeEnvelope(input);
    if (!envelope.isEmpty()) {
        checkLatitude(envelope.getYMin());
        checkLatitude(envelope.getYMax());
        checkLongitude(envelope.getXMin());
        checkLongitude(envelope.getXMax());
    }
    OGCGeometry geometry = deserialize(input);
    if (geometry.is3D()) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Cannot convert 3D geometry to a spherical geography");
    }
    GeometryCursor cursor = geometry.getEsriGeometryCursor();
    while (true) {
        com.esri.core.geometry.Geometry subGeometry = cursor.next();
        if (subGeometry == null) {
            break;
        }
        if (!GEOMETRY_TYPES_FOR_SPHERICAL_GEOGRAPHY.contains(subGeometry.getType())) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Cannot convert geometry of this type to spherical geography: " + subGeometry.getType());
        }
    }
    return input;
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometryCursor(com.esri.core.geometry.GeometryCursor) ListeningGeometryCursor(com.esri.core.geometry.ListeningGeometryCursor) TrinoException(io.trino.spi.TrinoException) GeometrySerde.deserializeEnvelope(io.trino.geospatial.serde.GeometrySerde.deserializeEnvelope) Envelope(com.esri.core.geometry.Envelope) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 52 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class GeoFunctions method geometryFromHadoopShape.

@Description("Returns a Geometry type object from Spatial Framework for Hadoop representation")
@ScalarFunction("geometry_from_hadoop_shape")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice geometryFromHadoopShape(@SqlType(VARBINARY) Slice input) {
    requireNonNull(input, "input is null");
    try {
        OGCGeometry geometry = OGCGeometry.fromEsriShape(getShapeByteBuffer(input));
        String wkt = geometryToWkt(geometry.getEsriGeometry(), getWktExportFlags(input));
        return serialize(OGCGeometry.fromText(wkt));
    } catch (IndexOutOfBoundsException | UnsupportedOperationException | IllegalArgumentException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid Hadoop shape", e);
    }
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) TrinoException(io.trino.spi.TrinoException) OGCLineString(com.esri.core.geometry.ogc.OGCLineString) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 53 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class GeoFunctions method geometryFromText.

private static OGCGeometry geometryFromText(Slice input) {
    OGCGeometry geometry;
    try {
        geometry = OGCGeometry.fromText(input.toStringUtf8());
    } catch (IllegalArgumentException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid WKT: " + input.toStringUtf8(), e);
    }
    geometry.setSpatialReference(null);
    return geometry;
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) TrinoException(io.trino.spi.TrinoException)

Example 54 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class GeoFunctions method stMultiPoint.

@SqlNullable
@Description("Returns a multi-point geometry formed from input points")
@ScalarFunction("ST_MultiPoint")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stMultiPoint(@SqlType("array(" + GEOMETRY_TYPE_NAME + ")") Block input) {
    MultiPoint multipoint = new MultiPoint();
    for (int i = 0; i < input.getPositionCount(); i++) {
        if (input.isNull(i)) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to ST_MultiPoint: null at index %s", i + 1));
        }
        Slice slice = GEOMETRY.getSlice(input, i);
        OGCGeometry geometry = deserialize(slice);
        if (!(geometry instanceof OGCPoint)) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to ST_MultiPoint: geometry is not a point: %s at index %s", geometry.geometryType(), i + 1));
        }
        OGCPoint point = (OGCPoint) geometry;
        if (point.isEmpty()) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to ST_MultiPoint: empty point at index %s", i + 1));
        }
        multipoint.add(point.X(), point.Y());
    }
    if (multipoint.getPointCount() == 0) {
        return null;
    }
    return serialize(createFromEsriGeometry(multipoint, null, true));
}
Also used : MultiPoint(com.esri.core.geometry.MultiPoint) OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) TrinoException(io.trino.spi.TrinoException) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 55 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class FileSystemExchangeManager method createExchange.

@Override
public Exchange createExchange(ExchangeContext context, int outputPartitionCount) {
    Optional<SecretKey> secretKey = Optional.empty();
    if (exchangeEncryptionEnabled) {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(KEY_BITS);
            secretKey = Optional.of(keyGenerator.generateKey());
        } catch (NoSuchAlgorithmException e) {
            throw new TrinoException(GENERIC_INTERNAL_ERROR, "Failed to generate new secret key: " + e.getMessage(), e);
        }
    }
    FileSystemExchange exchange = new FileSystemExchange(baseDirectory, exchangeStorage, context, outputPartitionCount, secretKey, executor);
    exchange.initialize();
    return exchange;
}
Also used : SecretKey(javax.crypto.SecretKey) TrinoException(io.trino.spi.TrinoException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

TrinoException (io.trino.spi.TrinoException)623 IOException (java.io.IOException)151 ImmutableList (com.google.common.collect.ImmutableList)105 List (java.util.List)100 Type (io.trino.spi.type.Type)93 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)90 SchemaTableName (io.trino.spi.connector.SchemaTableName)83 Path (org.apache.hadoop.fs.Path)83 Optional (java.util.Optional)79 ArrayList (java.util.ArrayList)77 Map (java.util.Map)76 ImmutableMap (com.google.common.collect.ImmutableMap)70 Objects.requireNonNull (java.util.Objects.requireNonNull)69 ConnectorSession (io.trino.spi.connector.ConnectorSession)63 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)62 ImmutableSet (com.google.common.collect.ImmutableSet)56 VarcharType (io.trino.spi.type.VarcharType)54 Set (java.util.Set)54 Slice (io.airlift.slice.Slice)53 Table (io.trino.plugin.hive.metastore.Table)52