Skip to content

Commit

Permalink
also detect non static variables for generic aggregates
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids authored and nyalldawson committed Oct 16, 2020
1 parent d1f3f9d commit c21141b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -888,11 +888,32 @@ static QVariant fcnAggregateGeneric( QgsAggregateCalculator::Aggregate aggregate
parameters.filter = groupByClause;
}

QString cacheKey = QStringLiteral( "agg:%1:%2:%3:%4:%5" ).arg( vl->id(),
QString::number( static_cast< int >( aggregate ) ),
subExpression,
parameters.filter,
orderBy );
QgsExpression subExp( subExpression );
QgsExpression filterExp( parameters.filter );

bool isStatic = true;
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;
}
}

QString cacheKey;
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 );
}
else
{
cacheKey = QStringLiteral( "agg:%1:%2:%3:%4:%5" ).arg( vl->id(), QString::number( aggregate ), subExpression, parameters.filter, orderBy );
}

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

Expand Down
5 changes: 5 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -2045,6 +2045,11 @@ class TestQgsExpression: public QObject
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" ) );

// also test for generic aggregates
exp = QString( "with_variable('my_var',\"col1\", sum(expression:=\"col1\", filter:=\"col1\"=@my_var))" );
res = exp.evaluate( &context ).toInt();
QCOMPARE( res, f.attribute( "col1" ).toInt() );
}
}

Expand Down

0 comments on commit c21141b

Please sign in to comment.