Search in sources :

Example 1 with VertexiumCypherScope

use of org.vertexium.cypher.VertexiumCypherScope in project vertexium by visallo.

the class ReturnClauseExecutor method execute.

public VertexiumCypherScope execute(VertexiumCypherQueryContext ctx, boolean distinct, CypherReturnBody returnBody, VertexiumCypherScope scope) {
    List<CypherReturnItem> returnItems = returnBody.getReturnItems().stream().flatMap(ri -> {
        if (ri.getExpression() instanceof CypherAllLiteral) {
            return getAllFieldNamesAsReturnItems(scope);
        }
        return Stream.of(ri);
    }).collect(Collectors.toList());
    LinkedHashSet<String> columnNames = getColumnNames(returnItems);
    Stream<VertexiumCypherScope.Item> rows = scope.stream();
    long aggregationCount = aggregationCount(ctx, returnItems);
    if (returnItems.size() > 0 && aggregationCount == returnItems.size()) {
        rows = Stream.of(getReturnRow(ctx, returnItems, null, scope));
    } else if (aggregationCount > 0 && isGroupable(returnItems.get(0))) {
        Map<Optional<?>, VertexiumCypherScope> groups = groupBy(ctx, returnItems.get(0), rows);
        rows = groups.entrySet().stream().map(group -> getReturnRow(ctx, returnItems, group.getKey(), group.getValue()));
    } else {
        rows = rows.map(row -> getReturnRow(ctx, returnItems, null, row));
    }
    if (distinct) {
        rows = rows.distinct();
    }
    VertexiumCypherScope results = VertexiumCypherScope.newItemsScope(rows, columnNames, scope);
    return applyReturnBody(ctx, returnBody, results);
}
Also used : java.util(java.util) VertexiumException(org.vertexium.VertexiumException) VertexiumLogger(org.vertexium.util.VertexiumLogger) Collectors(java.util.stream.Collectors) VertexiumLoggerFactory(org.vertexium.util.VertexiumLoggerFactory) VertexiumCypherQueryContext(org.vertexium.cypher.VertexiumCypherQueryContext) org.vertexium.cypher.ast.model(org.vertexium.cypher.ast.model) Stream(java.util.stream.Stream) VertexiumCypherScope(org.vertexium.cypher.VertexiumCypherScope) StreamUtils.stream(org.vertexium.util.StreamUtils.stream) ObjectUtils(org.vertexium.cypher.utils.ObjectUtils) AggregationFunction(org.vertexium.cypher.functions.aggregate.AggregationFunction) CypherFunction(org.vertexium.cypher.functions.CypherFunction) StreamUtils(org.vertexium.util.StreamUtils) VertexiumCypherScope(org.vertexium.cypher.VertexiumCypherScope)

Example 2 with VertexiumCypherScope

use of org.vertexium.cypher.VertexiumCypherScope in project vertexium by visallo.

the class UnwindClauseExecutor method execute.

public VertexiumCypherScope execute(VertexiumCypherQueryContext ctx, List<CypherUnwindClause> clauses, VertexiumCypherScope scope) {
    List<VertexiumCypherScope> allResults = new ArrayList<>();
    VertexiumCypherScope accumulatorScope = scope;
    for (CypherUnwindClause clause : clauses) {
        List<VertexiumCypherScope.Item> clauseResults = new ArrayList<>();
        accumulatorScope.stream().forEach(item -> {
            List<VertexiumCypherScope.Item> items = execute(ctx, clause, item).collect(Collectors.toList());
            clauseResults.addAll(items);
        });
        accumulatorScope = VertexiumCypherScope.newItemsScope(clauseResults, scope);
        allResults.add(accumulatorScope);
    }
    return accumulatorScope;
}
Also used : ArrayList(java.util.ArrayList) CypherUnwindClause(org.vertexium.cypher.ast.model.CypherUnwindClause) VertexiumCypherScope(org.vertexium.cypher.VertexiumCypherScope)

Example 3 with VertexiumCypherScope

use of org.vertexium.cypher.VertexiumCypherScope in project vertexium by visallo.

