public Expression visit()

in asterix-graphix/src/main/java/org/apache/asterix/graphix/lang/rewrite/visitor/PopulateUnknownsVisitor.java [75:130]


    public Expression visit(SelectBlock selectBlock, ILangExpression arg) throws CompilationException {
        super.visit(selectBlock, arg);

        if (selectBlock.hasGroupbyClause()) {
            // Collect all variables that should belong in the GROUP-BY field list.
            Set<VariableExpr> userLiveVariables = new HashSet<>();
            if (selectBlock.hasFromClause() && selectBlock.getFromClause() instanceof FromGraphClause) {
                FromGraphClause fromGraphClause = (FromGraphClause) selectBlock.getFromClause();
                for (MatchClause matchClause : fromGraphClause.getMatchClauses()) {
                    for (PathPatternExpr pathExpression : matchClause.getPathExpressions()) {
                        if (pathExpression.getVariableExpr() != null) {
                            userLiveVariables.add(pathExpression.getVariableExpr());
                        }
                        for (VertexPatternExpr vertexExpression : pathExpression.getVertexExpressions()) {
                            userLiveVariables.add(vertexExpression.getVariableExpr());
                        }
                        for (EdgePatternExpr edgeExpression : pathExpression.getEdgeExpressions()) {
                            userLiveVariables.add(edgeExpression.getEdgeDescriptor().getVariableExpr());
                        }
                    }
                }
                if (!fromGraphClause.getCorrelateClauses().isEmpty()) {
                    List<AbstractBinaryCorrelateClause> correlateClauses = fromGraphClause.getCorrelateClauses();
                    for (AbstractBinaryCorrelateClause correlateClause : correlateClauses) {
                        userLiveVariables.add(correlateClause.getRightVariable());
                    }
                }

            } else if (selectBlock.hasFromClause()) {
                FromClause fromClause = selectBlock.getFromClause();
                for (FromTerm fromTerm : fromClause.getFromTerms()) {
                    userLiveVariables.add(fromTerm.getLeftVariable());
                    for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
                        userLiveVariables.add(correlateClause.getRightVariable());
                    }
                }
            }
            if (selectBlock.hasLetWhereClauses()) {
                for (AbstractClause abstractClause : selectBlock.getLetWhereList()) {
                    if (abstractClause.getClauseType() == Clause.ClauseType.LET_CLAUSE) {
                        LetClause letClause = (LetClause) abstractClause;
                        userLiveVariables.add(letClause.getVarExpr());
                    }
                }
            }

            // Add the live variables to our GROUP-BY field list.
            List<Pair<Expression, Identifier>> newGroupFieldList = new ArrayList<>();
            for (VariableExpr userLiveVariable : userLiveVariables) {
                String variableName = SqlppVariableUtil.toUserDefinedName(userLiveVariable.getVar().getValue());
                newGroupFieldList.add(new Pair<>(userLiveVariable, new Identifier(variableName)));
            }
            selectBlock.getGroupbyClause().setGroupFieldList(newGroupFieldList);
        }
        return null;
    }