Search in sources :

Example 1 with FieldAccessor

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

the class SqlppGroupBySugarVisitor method wrapAggregationArgument.

private Expression wrapAggregationArgument(Expression argExpr) throws CompilationException {
    Expression expr = argExpr;
    Set<VariableExpr> freeVars = SqlppRewriteUtil.getFreeVariable(expr);
    VariableExpr fromBindingVar = new VariableExpr(context.newVariable());
    FromTerm fromTerm = new FromTerm(groupVar, fromBindingVar, null, null);
    FromClause fromClause = new FromClause(Collections.singletonList(fromTerm));
    // Maps field variable expressions to field accesses.
    Map<Expression, Expression> varExprMap = new HashMap<>();
    for (VariableExpr usedVar : freeVars) {
        // Reference to a field in the group variable.
        if (fieldVars.contains(usedVar)) {
            // Rewrites to a reference to a field in the group variable.
            varExprMap.put(usedVar, new FieldAccessor(fromBindingVar, SqlppVariableUtil.toUserDefinedVariableName(usedVar.getVar())));
        }
    }
    // Select clause.
    SelectElement selectElement = new SelectElement(SqlppRewriteUtil.substituteExpression(expr, varExprMap, context));
    SelectClause selectClause = new SelectClause(selectElement, null, false);
    // Construct the select expression.
    SelectBlock selectBlock = new SelectBlock(selectClause, fromClause, null, null, null, null, null);
    SelectSetOperation selectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
    return new SelectExpression(null, selectSetOperation, null, null, true);
}
Also used : SelectElement(org.apache.asterix.lang.sqlpp.clause.SelectElement) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) HashMap(java.util.HashMap) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FieldAccessor(org.apache.asterix.lang.common.expression.FieldAccessor) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) 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) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm)

Example 2 with FieldAccessor

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

the class CloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FieldAccessor fa, VariableSubstitutionEnvironment env) throws CompilationException {
    Pair<ILangExpression, VariableSubstitutionEnvironment> p = fa.getExpr().accept(this, env);
    FieldAccessor newF = new FieldAccessor((Expression) p.first, fa.getIdent());
    return new Pair<>(newF, p.second);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) FieldAccessor(org.apache.asterix.lang.common.expression.FieldAccessor) 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 FieldAccessor

use of org.apache.asterix.lang.common.expression.FieldAccessor 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)

Aggregations

FieldAccessor (org.apache.asterix.lang.common.expression.FieldAccessor)3 HashMap (java.util.HashMap)2 Expression (org.apache.asterix.lang.common.base.Expression)2 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)2 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)2 DistinctClause (org.apache.asterix.lang.aql.clause.DistinctClause)1 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 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)1 LetClause (org.apache.asterix.lang.common.clause.LetClause)1 WhereClause (org.apache.asterix.lang.common.clause.WhereClause)1 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)1 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)1 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)1 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)1 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)1 FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)1 SelectBlock (org.apache.asterix.lang.sqlpp.clause.SelectBlock)1 SelectClause (org.apache.asterix.lang.sqlpp.clause.SelectClause)1