diff --git a/src/engine/executors/aggregate-executor.js b/src/engine/executors/aggregate-executor.js index 9c50c163..d95ccc33 100644 --- a/src/engine/executors/aggregate-executor.js +++ b/src/engine/executors/aggregate-executor.js @@ -52,10 +52,6 @@ class AggregateExecutor { if ('having' in query) { iterator = this._executeHaving(iterator, query.having, options) } - // finally, apply each aggregate operation over the groups - iterator = query.aggregates.reduce((iter, agg) => { - return this._executeAggregate(iter, agg, options) - }, iterator) return iterator } return source diff --git a/src/engine/plan-builder.js b/src/engine/plan-builder.js index 354e6c80..ba3b6a18 100644 --- a/src/engine/plan-builder.js +++ b/src/engine/plan-builder.js @@ -26,6 +26,7 @@ SOFTWARE. const { Parser } = require('sparqljs') const { single } = require('asynciterator') +const AggregateOperator = require('../operators/aggregates/agg-operator.js') const BindOperator = require('../operators/bind-operator.js') const UnionOperator = require('../operators/union-operator.js') const DistinctIterator = require('../operators/distinct-operator.js') @@ -202,7 +203,7 @@ class PlanBuilder { } // Parse query variable to separate projection & aggregate variables - if (query.variables != null) { + if ('variables' in query) { const [projection, aggregates] = _.partition(query.variables, v => _.isString(v)) // add aggregates variables to projection variables query.variables = projection.concat(aggregates.map(agg => agg.variable)) @@ -212,6 +213,13 @@ class PlanBuilder { // Handles Aggregates graphIterator = this._aggExecutor.buildIterator(graphIterator, query, options) + // Handles transformers + if ('aggregates' in query) { + graphIterator = query.aggregates.reduce((iter, agg) => { + return new AggregateOperator(iter, agg, options) + }, graphIterator) + } + // Handles ORDER BY if ('order' in query) { graphIterator = new OrderByOperator(graphIterator, query.order, options) diff --git a/src/operators/expressions/sparql-expression.js b/src/operators/expressions/sparql-expression.js index b2cf1aee..32e88a5f 100644 --- a/src/operators/expressions/sparql-expression.js +++ b/src/operators/expressions/sparql-expression.js @@ -75,7 +75,12 @@ class SPARQLExpression { throw new Error(`Unsupported SPARQL aggregation: ${expression.aggregation}`) } const aggregation = aggregates[expression.aggregation] - return bindings => aggregation(expression.expression, bindings['__aggregate'], expression.separator) + return bindings => { + if ('__aggregate' in bindings) { + return aggregation(expression.expression, bindings['__aggregate'], expression.separator) + } + return bindings + } } throw new Error(`Unsupported SPARQL operation type found: ${expression.type}`) }