Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correctly determine if variables are static in aggregate expression and filter, give @parent in generic aggregates #39391

Merged
merged 5 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/core/expression/qgsexpressionfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,30 @@ static QVariant fcnAggregate( const QVariantList &values, const QgsExpressionCon
QString cacheKey;
QgsExpression subExp( subExpression );
QgsExpression filterExp( parameters.filter );

bool isStatic = true;
if ( filterExp.referencedVariables().contains( QStringLiteral( "parent" ) )
|| filterExp.referencedVariables().contains( QString() )
|| subExp.referencedVariables().contains( QStringLiteral( "parent" ) )
|| subExp.referencedVariables().contains( QString() ) )
{
isStatic = false;
}
else
{
const QSet<QString> refVars = filterExp.referencedVariables() + subExp.referencedVariables();
for ( const QString &varName : refVars )
{
const QgsExpressionContextScope *scope = context->activeScopeForVariable( varName );
if ( scope && !scope->isStatic( varName ) )
{
isStatic = false;
break;
}
}
}

if ( !isStatic )
{
cacheKey = QStringLiteral( "aggfcn:%1:%2:%3:%4:%5%6:%7" ).arg( vl->id(), QString::number( aggregate ), subExpression, parameters.filter,
QString::number( context->feature().id() ), QString( qHash( context->feature() ) ), orderBy );
Expand All @@ -651,8 +671,9 @@ static QVariant fcnAggregate( const QVariantList &values, const QgsExpressionCon
}

if ( context && context->hasCachedValue( cacheKey ) )
{
return context->cachedValue( cacheKey );

}
QgsExpressionContext subContext( *context );
QgsExpressionContextScope *subScope = new QgsExpressionContextScope();
subScope->setVariable( QStringLiteral( "parent" ), context->feature() );
Expand Down
19 changes: 19 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,25 @@ class TestQgsExpression: public QObject
QCOMPARE( res.toInt(), 1 );
}

void test_aggregate_with_variable()
{
// this checks that a variable can be non static in a aggregate, i.e. the result will change across the fetched features
// see https://github.com/qgis/QGIS/issues/33382
QgsExpressionContext context;
context.appendScope( QgsExpressionContextUtils::layerScope( mAggregatesLayer ) );
QgsFeature f;

QgsFeatureIterator it = mAggregatesLayer->getFeatures();

while ( it.nextFeature( f ) )
{
context.setFeature( f );
QgsExpression exp( QString( "with_variable('my_var',\"col1\", aggregate(layer:='aggregate_layer', aggregate:='concatenate_unique', expression:=\"col2\", filter:=\"col1\"=@my_var))" ) );
QString res = exp.evaluate( &context ).toString();
QCOMPARE( res, f.attribute( "col2" ) );
}
}

void aggregate_data()
{
QTest::addColumn<QString>( "string" );
Expand Down