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);
}
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;
}
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);
}
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);
}
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));
}
Aggregations