use of org.apache.asterix.lang.common.expression.CallExpr in project asterixdb by apache.
the class AbstractInlineUdfsVisitor method inlineUdfsInExpr.
protected Pair<Boolean, Expression> inlineUdfsInExpr(Expression expr, List<FunctionDecl> arg) throws CompilationException {
if (expr.getKind() != Kind.CALL_EXPRESSION) {
boolean r = expr.accept(this, arg);
return new Pair<>(r, expr);
}
CallExpr f = (CallExpr) expr;
boolean r = expr.accept(this, arg);
FunctionDecl implem = findFuncDeclaration(f.getFunctionSignature(), arg);
if (implem == null) {
return new Pair<>(r, expr);
} else {
// Rewrite the function body itself (without setting unbounded variables to dataset access).
// TODO(buyingyi): throw an exception for recursive function definition or limit the stack depth.
implem.setFuncBody(rewriteFunctionBody(implem.getFuncBody()));
// it's one of the functions we want to inline
List<LetClause> clauses = new ArrayList<>();
Iterator<VarIdentifier> paramIter = implem.getParamList().iterator();
VariableSubstitutionEnvironment subts = new VariableSubstitutionEnvironment();
for (Expression e : f.getExprList()) {
VarIdentifier param = paramIter.next();
// variable inlining to take care of this.
if (e.getKind() == Kind.VARIABLE_EXPRESSION) {
subts.addSubstituion(new VariableExpr(param), e);
} else {
VarIdentifier newV = context.newVariable();
Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = e.accept(cloneVisitor, new VariableSubstitutionEnvironment());
LetClause c = new LetClause(new VariableExpr(newV), (Expression) p1.first);
clauses.add(c);
subts.addSubstituion(new VariableExpr(param), new VariableExpr(newV));
}
}
Pair<ILangExpression, VariableSubstitutionEnvironment> p2 = implem.getFuncBody().accept(cloneVisitor, subts);
Expression resExpr;
if (clauses.isEmpty()) {
resExpr = (Expression) p2.first;
} else {
resExpr = generateQueryExpression(clauses, (Expression) p2.first);
}
return new Pair<>(true, resExpr);
}
}
use of org.apache.asterix.lang.common.expression.CallExpr in project asterixdb by apache.
the class CloneAndSubstituteVariablesVisitor method visit.
@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(CallExpr pf, VariableSubstitutionEnvironment env) throws CompilationException {
List<Expression> exprList = VariableCloneAndSubstitutionUtil.visitAndCloneExprList(pf.getExprList(), env, this);
CallExpr f = new CallExpr(pf.getFunctionSignature(), exprList);
return new Pair<>(f, env);
}
use of org.apache.asterix.lang.common.expression.CallExpr in project asterixdb by apache.
the class SqlppDeleteRewriteVisitor method visit.
@Override
public Void visit(DeleteStatement deleteStmt, Void visitArg) {
List<Expression> arguments = new ArrayList<>();
Identifier dataverseName = deleteStmt.getDataverseName();
Identifier datasetName = deleteStmt.getDatasetName();
String arg = dataverseName == null ? datasetName.getValue() : dataverseName.getValue() + "." + datasetName.getValue();
LiteralExpr argumentLiteral = new LiteralExpr(new StringLiteral(arg));
arguments.add(argumentLiteral);
CallExpr callExpression = new CallExpr(new FunctionSignature(FunctionConstants.ASTERIX_NS, "dataset", 1), arguments);
// From clause.
VariableExpr var = deleteStmt.getVariableExpr();
FromTerm fromTerm = new FromTerm(callExpression, var, null, null);
@SuppressWarnings("unchecked") FromClause fromClause = new FromClause(Collections.singletonList(fromTerm));
// Where clause.
WhereClause whereClause = null;
Expression condition = deleteStmt.getCondition();
if (condition != null) {
whereClause = new WhereClause(condition);
}
// Select clause.
VariableExpr returnExpr = new VariableExpr(var.getVar());
returnExpr.setIsNewVar(false);
SelectElement selectElement = new SelectElement(returnExpr);
SelectClause selectClause = new SelectClause(selectElement, null, false);
// Construct the select expression.
SelectBlock selectBlock = new SelectBlock(selectClause, fromClause, null, whereClause, null, null, null);
SelectSetOperation selectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
SelectExpression selectExpression = new SelectExpression(null, selectSetOperation, null, null, false);
Query query = new Query(false, false, selectExpression, 0);
query.setBody(selectExpression);
// return the delete statement.
deleteStmt.setQuery(query);
return null;
}
use of org.apache.asterix.lang.common.expression.CallExpr in project asterixdb by apache.
the class OperatorExpressionVisitor method processBetweenOperator.
private Expression processBetweenOperator(OperatorExpr operatorExpr, OperatorType opType) throws CompilationException {
// The grammar guarantees that the BETWEEN operator gets exactly three expressions.
Expression target = operatorExpr.getExprList().get(0);
Expression left = operatorExpr.getExprList().get(1);
Expression right = operatorExpr.getExprList().get(2);
// Creates the expression left <= target.
Expression leftComparison = createLessThanExpression(left, target, operatorExpr.getHints());
// Creates the expression target <= right.
Expression rightComparison = createLessThanExpression(target, right, operatorExpr.getHints());
OperatorExpr andExpr = new OperatorExpr();
andExpr.addOperand(leftComparison);
andExpr.addOperand(rightComparison);
andExpr.addOperator("and");
return opType == OperatorType.BETWEEN ? andExpr : new CallExpr(new FunctionSignature(null, "not", 1), new ArrayList<>(Collections.singletonList(andExpr)));
}
use of org.apache.asterix.lang.common.expression.CallExpr in project asterixdb by apache.
the class VariableCheckAndRewriteVisitor method visit.
@Override
public Expression visit(FieldAccessor fa, ILangExpression parent) throws CompilationException {
Expression leadingExpr = fa.getExpr();
if (leadingExpr.getKind() != Kind.VARIABLE_EXPRESSION) {
fa.setExpr(leadingExpr.accept(this, fa));
return fa;
} else {
VariableExpr varExpr = (VariableExpr) leadingExpr;
String lastIdentifier = fa.getIdent().getValue();
Expression resolvedExpr = resolve(varExpr, /** Resolves within the dataverse that has the same name as the variable name. */
SqlppVariableUtil.toUserDefinedVariableName(varExpr.getVar().getValue()).getValue(), lastIdentifier, fa, parent);
if (resolvedExpr.getKind() == Kind.CALL_EXPRESSION) {
CallExpr callExpr = (CallExpr) resolvedExpr;
if (callExpr.getFunctionSignature().equals(datasetFunction)) {
// The field access is resolved to be a dataset access in the form of "dataverse.dataset".
return resolvedExpr;
}
}
fa.setExpr(resolvedExpr);
return fa;
}
}
Aggregations