public JavaFunctionHelper(IExternalFunctionInfo finfo, IDataOutputProvider outputProvider, List<String> parameters) { this.finfo = finfo; this.outputProvider = outputProvider; this.pointableVisitor = new JObjectPointableVisitor(); this.pointableAllocator = new PointableAllocator(); this.arguments = new IJObject[finfo.getArgumentList().size()]; int index = 0; for (IAType param : finfo.getArgumentList()) { this.arguments[index++] = objectPool.allocate(param); } this.resultHolder = objectPool.allocate(finfo.getReturnType()); this.poolTypeInfo = new HashMap<>(); this.parameters = parameters; }
public static IFunctionDescriptor getExternalFunctionDescriptor(IExternalFunctionInfo finfo, IApplicationContext appCtx) throws AlgebricksException { switch (finfo.getKind()) { case SCALAR: return new ExternalScalarFunctionDescriptor(finfo, appCtx); case AGGREGATE: case UNNEST: throw new AlgebricksException("Unsupported function kind :" + finfo.getKind()); default: break; } return null; }
public ExternalFunction(IExternalFunctionInfo finfo, IScalarEvaluatorFactory args[], IHyracksTaskContext context, IApplicationContext appCtx) throws HyracksDataException { this.finfo = finfo; this.evaluatorFactories = args; argumentEvaluators = new IScalarEvaluator[args.length]; for (int i = 0; i < args.length; i++) { argumentEvaluators[i] = args[i].createScalarEvaluator(context); } ILibraryManager libraryManager = appCtx.getLibraryManager(); String[] fnameComponents = finfo.getFunctionIdentifier().getName().split("#"); String functionLibary = fnameComponents[0]; String dataverse = finfo.getFunctionIdentifier().getNamespace(); functionHelper = new JavaFunctionHelper(finfo, resultBuffer, libraryManager.getFunctionParameters(dataverse, finfo.getFunctionIdentifier().getName())); ClassLoader libraryClassLoader = libraryManager.getLibraryClassLoader(dataverse, functionLibary); String classname = finfo.getFunctionBody().trim(); Class<?> clazz; try { clazz = Class.forName(classname, true, libraryClassLoader); externalFunctionFactory = (IFunctionFactory) clazz.newInstance(); externalFunction = externalFunctionFactory.getExternalFunction(); } catch (Exception e) { throw new RuntimeDataException(ErrorCode.LIBRARY_EXTERNAL_FUNCTION_UNABLE_TO_LOAD_CLASS, e, classname); } }
@Override public IJObject getResultObject() { if (resultHolder == null) { resultHolder = objectPool.allocate(finfo.getReturnType()); } return resultHolder; }
public void setArguments(IFrameTupleReference tuple) throws AlgebricksException, IOException { for (int i = 0; i < evaluatorFactories.length; i++) { argumentEvaluators[i].evaluate(tuple, inputVal); // Type-cast the source array based on the input type that this function wants to receive. ATypeTag targetTypeTag = finfo.getArgumentList().get(i).getTypeTag(); ATypeTag sourceTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER .deserialize(inputVal.getByteArray()[inputVal.getStartOffset()]); if (sourceTypeTag != targetTypeTag) { castBuffer.reset(); ATypeHierarchy.convertNumericTypeByteArray(inputVal.getByteArray(), inputVal.getStartOffset(), inputVal.getLength(), targetTypeTag, castBuffer.getDataOutput(), true); functionHelper.setArgument(i, castBuffer); } else { functionHelper.setArgument(i, inputVal); } } }
@Override public void setResult(IJObject result) throws HyracksDataException { if (result == null || checkInvalidReturnValueType(result, finfo.getReturnType())) { isValidResult = false; } else { isValidResult = true; result.serialize(outputProvider.getDataOutput(), true); result.reset(); } }
public void setArgument(int index, IValueReference valueReference) throws IOException, AsterixException { IVisitablePointable pointable = null; IJObject jObject = null; IAType type = finfo.getArgumentList().get(index); switch (type.getTypeTag()) { case OBJECT: pointable = pointableAllocator.allocateRecordValue(type); pointable.set(valueReference); jObject = pointableVisitor.visit((ARecordVisitablePointable) pointable, getTypeInfo(index, type)); break; case ARRAY: case MULTISET: pointable = pointableAllocator.allocateListValue(type); pointable.set(valueReference); jObject = pointableVisitor.visit((AListVisitablePointable) pointable, getTypeInfo(index, type)); break; case ANY: throw new RuntimeDataException(ErrorCode.LIBRARY_JAVA_FUNCTION_HELPER_CANNOT_HANDLE_ARGU_TYPE, type.getTypeTag()); default: pointable = pointableAllocator.allocateFieldValue(type); pointable.set(valueReference); jObject = pointableVisitor.visit((AFlatValuePointable) pointable, getTypeInfo(index, type)); break; } arguments[index] = jObject; }
public static IExternalFunction getExternalFunctionEvaluator(IExternalFunctionInfo finfo, IScalarEvaluatorFactory args[], IHyracksTaskContext context, IApplicationContext appCtx) throws HyracksDataException { switch (finfo.getKind()) { case SCALAR: return new ExternalScalarFunction(finfo, args, context, appCtx); case AGGREGATE: case UNNEST: throw new RuntimeDataException(ErrorCode.LIBRARY_EXTERNAL_FUNCTION_UNSUPPORTED_KIND, finfo.getKind()); default: throw new RuntimeDataException(ErrorCode.LIBRARY_EXTERNAL_FUNCTION_UNKNOWN_KIND, finfo.getKind()); } } }