Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 3.10][VirtualLayer] Catch exception while updating virtual layer stats #39023

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/providers/virtual/qgsvirtuallayerprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,20 +568,30 @@ void QgsVirtualLayerProvider::updateStatistics() const
.arg( hasGeometry ? QStringLiteral( ",Min(MbrMinX(%1)),Min(MbrMinY(%1)),Max(MbrMaxX(%1)),Max(MbrMaxY(%1))" ).arg( quotedColumn( mDefinition.geometryField() ) ) : QString(),
mTableName,
subset );
Sqlite::Query q( mSqlite.get(), sql );
if ( q.step() == SQLITE_ROW )

try
{
mFeatureCount = q.columnInt64( 0 );
if ( hasGeometry )
Sqlite::Query q( mSqlite.get(), sql );
if ( q.step() == SQLITE_ROW )
{
double x1, y1, x2, y2;
x1 = q.columnDouble( 1 );
y1 = q.columnDouble( 2 );
x2 = q.columnDouble( 3 );
y2 = q.columnDouble( 4 );
mExtent = QgsRectangle( x1, y1, x2, y2 );
mFeatureCount = q.columnInt64( 0 );
if ( hasGeometry )
{
double x1, y1, x2, y2;
x1 = q.columnDouble( 1 );
y1 = q.columnDouble( 2 );
x2 = q.columnDouble( 3 );
y2 = q.columnDouble( 4 );
mExtent = QgsRectangle( x1, y1, x2, y2 );
}
mCachedStatistics = true;
}
mCachedStatistics = true;
}
catch ( std::runtime_error &e )
{
pushError( tr( "Error while executing feature count request : %1" ).arg( e.what() ) );
mFeatureCount = 0;
return;
}
}

Expand Down Expand Up @@ -709,4 +719,3 @@ QGISEXTERN QgsProviderGuiMetadata *providerGuiMetadataFactory()
return new QgsVirtualLayerProviderGuiMetadata();
}
#endif

20 changes: 20 additions & 0 deletions tests/src/python/test_provider_virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,26 @@ def test_subset_string(self):
self.assertEqual(gpkg_virtual_layer.subsetString(), '"join_value" = \'twenty\'')
self.assertEqual(gpkg_virtual_layer.featureCount(), 1)

def test_feature_count_on_error(self):
"""Test that triggered exception while getting feature count on a badly defined
virtual layer is correctly caught (see https://github.com/qgis/QGIS/issues/34378)"""

l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france", "ogr",
QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1)

df = QgsVirtualLayerDefinition()
df.setQuery('select error')

vl = QgsVectorLayer(df.toString(), "testq", "virtual")
self.assertEqual(vl.isValid(), False)
self.assertEqual(vl.featureCount(), 0)
ids = [f.id() for f in vl.getFeatures()]
self.assertEqual(len(ids), 0)

QgsProject.instance().removeMapLayer(l1.id())


if __name__ == '__main__':
unittest.main()