Search in sources :

Example 1 with LeftOuterUnnestOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator in project asterixdb by apache.

the class SqlppExpressionToPlanTranslator method generateUnnestForBinaryCorrelateRightBranch.

private Pair<ILogicalOperator, LogicalVariable> generateUnnestForBinaryCorrelateRightBranch(AbstractBinaryCorrelateClause binaryCorrelate, Mutable<ILogicalOperator> inputOpRef, boolean innerUnnest) throws CompilationException {
    LogicalVariable rightVar = context.newVarFromExpression(binaryCorrelate.getRightVariable());
    Expression rightExpr = binaryCorrelate.getRightExpression();
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(rightExpr, inputOpRef);
    ILogicalOperator unnestOp;
    if (binaryCorrelate.hasPositionalVariable()) {
        LogicalVariable pVar = context.newVarFromExpression(binaryCorrelate.getPositionalVariable());
        // We set the positional variable type as BIGINT type.
        unnestOp = innerUnnest ? new UnnestOperator(rightVar, new MutableObject<>(makeUnnestExpression(eo.first)), pVar, BuiltinType.AINT64, new PositionWriter()) : new LeftOuterUnnestOperator(rightVar, new MutableObject<>(makeUnnestExpression(eo.first)), pVar, BuiltinType.AINT64, new PositionWriter());
    } else {
        unnestOp = innerUnnest ? new UnnestOperator(rightVar, new MutableObject<>(makeUnnestExpression(eo.first))) : new LeftOuterUnnestOperator(rightVar, new MutableObject<>(makeUnnestExpression(eo.first)));
    }
    unnestOp.getInputs().add(eo.second);
    return new Pair<>(unnestOp, rightVar);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) 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) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 2 with LeftOuterUnnestOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator in project asterixdb by apache.

the class RemoveLeftOuterUnnestForLeftOuterJoinRule method rewritePre.

@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    ILogicalOperator op1 = opRef.getValue();
    // Checks the plan pattern.
    if (!checkOperatorPattern(op1)) {
        return false;
    }
    LeftOuterUnnestOperator outerUnnest = (LeftOuterUnnestOperator) op1;
    GroupByOperator gbyOperator = (GroupByOperator) outerUnnest.getInputs().get(0).getValue();
    LeftOuterJoinOperator lojOperator = (LeftOuterJoinOperator) gbyOperator.getInputs().get(0).getValue();
    // Checks whether the left outer unnest and the group-by operator are qualified for rewriting.
    Triple<Boolean, ILogicalExpression, ILogicalExpression> checkGbyResult = checkUnnestAndGby(outerUnnest, gbyOperator);
    // The argument for listify and not(is-missing(...)) check should be variables.
    if (!isVariableReference(checkGbyResult.second) || !isVariableReference(checkGbyResult.third)) {
        return false;
    }
    // Checks whether both the listify variable and the condition test variable are from the right input
    // branch of the left outer join.
    LogicalVariable listifyVar = ((VariableReferenceExpression) checkGbyResult.second).getVariableReference();
    LogicalVariable conditionTestVar = ((VariableReferenceExpression) checkGbyResult.third).getVariableReference();
    if (!checkListifyAndConditionVar(lojOperator, listifyVar, conditionTestVar)) {
        return false;
    }
    // Does the rewrite.
    removeGroupByAndOuterUnnest(opRef, context, outerUnnest, gbyOperator, lojOperator, listifyVar);
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) LeftOuterJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterJoinOperator) LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator)

Example 3 with LeftOuterUnnestOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator in project asterixdb by apache.

