use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.
the class GeoFunctions method stPoints.
@SqlNullable
@Description("Returns an array of points in a geometry")
@ScalarFunction("ST_Points")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block stPoints(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
Geometry geometry = deserialize(input);
if (geometry.isEmpty()) {
return null;
}
BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, geometry.getNumPoints());
buildPointsBlock(geometry, blockBuilder);
return blockBuilder.build();
}
use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.
the class GeoFunctions method stEndPoint.
@SqlNullable
@Description("Returns the last point of a LINESTRING geometry as a Point")
@ScalarFunction("ST_EndPoint")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stEndPoint(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
Geometry geometry = deserialize(input);
validateType("ST_EndPoint", geometry, EnumSet.of(LINE_STRING));
if (geometry.isEmpty()) {
return null;
}
return serialize(((LineString) geometry).getEndPoint());
}
use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.
the class MapEqualOperator method equals.
@TypeParameter("K")
@TypeParameter("V")
@SqlNullable
@SqlType(StandardTypes.BOOLEAN)
public static Boolean equals(@OperatorDependency(operator = EQUAL, argumentTypes = { "K", "K" }) MethodHandle keyEqualsFunction, @OperatorDependency(operator = HASH_CODE, argumentTypes = { "K" }) MethodHandle keyHashcodeFunction, @OperatorDependency(operator = EQUAL, argumentTypes = { "V", "V" }) MethodHandle valueEqualsFunction, @TypeParameter("K") Type keyType, @TypeParameter("V") Type valueType, @SqlType("map(K,V)") Block leftMapBlock, @SqlType("map(K,V)") Block rightMapBlock) {
MethodHandle keyBlockEqualsFunction = compose(keyEqualsFunction, nativeValueGetter(keyType));
MethodHandle keyBlockHashCode = compose(keyHashcodeFunction, nativeValueGetter(keyType));
return MapGenericEquality.genericEqual(keyType, keyHashcodeFunction, keyBlockEqualsFunction, keyBlockHashCode, leftMapBlock, rightMapBlock, (leftMapIndex, rightMapIndex) -> {
Object leftValue = readNativeValue(valueType, leftMapBlock, leftMapIndex);
if (leftValue == null) {
return null;
}
Object rightValue = readNativeValue(valueType, rightMapBlock, rightMapIndex);
if (rightValue == null) {
return null;
}
return (Boolean) valueEqualsFunction.invoke(leftValue, rightValue);
});
}
use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.
the class MapFromEntriesFunction method mapFromEntries.
@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,V)")
@SqlNullable
public Block mapFromEntries(@TypeParameter("map(K,V)") MapType mapType, SqlFunctionProperties properties, @SqlType("array(row(K,V))") Block block) {
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
RowType rowType = RowType.anonymous(ImmutableList.of(keyType, valueType));
int entryCount = block.getPositionCount();
BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, block.getPositionCount());
BlockBuilder resultBuilder = mapBlockBuilder.beginBlockEntry();
TypedSet uniqueKeys = new TypedSet(keyType, entryCount, "map_from_entries");
for (int i = 0; i < entryCount; i++) {
if (block.isNull(i)) {
mapBlockBuilder.closeEntry();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
}
Block rowBlock = rowType.getObject(block, i);
if (rowBlock.isNull(0)) {
mapBlockBuilder.closeEntry();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
if (uniqueKeys.contains(rowBlock, 0)) {
mapBlockBuilder.closeEntry();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", keyType.getObjectValue(properties, rowBlock, 0)));
}
uniqueKeys.add(rowBlock, 0);
keyType.appendTo(rowBlock, 0, resultBuilder);
valueType.appendTo(rowBlock, 1, resultBuilder);
}
mapBlockBuilder.closeEntry();
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.
the class JsonOperators method castToInteger.
@ScalarOperator(CAST)
@SqlNullable
@SqlType(INTEGER)
public static Long castToInteger(@SqlType(JSON) Slice json) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Long result = currentTokenAsInteger(parser);
// check no trailing token
checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to INTEGER");
return result;
} catch (PrestoException e) {
if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause());
}
throw e;
} catch (ArithmeticException | IOException | JsonCastException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e);
}
}
Aggregations