Skip to content
Permalink
Browse files

Allow setting order by clause for aggregate calculation

  • Loading branch information
nyalldawson committed May 8, 2019
1 parent 9f4d41b commit 6b16218c0fc3f4adab74eef9f292f52b3cceeb39
@@ -66,6 +66,8 @@ to a data provider for remote calculation.
QString filter;

QString delimiter;

QgsFeatureRequest::OrderBy orderBy;
};

QgsAggregateCalculator( const QgsVectorLayer *layer );
@@ -39,6 +39,7 @@ void QgsAggregateCalculator::setParameters( const AggregateParameters &parameter
{
mFilterExpression = parameters.filter;
mDelimiter = parameters.delimiter;
mOrderBy = parameters.orderBy;
}

QVariant QgsAggregateCalculator::calculate( QgsAggregateCalculator::Aggregate aggregate,
@@ -82,6 +83,8 @@ QVariant QgsAggregateCalculator::calculate( QgsAggregateCalculator::Aggregate ag
QgsFeatureRequest::NoFlags :
QgsFeatureRequest::NoGeometry )
.setSubsetOfAttributes( lst, mLayer->fields() );
if ( !mOrderBy.empty() )
request.setOrderBy( mOrderBy );
if ( !mFilterExpression.isEmpty() )
request.setFilterExpression( mFilterExpression );
if ( context )
@@ -22,6 +22,7 @@
#include "qgsstatisticalsummary.h"
#include "qgsdatetimestatisticalsummary.h"
#include "qgsstringstatisticalsummary.h"
#include "qgsfeaturerequest.h"
#include <QVariant>


@@ -103,6 +104,12 @@ class CORE_EXPORT QgsAggregateCalculator
* \see QgsAggregateCalculator::delimiter()
*/
QString delimiter;

/**
* Optional order by clauses.
* \since QGIS 3.8
*/
QgsFeatureRequest::OrderBy orderBy;
};

/**
@@ -183,6 +190,9 @@ class CORE_EXPORT QgsAggregateCalculator
//! Filter expression, or empty for no filter
QString mFilterExpression;

//! Order by clause
QgsFeatureRequest::OrderBy mOrderBy;

//! Delimiter to use for concatenate aggregate
QString mDelimiter;

@@ -21,6 +21,7 @@
QgsExpressionContext,
QgsExpressionContextScope,
QgsGeometry,
QgsFeatureRequest,
NULL
)
from qgis.PyQt.QtCore import QDateTime, QDate, QTime
@@ -151,6 +152,20 @@ def testNumeric(self):
val, ok = agg.calculate(t, 'flddbl')
self.assertFalse(ok)

# with order by
agg = QgsAggregateCalculator(layer)
val, ok = agg.calculate(QgsAggregateCalculator.ArrayAggregate, 'fldint')
self.assertEqual(val, [4, 2, 3, 2, 5, NULL, 8])
params = QgsAggregateCalculator.AggregateParameters()
params.orderBy = QgsFeatureRequest.OrderBy([QgsFeatureRequest.OrderByClause('fldint')])
agg.setParameters(params)
val, ok = agg.calculate(QgsAggregateCalculator.ArrayAggregate, 'fldint')
self.assertEqual(val, [2, 2, 3, 4, 5, 8, NULL])
params.orderBy = QgsFeatureRequest.OrderBy([QgsFeatureRequest.OrderByClause('flddbl')])
agg.setParameters(params)
val, ok = agg.calculate(QgsAggregateCalculator.ArrayAggregate, 'fldint')
self.assertEqual(val, [2, 2, 4, 8, 3, 5, NULL])

def testString(self):
""" Test calculation of aggregates on string fields"""

@@ -208,6 +223,18 @@ def testString(self):
val, ok = agg.calculate(t, 'fldstring')
self.assertFalse(ok)

# with order by
agg = QgsAggregateCalculator(layer)
val, ok = agg.calculate(QgsAggregateCalculator.ArrayAggregate, 'fldstring')
self.assertEqual(val, ['cc', 'aaaa', 'bbbbbbbb', 'aaaa', 'eeee', '', 'eeee', '', 'dddd'])
params = QgsAggregateCalculator.AggregateParameters()
params.orderBy = QgsFeatureRequest.OrderBy([QgsFeatureRequest.OrderByClause('fldstring')])
agg.setParameters(params)
val, ok = agg.calculate(QgsAggregateCalculator.ArrayAggregate, 'fldstring')
self.assertEqual(val, ['', '', 'aaaa', 'aaaa', 'bbbbbbbb', 'cc', 'dddd', 'eeee', 'eeee'])
val, ok = agg.calculate(QgsAggregateCalculator.StringConcatenate, 'fldstring')
self.assertEqual(val, 'aaaaaaaabbbbbbbbccddddeeeeeeee')

def testDateTime(self):
""" Test calculation of aggregates on date/datetime fields"""

0 comments on commit 6b16218

Please sign in to comment.
You can’t perform that action at this time.