Search in sources :

Example 1 with VarIdentifier

use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.

the class FunctionParser method getFunctionDecl.

public FunctionDecl getFunctionDecl(Function function) throws CompilationException {
    String functionBody = function.getFunctionBody();
    List<String> params = function.getParams();
    List<VarIdentifier> varIdentifiers = new ArrayList<VarIdentifier>();
    StringBuilder builder = new StringBuilder();
    builder.append(" use dataverse " + function.getDataverseName() + ";");
    builder.append(" declare function " + function.getName().split("@")[0]);
    builder.append("(");
    boolean first = true;
    for (String param : params) {
        VarIdentifier varId = new VarIdentifier(param);
        varIdentifiers.add(varId);
        if (first) {
            first = false;
        } else {
            builder.append(",");
        }
        builder.append(param);
    }
    builder.append("){\n").append(functionBody).append("\n}");
    IParser parser = parserFactory.createParser(new CharSequenceReader(builder));
    List<Statement> statements = parser.parse();
    FunctionDecl decl = (FunctionDecl) statements.get(1);
    return decl;
}
Also used : CharSequenceReader(org.apache.commons.io.input.CharSequenceReader) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) Statement(org.apache.asterix.lang.common.base.Statement) ArrayList(java.util.ArrayList) IParser(org.apache.asterix.lang.common.base.IParser) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl)

Example 2 with VarIdentifier

use of org.apache.asterix.lang.common.struct.VarIdentifier 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 3 with VarIdentifier

use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.

the class CloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FunctionDecl fd, VariableSubstitutionEnvironment env) throws CompilationException {
    List<VarIdentifier> newList = new ArrayList<>(fd.getParamList().size());
    for (VarIdentifier vi : fd.getParamList()) {
        VariableExpr varExpr = new VariableExpr(vi);
        if (!env.constainsOldVar(varExpr)) {
            throw new CompilationException("Parameter " + vi + " does not appear in the substitution list.");
        }
        Expression newExpr = env.findSubstitution(varExpr);
        if (newExpr.getKind() != Kind.VARIABLE_EXPRESSION) {
            throw new CompilationException("Parameter " + vi + " cannot be substituted by a non-variable expression.");
        }
        newList.add(((VariableExpr) newExpr).getVar());
    }
    Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = fd.getFuncBody().accept(this, env);
    FunctionDecl newF = new FunctionDecl(fd.getSignature(), newList, (Expression) p1.first);
    return new Pair<>(newF, env);
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) 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) ArrayList(java.util.ArrayList) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) 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 4 with VarIdentifier

use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.

the class LangRewritingContext method mapOldId.

/**
     * Generate a new variable with the same identifier (varValue) but a different Id.
     *
     * @param oldId
     *            , the old variable id
     * @param varValue
     *            , the identifier
     * @return the new varible.
     */
public VarIdentifier mapOldId(Integer oldId, String varValue) {
    int n = newId();
    VarIdentifier newVar = new VarIdentifier(varValue);
    newVar.setId(n);
    oldVarIdToNewVarId.put(oldId, newVar);
    return newVar;
}
Also used : VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier)

Example 5 with VarIdentifier

use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.

the class SqlppGroupByVisitor method visit.

@Override
public Expression visit(GroupbyClause gc, ILangExpression arg) throws CompilationException {
    // Puts all FROM binding variables into withVarList.
    FromClause fromClause = (FromClause) arg;
    Collection<VariableExpr> fromBindingVars = fromClause == null ? new ArrayList<>() : SqlppVariableUtil.getBindingVariables(fromClause);
    Map<Expression, VariableExpr> withVarMap = new HashMap<>();
    for (VariableExpr fromBindingVar : fromBindingVars) {
        VariableExpr varExpr = new VariableExpr();
        varExpr.setIsNewVar(false);
        varExpr.setVar(fromBindingVar.getVar());
        VariableExpr newVarExpr = (VariableExpr) SqlppRewriteUtil.deepCopy(varExpr);
        withVarMap.put(varExpr, newVarExpr);
    }
    // Sets the field list for the group variable.
    List<Pair<Expression, Identifier>> groupFieldList = new ArrayList<>();
    if (!gc.hasGroupFieldList()) {
        for (VariableExpr varExpr : fromBindingVars) {
            Pair<Expression, Identifier> varIdPair = new Pair<>(new VariableExpr(varExpr.getVar()), SqlppVariableUtil.toUserDefinedVariableName(varExpr.getVar()));
            groupFieldList.add(varIdPair);
        }
        gc.setGroupFieldList(groupFieldList);
    } else {
        for (Pair<Expression, Identifier> groupField : gc.getGroupFieldList()) {
            Expression newFieldExpr = groupField.first.accept(this, arg);
            groupFieldList.add(new Pair<>(newFieldExpr, groupField.second));
            // Adds a field binding variable into withVarList.
            VariableExpr bindingVar = new VariableExpr(new VarIdentifier(SqlppVariableUtil.toInternalVariableName(groupField.second.getValue())));
            withVarMap.put(newFieldExpr, bindingVar);
        }
    }
    gc.setGroupFieldList(groupFieldList);
    // Sets the group variable.
    if (!gc.hasGroupVar()) {
        VariableExpr groupVar = new VariableExpr(context.newVariable());
        gc.setGroupVar(groupVar);
    }
    // Adds the group variable into the "with" (i.e., re-binding) variable list.
    VariableExpr gbyVarRef = new VariableExpr(gc.getGroupVar().getVar());
    gbyVarRef.setIsNewVar(false);
    withVarMap.put(gbyVarRef, (VariableExpr) SqlppRewriteUtil.deepCopy(gbyVarRef));
    gc.setWithVarMap(withVarMap);
    // Call super.visit(...) to scope variables.
    return super.visit(gc, arg);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Identifier(org.apache.asterix.lang.common.struct.Identifier) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Aggregations

VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)15 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)10 Expression (org.apache.asterix.lang.common.base.Expression)6 ArrayList (java.util.ArrayList)5 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)4 FunctionDecl (org.apache.asterix.lang.common.statement.FunctionDecl)4 Identifier (org.apache.asterix.lang.common.struct.Identifier)4 HashMap (java.util.HashMap)3 LetClause (org.apache.asterix.lang.common.clause.LetClause)3 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)3 Pair (org.apache.hyracks.algebricks.common.utils.Pair)3 CompilationException (org.apache.asterix.common.exceptions.CompilationException)2 DistinctClause (org.apache.asterix.lang.aql.clause.DistinctClause)2 ForClause (org.apache.asterix.lang.aql.clause.ForClause)2 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)2 Clause (org.apache.asterix.lang.common.base.Clause)2 IParser (org.apache.asterix.lang.common.base.IParser)2 Statement (org.apache.asterix.lang.common.base.Statement)2 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)2 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)2