the class SqlppExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(JoinClause joinClause, Mutable<ILogicalOperator> inputRef) throws CompilationException {
    Mutable<ILogicalOperator> leftInputRef = uncorrelatedLeftBranchStack.pop();
    if (joinClause.getJoinType() == JoinType.INNER) {
        Pair<ILogicalOperator, LogicalVariable> rightBranch = generateUnnestForBinaryCorrelateRightBranch(joinClause, inputRef, true);
        // A join operator with condition TRUE.
        AbstractBinaryJoinOperator joinOperator = new InnerJoinOperator(new MutableObject<ILogicalExpression>(ConstantExpression.TRUE), leftInputRef, new MutableObject<ILogicalOperator>(rightBranch.first));
        Mutable<ILogicalOperator> joinOpRef = new MutableObject<>(joinOperator);
        // Add an additional filter operator.
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> conditionExprOpPair = langExprToAlgExpression(joinClause.getConditionExpression(), joinOpRef);
        SelectOperator filter = new SelectOperator(new MutableObject<ILogicalExpression>(conditionExprOpPair.first), false, null);
        filter.getInputs().add(conditionExprOpPair.second);
        return new Pair<>(filter, rightBranch.second);
    } else {
        // Creates a subplan operator.
        SubplanOperator subplanOp = new SubplanOperator();
        Mutable<ILogicalOperator> ntsRef = new MutableObject<>(new NestedTupleSourceOperator(new MutableObject<ILogicalOperator>(subplanOp)));
        subplanOp.getInputs().add(leftInputRef);
        // Enters the translation for a subplan.
        context.enterSubplan();
        // Adds an unnest operator to unnest to right expression.
        Pair<ILogicalOperator, LogicalVariable> rightBranch = generateUnnestForBinaryCorrelateRightBranch(joinClause, ntsRef, true);
        AbstractUnnestNonMapOperator rightUnnestOp = (AbstractUnnestNonMapOperator) rightBranch.first;
        // Adds an additional filter operator for the join condition.
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> conditionExprOpPair = langExprToAlgExpression(joinClause.getConditionExpression(), new MutableObject<ILogicalOperator>(rightUnnestOp));
        SelectOperator filter = new SelectOperator(new MutableObject<ILogicalExpression>(conditionExprOpPair.first), false, null);
        filter.getInputs().add(conditionExprOpPair.second);
        ILogicalOperator currentTopOp = filter;
        LogicalVariable varToListify;
        boolean hasRightPosVar = rightUnnestOp.getPositionalVariable() != null;
        if (hasRightPosVar) {
            // Creates record to get correlation between the two aggregate variables.
            ScalarFunctionCallExpression recordCreationFunc = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.CLOSED_RECORD_CONSTRUCTOR), // Field name for the listified right unnest var.
            new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AString("unnestvar")))), // The listified right unnest var
            new MutableObject<ILogicalExpression>(new VariableReferenceExpression(rightUnnestOp.getVariable())), // Field name for the listified right unnest positional var.
            new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AString("posvar")))), // The listified right unnest positional var.
            new MutableObject<ILogicalExpression>(new VariableReferenceExpression(rightUnnestOp.getPositionalVariable())));
            // Assigns the record constructor function to a record variable.
            LogicalVariable recordVar = context.newVar();
            AssignOperator assignOp = new AssignOperator(recordVar, new MutableObject<ILogicalExpression>(recordCreationFunc));
            assignOp.getInputs().add(new MutableObject<ILogicalOperator>(currentTopOp));
            // Sets currentTopOp and varToListify for later usages.
            currentTopOp = assignOp;
            varToListify = recordVar;
        } else {
            varToListify = rightUnnestOp.getVariable();
        }
        // Adds an aggregate operator to listfy unnest variables.
        AggregateFunctionCallExpression fListify = BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.LISTIFY, mkSingletonArrayList(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(varToListify))));
        LogicalVariable aggVar = context.newSubplanOutputVar();
        AggregateOperator aggOp = new AggregateOperator(mkSingletonArrayList(aggVar), mkSingletonArrayList(new MutableObject<ILogicalExpression>(fListify)));
        aggOp.getInputs().add(new MutableObject<ILogicalOperator>(currentTopOp));
        // Exits the translation of a subplan.
        context.exitSubplan();
        // Sets the nested subplan of the subplan operator.
        ILogicalPlan subplan = new ALogicalPlanImpl(new MutableObject<ILogicalOperator>(aggOp));
        subplanOp.getNestedPlans().add(subplan);
        // Outer unnest the aggregated var from the subplan.
        LogicalVariable outerUnnestVar = context.newVar();
        LeftOuterUnnestOperator outerUnnestOp = new LeftOuterUnnestOperator(outerUnnestVar, new MutableObject<ILogicalExpression>(makeUnnestExpression(new VariableReferenceExpression(aggVar))));
        outerUnnestOp.getInputs().add(new MutableObject<ILogicalOperator>(subplanOp));
        currentTopOp = outerUnnestOp;
        if (hasRightPosVar) {
            ScalarFunctionCallExpression fieldAccessForRightUnnestVar = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_INDEX), new MutableObject<ILogicalExpression>(new VariableReferenceExpression(outerUnnestVar)), new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AInt32(0)))));
            ScalarFunctionCallExpression fieldAccessForRightPosVar = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_INDEX), new MutableObject<ILogicalExpression>(new VariableReferenceExpression(outerUnnestVar)), new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AInt32(1)))));
            // Creates variables for assign.
            LogicalVariable rightUnnestVar = context.newVar();
            LogicalVariable rightPosVar = context.newVar();
            // Relate the assigned variables to the variable expression in AST.
            context.setVar(joinClause.getRightVariable(), rightUnnestVar);
            context.setVar(joinClause.getPositionalVariable(), rightPosVar);
            // Varaibles to assign.
            List<LogicalVariable> assignVars = new ArrayList<>();
            assignVars.add(rightUnnestVar);
            assignVars.add(rightPosVar);
            // Expressions for assign.
            List<Mutable<ILogicalExpression>> assignExprs = new ArrayList<>();
            assignExprs.add(new MutableObject<ILogicalExpression>(fieldAccessForRightUnnestVar));
            assignExprs.add(new MutableObject<ILogicalExpression>(fieldAccessForRightPosVar));
            // Creates the assign operator.
            AssignOperator assignOp = new AssignOperator(assignVars, assignExprs);
            assignOp.getInputs().add(new MutableObject<ILogicalOperator>(currentTopOp));
            currentTopOp = assignOp;
        } else {
            context.setVar(joinClause.getRightVariable(), outerUnnestVar);
        }
        return new Pair<>(currentTopOp, null);
    }
}
Also used : NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) AbstractUnnestNonMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractUnnestNonMapOperator) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) InnerJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InnerJoinOperator) AbstractBinaryJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractBinaryJoinOperator) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ALogicalPlanImpl(org.apache.hyracks.algebricks.core.algebra.plan.ALogicalPlanImpl) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) AString(org.apache.asterix.om.base.AString) MutableObject(org.apache.commons.lang3.mutable.MutableObject) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) AInt32(org.apache.asterix.om.base.AInt32) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Example 4 with LeftOuterUnnestOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator in project asterixdb by apache.

