Skip to content

Commit adb13e1

Browse files
committed
Fix aggregate when group_by value is NULL
1 parent b798c3a commit adb13e1

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/core/qgsexpression.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -905,10 +905,13 @@ static QVariant fcnAggregateGeneric( QgsAggregateCalculator::Aggregate aggregate
905905
{
906906
QgsExpression groupByExp( groupBy );
907907
QVariant groupByValue = groupByExp.evaluate( context );
908+
QString groupByClause = QStringLiteral( "%1 %2 %3" ).arg( groupBy,
909+
groupByValue.isNull() ? "is" : "=",
910+
QgsExpression::quotedValue( groupByValue ) );
908911
if ( !parameters.filter.isEmpty() )
909-
parameters.filter = QStringLiteral( "(%1) AND (%2=%3)" ).arg( parameters.filter, groupBy, QgsExpression::quotedValue( groupByValue ) );
912+
parameters.filter = QStringLiteral( "(%1) AND (%2)" ).arg( parameters.filter, groupByClause );
910913
else
911-
parameters.filter = QStringLiteral( "(%2 = %3)" ).arg( groupBy, QgsExpression::quotedValue( groupByValue ) );
914+
parameters.filter = groupByClause;
912915
}
913916

914917
QString cacheKey = QStringLiteral( "agg:%1:%2:%3:%4" ).arg( vl->id(),

tests/src/core/testqgsexpression.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -120,38 +120,44 @@ class TestQgsExpression: public QObject
120120
QgsProject::instance()->addMapLayer( mMemoryLayer );
121121

122122
// test layer for aggregates
123-
mAggregatesLayer = new QgsVectorLayer( QStringLiteral( "Point?field=col1:integer&field=col2:string&field=col3:integer" ), QStringLiteral( "aggregate_layer" ), QStringLiteral( "memory" ) );
123+
mAggregatesLayer = new QgsVectorLayer( QStringLiteral( "Point?field=col1:integer&field=col2:string&field=col3:integer&field=col4:string" ), QStringLiteral( "aggregate_layer" ), QStringLiteral( "memory" ) );
124124
QVERIFY( mAggregatesLayer->isValid() );
125125
QgsFeature af1( mAggregatesLayer->dataProvider()->fields(), 1 );
126126
af1.setGeometry( QgsGeometry::fromPoint( QgsPoint( 0, 0 ) ) );
127127
af1.setAttribute( QStringLiteral( "col1" ), 4 );
128128
af1.setAttribute( QStringLiteral( "col2" ), "test" );
129129
af1.setAttribute( QStringLiteral( "col3" ), 2 );
130+
af1.setAttribute( QStringLiteral( "col4" ), QVariant( QVariant::String ) );
130131
QgsFeature af2( mAggregatesLayer->dataProvider()->fields(), 2 );
131132
af2.setGeometry( QgsGeometry::fromPoint( QgsPoint( 1, 0 ) ) );
132133
af2.setAttribute( QStringLiteral( "col1" ), 2 );
133134
af2.setAttribute( QStringLiteral( "col2" ), QVariant( QVariant::String ) );
134135
af2.setAttribute( QStringLiteral( "col3" ), 1 );
136+
af2.setAttribute( QStringLiteral( "col4" ), QVariant( QVariant::String ) );
135137
QgsFeature af3( mAggregatesLayer->dataProvider()->fields(), 3 );
136138
af3.setGeometry( QgsGeometry::fromPoint( QgsPoint( 2, 0 ) ) );
137139
af3.setAttribute( QStringLiteral( "col1" ), 3 );
138140
af3.setAttribute( QStringLiteral( "col2" ), "test333" );
139141
af3.setAttribute( QStringLiteral( "col3" ), 2 );
142+
af3.setAttribute( QStringLiteral( "col4" ), QVariant( QVariant::String ) );
140143
QgsFeature af4( mAggregatesLayer->dataProvider()->fields(), 4 );
141144
af4.setGeometry( QgsGeometry::fromPoint( QgsPoint( 3, 0 ) ) );
142145
af4.setAttribute( QStringLiteral( "col1" ), 2 );
143146
af4.setAttribute( QStringLiteral( "col2" ), "test4" );
144147
af4.setAttribute( QStringLiteral( "col3" ), 2 );
148+
af4.setAttribute( QStringLiteral( "col4" ), "" );
145149
QgsFeature af5( mAggregatesLayer->dataProvider()->fields(), 5 );
146150
af5.setGeometry( QgsGeometry::fromPoint( QgsPoint( 4, 0 ) ) );
147151
af5.setAttribute( QStringLiteral( "col1" ), 5 );
148152
af5.setAttribute( QStringLiteral( "col2" ), QVariant( QVariant::String ) );
149153
af5.setAttribute( QStringLiteral( "col3" ), 3 );
154+
af5.setAttribute( QStringLiteral( "col4" ), "test" );
150155
QgsFeature af6( mAggregatesLayer->dataProvider()->fields(), 6 );
151156
af6.setGeometry( QgsGeometry::fromPoint( QgsPoint( 5, 0 ) ) );
152157
af6.setAttribute( QStringLiteral( "col1" ), 8 );
153158
af6.setAttribute( QStringLiteral( "col2" ), "test4" );
154159
af6.setAttribute( QStringLiteral( "col3" ), 3 );
160+
af6.setAttribute( QStringLiteral( "col4" ), "test" );
155161
mAggregatesLayer->dataProvider()->addFeatures( QgsFeatureList() << af1 << af2 << af3 << af4 << af5 << af6 );
156162
QgsProject::instance()->addMapLayer( mAggregatesLayer );
157163

@@ -1417,12 +1423,13 @@ class TestQgsExpression: public QObject
14171423
QTest::newRow( "filter" ) << "sum(\"col1\", NULL, \"col1\" >= 5)" << false << QVariant( 13 );
14181424
QTest::newRow( "filter named" ) << "sum(expression:=\"col1\", filter:=\"col1\" >= 5)" << false << QVariant( 13 );
14191425
QTest::newRow( "filter no matching" ) << "sum(expression:=\"col1\", filter:=\"col1\" <= -5)" << false << QVariant( 0 );
1420-
QTest::newRow( "filter no matching max" ) << "maximum('test',\"xcvxcv\" * 2)" << false << QVariant();
1426+
QTest::newRow( "filter no matching max" ) << "maximum(\"col1\", filter:=\"col1\" <= -5)" << false << QVariant();
14211427

14221428
QTest::newRow( "group by" ) << "sum(\"col1\", \"col3\")" << false << QVariant( 9 );
14231429
QTest::newRow( "group by and filter" ) << "sum(\"col1\", \"col3\", \"col1\">=3)" << false << QVariant( 7 );
14241430
QTest::newRow( "group by and filter named" ) << "sum(expression:=\"col1\", group_by:=\"col3\", filter:=\"col1\">=3)" << false << QVariant( 7 );
14251431
QTest::newRow( "group by expression" ) << "sum(\"col1\", \"col1\" % 2)" << false << QVariant( 16 );
1432+
QTest::newRow( "group by with null value" ) << "sum(\"col1\", \"col4\")" << false << QVariant( 9 );
14261433
}
14271434

14281435
void selection()
@@ -1483,6 +1490,7 @@ class TestQgsExpression: public QObject
14831490
af1.setAttribute( QStringLiteral( "col1" ), 4 );
14841491
af1.setAttribute( QStringLiteral( "col2" ), "test" );
14851492
af1.setAttribute( QStringLiteral( "col3" ), 2 );
1493+
af1.setAttribute( QStringLiteral( "col4" ), QVariant() );
14861494
context.setFeature( af1 );
14871495

14881496
QFETCH( QString, string );

0 commit comments

Comments
 (0)