public void endLeftMatch()

in asterix-graphix/src/main/java/org/apache/asterix/graphix/lang/rewrite/lower/LoweringEnvironment.java [138:223]


    public void endLeftMatch() throws CompilationException {
        if (leftClauseCollection.getNonRepresentativeClauses().isEmpty()) {
            // This is an extraneous LEFT-MATCH. Do not modify anything.
            leftClauseCollection = null;
            return;
        }

        // Build our substitution visitor and environment.
        VariableRemapCloneVisitor remapCloneVisitor = new VariableRemapCloneVisitor(graphixRewritingContext);
        VariableExpr nestingVariable = graphixRewritingContext.getGraphixVariableCopy("_LeftMatch");
        final Consumer<VariableExpr> substitutionAdder = v -> {
            VariableExpr nestingVariableCopy = new VariableExpr(nestingVariable.getVar());
            FieldAccessor fieldAccessor = new FieldAccessor(nestingVariableCopy, v.getVar());
            remapCloneVisitor.addSubstitution(v, fieldAccessor);
        };

        // Build up our projection list.
        List<Projection> projectionList = new ArrayList<>();
        List<AbstractClause> leftLowerClauses = leftClauseCollection.getNonRepresentativeClauses();
        for (AbstractClause workingClause : leftLowerClauses) {
            if (workingClause.getClauseType() == Clause.ClauseType.WHERE_CLAUSE) {
                continue;
            }

            // Identify our right variable.
            VariableExpr rightVariable;
            if (workingClause.getClauseType() == Clause.ClauseType.LET_CLAUSE) {
                rightVariable = ((LetClause) workingClause).getVarExpr();

            } else if (workingClause instanceof AbstractBinaryCorrelateClause) {
                rightVariable = ((AbstractBinaryCorrelateClause) workingClause).getRightVariable();

            } else {
                throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, "Illegal clause found!");
            }
            projectionList.add(new Projection(Projection.Kind.NAMED_EXPR, rightVariable,
                    SqlppVariableUtil.toUserDefinedVariableName(rightVariable.getVar()).getValue()));
            substitutionAdder.accept(rightVariable);
        }

        // Nestle our clauses in a SELECT-BLOCK.
        LowerListClause leftLowerClause = new LowerListClause(leftClauseCollection);
        SelectClause selectClause = new SelectClause(null, new SelectRegular(projectionList), false);
        SelectBlock selectBlock = new SelectBlock(selectClause, null, null, null, null);
        selectBlock.setFromClause(new FromGraphClause(leftLowerClause));
        SetOperationInput setOperationInput = new SetOperationInput(selectBlock, null);
        SelectSetOperation selectSetOperation = new SelectSetOperation(setOperationInput, null);
        SelectExpression selectExpression = new SelectExpression(null, selectSetOperation, null, null, true);

        // Merge the collection we just built with our main sequence.
        IVisitorExtension visitorExtension = leftLowerClause.getVisitorExtension();
        Expression conditionExpression = generateJoinCondition(leftLowerClauses.listIterator(), visitorExtension);
        VariableExpr nestingVariableCopy = graphixDeepCopyVisitor.visit(nestingVariable, null);
        JoinClause leftJoinClause = new JoinClause(JoinType.LEFTOUTER, selectExpression, nestingVariableCopy, null,
                (Expression) remapCloneVisitor.substitute(conditionExpression), Literal.Type.MISSING);
        mainClauseCollection.addNonRepresentativeClause(leftJoinClause);

        // Introduce our representative variables back into our main sequence.
        for (LetClause representativeVertexBinding : leftClauseCollection.getRepresentativeVertexBindings()) {
            VariableExpr representativeVariable = representativeVertexBinding.getVarExpr();
            VariableExpr representativeVariableCopy = graphixDeepCopyVisitor.visit(representativeVariable, null);
            Expression rightExpression = representativeVertexBinding.getBindingExpr();
            Expression reboundExpression = (Expression) remapCloneVisitor.substitute(rightExpression);
            mainClauseCollection.addVertexBinding(representativeVariableCopy, reboundExpression);
        }
        for (LetClause representativeEdgeBinding : leftClauseCollection.getRepresentativeEdgeBindings()) {
            VariableExpr representativeVariable = representativeEdgeBinding.getVarExpr();
            VariableExpr representativeVariableCopy = graphixDeepCopyVisitor.visit(representativeVariable, null);
            Expression rightExpression = representativeEdgeBinding.getBindingExpr();
            Expression reboundExpression = (Expression) remapCloneVisitor.substitute(rightExpression);
            mainClauseCollection.addEdgeBinding(representativeVariableCopy, reboundExpression);
        }
        for (LetClause representativePathBinding : leftClauseCollection.getRepresentativePathBindings()) {
            VariableExpr representativeVariable = representativePathBinding.getVarExpr();
            VariableExpr representativeVariableCopy = graphixDeepCopyVisitor.visit(representativeVariable, null);
            Expression rightExpression = representativePathBinding.getBindingExpr();
            Expression reboundExpression = (Expression) remapCloneVisitor.substitute(rightExpression);
            mainClauseCollection.addPathBinding(representativeVariableCopy, reboundExpression);
        }

        // Do not reintroduce our vertex, edge, and path bindings.
        leftClauseCollection.getRepresentativeVertexBindings().clear();
        leftClauseCollection.getRepresentativeEdgeBindings().clear();
        leftClauseCollection.getRepresentativePathBindings().clear();
        leftClauseCollection = null;
    }