Search in sources :

Example 1 with ExprAggregator

use of org.apache.jena.sparql.expr.ExprAggregator in project jena by apache.

the class Query method allocAggregate.

public Expr allocAggregate(Aggregator agg) {
    // We need to track the aggregators in case one aggregator is used twice, e.g. in HAVING and in SELECT expression
    // (is that much harm to do twice?  Yes, if distinct.)
    String key = agg.key();
    Var v = aggregatorsAllocated.get(key);
    if (v != null) {
        ExprAggregator eAgg = aggregatorsMap.get(v);
        if (!agg.equals(eAgg.getAggregator()))
            Log.warn(Query.class, "Internal inconsistency: Aggregator: " + agg);
        return eAgg;
    }
    // Allocate.
    v = allocInternVar();
    ExprAggregator aggExpr = new ExprAggregator(v, agg);
    aggregatorsAllocated.put(key, v);
    aggregatorsMap.put(v, aggExpr);
    aggregators.add(aggExpr);
    return aggExpr;
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) ExprAggregator(org.apache.jena.sparql.expr.ExprAggregator)

Example 2 with ExprAggregator

use of org.apache.jena.sparql.expr.ExprAggregator in project jena by apache.

the class OpRewriter method visit.

@Override
public void visit(OpGroup opGroup) {
    opGroup.getSubOp().visit(this);
    ExprRewriter expRewriter = new ExprRewriter(values);
    VarExprList groupVars = rewrite(opGroup.getGroupVars());
    List<ExprAggregator> aggregators = new ArrayList<>();
    for (ExprAggregator ea : opGroup.getAggregators()) {
        ea.visit(expRewriter);
        aggregators.add((ExprAggregator) expRewriter.pop());
    }
    push(new OpGroup(pop(), groupVars, aggregators));
}
Also used : ArrayList(java.util.ArrayList) ExprAggregator(org.apache.jena.sparql.expr.ExprAggregator)

Example 3 with ExprAggregator

use of org.apache.jena.sparql.expr.ExprAggregator in project jena by apache.

the class AggregationHandler method add.

/**
	 * Add and expression aggregator and variable to the mapping.
	 * 
	 * if the expr parameter is not an instance of ExprAggregator then no action is taken.
	 * 
	 * @param expr The expression to add.
	 * @param var The variable that it is bound to.
	 */
public void add(Expr expr, Var var) {
    if (expr instanceof ExprAggregator) {
        ExprAggregator eAgg = (ExprAggregator) expr;
        Expr expr2 = query.allocAggregate(eAgg.getAggregator());
        aggMap.put(var, (ExprAggregator) expr2);
    }
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) ExprAggregator(org.apache.jena.sparql.expr.ExprAggregator)

Example 4 with ExprAggregator

use of org.apache.jena.sparql.expr.ExprAggregator in project jena by apache.

the class QueryIterGroup method calc.

