public Expression visit()

in asterix-graphix/src/main/java/org/apache/asterix/graphix/lang/rewrite/visitor/PreRewriteCheckVisitor.java [241:293]


    public Expression visit(EdgePatternExpr edgeExpression, ILangExpression arg) throws CompilationException {
        PreRewriteCheckEnvironment workingEnvironment = environmentMap.get(arg);
        EdgeDescriptor edgeDescriptor = edgeExpression.getEdgeDescriptor();
        Set<ElementLabel> environmentLabels = workingEnvironment.edgeLabels;
        if (environmentLabels.isEmpty()) {
            throw new CompilationException(ErrorCode.COMPILATION_ERROR, edgeExpression.getSourceLocation(),
                    "Query edge given, but no edge is defined in the schema.");
        }

        for (ElementLabel edgeLabel : edgeDescriptor.getEdgeLabels()) {
            if (environmentLabels.stream().noneMatch(e -> e.getLabelName().equals(edgeLabel.getLabelName()))) {
                throw new CompilationException(ErrorCode.COMPILATION_ERROR, edgeExpression.getSourceLocation(),
                        "Edge label " + edgeLabel + " does not exist in the given graph schema.");
            }
        }
        if (edgeDescriptor.getFilterExpr() != null) {
            edgeDescriptor.getFilterExpr().accept(new AbstractGraphixQueryVisitor() {
                @Override
                public Expression visit(VariableExpr varExpr, ILangExpression arg) throws CompilationException {
                    if (workingEnvironment.allElementVariables.contains(varExpr.getVar())) {
                        SourceLocation sourceLocation = edgeDescriptor.getFilterExpr().getSourceLocation();
                        throw new CompilationException(ErrorCode.COMPILATION_ERROR, sourceLocation,
                                "Cannot reference other graph elements in a filter expression! Consider putting your "
                                        + "condition in a WHERE clause.");
                    }
                    return super.visit(varExpr, arg);
                }
            }, null);
        }
        if (edgeDescriptor.getVariableExpr() != null) {
            Identifier edgeIdentifier = edgeDescriptor.getVariableExpr().getVar();
            if (workingEnvironment.edgeVariables.contains(edgeIdentifier)) {
                throw new CompilationException(ErrorCode.COMPILATION_ERROR, edgeExpression.getSourceLocation(),
                        "Edge " + edgeIdentifier + " defined more than once. Edges can only connect two vertices.");
            }
            workingEnvironment.edgeVariables.add(edgeIdentifier);
            workingEnvironment.allElementVariables.add(edgeIdentifier);
        }
        if (edgeDescriptor.getPatternType() == EdgeDescriptor.PatternType.PATH) {
            Integer minimumHops = edgeDescriptor.getMinimumHops();
            Integer maximumHops = edgeDescriptor.getMaximumHops();
            if ((maximumHops != null && maximumHops == 0) || (minimumHops != null && minimumHops == 0)) {
                throw new CompilationException(ErrorCode.COMPILATION_ERROR, edgeExpression.getSourceLocation(),
                        "Sub-path edges cannot have a hop length less than 1.");

            } else if (minimumHops != null && maximumHops != null && maximumHops < minimumHops) {
                throw new CompilationException(ErrorCode.COMPILATION_ERROR, edgeExpression.getSourceLocation(),
                        "Sub-path edges cannot have a maximum hop length (" + maximumHops
                                + ") less than the minimum hop length (" + minimumHops + ").");
            }
        }
        return edgeExpression;
    }