use of com.facebook.presto.spi.PrestoException in project presto by prestodb.
the class ColorFunctions method color.
@ScalarFunction
@LiteralParameters("x")
@SqlType(ColorType.NAME)
public static long color(@SqlType("varchar(x)") Slice color) {
int rgb = parseRgb(color);
if (rgb != -1) {
return rgb;
}
// encode system colors (0-15) as negative values, offset by one
try {
SystemColor systemColor = SystemColor.valueOf(upper(color).toStringUtf8());
int index = systemColor.getIndex();
return -(index + 1);
} catch (IllegalArgumentException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid color: '%s'", color.toStringUtf8()), e);
}
}
use of com.facebook.presto.spi.PrestoException in project presto by prestodb.
the class ArrayConcatFunction method specialize.
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
if (arity < 2) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "There must be two or more arguments to " + FUNCTION_NAME);
}
Type elementType = boundVariables.getTypeVariable("E");
VarArgsToArrayAdapterGenerator.MethodHandleAndConstructor methodHandleAndConstructor = generateVarArgsToArrayAdapter(Block.class, Block.class, arity, METHOD_HANDLE.bindTo(elementType), USER_STATE_FACTORY.bindTo(elementType));
return new ScalarFunctionImplementation(false, nCopies(arity, false), nCopies(arity, false), methodHandleAndConstructor.getMethodHandle(), Optional.of(methodHandleAndConstructor.getConstructor()), isDeterministic());
}
use of com.facebook.presto.spi.PrestoException in project presto by prestodb.
the class ArrayLessThanOrEqualOperator method lessThanOrEqualLong.
@TypeParameter("E")
@TypeParameterSpecialization(name = "E", nativeContainerType = long.class)
@SqlType(StandardTypes.BOOLEAN)
public static boolean lessThanOrEqualLong(@OperatorDependency(operator = LESS_THAN, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle lessThanFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
int len = Math.min(leftArray.getPositionCount(), rightArray.getPositionCount());
int index = 0;
while (index < len) {
checkElementNotNull(leftArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
checkElementNotNull(rightArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
long leftElement = type.getLong(leftArray, index);
long rightElement = type.getLong(rightArray, index);
try {
if ((boolean) lessThanFunction.invokeExact(leftElement, rightElement)) {
return true;
}
if ((boolean) lessThanFunction.invokeExact(rightElement, leftElement)) {
return false;
}
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, Error.class);
Throwables.propagateIfInstanceOf(t, PrestoException.class);
throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
}
index++;
}
return leftArray.getPositionCount() <= rightArray.getPositionCount();
}
use of com.facebook.presto.spi.PrestoException in project presto by prestodb.
the class JsonFunctions method jsonParse.
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.JSON)
public static Slice jsonParse(@SqlType("varchar(x)") Slice slice) {
try {
byte[] in = slice.getBytes();
SliceOutput dynamicSliceOutput = new DynamicSliceOutput(in.length);
SORTED_MAPPER.writeValue((OutputStream) dynamicSliceOutput, SORTED_MAPPER.readValue(in, Object.class));
return dynamicSliceOutput.slice();
} catch (Exception e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Cannot convert '%s' to JSON", slice.toStringUtf8()));
}
}
use of com.facebook.presto.spi.PrestoException in project presto by prestodb.
the class JsonOperators method castToBoolean.
@ScalarOperator(CAST)
@SqlNullable
@SqlType(BOOLEAN)
public static Boolean castToBoolean(@SqlType(JSON) Slice json) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Boolean result;
switch(parser.getCurrentToken()) {
case VALUE_NULL:
result = null;
break;
case VALUE_STRING:
result = VarcharOperators.castToBoolean(Slices.utf8Slice(parser.getText()));
break;
case VALUE_NUMBER_FLOAT:
result = DoubleOperators.castToBoolean(parser.getDoubleValue());
break;
case VALUE_NUMBER_INT:
result = BigintOperators.castToBoolean(parser.getLongValue());
break;
case VALUE_TRUE:
result = true;
break;
case VALUE_FALSE:
result = false;
break;
default:
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BOOLEAN));
}
// check no trailing token
checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BOOLEAN");
return result;
} catch (IOException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BOOLEAN));
}
}
Aggregations