the class WithClauseExecutor method execute.

public VertexiumCypherScope execute(VertexiumCypherQueryContext ctx, CypherWithClause clause, VertexiumCypherScope scope) {
    LOGGER.debug("execute: %s", clause);
    VertexiumCypherScope results = ctx.getReturnClauseExecutor().execute(ctx, clause.isDistinct(), clause.getReturnBody(), scope);
    Stream<VertexiumCypherScope.Item> rows = results.stream();
    if (clause.getWhere() != null) {
        rows = ctx.getExpressionExecutor().applyWhereToResults(ctx, rows, clause.getWhere());
    }
    // TODO remove need to materialize where
    List<VertexiumCypherScope.Item> rowsList = rows.collect(Collectors.toList());
    return VertexiumCypherScope.newItemsScope(rowsList, results.getColumnNames(), null);
}
Also used : VertexiumCypherScope(org.vertexium.cypher.VertexiumCypherScope)

Example 4 with VertexiumCypherScope

use of org.vertexium.cypher.VertexiumCypherScope in project vertexium by visallo.

the class ExpressionExecutor method executeExpression.

public Object executeExpression(VertexiumCypherQueryContext ctx, CypherAstBase expression, ExpressionScope scope) {
    if (expression == null) {
        return null;
    }
    if (expression instanceof CypherExpression) {
        if (expression instanceof CypherBinaryExpression) {
            return executeBinaryExpression(ctx, (CypherBinaryExpression) expression, scope);
        } else if (expression instanceof CypherComparisonExpression) {
            return executeComparisonExpression(ctx, (CypherComparisonExpression) expression, scope);
        } else if (expression instanceof CypherUnaryExpression) {
            return executeUnaryExpression(ctx, (CypherUnaryExpression) expression, scope);
        } else if (expression instanceof CypherTrueExpression) {
            return true;
        } else if (expression instanceof CypherNegateExpression) {
            return executeNegateExpression(ctx, (CypherNegateExpression) expression, scope);
        }
        throw new VertexiumCypherNotImplemented("" + expression);
    }
    if (expression instanceof CypherListLiteral) {
        // noinspection unchecked
        CypherListLiteral<? extends CypherAstBase> list = (CypherListLiteral<? extends CypherAstBase>) expression;
        return executeList(ctx, list, scope);
    }
    if (expression instanceof CypherLiteral) {
        CypherLiteral literal = (CypherLiteral) expression;
        return literal.getValue();
    }
    if (expression instanceof CypherVariable) {
        CypherVariable variable = (CypherVariable) expression;
        return executeObject(ctx, executeVariable(ctx, variable, scope), scope);
    }
    if (expression instanceof CypherLookup) {
        CypherLookup lookup = (CypherLookup) expression;
        return executeLookup(ctx, lookup, scope);
    }
    if (expression instanceof CypherFunctionInvocation) {
        CypherFunctionInvocation functionInvocation = (CypherFunctionInvocation) expression;
        return executeFunctionInvocation(ctx, functionInvocation, scope);
    }
    if (expression instanceof CypherIn) {
        CypherIn in = (CypherIn) expression;
        return executeIn(ctx, in, scope);
    }
    if (expression instanceof CypherArrayAccess) {
        CypherArrayAccess arrayAccess = (CypherArrayAccess) expression;
        return executeArrayAccess(ctx, arrayAccess, scope);
    }
    if (expression instanceof CypherArraySlice) {
        CypherArraySlice arraySlice = (CypherArraySlice) expression;
        return executeArraySlice(ctx, arraySlice, scope);
    }
    if (expression instanceof CypherParameter) {
        CypherParameter parameter = (CypherParameter) expression;
        return executeParameter(ctx, parameter);
    }
    if (expression instanceof CypherIsNull) {
        CypherIsNull isNull = (CypherIsNull) expression;
        return executeIsNull(ctx, isNull, scope);
    }
    if (expression instanceof CypherIsNotNull) {
        CypherIsNotNull isNotNull = (CypherIsNotNull) expression;
        return executeIsNotNull(ctx, isNotNull, scope);
    }
    if (expression instanceof CypherListComprehension) {
        CypherListComprehension listComprehension = (CypherListComprehension) expression;
        return executeListComprehension(ctx, listComprehension, scope);
    }
    if (expression instanceof CypherStringMatch) {
        CypherStringMatch startWith = (CypherStringMatch) expression;
        return executeStringMatch(ctx, startWith, scope);
    }
    if (expression instanceof CypherPatternComprehension) {
        CypherPatternComprehension patternComprehension = (CypherPatternComprehension) expression;
        VertexiumCypherScope matchScope = scope instanceof VertexiumCypherScope ? (VertexiumCypherScope) scope : VertexiumCypherScope.newSingleItemScope((VertexiumCypherScope.Item) scope);
        VertexiumCypherScope results = ctx.getMatchClauseExecutor().execute(ctx, Lists.newArrayList(patternComprehension.getMatchClause()), matchScope);
        return results.stream().map(item -> executeExpression(ctx, patternComprehension.getExpression(), item));
    }
    throw new VertexiumException("not implemented \"" + expression.getClass().getName() + "\": " + expression);
}
Also used : VertexiumCypherNotImplemented(org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented) VertexiumException(org.vertexium.VertexiumException) VertexiumCypherScope(org.vertexium.cypher.VertexiumCypherScope)

