Search in sources :

Example 1 with IExpressionAnnotation

use of org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation in project asterixdb by apache.

the class LogicalExpressionDeepCopyWithNewVariablesVisitor method deepCopyAnnotations.

private void deepCopyAnnotations(AbstractFunctionCallExpression src, AbstractFunctionCallExpression dest) {
    Map<Object, IExpressionAnnotation> srcAnnotations = src.getAnnotations();
    Map<Object, IExpressionAnnotation> destAnnotations = dest.getAnnotations();
    for (Map.Entry<Object, IExpressionAnnotation> annotationEntry : srcAnnotations.entrySet()) {
        IExpressionAnnotation annotation = annotationEntry.getValue().copy();
        destAnnotations.put(annotationEntry.getKey(), annotation);
    }
}
Also used : MutableObject(org.apache.commons.lang3.mutable.MutableObject) IExpressionAnnotation(org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation) Map(java.util.Map)

Example 2 with IExpressionAnnotation

use of org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation 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)

Example 3 with IExpressionAnnotation

use of org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation in project asterixdb by apache.

the class LangExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(OperatorExpr op, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    List<OperatorType> ops = op.getOpList();
    int nOps = ops.size();
    if (nOps > 0 && (ops.get(0) == OperatorType.AND || ops.get(0) == OperatorType.OR)) {
        return visitAndOrOperator(op, tupSource);
    }
    List<Expression> exprs = op.getExprList();
    Mutable<ILogicalOperator> topOp = tupSource;
    ILogicalExpression currExpr = null;
    for (int i = 0; i <= nOps; i++) {
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = langExprToAlgExpression(exprs.get(i), topOp);
        topOp = p.second;
        ILogicalExpression e = p.first;
        // now look at the operator
        if (i < nOps) {
            if (OperatorExpr.opIsComparison(ops.get(i))) {
                AbstractFunctionCallExpression c = createComparisonExpression(ops.get(i));
                // chain the operators
                if (i == 0) {
                    c.getArguments().add(new MutableObject<>(e));
                    currExpr = c;
                    if (op.isBroadcastOperand(i)) {
                        BroadcastExpressionAnnotation bcast = new BroadcastExpressionAnnotation();
                        bcast.setObject(BroadcastSide.LEFT);
                        c.getAnnotations().put(BroadcastExpressionAnnotation.BROADCAST_ANNOTATION_KEY, bcast);
                    }
                } else {
                    ((AbstractFunctionCallExpression) currExpr).getArguments().add(new MutableObject<>(e));
                    c.getArguments().add(new MutableObject<>(currExpr));
                    currExpr = c;
                    if (i == 1 && op.isBroadcastOperand(i)) {
                        BroadcastExpressionAnnotation bcast = new BroadcastExpressionAnnotation();
                        bcast.setObject(BroadcastSide.RIGHT);
                        c.getAnnotations().put(BroadcastExpressionAnnotation.BROADCAST_ANNOTATION_KEY, bcast);
                    }
                }
            } else {
                AbstractFunctionCallExpression f = createFunctionCallExpressionForBuiltinOperator(ops.get(i));
                if (i == 0) {
                    f.getArguments().add(new MutableObject<>(e));
                    currExpr = f;
                } else {
                    ((AbstractFunctionCallExpression) currExpr).getArguments().add(new MutableObject<>(e));
                    f.getArguments().add(new MutableObject<>(currExpr));
                    currExpr = f;
                }
            }
        } else {
            // don't forget the last expression...
            ((AbstractFunctionCallExpression) currExpr).getArguments().add(new MutableObject<>(e));
            if (i == 1 && op.isBroadcastOperand(i)) {
                BroadcastExpressionAnnotation bcast = new BroadcastExpressionAnnotation();
                bcast.setObject(BroadcastSide.RIGHT);
                ((AbstractFunctionCallExpression) currExpr).getAnnotations().put(BroadcastExpressionAnnotation.BROADCAST_ANNOTATION_KEY, bcast);
            }
        }
    }
    // Add hints as annotations.
    if (op.hasHints() && (currExpr instanceof AbstractFunctionCallExpression)) {
        AbstractFunctionCallExpression currFuncExpr = (AbstractFunctionCallExpression) currExpr;
        for (IExpressionAnnotation hint : op.getHints()) {
            currFuncExpr.getAnnotations().put(hint, hint);
        }
    }
    LogicalVariable assignedVar = context.newVar();
    AssignOperator a = new AssignOperator(assignedVar, new MutableObject<>(currExpr));
    a.getInputs().add(topOp);
    return new Pair<>(a, assignedVar);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) IExpressionAnnotation(org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) OperatorType(org.apache.asterix.lang.common.struct.OperatorType) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) 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) BroadcastExpressionAnnotation(org.apache.hyracks.algebricks.core.algebra.expressions.BroadcastExpressionAnnotation) 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 IExpressionAnnotation

use of org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation in project asterixdb by apache.

the class OperatorExpressionVisitor method createLessThanExpression.

private Expression createLessThanExpression(Expression lhs, Expression rhs, List<IExpressionAnnotation> hints) throws CompilationException {
    OperatorExpr comparison = new OperatorExpr();
    comparison.addOperand(lhs);
    comparison.addOperand(rhs);
    comparison.addOperator("<=");
    if (hints != null) {
        for (IExpressionAnnotation hint : hints) {
            comparison.addHint(hint);
        }
    }
    return comparison;
}
Also used : OperatorExpr(org.apache.asterix.lang.common.expression.OperatorExpr) IExpressionAnnotation(org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation)

Example 5 with IExpressionAnnotation

use of org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation in project asterixdb by apache.

the class JoinUtils method getBroadcastJoinSide.

private static BroadcastSide getBroadcastJoinSide(ILogicalExpression e, List<LogicalVariable> varsLeft, List<LogicalVariable> varsRight) {
    if (e.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return null;
    }
    AbstractFunctionCallExpression fexp = (AbstractFunctionCallExpression) e;
    IExpressionAnnotation ann = fexp.getAnnotations().get(BroadcastExpressionAnnotation.BROADCAST_ANNOTATION_KEY);
    if (ann == null) {
        return null;
    }
    BroadcastSide side = (BroadcastSide) ann.getObject();
    if (side == null) {
        return null;
    }
    int i;
    switch(side) {
        case LEFT:
            i = 0;
            break;
        case RIGHT:
            i = 1;
            break;
        default:
            return null;
    }
    ArrayList<LogicalVariable> vars = new ArrayList<>();
    fexp.getArguments().get(i).getValue().getUsedVariables(vars);
    if (varsLeft.containsAll(vars)) {
        return BroadcastSide.LEFT;
    } else if (varsRight.containsAll(vars)) {
        return BroadcastSide.RIGHT;
    } else {
        return null;
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) IExpressionAnnotation(org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation) BroadcastSide(org.apache.hyracks.algebricks.core.algebra.expressions.BroadcastExpressionAnnotation.BroadcastSide)

Aggregations

IExpressionAnnotation (org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation)5 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)3 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)3 ArrayList (java.util.ArrayList)2 Expression (org.apache.asterix.lang.common.base.Expression)2 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)2 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)2 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)2 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)2 Mutable (org.apache.commons.lang3.mutable.Mutable)2 Pair (org.apache.hyracks.algebricks.common.utils.Pair)2 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)2 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)2 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)2 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)2 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)2 UnnestingFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression)2 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)2 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)2 Map (java.util.Map)1