private static Iterator<Binding> calc(final QueryIterator iter, final VarExprList groupVarExpr, final List<ExprAggregator> aggregators, final ExecutionContext execCxt) {
    return new IteratorDelayedInitialization<Binding>() {

        @Override
        protected Iterator<Binding> initializeIterator() {
            boolean noAggregators = (aggregators == null || aggregators.isEmpty());
            // Phase 1 : assign bindings to buckets by key and pump through the aggregators.
            Multimap<Binding, Pair<Var, Accumulator>> accumulators = HashMultimap.create();
            while (iter.hasNext()) {
                Binding b = iter.nextBinding();
                Binding key = genKey(groupVarExpr, b, execCxt);
                if (noAggregators) {
                    // Put in a dummy to remember the input.
                    accumulators.put(key, placeholder);
                    continue;
                }
                // Create if does not exist.
                if (!accumulators.containsKey(key)) {
                    for (ExprAggregator agg : aggregators) {
                        Accumulator x = agg.getAggregator().createAccumulator();
                        Var v = agg.getVar();
                        accumulators.put(key, Pair.create(v, x));
                    }
                }
                // Do the per-accumulator calculation.
                for (Pair<Var, Accumulator> pair : accumulators.get(key)) pair.getRight().accumulate(b, execCxt);
            }
            if (accumulators.isEmpty()) {
                if (noAggregators) {
                    // ==> No result rows.
                    return Iter.nullIterator();
                }
                BindingMap binding = BindingFactory.create();
                for (ExprAggregator agg : aggregators) {
                    Var v = agg.getVar();
                    Node value = agg.getAggregator().getValueEmpty();
                    if (value != null) {
                        binding.add(v, value);
                    }
                }
                if (binding == null)
                    // This does not happen if there are any aggregators. 
                    return Iter.nullIterator();
                // cast to get the static type inference to work.
                return Iter.singletonIter((Binding) binding);
            }
            if (noAggregators)
                // We used placeholder so there are always the key. 
                return accumulators.keySet().iterator();
            List<Binding> results = new ArrayList<>();
            for (Binding k : accumulators.keySet()) {
                Collection<Pair<Var, Accumulator>> accs = accumulators.get(k);
                BindingMap b = BindingFactory.create(k);
                for (Pair<Var, Accumulator> pair : accs) {
                    Var v = pair.getLeft();
                    NodeValue value = pair.getRight().getValue();
                    Node n = (value == null) ? null : value.asNode();
                    if (v == null || n == null) {
                    } else
                        b.add(v, n);
                }
                results.add(b);
            }
            return results.iterator();
        }
    };
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Accumulator(org.apache.jena.sparql.expr.aggregate.Accumulator) NodeValue(org.apache.jena.sparql.expr.NodeValue) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap) IteratorDelayedInitialization(org.apache.jena.atlas.iterator.IteratorDelayedInitialization) ExprAggregator(org.apache.jena.sparql.expr.ExprAggregator) Pair(org.apache.jena.atlas.lib.Pair)

Example 5 with ExprAggregator

use of org.apache.jena.sparql.expr.ExprAggregator in project jena by apache.

the class SelectHandlerTest method testAddAggregateStringVar.

@Test
public void testAddAggregateStringVar() {
    Var v = Var.alloc("foo");
    handler.addVar("count(*)", v);
    VarExprList expr = query.getProject();
    assertEquals(1, expr.size());
    Expr e = expr.getExpr(Var.alloc("foo"));
    assertNotNull("expression should not be null", e);
    assertTrue("Should be an ExprAggregator", e instanceof ExprAggregator);
    assertTrue("Should be AggCount", ((ExprAggregator) e).getAggregator() instanceof AggCount);
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) Var(org.apache.jena.sparql.core.Var) AggCount(org.apache.jena.sparql.expr.aggregate.AggCount) ExprAggregator(org.apache.jena.sparql.expr.ExprAggregator) VarExprList(org.apache.jena.sparql.core.VarExprList) Test(org.junit.Test)

Aggregations

ExprAggregator (org.apache.jena.sparql.expr.ExprAggregator)6 Var (org.apache.jena.sparql.core.Var)3 Expr (org.apache.jena.sparql.expr.Expr)3 ArrayList (java.util.ArrayList)2 VarExprList (org.apache.jena.sparql.core.VarExprList)2 Test (org.junit.Test)2 IteratorDelayedInitialization (org.apache.jena.atlas.iterator.IteratorDelayedInitialization)1 Pair (org.apache.jena.atlas.lib.Pair)1 Node (org.apache.jena.graph.Node)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)1 ExprVar (org.apache.jena.sparql.expr.ExprVar)1 NodeValue (org.apache.jena.sparql.expr.NodeValue)1 Accumulator (org.apache.jena.sparql.expr.aggregate.Accumulator)1 AggCount (org.apache.jena.sparql.expr.aggregate.AggCount)1 AggSum (org.apache.jena.sparql.expr.aggregate.AggSum)1