Skip to content

Commit 77d2284

Browse files
committedJun 20, 2018
Fix calculation of relation aggregate in virtual field with same name as related field
1 parent 4132dbc commit 77d2284

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed
 

‎python/core/auto_generated/expression/qgsexpressionfunction.sip.in

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ Represents a single parameter passed to a function.
3838

3939
Parameter( const QString &name,
4040
bool optional = false,
41-
const QVariant &defaultValue = QVariant() );
41+
const QVariant &defaultValue = QVariant(),
42+
bool isSubExpression = false );
4243
%Docstring
4344
Constructor for Parameter.
4445

4546
:param name: parameter name, used when named parameter are specified in an expression
4647
:param optional: set to true if parameter should be optional
4748
:param defaultValue: default value to use for optional parameters
49+
:param isSubExpression: set to true if this parameter is a sub-expression
4850
%End
4951

5052
QString name() const;
@@ -60,6 +62,14 @@ Returns true if the parameter is optional.
6062
QVariant defaultValue() const;
6163
%Docstring
6264
Returns the default value for the parameter.
65+
%End
66+
67+
bool isSubExpression() const;
68+
%Docstring
69+
Returns true if parameter argument is a separate sub-expression, and
70+
should not be checked while determining referenced columns for the expression.
71+
72+
.. versionadded:: 3.2
6373
%End
6474

6575
bool operator==( const QgsExpressionFunction::Parameter &other ) const;

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4113,8 +4113,8 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
41134113
QgsExpressionFunction::ParameterList()
41144114
<< QgsExpressionFunction::Parameter( QStringLiteral( "layer" ) )
41154115
<< QgsExpressionFunction::Parameter( QStringLiteral( "aggregate" ) )
4116-
<< QgsExpressionFunction::Parameter( QStringLiteral( "expression" ) )
4117-
<< QgsExpressionFunction::Parameter( QStringLiteral( "filter" ), true )
4116+
<< QgsExpressionFunction::Parameter( QStringLiteral( "expression" ), false, QVariant(), true )
4117+
<< QgsExpressionFunction::Parameter( QStringLiteral( "filter" ), true, QVariant(), true )
41184118
<< QgsExpressionFunction::Parameter( QStringLiteral( "concatenator" ), true ),
41194119
fcnAggregate,
41204120
QStringLiteral( "Aggregates" ),
@@ -4177,7 +4177,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
41774177
true
41784178
)
41794179

4180-
<< new QgsStaticExpressionFunction( QStringLiteral( "relation_aggregate" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "relation" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "aggregate" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "expression" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "concatenator" ), true ),
4180+
<< new QgsStaticExpressionFunction( QStringLiteral( "relation_aggregate" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "relation" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "aggregate" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "expression" ), false, QVariant(), true ) << QgsExpressionFunction::Parameter( QStringLiteral( "concatenator" ), true ),
41814181
fcnAggregateRelation, QStringLiteral( "Aggregates" ), QString(), false, QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES, true )
41824182

41834183
<< new QgsStaticExpressionFunction( QStringLiteral( "count" ), aggParams, fcnAggregateCount, QStringLiteral( "Aggregates" ), QString(), false, QSet<QString>(), true )

‎src/core/expression/qgsexpressionfunction.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ class CORE_EXPORT QgsExpressionFunction
5858
* \param name parameter name, used when named parameter are specified in an expression
5959
* \param optional set to true if parameter should be optional
6060
* \param defaultValue default value to use for optional parameters
61+
* \param isSubExpression set to true if this parameter is a sub-expression
6162
*/
6263
Parameter( const QString &name,
6364
bool optional = false,
64-
const QVariant &defaultValue = QVariant() )
65+
const QVariant &defaultValue = QVariant(),
66+
bool isSubExpression = false )
6567
: mName( name )
6668
, mOptional( optional )
6769
, mDefaultValue( defaultValue )
70+
, mIsSubExpression( isSubExpression )
6871
{}
6972

7073
//! Returns the name of the parameter.
@@ -76,15 +79,23 @@ class CORE_EXPORT QgsExpressionFunction
7679
//! Returns the default value for the parameter.
7780
QVariant defaultValue() const { return mDefaultValue; }
7881

82+
/**
83+
* Returns true if parameter argument is a separate sub-expression, and
84+
* should not be checked while determining referenced columns for the expression.
85+
* \since QGIS 3.2
86+
*/
87+
bool isSubExpression() const { return mIsSubExpression; }
88+
7989
bool operator==( const QgsExpressionFunction::Parameter &other ) const
8090
{
8191
return ( QString::compare( mName, other.mName, Qt::CaseInsensitive ) == 0 );
8292
}
8393

8494
private:
8595
QString mName;
86-
bool mOptional;
96+
bool mOptional = false;
8797
QVariant mDefaultValue;
98+
bool mIsSubExpression = false;
8899
};
89100

90101
//! List of parameters, used for function definition

‎src/core/expression/qgsexpressionnodeimpl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,10 +965,13 @@ QSet<QString> QgsExpressionNodeFunction::referencedColumns() const
965965
return functionColumns;
966966
}
967967

968+
int paramIndex = 0;
968969
const QList< QgsExpressionNode * > nodeList = mArgs->list();
969970
for ( QgsExpressionNode *n : nodeList )
970971
{
971-
functionColumns.unite( n->referencedColumns() );
972+
if ( fd->parameters().count() <= paramIndex || !fd->parameters().at( paramIndex ).isSubExpression() )
973+
functionColumns.unite( n->referencedColumns() );
974+
paramIndex++;
972975
}
973976

974977
return functionColumns;

‎tests/src/core/testqgsexpression.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,16 @@ class TestQgsExpression: public QObject
18431843
refColsSet.insert( col.toLower() );
18441844

18451845
QCOMPARE( refColsSet, expectedCols );
1846+
1847+
expectedCols.clear();
1848+
expectedCols << QgsFeatureRequest::ALL_ATTRIBUTES
1849+
<< QStringLiteral( "parent_col1" )
1850+
<< QStringLiteral( "parent_col2" );
1851+
// sub expression fields, "child_field", "child_field2" should not be included in referenced columns
1852+
exp = QgsExpression( QStringLiteral( "relation_aggregate(relation:=\"parent_col1\" || 'my_rel',aggregate:='sum' || \"parent_col2\", expression:=\"child_field\" * \"child_field2\")" ) );
1853+
QCOMPARE( exp.hasParserError(), false );
1854+
refCols = exp.referencedColumns();
1855+
QCOMPARE( refCols, expectedCols );
18461856
}
18471857

18481858
void referenced_variables()

0 commit comments

Comments
 (0)
Please sign in to comment.