qgis / QGIS

QGIS is a free, open source, cross platform (lin/win/mac) geographical information system (GIS)
https://qgis.org
GNU General Public License v2.0
10.59k stars 3k forks source link

Expression @row_number does not work as expected #53536

Open TobiasRosskopf opened 1 year ago

TobiasRosskopf commented 1 year ago

What is the bug or the crash?

If I use the expression @row_number in the attribute table of a vector layer it does not return the actual row number like expected when sorted on an other field. Instead it has the same behaviour like @id.

QGIS_Bug_@row_number

Steps to reproduce the issue

  1. Create a new vector layer.
  2. Add a string attribute to the layer.
  3. Add multiple features to the layer with unsorted strings (also NULL strings).
  4. Sort table by the string attribute.
  5. Add an integer attribute for the id to the layer and fill it with the expression @id.
  6. Add an integer attribute for the row number to the layer and fill it with the expression @row_number.
  7. The columns of the two integer attributes are exactly the same!

Versions

QGIS-Version 3.28.6-Firenze QGIS-Codeversion 868c9fa03b Qt-Version 5.15.3 Python-Version 3.9.5 GDAL-Version 3.6.4 PROJ-Version 9.2.0 EPSG-Registraturdatenbankversion v10.082 (2023-02-06) GEOS-Version 3.11.2-CAPI-1.17.2 SQLite-Version 3.41.1 PDAL-Version 2.5.2 PostgreSQL-Client-Version unknown SpatiaLite-Version 5.0.1 QWT-Version 6.1.6 QScintilla2-Version 2.13.1 BS-Version Windows 10 Version 2009

Aktive Python-Erweiterungen AnotherDXF2Shape 1.2.7 BezierEditing version 1.3.6 bgs_basin_tools 0.2.0 bgs_connect 1.3.2 bgs_map_tiles 0.1.2 bgs_radolan 0.1 cigeoe_feature_nodes_z_tag 1.0 crayfish 3.6.0 custom_titlebar 1.0.0 extractor 1.0 flowTrace 1.1.1 LandXml 2.4 linking_relation_editor v1.1.4 mapswipetool_plugin 1.2 Mergin 2023.1 mmqgis 2021.9.10 ntv2_transformations 0.20 ORStools 1.5.3 pluginbuilder3 3.2.1 plugin_reloader 0.9.3 processing_saga_nextgen 0.0.7 profiletool 4.2.6 pyqgis_resource_browser 0.3.0 qfieldsync v4.5.0 qgis2rasterlite 0.5 Qgis2threejs 2.7.1 qgis2web 3.16.0 QgisModelBaker v7.4.2 QuickOSM 2.2.2 QuickPrint 3.6.1 quick_map_services 0.19.33 radolan2map 1.7 SpreadsheetLayers 2.1.0 stationlines 0.2.0 StreetView 3.2 TerrainShading 0.9.3 db_manager 0.1.20 grassprovider 2.12.99 MetaSearch 0.3.6 processing 2.12.99 sagaprovider 2.12.99

Supported QGIS version

New profile

Additional context

No response

roya0045 commented 1 year ago

What generates the row number is a standard iterator that iterate on feature based on the standard order, to fix this the same ordering&filtering as the attribute table would have to be applied to the feature request to have the same row order as viewed in the attribute table.

I can see how this can be seen as a bug.

For ref: https://github.com/qgis/QGIS/blob/951d7e3307edfd81ef9469275acceb3947a8682c/src/gui/vector/qgsfieldcalculator.cpp#L286-L306