use of org.openrdf.query.algebra.ProjectionElemList in project incubator-rya by apache.
the class AggregationPipelineQueryNode method project.
/**
* Add a SPARQL projection or multi-projection operation to the pipeline.
* The number of documents produced by the pipeline after this operation
* will be the number of documents entering this stage (the number of
* intermediate results) multiplied by the number of
* {@link ProjectionElemList}s supplied here. Empty projections are
* unsupported; if one or more projections given binds zero variables, then
* the pipeline will be unchanged and the method will return false.
* @param projections One or more projections, i.e. mappings from the result
* at this stage of the query into a set of variables.
* @return true if the projection(s) were added to the pipeline.
*/
public boolean project(Iterable<ProjectionElemList> projections) {
if (projections == null || !projections.iterator().hasNext()) {
return false;
}
List<Bson> projectOpts = new LinkedList<>();
Set<String> bindingNamesUnion = new HashSet<>();
Set<String> bindingNamesIntersection = null;
for (ProjectionElemList projection : projections) {
if (projection.getElements().isEmpty()) {
// Empty projections are unsupported -- fail when seen
return false;
}
Document valueDoc = new Document();
Document hashDoc = new Document();
Document typeDoc = new Document();
Set<String> projectionBindingNames = new HashSet<>();
for (ProjectionElem elem : projection.getElements()) {
String to = elem.getTargetName();
// If the 'to' name is invalid, replace it internally
if (!isValidFieldName(to)) {
to = replace(to);
}
String from = elem.getSourceName();
// If the 'from' name is invalid, use the internal substitute
if (varToOriginalName.containsValue(from)) {
from = varToOriginalName.inverse().get(from);
}
projectionBindingNames.add(to);
if (to.equals(from)) {
valueDoc.append(to, 1);
hashDoc.append(to, 1);
typeDoc.append(to, 1);
} else {
valueDoc.append(to, valueFieldExpr(from));
hashDoc.append(to, hashFieldExpr(from));
typeDoc.append(to, typeFieldExpr(from));
}
}
bindingNamesUnion.addAll(projectionBindingNames);
if (bindingNamesIntersection == null) {
bindingNamesIntersection = new HashSet<>(projectionBindingNames);
} else {
bindingNamesIntersection.retainAll(projectionBindingNames);
}
projectOpts.add(new Document().append(VALUES, valueDoc).append(HASHES, hashDoc).append(TYPES, typeDoc).append(LEVEL, "$" + LEVEL).append(TIMESTAMP, "$" + TIMESTAMP));
}
if (projectOpts.size() == 1) {
pipeline.add(Aggregates.project(projectOpts.get(0)));
} else {
String listKey = "PROJECTIONS";
Bson projectIndividual = Projections.fields(Projections.computed(VALUES, "$" + listKey + "." + VALUES), Projections.computed(HASHES, "$" + listKey + "." + HASHES), Projections.computed(TYPES, "$" + listKey + "." + TYPES), Projections.include(LEVEL), Projections.include(TIMESTAMP));
pipeline.add(Aggregates.project(Projections.computed(listKey, projectOpts)));
pipeline.add(Aggregates.unwind("$" + listKey));
pipeline.add(Aggregates.project(projectIndividual));
}
assuredBindingNames.clear();
bindingNames.clear();
assuredBindingNames.addAll(bindingNamesIntersection);
bindingNames.addAll(bindingNamesUnion);
return true;
}
use of org.openrdf.query.algebra.ProjectionElemList in project incubator-rya by apache.
the class ConstructConsequentVisitorTest method testConcreteSP.
@Test
public void testConcreteSP() {
Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new ValueConstant(FOAF.PERSON), "x"), new ExtensionElem(new ValueConstant(RDF.TYPE), "y"), new ExtensionElem(new ValueConstant(OWL.CLASS), "z"));
Projection projection = new Projection(extension, new ProjectionElemList(new ProjectionElem("x", "subject"), new ProjectionElem("y", "predicate"), new ProjectionElem("z", "object")));
ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
projection.visit(visitor);
Set<StatementPattern> expected = Sets.newHashSet(new StatementPattern(s(FOAF.PERSON), p(RDF.TYPE), o(OWL.CLASS)));
Assert.assertEquals(expected, visitor.getConsequents());
}
use of org.openrdf.query.algebra.ProjectionElemList in project incubator-rya by apache.
the class ConstructConsequentVisitorTest method testMissingVariables.
@Test
public void testMissingVariables() {
Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new ValueConstant(FOAF.PERSON), "x"), new ExtensionElem(new ValueConstant(RDF.TYPE), "y"));
Projection projection = new Projection(extension, new ProjectionElemList(new ProjectionElem("x", "s"), new ProjectionElem("y", "predicate"), new ProjectionElem("z", "object")));
ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
projection.visit(visitor);
Set<StatementPattern> expected = Sets.newHashSet(new StatementPattern(s(null), p(RDF.TYPE), o(null)));
Assert.assertEquals(expected, visitor.getConsequents());
}
use of org.openrdf.query.algebra.ProjectionElemList in project incubator-rya by apache.
the class SparqlFluoQueryBuilder method getConstructGraphVarOrder.
private static VariableOrder getConstructGraphVarOrder(final Reduced node) {
// get child node
final QueryModelNode child = node.getArg();
Preconditions.checkArgument(child instanceof Projection || child instanceof MultiProjection);
final UnaryTupleOperator unary = (UnaryTupleOperator) child;
// get ProjectionElemList to build ConstructGraph
final List<ProjectionElemList> projections = new ArrayList<>();
if (unary instanceof Projection) {
projections.add(((Projection) unary).getProjectionElemList());
} else {
projections.addAll(((MultiProjection) unary).getProjections());
}
return getConstructGraphVarOrder(projections);
}
use of org.openrdf.query.algebra.ProjectionElemList in project incubator-rya by apache.
the class MultiProjectionEvaluator method make.
/**
* Make a {@link MultiProjectionEvaluator} that processes the logic of a {@link MultiProjection}.
*
* @param multiProjection - Defines the projections that will be processed. (not null)
* @param bNodeIdFactory - Creates the IDs for Blank Nodes. (not null)
* @return A {@link MultiProjectionEvaluator} for the provided {@link MultiProjection}.
*/
public static MultiProjectionEvaluator make(final MultiProjection multiProjection, final BNodeIdFactory bNodeIdFactory) {
requireNonNull(multiProjection);
// Figure out if there are extensions.
final TupleExpr arg = multiProjection.getArg();
final Optional<Extension> extension = (arg instanceof Extension) ? Optional.of((Extension) arg) : Optional.empty();
// If there are, iterate through them and find any blank node source names.
final Set<String> blankNodeSourceNames = new HashSet<>();
if (extension.isPresent()) {
for (final ExtensionElem elem : extension.get().getElements()) {
if (elem.getExpr() instanceof BNodeGenerator) {
blankNodeSourceNames.add(elem.getName());
}
}
}
// Create a ProjectionEvaluator for each projection that is part of the multi.
final Set<ProjectionEvaluator> projections = new HashSet<>();
for (final ProjectionElemList projectionElemList : multiProjection.getProjections()) {
projections.add(new ProjectionEvaluator(projectionElemList, extension));
}
return new MultiProjectionEvaluator(projections, blankNodeSourceNames, bNodeIdFactory);
}
Aggregations