use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class LangExpressionToPlanTranslator method lookupUserDefinedFunction.
private AbstractFunctionCallExpression lookupUserDefinedFunction(FunctionSignature signature, List<Mutable<ILogicalExpression>> args) throws MetadataException {
if (signature.getNamespace() == null) {
return null;
}
Function function = MetadataManager.INSTANCE.getFunction(metadataProvider.getMetadataTxnContext(), signature);
if (function == null) {
return null;
}
AbstractFunctionCallExpression f;
if (function.getLanguage().equalsIgnoreCase(Function.LANGUAGE_JAVA)) {
IFunctionInfo finfo = ExternalFunctionCompilerUtil.getExternalFunctionInfo(metadataProvider.getMetadataTxnContext(), function);
f = new ScalarFunctionCallExpression(finfo, args);
} else if (function.getLanguage().equalsIgnoreCase(Function.LANGUAGE_AQL)) {
IFunctionInfo finfo = FunctionUtil.getFunctionInfo(signature);
f = new ScalarFunctionCallExpression(finfo, args);
} else {
throw new MetadataException(" User defined functions written in " + function.getLanguage() + " are not supported");
}
return f;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class LangExpressionToPlanTranslator method lookupBuiltinFunction.
private AbstractFunctionCallExpression lookupBuiltinFunction(String functionName, int arity, List<Mutable<ILogicalExpression>> args) {
AbstractFunctionCallExpression f;
FunctionIdentifier fi = new FunctionIdentifier(AlgebricksBuiltinFunctions.ALGEBRICKS_NS, functionName, arity);
FunctionInfo afi = BuiltinFunctions.lookupFunction(fi);
FunctionIdentifier builtinAquafi = afi == null ? null : afi.getFunctionIdentifier();
if (builtinAquafi != null) {
fi = builtinAquafi;
} else {
fi = new FunctionIdentifier(FunctionConstants.ASTERIX_NS, functionName, arity);
afi = BuiltinFunctions.lookupFunction(fi);
if (afi == null) {
return null;
}
}
if (BuiltinFunctions.isBuiltinAggregateFunction(fi)) {
f = BuiltinFunctions.makeAggregateFunctionExpression(fi, args);
} else if (BuiltinFunctions.isBuiltinUnnestingFunction(fi)) {
UnnestingFunctionCallExpression ufce = new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(fi), args);
ufce.setReturnsUniqueValues(BuiltinFunctions.returnsUniqueValues(fi));
f = ufce;
} else {
f = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(fi), args);
}
return f;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class LangExpressionToPlanTranslator method translateUpsert.
private ILogicalOperator translateUpsert(DatasetDataSource targetDatasource, Mutable<ILogicalExpression> varRef, List<Mutable<ILogicalExpression>> varRefsForLoading, List<Mutable<ILogicalExpression>> additionalFilteringExpressions, ILogicalOperator assign, List<String> additionalFilteringField, LogicalVariable unnestVar, ILogicalOperator topOp, List<Mutable<ILogicalExpression>> exprs, LogicalVariable resVar, AssignOperator additionalFilteringAssign, ICompiledDmlStatement stmt) throws AlgebricksException {
if (!targetDatasource.getDataset().allow(topOp, DatasetUtil.OP_UPSERT)) {
throw new AlgebricksException(targetDatasource.getDataset().getDatasetName() + ": upsert into dataset is not supported on Datasets with Meta records");
}
ProjectOperator project = (ProjectOperator) topOp;
CompiledUpsertStatement compiledUpsert = (CompiledUpsertStatement) stmt;
Expression returnExpression = compiledUpsert.getReturnExpression();
InsertDeleteUpsertOperator upsertOp;
ILogicalOperator rootOperator;
if (targetDatasource.getDataset().hasMetaPart()) {
if (returnExpression != null) {
throw new AlgebricksException("Returning not allowed on datasets with Meta records");
}
AssignOperator metaAndKeysAssign;
List<LogicalVariable> metaAndKeysVars;
List<Mutable<ILogicalExpression>> metaAndKeysExprs;
List<Mutable<ILogicalExpression>> metaExpSingletonList;
metaAndKeysVars = new ArrayList<>();
metaAndKeysExprs = new ArrayList<>();
// add the meta function
IFunctionInfo finfoMeta = FunctionUtil.getFunctionInfo(BuiltinFunctions.META);
ScalarFunctionCallExpression metaFunction = new ScalarFunctionCallExpression(finfoMeta, new MutableObject<>(new VariableReferenceExpression(unnestVar)));
// create assign for the meta part
LogicalVariable metaVar = context.newVar();
metaExpSingletonList = new ArrayList<>(1);
metaExpSingletonList.add(new MutableObject<>(new VariableReferenceExpression(metaVar)));
metaAndKeysVars.add(metaVar);
metaAndKeysExprs.add(new MutableObject<>(metaFunction));
project.getVariables().add(metaVar);
varRefsForLoading.clear();
for (Mutable<ILogicalExpression> assignExpr : exprs) {
if (assignExpr.getValue().getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
AbstractFunctionCallExpression funcCall = (AbstractFunctionCallExpression) assignExpr.getValue();
funcCall.substituteVar(resVar, unnestVar);
LogicalVariable pkVar = context.newVar();
metaAndKeysVars.add(pkVar);
metaAndKeysExprs.add(new MutableObject<>(assignExpr.getValue()));
project.getVariables().add(pkVar);
varRefsForLoading.add(new MutableObject<>(new VariableReferenceExpression(pkVar)));
}
}
// A change feed, we don't need the assign to access PKs
upsertOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, metaExpSingletonList, InsertDeleteUpsertOperator.Kind.UPSERT, false);
// Create and add a new variable used for representing the original record
upsertOp.setPrevRecordVar(context.newVar());
upsertOp.setPrevRecordType(targetDatasource.getItemType());
if (targetDatasource.getDataset().hasMetaPart()) {
List<LogicalVariable> metaVars = new ArrayList<>();
metaVars.add(context.newVar());
upsertOp.setPrevAdditionalNonFilteringVars(metaVars);
List<Object> metaTypes = new ArrayList<>();
metaTypes.add(targetDatasource.getMetaItemType());
upsertOp.setPrevAdditionalNonFilteringTypes(metaTypes);
}
if (additionalFilteringField != null) {
upsertOp.setPrevFilterVar(context.newVar());
upsertOp.setPrevFilterType(((ARecordType) targetDatasource.getItemType()).getFieldType(additionalFilteringField.get(0)));
additionalFilteringAssign.getInputs().clear();
additionalFilteringAssign.getInputs().add(assign.getInputs().get(0));
upsertOp.getInputs().add(new MutableObject<>(additionalFilteringAssign));
} else {
upsertOp.getInputs().add(assign.getInputs().get(0));
}
metaAndKeysAssign = new AssignOperator(metaAndKeysVars, metaAndKeysExprs);
metaAndKeysAssign.getInputs().add(topOp.getInputs().get(0));
topOp.getInputs().set(0, new MutableObject<>(metaAndKeysAssign));
upsertOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
} else {
upsertOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, InsertDeleteUpsertOperator.Kind.UPSERT, false);
upsertOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
upsertOp.getInputs().add(new MutableObject<>(assign));
// Create and add a new variable used for representing the original record
ARecordType recordType = (ARecordType) targetDatasource.getItemType();
upsertOp.setPrevRecordVar(context.newVar());
upsertOp.setPrevRecordType(recordType);
if (additionalFilteringField != null) {
upsertOp.setPrevFilterVar(context.newVar());
upsertOp.setPrevFilterType(recordType.getFieldType(additionalFilteringField.get(0)));
}
}
rootOperator = new DelegateOperator(new CommitOperator(returnExpression == null));
rootOperator.getInputs().add(new MutableObject<>(upsertOp));
// Compiles the return expression.
return processReturningExpression(rootOperator, upsertOp, compiledUpsert);
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class LangExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(QuantifiedExpression qe, Mutable<ILogicalOperator> tupSource) throws CompilationException {
Mutable<ILogicalOperator> topOp = tupSource;
ILogicalOperator firstOp = null;
Mutable<ILogicalOperator> lastOp = null;
for (QuantifiedPair qt : qe.getQuantifiedList()) {
Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo1 = langExprToAlgExpression(qt.getExpr(), topOp);
topOp = eo1.second;
LogicalVariable uVar = context.newVarFromExpression(qt.getVarExpr());
ILogicalOperator u = new UnnestOperator(uVar, new MutableObject<>(makeUnnestExpression(eo1.first)));
if (firstOp == null) {
firstOp = u;
}
if (lastOp != null) {
u.getInputs().add(lastOp);
}
lastOp = new MutableObject<>(u);
}
// We make all the unnest correspond. to quantif. vars. sit on top
// in the hope of enabling joins & other optimiz.
firstOp.getInputs().add(topOp);
topOp = lastOp;
Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo2 = langExprToAlgExpression(qe.getSatisfiesExpr(), topOp);
AggregateFunctionCallExpression fAgg;
SelectOperator s;
if (qe.getQuantifier() == Quantifier.SOME) {
s = new SelectOperator(new MutableObject<>(eo2.first), false, null);
s.getInputs().add(eo2.second);
fAgg = BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.NON_EMPTY_STREAM, new ArrayList<>());
} else {
// EVERY
List<Mutable<ILogicalExpression>> satExprList = new ArrayList<>(1);
satExprList.add(new MutableObject<>(eo2.first));
s = new SelectOperator(new MutableObject<>(new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(AlgebricksBuiltinFunctions.NOT), satExprList)), false, null);
s.getInputs().add(eo2.second);
fAgg = BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.EMPTY_STREAM, new ArrayList<>());
}
LogicalVariable qeVar = context.newVar();
AggregateOperator a = new AggregateOperator(mkSingletonArrayList(qeVar), (List) mkSingletonArrayList(new MutableObject<>(fAgg)));
a.getInputs().add(new MutableObject<>(s));
return new Pair<>(a, qeVar);
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class SqlppExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(CaseExpression caseExpression, Mutable<ILogicalOperator> tupSource) throws CompilationException {
//Creates a series of subplan operators, one for each branch.
Mutable<ILogicalOperator> currentOpRef = tupSource;
ILogicalOperator currentOperator = null;
List<Expression> whenExprList = caseExpression.getWhenExprs();
List<Expression> thenExprList = caseExpression.getThenExprs();
List<ILogicalExpression> branchCondVarReferences = new ArrayList<>();
List<ILogicalExpression> allVarReferences = new ArrayList<>();
for (int index = 0; index < whenExprList.size(); ++index) {
Pair<ILogicalOperator, LogicalVariable> whenExprResult = whenExprList.get(index).accept(this, currentOpRef);
currentOperator = whenExprResult.first;
// Variable whenConditionVar is corresponds to the current "WHEN" condition.
LogicalVariable whenConditionVar = whenExprResult.second;
Mutable<ILogicalExpression> branchEntraceConditionExprRef = new MutableObject<>(new VariableReferenceExpression(whenConditionVar));
// even though multiple "WHEN" conditions can be satisfied.
if (!branchCondVarReferences.isEmpty()) {
// The additional filter generated here makes sure the the tuple has not
// entered other matched "WHEN...THEN" case.
List<Mutable<ILogicalExpression>> andArgs = new ArrayList<>();
andArgs.add(generateNoMatchedPrecedingWhenBranchesFilter(branchCondVarReferences));
andArgs.add(branchEntraceConditionExprRef);
// A "THEN" branch can be entered only when the tuple has not enter any other preceding
// branches and the current "WHEN" condition is TRUE.
branchEntraceConditionExprRef = new MutableObject<>(new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.AND), andArgs));
}
// Translates the corresponding "THEN" expression.
Pair<ILogicalOperator, LogicalVariable> opAndVarForThen = constructSubplanOperatorForBranch(currentOperator, branchEntraceConditionExprRef, thenExprList.get(index));
branchCondVarReferences.add(new VariableReferenceExpression(whenConditionVar));
allVarReferences.add(new VariableReferenceExpression(whenConditionVar));
allVarReferences.add(new VariableReferenceExpression(opAndVarForThen.second));
currentOperator = opAndVarForThen.first;
currentOpRef = new MutableObject<>(currentOperator);
}
// Creates a subplan for the "ELSE" branch.
Mutable<ILogicalExpression> elseCondExprRef = generateNoMatchedPrecedingWhenBranchesFilter(branchCondVarReferences);
Pair<ILogicalOperator, LogicalVariable> opAndVarForElse = constructSubplanOperatorForBranch(currentOperator, elseCondExprRef, caseExpression.getElseExpr());
// Uses switch-case function to select the results of two branches.
LogicalVariable selectVar = context.newVar();
List<Mutable<ILogicalExpression>> arguments = new ArrayList<>();
arguments.add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(ABoolean.TRUE))));
for (ILogicalExpression argVar : allVarReferences) {
arguments.add(new MutableObject<>(argVar));
}
arguments.add(new MutableObject<>(new VariableReferenceExpression(opAndVarForElse.second)));
AbstractFunctionCallExpression swithCaseExpr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SWITCH_CASE), arguments);
AssignOperator assignOp = new AssignOperator(selectVar, new MutableObject<>(swithCaseExpr));
assignOp.getInputs().add(new MutableObject<>(opAndVarForElse.first));
// Unnests the selected (a "THEN" or "ELSE" branch) result.
LogicalVariable unnestVar = context.newVar();
UnnestOperator unnestOp = new UnnestOperator(unnestVar, new MutableObject<>(new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), Collections.singletonList(new MutableObject<>(new VariableReferenceExpression(selectVar))))));
unnestOp.getInputs().add(new MutableObject<>(assignOp));
// Produces the final assign operator.
LogicalVariable resultVar = context.newVar();
AssignOperator finalAssignOp = new AssignOperator(resultVar, new MutableObject<>(new VariableReferenceExpression(unnestVar)));
finalAssignOp.getInputs().add(new MutableObject<>(unnestOp));
return new Pair<>(finalAssignOp, resultVar);
}
Aggregations