Search in sources :

Example 1 with CallExpr

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);
    }
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause) ArrayList(java.util.ArrayList) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair)

Example 2 with CallExpr

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);
}
Also used : ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair)

Example 3 with CallExpr

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;
}
Also used : SelectElement(org.apache.asterix.lang.sqlpp.clause.SelectElement) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) Query(org.apache.asterix.lang.common.statement.Query) ArrayList(java.util.ArrayList) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) Identifier(org.apache.asterix.lang.common.struct.Identifier) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) SetOperationInput(org.apache.asterix.lang.sqlpp.struct.SetOperationInput) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm)

Example 4 with CallExpr

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)));
}
Also used : OperatorExpr(org.apache.asterix.lang.common.expression.OperatorExpr) Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) ArrayList(java.util.ArrayList) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 5 with CallExpr

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;
    }
}
Also used : Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr)

Aggregations

Expression (org.apache.asterix.lang.common.base.Expression)9 CallExpr (org.apache.asterix.lang.common.expression.CallExpr)9 ArrayList (java.util.ArrayList)7 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)7 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)4 LiteralExpr (org.apache.asterix.lang.common.expression.LiteralExpr)4 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)4 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)4 StringLiteral (org.apache.asterix.lang.common.literal.StringLiteral)4 WhereClause (org.apache.asterix.lang.common.clause.WhereClause)2 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)2 Query (org.apache.asterix.lang.common.statement.Query)2 Identifier (org.apache.asterix.lang.common.struct.Identifier)2 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)2 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)2 Pair (org.apache.hyracks.algebricks.common.utils.Pair)2 ForClause (org.apache.asterix.lang.aql.clause.ForClause)1 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)1 Clause (org.apache.asterix.lang.common.base.Clause)1 LetClause (org.apache.asterix.lang.common.clause.LetClause)1