Skip to content
Permalink
Browse files

Fix unique values on GPKG pk

Fixes #21311
  • Loading branch information
elpaso committed Feb 19, 2019
1 parent 7fa9bc1 commit 1689c9364b995dd1b5a2723eab66f1af0b4e92a9
Showing with 30 additions and 0 deletions.
  1. +8 −0 src/providers/ogr/qgsogrprovider.cpp
  2. +22 −0 tests/src/python/test_provider_ogr_gpkg.py
@@ -3618,7 +3618,15 @@ QSet<QVariant> QgsOgrProvider::uniqueValues( int index, int limit ) const
return uniqueValues; //not a provider field
}


QByteArray sql = "SELECT DISTINCT " + quotedIdentifier( textEncoding()->fromUnicode( fld.name() ) );

// GPKG fid
if ( mGDALDriverName == QLatin1String( "GPKG" ) && mFirstFieldIsFid && index == 0 )
{
sql += ", " + quotedIdentifier( textEncoding()->fromUnicode( fld.name() ) ) + " AS fid2";
}

sql += " FROM " + quotedIdentifier( mOgrLayer->name() );

if ( !mSubsetString.isEmpty() )
@@ -1322,6 +1322,28 @@ def testGeopackageLayerMetadata(self):
self.assertEqual(vl1.metadata().title(), 'my title')
self.assertEqual(vl1.metadata().abstract(), 'my desc')

def testUniqueValuesOnFidColumn(self):
"""Test regression #21311 OGR provider returns an empty set for GPKG uniqueValues"""

tmpfile = os.path.join(self.basetestpath, 'testGeopackageUniqueValuesOnFidColumn.gpkg')
ds = ogr.GetDriverByName('GPKG').CreateDataSource(tmpfile)
lyr = ds.CreateLayer('test', geom_type=ogr.wkbPolygon)
lyr.CreateField(ogr.FieldDefn('str_field', ogr.OFTString))
f = ogr.Feature(lyr.GetLayerDefn())
f.SetGeometry(ogr.CreateGeometryFromWkt('POLYGON ((0 0,0 1,1 1,1 0,0 0))'))
f.SetField('str_field', 'one')
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetGeometry(ogr.CreateGeometryFromWkt('POLYGON ((0 0,0 2,2 2,2 0,0 0))'))
f.SetField('str_field', 'two')
lyr.CreateFeature(f)
f = None
ds = None
vl1 = QgsVectorLayer('{}'.format(tmpfile) + "|layername=" + "test", 'test', 'ogr')
self.assertTrue(vl1.isValid())
self.assertEqual(vl1.uniqueValues(0), {1, 2})
self.assertEqual(vl1.uniqueValues(1), {'one', 'two'})


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

0 comments on commit 1689c93

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