Skip to content
Permalink
Browse files

also detect non static variables for generic aggregates

  • Loading branch information
3nids authored and nyalldawson committed Oct 16, 2020
1 parent d1f3f9d commit c21141b40565614d40423c880c83ac87ed9ccdbf
Showing with 31 additions and 5 deletions.
  1. +26 −5 src/core/expression/qgsexpressionfunction.cpp
  2. +5 −0 tests/src/core/testqgsexpression.cpp
@@ -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 );

@@ -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() );
}
}

0 comments on commit c21141b

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