public static ImplementationDependency createDependency(Annotation annotation, Set<String> literalParameters) { if (annotation instanceof TypeParameter) { return new TypeImplementationDependency(((TypeParameter) annotation).value()); } if (annotation instanceof LiteralParameter) { return new LiteralImplementationDependency(((LiteralParameter) annotation).value()); } if (annotation instanceof FunctionDependency) { FunctionDependency functionDependency = (FunctionDependency) annotation; return new FunctionImplementationDependency( functionDependency.name(), parseTypeSignature(functionDependency.returnType(), literalParameters), Arrays.stream(functionDependency.argumentTypes()) .map(signature -> parseTypeSignature(signature, literalParameters)) .collect(toImmutableList()), toInvocationConvention(functionDependency.convention())); } if (annotation instanceof OperatorDependency) { OperatorDependency operatorDependency = (OperatorDependency) annotation; return new OperatorImplementationDependency( operatorDependency.operator(), parseTypeSignature(operatorDependency.returnType(), literalParameters), Arrays.stream(operatorDependency.argumentTypes()) .map(signature -> parseTypeSignature(signature, literalParameters)) .collect(toImmutableList()), toInvocationConvention(operatorDependency.convention())); } throw new IllegalArgumentException("Unsupported annotation " + annotation.getClass().getSimpleName()); }
@Description("Returns index of first occurrence of a substring (or 0 if not found)") @ScalarFunction("index") @SqlType(StandardTypes.BIGINT) public static long index( @FunctionDependency( name = "strpos", returnType = StandardTypes.BIGINT, argumentTypes = {StandardTypes.VARCHAR, StandardTypes.VARCHAR}) MethodHandle method, @SqlType(StandardTypes.VARCHAR) Slice string, @SqlType(StandardTypes.VARCHAR) Slice substring) { try { return (long) method.invokeExact(string, substring); } catch (Throwable t) { throwIfInstanceOf(t, Error.class); throwIfInstanceOf(t, PrestoException.class); throw new PrestoException(GENERIC_INTERNAL_ERROR, t); } }
@SqlType(StandardTypes.INTEGER) public static long testRegularConvention( @FunctionDependency(name = "add", returnType = StandardTypes.INTEGER, argumentTypes = {StandardTypes.INTEGER, StandardTypes.INTEGER}, convention = @Convention(arguments = {NEVER_NULL, NEVER_NULL}, result = FAIL_ON_NULL)) MethodHandle function, @SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right) { try { return (long) function.invokeExact(left, right); } catch (Throwable t) { throwIfInstanceOf(t, Error.class); throwIfInstanceOf(t, PrestoException.class); throw new PrestoException(GENERIC_INTERNAL_ERROR, t); } } }
public static ImplementationDependency createDependency(Annotation annotation, Set<String> literalParameters) { if (annotation instanceof TypeParameter) { return new TypeImplementationDependency(((TypeParameter) annotation).value()); } if (annotation instanceof LiteralParameter) { return new LiteralImplementationDependency(((LiteralParameter) annotation).value()); } if (annotation instanceof FunctionDependency) { FunctionDependency functionDependency = (FunctionDependency) annotation; return new FunctionImplementationDependency( functionDependency.name(), parseTypeSignature(functionDependency.returnType(), literalParameters), Arrays.stream(functionDependency.argumentTypes()) .map(signature -> parseTypeSignature(signature, literalParameters)) .collect(toImmutableList()), toInvocationConvention(functionDependency.convention())); } if (annotation instanceof OperatorDependency) { OperatorDependency operatorDependency = (OperatorDependency) annotation; return new OperatorImplementationDependency( operatorDependency.operator(), parseTypeSignature(operatorDependency.returnType(), literalParameters), Arrays.stream(operatorDependency.argumentTypes()) .map(signature -> parseTypeSignature(signature, literalParameters)) .collect(toImmutableList()), toInvocationConvention(operatorDependency.convention())); } throw new IllegalArgumentException("Unsupported annotation " + annotation.getClass().getSimpleName()); }
@SqlType(StandardTypes.INTEGER) public static long testRegularConvention( @FunctionDependency(name = "add", returnType = StandardTypes.INTEGER, argumentTypes = {StandardTypes.INTEGER, StandardTypes.INTEGER}, convention = @Convention(arguments = {NEVER_NULL, NEVER_NULL}, result = FAIL_ON_NULL)) MethodHandle function, @SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right) { try { return (long) function.invokeExact(left, right); } catch (Throwable t) { throwIfInstanceOf(t, Error.class); throwIfInstanceOf(t, PrestoException.class); throw new PrestoException(GENERIC_INTERNAL_ERROR, t); } } }
@SqlType(StandardTypes.INTEGER) public static long testBlockPositionConvention( @FunctionDependency( name = "add", returnType = StandardTypes.INTEGER, argumentTypes = {StandardTypes.INTEGER, StandardTypes.INTEGER}, convention = @Convention(arguments = {NEVER_NULL, BLOCK_POSITION}, result = FAIL_ON_NULL)) MethodHandle function, @SqlType("array(int)") Block array) { long sum = 0; for (int i = 0; i < array.getPositionCount(); i++) { try { sum = (long) function.invokeExact(sum, array, i); } catch (Throwable t) { throwIfInstanceOf(t, Error.class); throwIfInstanceOf(t, PrestoException.class); throw new PrestoException(GENERIC_INTERNAL_ERROR, t); } } return sum; } }
@SqlType(StandardTypes.INTEGER) public static long testBlockPositionConvention( @FunctionDependency( name = "add", returnType = StandardTypes.INTEGER, argumentTypes = {StandardTypes.INTEGER, StandardTypes.INTEGER}, convention = @Convention(arguments = {NEVER_NULL, BLOCK_POSITION}, result = FAIL_ON_NULL)) MethodHandle function, @SqlType("array(int)") Block array) { long sum = 0; for (int i = 0; i < array.getPositionCount(); i++) { try { sum = (long) function.invokeExact(sum, array, i); } catch (Throwable t) { throwIfInstanceOf(t, Error.class); throwIfInstanceOf(t, PrestoException.class); throw new PrestoException(GENERIC_INTERNAL_ERROR, t); } } return sum; } }