use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.
the class MetadataNode method getDataverseFunctions.
@Override
public List<Function> getDataverseFunctions(JobId jobId, String dataverseName) throws MetadataException, RemoteException {
try {
ITupleReference searchKey = createTuple(dataverseName);
FunctionTupleTranslator tupleReaderWriter = tupleTranslatorProvider.getFunctionTupleTranslator(false);
IValueExtractor<Function> valueExtractor = new MetadataEntityValueExtractor<>(tupleReaderWriter);
List<Function> results = new ArrayList<>();
searchIndex(jobId, MetadataPrimaryIndexes.FUNCTION_DATASET, searchKey, valueExtractor, results);
return results;
} catch (HyracksDataException e) {
throw new MetadataException(e);
}
}
use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.
the class FunctionUtil method retrieveUsedStoredFunctions.
/**
* Retrieve stored functions (from CREATE FUNCTION statements) that have been used in an expression.
*
* @param metadataProvider,
* the metadata provider
* @param expression,
* the expression for analysis
* @param declaredFunctions,
* a set of declared functions in the query, which can potentially override stored functions.
* @param functionCollector,
* for collecting function calls in the <code>expression</code>
* @param functionParser,
* for parsing stored functions in the string represetnation.
* @param functionNormalizer,
* for normalizing function names.
* @throws CompilationException
*/
public static List<FunctionDecl> retrieveUsedStoredFunctions(MetadataProvider metadataProvider, Expression expression, List<FunctionSignature> declaredFunctions, List<FunctionDecl> inputFunctionDecls, IFunctionCollector functionCollector, IFunctionParser functionParser, IFunctionNormalizer functionNormalizer) throws CompilationException {
List<FunctionDecl> functionDecls = inputFunctionDecls == null ? new ArrayList<>() : new ArrayList<>(inputFunctionDecls);
if (expression == null) {
return functionDecls;
}
String value = metadataProvider.getConfig().get(FunctionUtil.IMPORT_PRIVATE_FUNCTIONS);
boolean includePrivateFunctions = (value != null) ? Boolean.valueOf(value.toLowerCase()) : false;
Set<FunctionSignature> functionCalls = functionCollector.getFunctionCalls(expression);
for (FunctionSignature signature : functionCalls) {
if (declaredFunctions != null && declaredFunctions.contains(signature)) {
continue;
}
if (signature.getNamespace() == null) {
signature.setNamespace(metadataProvider.getDefaultDataverseName());
}
String namespace = signature.getNamespace();
// Checks the existence of the referred dataverse.
if (metadataProvider.findDataverse(namespace) == null && !namespace.equals(FunctionConstants.ASTERIX_NS)) {
throw new CompilationException("In function call \"" + namespace + "." + signature.getName() + "(...)\", the dataverse \"" + namespace + "\" cannot be found!");
}
Function function = lookupUserDefinedFunctionDecl(metadataProvider.getMetadataTxnContext(), signature);
if (function == null) {
FunctionSignature normalizedSignature = functionNormalizer == null ? signature : functionNormalizer.normalizeBuiltinFunctionSignature(signature);
if (BuiltinFunctions.isBuiltinCompilerFunction(normalizedSignature, includePrivateFunctions)) {
continue;
}
StringBuilder messageBuilder = new StringBuilder();
if (!functionDecls.isEmpty()) {
messageBuilder.append("function " + functionDecls.get(functionDecls.size() - 1).getSignature() + " depends upon function " + signature + " which is undefined");
} else {
messageBuilder.append("function " + signature + " is not defined");
}
throw new CompilationException(messageBuilder.toString());
}
if (function.getLanguage().equalsIgnoreCase(Function.LANGUAGE_AQL)) {
FunctionDecl functionDecl = functionParser.getFunctionDecl(function);
if (functionDecl != null) {
if (functionDecls.contains(functionDecl)) {
throw new CompilationException("Recursive invocation " + functionDecls.get(functionDecls.size() - 1).getSignature() + " <==> " + functionDecl.getSignature());
}
functionDecls.add(functionDecl);
functionDecls = retrieveUsedStoredFunctions(metadataProvider, functionDecl.getFuncBody(), declaredFunctions, functionDecls, functionCollector, functionParser, functionNormalizer);
}
}
}
return functionDecls;
}
use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.
the class SubscribeFeedStatement method initialize.
public void initialize(MetadataTransactionContext mdTxnCtx) throws MetadataException {
this.query = new Query(false);
EntityId sourceFeedId = connectionRequest.getReceivingFeedId();
Feed subscriberFeed = MetadataManager.INSTANCE.getFeed(mdTxnCtx, connectionRequest.getReceivingFeedId().getDataverse(), connectionRequest.getReceivingFeedId().getEntityName());
if (subscriberFeed == null) {
throw new IllegalStateException(" Subscriber feed " + subscriberFeed + " not found.");
}
String feedOutputType = getOutputType(mdTxnCtx);
StringBuilder builder = new StringBuilder();
builder.append("use dataverse " + sourceFeedId.getDataverse() + ";\n");
builder.append("set" + " " + FunctionUtil.IMPORT_PRIVATE_FUNCTIONS + " " + "'" + Boolean.TRUE + "'" + ";\n");
builder.append("set" + " " + FeedActivityDetails.FEED_POLICY_NAME + " " + "'" + connectionRequest.getPolicy() + "'" + ";\n");
builder.append("insert into dataset " + connectionRequest.getTargetDataset() + " ");
builder.append(" (" + " for $x in feed-collect ('" + sourceFeedId.getDataverse() + "'" + "," + "'" + sourceFeedId.getEntityName() + "'" + "," + "'" + connectionRequest.getReceivingFeedId().getEntityName() + "'" + "," + "'" + connectionRequest.getSubscriptionLocation().name() + "'" + "," + "'" + connectionRequest.getTargetDataset() + "'" + "," + "'" + feedOutputType + "'" + ")");
List<FunctionSignature> functionsToApply = connectionRequest.getFunctionsToApply();
if ((functionsToApply != null) && functionsToApply.isEmpty()) {
builder.append(" return $x");
} else {
Function function;
String rValueName = "x";
String lValueName = "y";
int variableIndex = 0;
for (FunctionSignature appliedFunction : functionsToApply) {
function = MetadataManager.INSTANCE.getFunction(mdTxnCtx, appliedFunction);
variableIndex++;
switch(function.getLanguage().toUpperCase()) {
case Function.LANGUAGE_AQL:
builder.append(" let " + "$" + lValueName + variableIndex + ":=" + function.getName() + "(" + "$" + rValueName + ")");
rValueName = lValueName + variableIndex;
break;
case Function.LANGUAGE_JAVA:
builder.append(" let " + "$" + lValueName + variableIndex + ":=" + function.getName() + "(" + "$" + rValueName + ")");
rValueName = lValueName + variableIndex;
break;
}
builder.append("\n");
}
builder.append("return $" + lValueName + variableIndex);
}
builder.append(")");
builder.append(";");
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Connect feed statement translated to\n" + builder.toString());
}
IParser parser = parserFactory.createParser(new StringReader(builder.toString()));
List<Statement> statements;
try {
statements = parser.parse();
query = ((InsertStatement) statements.get(INSERT_STATEMENT_POS)).getQuery();
} catch (CompilationException pe) {
throw new MetadataException(pe);
}
}
Aggregations