Skip to content

Commit

Permalink
provide parent to generic aggregate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids authored and nyalldawson committed Oct 16, 2020
1 parent c21141b commit 28a8583
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -674,6 +674,7 @@ static QVariant fcnAggregate( const QVariantList &values, const QgsExpressionCon
{
return context->cachedValue( cacheKey );
}

QgsExpressionContext subContext( *context );
QgsExpressionContextScope *subScope = new QgsExpressionContextScope();
subScope->setVariable( QStringLiteral( "parent" ), context->feature() );
Expand Down Expand Up @@ -921,6 +922,9 @@ static QVariant fcnAggregateGeneric( QgsAggregateCalculator::Aggregate aggregate
bool ok = false;

QgsExpressionContext subContext( *context );
QgsExpressionContextScope *subScope = new QgsExpressionContextScope();
subScope->setVariable( QStringLiteral( "parent" ), context->feature() );
subContext.appendScope( subScope );
result = vl->aggregate( aggregate, subExpression, parameters, &subContext, &ok );

if ( !ok )
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -2194,6 +2194,8 @@ class TestQgsExpression: public QObject
QTest::newRow( "group by and filter named" ) << "sum(expression:=\"col1\", group_by:=\"col3\", filter:=\"col1\">=3)" << false << QVariant( 7 );
QTest::newRow( "group by expression" ) << "sum(\"col1\", \"col1\" % 2)" << false << QVariant( 14 );
QTest::newRow( "group by with null value" ) << "sum(\"col1\", \"col4\")" << false << QVariant( 8 );

QTest::newRow( "filter by @parent attribute in generic aggregate" ) << "maximum(\"col1\", filter:=\"col1\"<attribute(@parent,'col1'))" << false << QVariant( 3 );
}

void maptip_display_data()
Expand Down

0 comments on commit 28a8583

Please sign in to comment.