the class LogicalOperatorDeepCopyWithNewVariablesVisitor method visitLeftOuterUnnestOperator.

@Override
public ILogicalOperator visitLeftOuterUnnestOperator(LeftOuterUnnestOperator op, ILogicalOperator arg) throws AlgebricksException {
    LeftOuterUnnestOperator opCopy = new LeftOuterUnnestOperator(deepCopyVariable(op.getVariable()), exprDeepCopyVisitor.deepCopyExpressionReference(op.getExpressionRef()), deepCopyVariable(op.getPositionalVariable()), op.getPositionalVariableType(), op.getPositionWriter());
    deepCopyInputsAnnotationsAndExecutionMode(op, arg, opCopy);
    return opCopy;
}
Also used : LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator)

Example 5 with LeftOuterUnnestOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator in project asterixdb by apache.

the class IsomorphismOperatorVisitor method visitLeftOuterUnnestOperator.

@Override
public Boolean visitLeftOuterUnnestOperator(LeftOuterUnnestOperator op, ILogicalOperator arg) throws AlgebricksException {
    AbstractLogicalOperator aop = (AbstractLogicalOperator) arg;
    if (aop.getOperatorTag() != LogicalOperatorTag.LEFT_OUTER_UNNEST) {
        return Boolean.FALSE;
    }
    LeftOuterUnnestOperator unnestOpArg = (LeftOuterUnnestOperator) copyAndSubstituteVar(op, arg);
    boolean isomorphic = VariableUtilities.varListEqualUnordered(op.getVariables(), unnestOpArg.getVariables()) && variableEqual(op.getPositionalVariable(), unnestOpArg.getPositionalVariable());
    if (!isomorphic) {
        return Boolean.FALSE;
    }
    isomorphic = op.getExpressionRef().getValue().equals(unnestOpArg.getExpressionRef().getValue());
    return isomorphic;
}
Also used : AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator)

Aggregations

LeftOuterUnnestOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator)5 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)3 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)3 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)3 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)3 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)2 Mutable (org.apache.commons.lang3.mutable.Mutable)2 Pair (org.apache.hyracks.algebricks.common.utils.Pair)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 ArrayList (java.util.ArrayList)1 Expression (org.apache.asterix.lang.common.base.Expression)1 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)1 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)1 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)1 AInt32 (org.apache.asterix.om.base.AInt32)1 AString (org.apache.asterix.om.base.AString)1 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1