Search in sources :

Example 11 with SqlNullable

use of io.prestosql.spi.function.SqlNullable in project hetu-core by openlookeng.

the class JsonOperators method castToBigint.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(BIGINT)
public static Long castToBigint(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Long result = currentTokenAsBigint(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BIGINT");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, String.format("Cannot cast '%s' to %s", json.toStringUtf8(), BIGINT), e);
    }
}
Also used : JsonCastException(io.prestosql.util.JsonCastException) PrestoException(io.prestosql.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(io.prestosql.spi.function.ScalarOperator) SqlNullable(io.prestosql.spi.function.SqlNullable) SqlType(io.prestosql.spi.function.SqlType)

Example 12 with SqlNullable

use of io.prestosql.spi.function.SqlNullable in project hetu-core by openlookeng.

the class JsonOperators method castToTinyint.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(TINYINT)
public static Long castToTinyint(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Long result = currentTokenAsTinyint(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to TINYINT");
        return result;
    } catch (PrestoException e) {
        if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, String.format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause());
        }
        throw e;
    } catch (IllegalArgumentException | IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, String.format("Cannot cast '%s' to %s", json.toStringUtf8(), TINYINT), e);
    }
}
Also used : JsonCastException(io.prestosql.util.JsonCastException) PrestoException(io.prestosql.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(io.prestosql.spi.function.ScalarOperator) SqlNullable(io.prestosql.spi.function.SqlNullable) SqlType(io.prestosql.spi.function.SqlType)

Example 13 with SqlNullable

use of io.prestosql.spi.function.SqlNullable in project hetu-core by openlookeng.

the class GeoFunctions method invalidReason.

@Description("Returns the reason for why the input geometry is not valid. Returns null if the input is valid.")
@ScalarFunction("geometry_invalid_reason")
@SqlType(VARCHAR)
@SqlNullable
public static Slice invalidReason(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    GeometryCursor cursor = deserialize(input).getEsriGeometryCursor();
    NonSimpleResult result = new NonSimpleResult();
    while (true) {
        com.esri.core.geometry.Geometry geometry = cursor.next();
        if (geometry == null) {
            return null;
        }
        if (!OperatorSimplifyOGC.local().isSimpleOGC(geometry, null, true, result, null)) {
            String reasonText = NON_SIMPLE_REASONS.getOrDefault(result.m_reason, result.m_reason.name());
            if (!(geometry instanceof MultiVertexGeometry)) {
                return utf8Slice(reasonText);
            }
            MultiVertexGeometry multiVertexGeometry = (MultiVertexGeometry) geometry;
            if (result.m_vertexIndex1 >= 0 && result.m_vertexIndex2 >= 0) {
                Point point1 = multiVertexGeometry.getPoint(result.m_vertexIndex1);
                Point point2 = multiVertexGeometry.getPoint(result.m_vertexIndex2);
                return utf8Slice(format("%s at or near (%s %s) and (%s %s)", reasonText, point1.getX(), point1.getY(), point2.getX(), point2.getY()));
            }
            if (result.m_vertexIndex1 >= 0) {
                Point point = multiVertexGeometry.getPoint(result.m_vertexIndex1);
                return utf8Slice(format("%s at or near (%s %s)", reasonText, point.getX(), point.getY()));
            }
            return utf8Slice(reasonText);
        }
    }
}
Also used : GeometryCursor(com.esri.core.geometry.GeometryCursor) ListeningGeometryCursor(com.esri.core.geometry.ListeningGeometryCursor) MultiVertexGeometry(com.esri.core.geometry.MultiVertexGeometry) NonSimpleResult(com.esri.core.geometry.NonSimpleResult) OGCLineString(com.esri.core.geometry.ogc.OGCLineString) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Example 14 with SqlNullable

use of io.prestosql.spi.function.SqlNullable in project hetu-core by openlookeng.

the class GeoFunctions method stY.

@SqlNullable
@Description("Return the Y coordinate of the point")
@ScalarFunction("ST_Y")
@SqlType(DOUBLE)
public static Double stY(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    validateType("ST_Y", geometry, EnumSet.of(POINT));
    if (geometry.isEmpty()) {
        return null;
    }
    return ((OGCPoint) geometry).Y();
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Example 15 with SqlNullable

use of io.prestosql.spi.function.SqlNullable in project hetu-core by openlookeng.

the class GeoFunctions method stNumInteriorRings.

@SqlNullable
@Description("Returns the cardinality of the collection of interior rings of a polygon")
@ScalarFunction("ST_NumInteriorRing")
@SqlType(BIGINT)
public static Long stNumInteriorRings(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    validateType("ST_NumInteriorRing", geometry, EnumSet.of(POLYGON));
    if (geometry.isEmpty()) {
        return null;
    }
    return Long.valueOf(((OGCPolygon) geometry).numInteriorRing());
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

SqlNullable (io.prestosql.spi.function.SqlNullable)52 SqlType (io.prestosql.spi.function.SqlType)52 ScalarFunction (io.prestosql.spi.function.ScalarFunction)40 Description (io.prestosql.spi.function.Description)34 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)28 JsonParser (com.fasterxml.jackson.core.JsonParser)14 PrestoException (io.prestosql.spi.PrestoException)14 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)14 IOException (java.io.IOException)14 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)11 MultiPoint (com.esri.core.geometry.MultiPoint)9 Point (com.esri.core.geometry.Point)9 BlockBuilder (io.prestosql.spi.block.BlockBuilder)8 ScalarOperator (io.prestosql.spi.function.ScalarOperator)8 JsonCastException (io.prestosql.util.JsonCastException)8 LiteralParameters (io.prestosql.spi.function.LiteralParameters)7 JsonToken (com.fasterxml.jackson.core.JsonToken)6 Slice (io.airlift.slice.Slice)6 MultiPath (com.esri.core.geometry.MultiPath)4 Block (io.prestosql.spi.block.Block)4