@Override public void meet(Projection node) { projection = node; node.getArg().visit(this); projection = null; }
/** * Make a {@link ProjectionEvaluator} that processes the logic of a {@link Projection}. * * @param projection - Defines the projection that will be processed. (not null) * @return A {@link ProjectionEvaluator} for the provided {@link Projection}. */ public static ProjectionEvaluator make(final Projection projection) { requireNonNull(projection); final ProjectionElemList projectionElems = projection.getProjectionElemList(); final TupleExpr arg = projection.getArg(); final Optional<Extension> extension = arg instanceof Extension ? Optional.of((Extension)arg) : Optional.empty(); return new ProjectionEvaluator(projectionElems, extension); }
private UnaryTupleOperator projection() { if (!mProjectionPatterns.isEmpty()) { return multiProjection(); } else { Extension aExt = null; ProjectionElemList aList = new ProjectionElemList(); for (String aVar : mProjectionVars) { aList.addElement(new ProjectionElem(aVar)); } Projection aProjection = new Projection(); aProjection.setProjectionElemList(aList); if (aExt != null) { aProjection.setArg(aExt); } return aProjection; } }
@Override public void meet(Projection projectionNode) throws Exception { projectionNode.visitChildren(this); if (projectionNode.getArg() instanceof AggregationPipelineQueryNode && projectionNode.getParentNode() != null) { AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) projectionNode.getArg(); if (pipelineNode.project(Arrays.asList(projectionNode.getProjectionElemList()))) { projectionNode.replaceWith(pipelineNode); } } }
@Override public Projection clone() { Projection clone = (Projection)super.clone(); clone.setProjectionElemList(getProjectionElemList().clone()); return clone; }
@Override protected BindingSet convert(BindingSet sourceBindings) throws QueryEvaluationException { return project(projection.getProjectionElemList(), sourceBindings, parentBindings, !isOuterProjection); }
if (projection.isSubquery()) { pushDownBindings = new QueryBindingSet(); for (ProjectionElem pe : projection.getProjectionElemList().getElements()) { Value targetValue = bindings.getValue(pe.getSourceName()); if (targetValue != null) { return parent.push(bs == null ? null : ProjectionIterator.project(projection.getProjectionElemList(), bs, bindings, includeAll)); }, projection.getArg(), pushDownBindings);
/** * * This method moves the Filters of a specified {@link TupleExpr} to the top * of the TupleExpr. * * @param query * - query whose filters will be relocated * @return - TupleExpr with filters relocated to top */ public static TupleExpr moveFiltersToTop(TupleExpr query) { ProjectionAndFilterGatherer fg = new ProjectionAndFilterGatherer(); query.visit(fg); List<ValueExpr> filterCond = new ArrayList<>(fg.filterCond); Projection projection = fg.projection; if (filterCond.size() == 0) { return query; } Filter first = new Filter(); first.setCondition(filterCond.remove(0)); Filter current = first; for (ValueExpr cond : filterCond) { Filter filter = new Filter(null, cond); current.setArg(filter); current = filter; } TupleExpr te = projection.getArg(); projection.setArg(first); current.setArg(te); return query; }
@Override public TupleExpr visit(ASTDescribe node, Object data) throws VisitorException { TupleExpr tupleExpr = (TupleExpr)data; if (tupleExpr == null) { tupleExpr = new SingletonSet(); } Extension e = new Extension(); ProjectionElemList projectionElements = new ProjectionElemList(); for (int i = 0; i < node.jjtGetNumChildren(); i++) { ValueExpr resource = (ValueExpr)node.jjtGetChild(i).jjtAccept(this, null); if (resource instanceof Var) { projectionElements.addElement(new ProjectionElem(((Var)resource).getName())); } else { String alias = "_describe_" + UUID.randomUUID().toString().replaceAll("-", "_"); ExtensionElem elem = new ExtensionElem(resource, alias); e.addElement(elem); projectionElements.addElement(new ProjectionElem(alias)); } } if (!e.getElements().isEmpty()) { e.setArg(tupleExpr); tupleExpr = e; } Projection p = new Projection(tupleExpr, projectionElements); return new DescribeOperator(p); }
@Test public void testEmptyProjection() throws Exception { StatementPattern isClass = new StatementPattern(constant(UNDERGRAD), constant(RDF.TYPE), constant(OWL.CLASS)); QueryRoot queryTree = new QueryRoot(new Projection(isClass, new ProjectionElemList())); SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection); queryTree.visit(visitor); Assert.assertTrue(queryTree.getArg() instanceof Projection); Projection projectNode = (Projection) queryTree.getArg(); Assert.assertTrue(projectNode.getArg() instanceof AggregationPipelineQueryNode); AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) projectNode.getArg(); Assert.assertEquals(Sets.newHashSet(), pipelineNode.getAssuredBindingNames()); }
private Projection visitResultVariables(Resource resultVars, Map<String, ProjectionElem> previousProjElems) throws RDF4JException { ProjectionElemList projElemList = new ProjectionElemList(); Iteration<? extends Resource, QueryEvaluationException> iter = TripleSources.listResources( resultVars, store); while (iter.hasNext()) { Resource r = iter.next(); ProjectionElem projElem = visitResultVariable(r, previousProjElems); projElemList.addElement(projElem); } Projection proj = new Projection(); proj.setProjectionElemList(projElemList); tupleRoot = proj; return proj; }
result = new Projection(result, projElemList); ((Projection)result).setProjectionContext(graphPattern.getContextVar());
@Override public void meet(Order node) { for (OrderElem e : node.getElements()) { e.visit(this); } if (variablesProjected) { QueryModelNode parent = node.getParentNode(); if (projection == parent) { node.replaceWith(node.getArg().clone()); node.setArg(projection.clone()); Order replacement = node.clone(); projection.replaceWith(replacement); QueryModelNode distinct = replacement.getParentNode(); if (distinct instanceof Distinct) { distinct.replaceWith(new Reduced(replacement.clone())); } } } }
private TupleExpr buildTuple(final List<QueryModelNode> nodes, final List<Filter> filters, final Projection projection) { final Projection proj = projection.clone(); Join join = null; join = new Join((TupleExpr) nodes.get(0).clone(), (TupleExpr) nodes.get(1).clone()); for (int i = 2; i < nodes.size(); i++) { join = new Join(join, (TupleExpr) nodes.get(i).clone()); } if (filters.size() == 0) { proj.setArg(join); return proj; } else { TupleExpr queryPlan = join; for (final Filter f : filters) { final Filter filt = f.clone(); filt.setArg(queryPlan); queryPlan = filt; } proj.setArg(queryPlan); return proj; } }
public Projection(TupleExpr arg, ProjectionElemList elements) { this(arg); setProjectionElemList(elements); }
public void visitDescribe(Resource describe) throws RDF4JException { Value resultNodes = TripleSources.singleValue(describe, SP.RESULT_NODES_PROPERTY, store); if (!(resultNodes instanceof Resource)) { throw new MalformedSpinException( String.format("Value of %s is not a resource", SP.RESULT_NODES_PROPERTY)); } projElems = new LinkedHashMap<String, ProjectionElem>(); Projection projection = visitResultNodes((Resource)resultNodes); TupleExpr whereExpr = visitWhere(describe); projection.setArg(whereExpr); addSourceExpressions(projection, projElems.values()); }
@Override public ExternalSet clone() { final ExternalTupleSet clone = (ExternalTupleSet) super.clone(); clone.setProjectionExpr(this.tupleExpr.clone()); clone.tableVarMap = Maps.newHashMap(); for(final String s: this.tableVarMap.keySet()) { clone.tableVarMap.put(s,this.tableVarMap.get(s)); } clone.supportedVarOrders = Maps.newHashMap(); for(final String s: this.supportedVarOrders.keySet()) { clone.supportedVarOrders.put(s,this.supportedVarOrders.get(s)); } return clone; }
@Override protected BindingSet convert(BindingSet sourceBindings) throws QueryEvaluationException { return project(projection.getProjectionElemList(), sourceBindings, parentBindings, !isOuterProjection); }
Projection projection = visitResultVariables((Resource)resultVars, oldProjElems); TupleExpr whereExpr = visitWhere(select); projection.setArg(whereExpr); group.setArg(projection.getArg()); projection.setArg(group); if (having instanceof Resource) { TupleExpr havingExpr = visitHaving((Resource)having); projection.setArg(havingExpr); if (orderby instanceof Resource) { Order order = visitOrderBy((Resource)orderby); order.setArg(projection.getArg()); projection.setArg(order);
@Override public TupleExpr visit(ASTDescribe node, Object data) throws VisitorException { TupleExpr tupleExpr = (TupleExpr)data; if (tupleExpr == null) { tupleExpr = new SingletonSet(); } Extension e = new Extension(); ProjectionElemList projectionElements = new ProjectionElemList(); for (int i = 0; i < node.jjtGetNumChildren(); i++) { ValueExpr resource = (ValueExpr)node.jjtGetChild(i).jjtAccept(this, null); if (resource instanceof Var) { projectionElements.addElement(new ProjectionElem(((Var)resource).getName())); } else { String alias = "_describe_" + UUID.randomUUID().toString().replaceAll("-", "_"); ExtensionElem elem = new ExtensionElem(resource, alias); e.addElement(elem); projectionElements.addElement(new ProjectionElem(alias)); } } if (!e.getElements().isEmpty()) { e.setArg(tupleExpr); tupleExpr = e; } Projection p = new Projection(tupleExpr, projectionElements); return new DescribeOperator(p); }