Example 5 with VertexiumCypherScope

use of org.vertexium.cypher.VertexiumCypherScope in project vertexium by visallo.

the class MergeClauseExecutor method execute.

public VertexiumCypherScope execute(VertexiumCypherQueryContext ctx, CypherMergeClause clause, VertexiumCypherScope scope) {
    LOGGER.debug("execute: %s", clause);
    // need to materialize the scope
    scope.run();
    PatternPartMatchConstraint patternPartConstraint = new MatchConstraintBuilder().patternPartToConstraints(clause.getPatternPart(), false);
    Stream<VertexiumCypherScope> results = scope.stream().map(item -> {
        Stream<VertexiumCypherScope.Item> patternPartResults = ctx.getMatchClauseExecutor().executePatternPartConstraint(ctx, patternPartConstraint, item);
        return StreamUtils.ifEmpty(patternPartResults, () -> {
            Stream<VertexiumCypherScope.Item> createResults = executeCreate(ctx, clause, patternPartConstraint, item);
            return VertexiumCypherScope.newItemsScope(createResults, scope);
        }, (stream) -> executeMatch(ctx, clause, stream, scope));
    });
    return results.collect(VertexiumCypherScope.concatStreams(scope));
}
Also used : PatternPartMatchConstraint(org.vertexium.cypher.executor.models.match.PatternPartMatchConstraint) MatchConstraintBuilder(org.vertexium.cypher.executor.utils.MatchConstraintBuilder) VertexiumCypherScope(org.vertexium.cypher.VertexiumCypherScope)

Aggregations

VertexiumCypherScope (org.vertexium.cypher.VertexiumCypherScope)8 VertexiumException (org.vertexium.VertexiumException)3 ArrayList (java.util.ArrayList)2 ImmutableList (com.google.common.collect.ImmutableList)1 java.util (java.util)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 VertexiumCypherQueryContext (org.vertexium.cypher.VertexiumCypherQueryContext)1 org.vertexium.cypher.ast.model (org.vertexium.cypher.ast.model)1 CypherUnwindClause (org.vertexium.cypher.ast.model.CypherUnwindClause)1 VertexiumCypherNotImplemented (org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented)1 PatternPartMatchConstraint (org.vertexium.cypher.executor.models.match.PatternPartMatchConstraint)1 MatchConstraintBuilder (org.vertexium.cypher.executor.utils.MatchConstraintBuilder)1 CypherFunction (org.vertexium.cypher.functions.CypherFunction)1 AggregationFunction (org.vertexium.cypher.functions.aggregate.AggregationFunction)1 ObjectUtils (org.vertexium.cypher.utils.ObjectUtils)1 StreamUtils (org.vertexium.util.StreamUtils)1 StreamUtils.stream (org.vertexium.util.StreamUtils.stream)1