mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-25 00:58:06 -05:00
Merge pull request #32114 from troopa81/fix_feature_count_estimated_metadata
Fix featureCount on postgres view when flag estimatedmetadata is set
This commit is contained in:
commit
8913fb34e6
@ -3353,20 +3353,38 @@ long QgsPostgresProvider::featureCount() const
|
||||
// use estimated metadata even when there is a where clause,
|
||||
// although we get an incorrect feature count for the subset
|
||||
// - but make huge dataset usable.
|
||||
long num = -1;
|
||||
if ( !mIsQuery && mUseEstimatedMetadata )
|
||||
{
|
||||
sql = QStringLiteral( "SELECT reltuples::bigint FROM pg_catalog.pg_class WHERE oid=regclass(%1)::oid" ).arg( quotedValue( mQuery ) );
|
||||
if ( connectionRO()->pgVersion() >= 90000 )
|
||||
{
|
||||
// parse explain output to estimate feature count
|
||||
// we don't use pg_class reltuples because it returns 0 for view
|
||||
sql = QStringLiteral( "EXPLAIN (FORMAT JSON) SELECT count(*) FROM %1%2" ).arg( mQuery, filterWhereClause() );
|
||||
QgsPostgresResult result( connectionRO()->PQexec( sql ) );
|
||||
|
||||
const QString json = result.PQgetvalue( 0, 0 );
|
||||
const QVariantList explain = QgsJsonUtils::parseJson( json ).toList();
|
||||
const QVariantMap countPlan = explain.count() ? explain[0].toMap().value( "Plan" ).toMap() : QVariantMap();
|
||||
const QVariantList queryPlan = countPlan.value( "Plans" ).toList();
|
||||
const QVariant nbRows = queryPlan.count() ? queryPlan[0].toMap().value( "Plan Rows" ) : QVariant();
|
||||
|
||||
if ( nbRows.isValid() )
|
||||
num = nbRows.toInt();
|
||||
else
|
||||
QgsLogger::warning( QStringLiteral( "Cannot parse JSON explain result to estimate feature count (%1) : %2" ).arg( sql, json ) );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sql = QStringLiteral( "SELECT count(*) FROM %1%2" ).arg( mQuery, filterWhereClause() );
|
||||
QgsPostgresResult result( connectionRO()->PQexec( sql ) );
|
||||
|
||||
QgsDebugMsg( "number of features as text: " + result.PQgetvalue( 0, 0 ) );
|
||||
|
||||
num = result.PQgetvalue( 0, 0 ).toLong();
|
||||
}
|
||||
|
||||
QgsPostgresResult result( connectionRO()->PQexec( sql ) );
|
||||
|
||||
QgsDebugMsg( "number of features as text: " + result.PQgetvalue( 0, 0 ) );
|
||||
|
||||
long num = result.PQgetvalue( 0, 0 ).toLong();
|
||||
mShared->setFeaturesCounted( num );
|
||||
|
||||
QgsDebugMsg( "number of features: " + QString::number( num ) );
|
||||
|
@ -1410,6 +1410,16 @@ class TestPyQgsPostgresProvider(unittest.TestCase, ProviderTestCase):
|
||||
for f in vl0.getFeatures():
|
||||
self.assertNotEqual(f.attribute(0), NULL)
|
||||
|
||||
def testFeatureCountEstimatedOnView(self):
|
||||
"""
|
||||
Test feature count on view when estimated data is enabled
|
||||
"""
|
||||
self.execSQLCommand('DROP VIEW IF EXISTS qgis_test.somedataview')
|
||||
self.execSQLCommand('CREATE VIEW qgis_test.somedataview AS SELECT * FROM qgis_test."someData"')
|
||||
vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pk\' estimatedmetadata=true srid=4326 type=POINT table="qgis_test"."somedataview" (geom) sql=', 'test', 'postgres')
|
||||
self.assertTrue(vl.isValid())
|
||||
self.assertTrue(self.source.featureCount() > 0)
|
||||
|
||||
|
||||
class TestPyQgsPostgresProviderCompoundKey(unittest.TestCase, ProviderTestCase):
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user