Skip to content

Commit 3ea67af

Browse files
committed
[bugfix] Create a b-tree expr when rule based renderer has more than 50 rules
Fixes #19441 Layers with 80+ rule-based symbology do not render
1 parent 9a1f129 commit 3ea67af

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/core/symbology/qgsrulebasedrenderer.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,35 @@ bool QgsRuleBasedRenderer::Rule::startRender( QgsRenderContext &context, const Q
409409
if ( subfilters.length() > 1 || !subfilters.value( 0 ).isEmpty() )
410410
{
411411
if ( subfilters.contains( QStringLiteral( "TRUE" ) ) )
412+
{
412413
sf = QStringLiteral( "TRUE" );
414+
}
415+
// If we have more than 50 rules (to stay on the safe side) make a binary tree or SQLITE will fail,
416+
// see: http://issues.qgis.org/issues/19441
417+
else if ( subfilters.count() > 50 )
418+
{
419+
std::function<QString( const QStringList & )>bt = [ &bt ]( const QStringList & subf )
420+
{
421+
if ( subf.count( ) == 1 )
422+
{
423+
return subf.at( 0 );
424+
}
425+
else if ( subf.count( ) == 2 )
426+
{
427+
return subf.join( QStringLiteral( ") OR (" ) ).prepend( '(' ).append( ')' );
428+
}
429+
else
430+
{
431+
int midpos = static_cast<int>( subf.length() / 2 );
432+
return QStringLiteral( "(%1) OR (%2)" ).arg( bt( subf.mid( 0, midpos ) ) ).arg( bt( subf.mid( midpos ) ) );
433+
}
434+
};
435+
sf = bt( subfilters );
436+
}
413437
else
438+
{
414439
sf = subfilters.join( QStringLiteral( ") OR (" ) ).prepend( '(' ).append( ')' );
440+
}
415441
}
416442

417443
// Now join the subfilters with their parent (this) based on if

tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ SET(TESTS
9393
testqgsdxfexport.cpp
9494
testqgsellipsemarker.cpp
9595
testqgsexpressioncontext.cpp
96+
testqgssqliteexpressioncompiler.cpp
9697
testqgsexpression.cpp
9798
testqgsfeature.cpp
9899
testqgsfields.cpp

tests/src/core/testqgsrulebasedrenderer.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,42 @@ class TestQgsRuleBasedRenderer: public QObject
143143
delete clone;
144144
}
145145

146+
/**
147+
* test_many_rules_expression_filter checks that with > 50 rules we have a binary tree (log2(N))
148+
*/
149+
void test_many_rules_expression_filter()
150+
{
151+
152+
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "point?field=fld:int" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );
153+
QgsRenderContext ctx; // dummy render context
154+
ctx.expressionContext().setFields( layer->fields() );
155+
156+
std::function<QString( const int ruleCount )> makeFilter = [ & ]( const int rc ) -> QString
157+
{
158+
159+
// prepare renderer
160+
RRule *rootRule = new RRule( nullptr );
161+
for ( int i = 0; i < rc; i++ )
162+
{
163+
rootRule->appendChild( new RRule( nullptr, 0, 0, QStringLiteral( "%1" ).arg( i ) ) );
164+
}
165+
QgsRuleBasedRenderer r( rootRule );
166+
r.startRender( ctx, layer->fields() );
167+
r.stopRender( ctx );
168+
return r.filter();
169+
};
170+
171+
QCOMPARE( makeFilter( 1 ), QString( "(0)" ) );
172+
QCOMPARE( makeFilter( 2 ), QString( "(0) OR (1)" ) );
173+
QCOMPARE( makeFilter( 3 ), QString( "(0) OR (1) OR (2)" ) );
174+
QCOMPARE( makeFilter( 10 ), QString( "(0) OR (1) OR (2) OR (3) OR (4) OR (5) OR (6) OR (7) OR (8) OR (9)" ) );
175+
QCOMPARE( makeFilter( 51 ), QString( "(((((0) OR ((1) OR (2))) OR ((3) OR ((4) OR (5)))) OR (((6) OR ((7) OR (8))) OR ((9) OR ((10) OR (11))))) OR "
176+
"((((12) OR ((13) OR (14))) OR ((15) OR ((16) OR (17)))) OR (((18) OR ((19) OR (20))) OR (((21) OR (22)) OR ((23) OR (24)))))) OR "
177+
"(((((25) OR ((26) OR (27))) OR ((28) OR ((29) OR (30)))) OR (((31) OR ((32) OR (33))) OR (((34) OR (35)) OR ((36) OR (37))))) OR "
178+
"((((38) OR ((39) OR (40))) OR ((41) OR ((42) OR (43)))) OR (((44) OR ((45) OR (46))) OR (((47) OR (48)) OR ((49) OR (50))))))" ) );
179+
180+
}
181+
146182
private:
147183
void xml2domElement( const QString &testFile, QDomDocument &doc )
148184
{

0 commit comments

Comments
 (0)