Search in sources :

Example 26 with VariableExpr

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

the class SetOperationVisitor method visit.

@Override
public Expression visit(SelectExpression selectExpression, ILangExpression arg) throws CompilationException {
    // Recursively visit nested select expressions.
    SelectSetOperation selectSetOperation = selectExpression.getSelectSetOperation();
    if (!selectSetOperation.hasRightInputs() || !(selectExpression.hasOrderby() || selectExpression.hasLimit())) {
        return super.visit(selectExpression, arg);
    }
    OrderbyClause orderBy = selectExpression.getOrderbyClause();
    LimitClause limit = selectExpression.getLimitClause();
    // Wraps the set operation part with a subquery.
    SelectExpression nestedSelectExpression = new SelectExpression(null, selectSetOperation, null, null, true);
    // Binding variable for the subquery.
    VariableExpr newBindingVar = new VariableExpr(context.newVariable());
    FromTerm newFromTerm = new FromTerm(nestedSelectExpression, newBindingVar, null, null);
    FromClause newFromClause = new FromClause(new ArrayList<>(Collections.singletonList(newFromTerm)));
    SelectClause selectClause = new SelectClause(new SelectElement(newBindingVar), null, false);
    SelectBlock selectBlock = new SelectBlock(selectClause, newFromClause, null, null, null, null, null);
    SelectSetOperation newSelectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
    // Puts together the generated select-from-where query and order by/limit.
    SelectExpression newSelectExpression = new SelectExpression(selectExpression.getLetList(), newSelectSetOperation, orderBy, limit, selectExpression.isSubquery());
    return super.visit(newSelectExpression, arg);
}
Also used : LimitClause(org.apache.asterix.lang.common.clause.LimitClause) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) SelectElement(org.apache.asterix.lang.sqlpp.clause.SelectElement) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) SetOperationInput(org.apache.asterix.lang.sqlpp.struct.SetOperationInput) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) OrderbyClause(org.apache.asterix.lang.common.clause.OrderbyClause) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm)

Example 27 with VariableExpr

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

the class SqlppRewriteUtil method getFreeVariable.

public static Set<VariableExpr> getFreeVariable(Expression expr) throws CompilationException {
    Set<VariableExpr> vars = new HashSet<>();
    FreeVariableVisitor visitor = new FreeVariableVisitor();
    expr.accept(visitor, vars);
    return vars;
}
Also used : VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) FreeVariableVisitor(org.apache.asterix.lang.sqlpp.visitor.FreeVariableVisitor) HashSet(java.util.HashSet)

Example 28 with VariableExpr

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

the class SqlppVariableUtil method getBindingVariables.

public static Collection<VariableExpr> getBindingVariables(GroupbyClause gbyClause) {
    List<VariableExpr> bindingVars = new ArrayList<>();
    if (gbyClause == null) {
        return bindingVars;
    }
    for (GbyVariableExpressionPair gbyKey : gbyClause.getGbyPairList()) {
        VariableExpr var = gbyKey.getVar();
        if (var != null) {
            bindingVars.add(var);
        }
    }
    for (GbyVariableExpressionPair gbyKey : gbyClause.getDecorPairList()) {
        VariableExpr var = gbyKey.getVar();
        if (var != null) {
            bindingVars.add(var);
        }
    }
    if (gbyClause.hasWithMap()) {
        bindingVars.addAll(gbyClause.getWithVarMap().values());
    }
    bindingVars.add(gbyClause.getGroupVar());
    return bindingVars;
}
Also used : GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) ArrayList(java.util.ArrayList) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 29 with VariableExpr

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

the class QueryPrintVisitor method visit.

@Override
public Void visit(GroupbyClause gc, Integer step) throws CompilationException {
    out.println(skip(step) + "Groupby");
    for (GbyVariableExpressionPair pair : gc.getGbyPairList()) {
        if (pair.getVar() != null) {
            pair.getVar().accept(this, step + 1);
            out.println(skip(step + 1) + ":=");
        }
        pair.getExpr().accept(this, step + 1);
    }
    if (gc.hasDecorList()) {
        out.println(skip(step + 1) + "Decor");
        for (GbyVariableExpressionPair pair : gc.getDecorPairList()) {
            if (pair.getVar() != null) {
                pair.getVar().accept(this, step + 1);
                out.println(skip(step + 1) + ":=");
            }
            pair.getExpr().accept(this, step + 1);
        }
    }
    if (gc.hasWithMap()) {
        out.println(skip(step + 1) + "With");
        for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) {
            Expression key = entry.getKey();
            VariableExpr value = entry.getValue();
            key.accept(this, step + 1);
            if (!key.equals(value)) {
                out.println(skip(step + 1) + "AS");
                value.accept(this, step + 1);
            }
        }
    }
    out.println();
    return null;
}
Also used : TypeReferenceExpression(org.apache.asterix.lang.common.expression.TypeReferenceExpression) TypeExpression(org.apache.asterix.lang.common.expression.TypeExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 30 with VariableExpr

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

the class LangExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(CallExpr fcall, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    LogicalVariable v = context.newVar();
    FunctionSignature signature = fcall.getFunctionSignature();
    List<Mutable<ILogicalExpression>> args = new ArrayList<>();
    Mutable<ILogicalOperator> topOp = tupSource;
    for (Expression expr : fcall.getExprList()) {
        switch(expr.getKind()) {
            case VARIABLE_EXPRESSION:
                LogicalVariable var = context.getVar(((VariableExpr) expr).getVar().getId());
                args.add(new MutableObject<>(new VariableReferenceExpression(var)));
                break;
            case LITERAL_EXPRESSION:
                LiteralExpr val = (LiteralExpr) expr;
                args.add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(ConstantHelper.objectFromLiteral(val.getValue())))));
                break;
            default:
                Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(expr, topOp);
                AbstractLogicalOperator o1 = (AbstractLogicalOperator) eo.second.getValue();
                args.add(new MutableObject<>(eo.first));
                if (o1 != null && !(o1.getOperatorTag() == LogicalOperatorTag.ASSIGN && hasOnlyChild(o1, topOp))) {
                    topOp = eo.second;
                }
                break;
        }
    }
    AbstractFunctionCallExpression f;
    if ((f = lookupUserDefinedFunction(signature, args)) == null) {
        f = lookupBuiltinFunction(signature.getName(), signature.getArity(), args);
    }
    if (f == null) {
        throw new CompilationException(" Unknown function " + signature.getName() + "@" + signature.getArity());
    }
    // Put hints into function call expr.
    if (fcall.hasHints()) {
        for (IExpressionAnnotation hint : fcall.getHints()) {
            f.getAnnotations().put(hint, hint);
        }
    }
    AssignOperator op = new AssignOperator(v, new MutableObject<>(f));
    if (topOp != null) {
        op.getInputs().add(topOp);
    }
    return new Pair<>(op, v);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) CompilationException(org.apache.asterix.common.exceptions.CompilationException) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) IExpressionAnnotation(org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) Expression(org.apache.asterix.lang.common.base.Expression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair)

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