Search in sources :

Example 41 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class ClauseComparator method collectProducedVariablesFromGroupby.

// Collects produced variables from group-by.
private List<VariableExpr> collectProducedVariablesFromGroupby(GroupbyClause gbyClause) {
    List<VariableExpr> producedVars = new ArrayList<VariableExpr>();
    for (GbyVariableExpressionPair keyPair : gbyClause.getGbyPairList()) {
        producedVars.add(keyPair.getVar());
    }
    for (GbyVariableExpressionPair keyPair : gbyClause.getDecorPairList()) {
        producedVars.add(keyPair.getVar());
    }
    producedVars.addAll(gbyClause.getWithVarMap().values());
    return producedVars;
}
Also used : GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) ArrayList(java.util.ArrayList) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 42 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class ClauseComparator method extractDefinedCollectionVariables.

// Extracts the variables to be substituted with a path access.
private Map<VariableExpr, Expression> extractDefinedCollectionVariables(List<Clause> clauses, GroupbyClause cuttingGbyClause, String generatedAlias) {
    Map<VariableExpr, Expression> varExprMap = new HashMap<VariableExpr, Expression>();
    List<VariableExpr> varToSubstitute = collectProducedVariablesFromGroupby(cuttingGbyClause);
    int gbyIndex = clauses.indexOf(cuttingGbyClause);
    for (int i = gbyIndex + 1; i < clauses.size(); i++) {
        Clause cl = clauses.get(i);
        if (cl.getClauseType() == ClauseType.LET_CLAUSE) {
            varToSubstitute.add(((LetClause) cl).getVarExpr());
        }
    }
    for (VariableExpr var : varToSubstitute) {
        varExprMap.put(var, new FieldAccessor(new VariableExpr(new VarIdentifier(generatedAlias)), new VarIdentifier(var.getVar().getValue().substring(1))));
    }
    return varExprMap;
}
Also used : HashMap(java.util.HashMap) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ForClause(org.apache.asterix.lang.aql.clause.ForClause) DistinctClause(org.apache.asterix.lang.aql.clause.DistinctClause) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) LetClause(org.apache.asterix.lang.common.clause.LetClause) Clause(org.apache.asterix.lang.common.base.Clause) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) FieldAccessor(org.apache.asterix.lang.common.expression.FieldAccessor)

Example 43 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class AqlDeleteRewriteVisitor 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);
    List<Clause> clauseList = new ArrayList<>();
    VariableExpr var = deleteStmt.getVariableExpr();
    Clause forClause = new ForClause(var, callExpression);
    clauseList.add(forClause);
    Clause whereClause = null;
    Expression condition = deleteStmt.getCondition();
    if (condition != null) {
        whereClause = new WhereClause(condition);
        clauseList.add(whereClause);
    }
    VariableExpr returnExpr = new VariableExpr(var.getVar());
    returnExpr.setIsNewVar(false);
    FLWOGRExpression flowgr = new FLWOGRExpression(clauseList, returnExpr);
    Query query = new Query(false);
    query.setBody(flowgr);
    deleteStmt.setQuery(query);
    return null;
}
Also used : Query(org.apache.asterix.lang.common.statement.Query) ArrayList(java.util.ArrayList) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) ForClause(org.apache.asterix.lang.aql.clause.ForClause) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) Identifier(org.apache.asterix.lang.common.struct.Identifier) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) Expression(org.apache.asterix.lang.common.base.Expression) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ForClause(org.apache.asterix.lang.aql.clause.ForClause) Clause(org.apache.asterix.lang.common.base.Clause) WhereClause(org.apache.asterix.lang.common.clause.WhereClause)

Example 44 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class AqlQueryRewriter method wrapInLets.

private void wrapInLets() {
    // it into a let clause.
    if (topStatement == null) {
        return;
    }
    Expression body = topStatement.getBody();
    if (body.getKind() != Kind.FLWOGR_EXPRESSION) {
        VarIdentifier var = context.newVariable();
        VariableExpr v = new VariableExpr(var);
        LetClause c1 = new LetClause(v, body);
        ArrayList<Clause> clauseList = new ArrayList<>(1);
        clauseList.add(c1);
        FLWOGRExpression newBody = new FLWOGRExpression(clauseList, new VariableExpr(var));
        topStatement.setBody(newBody);
    }
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) ArrayList(java.util.ArrayList) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ForClause(org.apache.asterix.lang.aql.clause.ForClause) DistinctClause(org.apache.asterix.lang.aql.clause.DistinctClause) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) LetClause(org.apache.asterix.lang.common.clause.LetClause) Clause(org.apache.asterix.lang.common.base.Clause)

Example 45 with VariableExpr

use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(JoinClause joinClause, VariableSubstitutionEnvironment env) throws CompilationException {
    VariableExpr rightVar = joinClause.getRightVariable();
    VariableExpr newRightVar = generateNewVariable(context, rightVar);
    VariableExpr newRightPosVar = joinClause.hasPositionalVariable() ? generateNewVariable(context, joinClause.getPositionalVariable()) : null;
    // Visits the right expression.
    Expression newRightExpr = (Expression) visitUnnesBindingExpression(joinClause.getRightExpression(), env).first;
    // Visits the condition.
    VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
    currentEnv.removeSubstitution(newRightVar);
    if (newRightPosVar != null) {
        currentEnv.removeSubstitution(newRightPosVar);
    }
    // The condition can refer to the newRightVar and newRightPosVar.
    Expression conditionExpr = (Expression) joinClause.getConditionExpression().accept(this, currentEnv).first;
    JoinClause newJoinClause = new JoinClause(joinClause.getJoinType(), newRightExpr, newRightVar, newRightPosVar, conditionExpr);
    return new Pair<>(newJoinClause, currentEnv);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) JoinClause(org.apache.asterix.lang.sqlpp.clause.JoinClause) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Aggregations

VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)59 Expression (org.apache.asterix.lang.common.base.Expression)35 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)29 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)20 ArrayList (java.util.ArrayList)19 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)18 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)17 Pair (org.apache.hyracks.algebricks.common.utils.Pair)16 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)13 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)12 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)11 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)11 HashSet (java.util.HashSet)9 HashMap (java.util.HashMap)8 LetClause (org.apache.asterix.lang.common.clause.LetClause)8 Identifier (org.apache.asterix.lang.common.struct.Identifier)8 ForClause (org.apache.asterix.lang.aql.clause.ForClause)7 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)7 FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)6 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)6