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

escaping underscore in query builder works for postgis layer, but not for shape layer #58097

Closed
2 tasks done
janhec opened this issue Jul 14, 2024 · 9 comments
Closed
2 tasks done
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Data Provider Related to specific vector, raster or mesh data providers Feedback Waiting on the submitter for answers

Comments

@janhec
Copy link

janhec commented Jul 14, 2024

What is the bug or the crash?

A WHERE clause in query builder with e.g. name like '%\_' does not function when the source layer is a shapefile.
Probably this escape is not implemented in the shapefile driver, whereas it is in the postgis/postgresql driver.
This is confusing and I do not think there is a workaround, as functions (such as replace) are not supported in query builder.

Steps to reproduce the issue

Create and/or load a shapefile in qgis (3.36), having a text column (say name) where some rows contain an underscore.
Try filtering rows with an underscore through
name like '%\_'
This will not return any row.
For comparison, load a similar postgis layer and apply the same query builder expression.
This will filter the rows properly.

Versions

<style type="text/css"> p, li { white-space: pre-wrap; } </style>
QGIS version 3.36.3-Maidenhead QGIS code revision 2df9655
Qt version 5.15.13
Python version 3.12.3
GDAL/OGR version 3.9.0
PROJ version 9.4.0
EPSG Registry database version v11.004 (2024-02-24)
GEOS version 3.12.1-CAPI-1.18.1
SQLite version 3.45.1
PDAL version 2.6.3
PostgreSQL client version 16.2
SpatiaLite version 5.1.0
QWT version 6.2.0
QScintilla2 version 2.14.1
OS version Windows 11 Version 2009
       
Active Python plugins
BezierEditing version 1.3.8
cesium_ion v1.0.2
CityJSON-loader 0.8.1
converteer_populatie 0.1
GeoCoding 2.19
inspireNL 2.8
mmqgis 2021.9.10
moving_feature 2.0
nominatim_locator_filter 0.3.2
pdokservicesplugin 5.1.0
pdok_locatieserver_locator_filter 0.2.1
pluginbuilder3 3.2.1
plugin_reloader 0.9.4
postgis_geoprocessing 0.9
quick_map_services 0.19.34
spline 0.2
SpreadsheetLayers 2.1.2
StreetView 3.2
db_manager 0.1.20
MetaSearch 0.3.6
processing 2.12.99
QGIS version 3.36.3-Maidenhead QGIS code revision [2df9655](https://github.com/qgis/QGIS/commit/2df96554) Qt version 5.15.13 Python version 3.12.3 GDAL/OGR version 3.9.0 PROJ version 9.4.0 EPSG Registry database version v11.004 (2024-02-24) GEOS version 3.12.1-CAPI-1.18.1 SQLite version 3.45.1 PDAL version 2.6.3 PostgreSQL client version 16.2 SpatiaLite version 5.1.0 QWT version 6.2.0 QScintilla2 version 2.14.1 OS version Windows 11 Version 2009

Active Python plugins
BezierEditing
version 1.3.8
cesium_ion
v1.0.2
CityJSON-loader
0.8.1
converteer_populatie
0.1
GeoCoding
2.19
inspireNL
2.8
mmqgis
2021.9.10
moving_feature
2.0
nominatim_locator_filter
0.3.2
pdokservicesplugin
5.1.0
pdok_locatieserver_locator_filter
0.2.1
pluginbuilder3
3.2.1
plugin_reloader
0.9.4
postgis_geoprocessing
0.9
quick_map_services
0.19.34
spline
0.2
SpreadsheetLayers
2.1.2
StreetView
3.2
db_manager
0.1.20
MetaSearch
0.3.6
processing
2.12.99

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

Additional context

No response

@janhec janhec added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Jul 14, 2024
@agiudiceandrea agiudiceandrea added the Data Provider Related to specific vector, raster or mesh data providers label Jul 14, 2024
@agiudiceandrea
Copy link
Contributor

agiudiceandrea commented Jul 14, 2024

@janhec, the correct syntax to use depends on the underlying driver. In the case of the ESRI Shapefile format, the driver responsible to parse and execute the filter query (WHERE clause) is the GDAL/OGR driver which in turns normally uses the OGR SQL dialect for such layer format.
See for reference https://gdal.org/user/ogr_sql_dialect.html, https://gdal.org/user/ogr_sql_dialect.html.

As stated in https://gdal.org/user/ogr_sql_dialect.html#where, you need to use an ESCAPE escape_char clause in order to define the escape character.

For example, in your case you can use name LIKE '%/_' ESCAPE '/' to filter in all the features with the field name containing a string where the last char is an underscore.

@agiudiceandrea agiudiceandrea added the Feedback Waiting on the submitter for answers label Jul 14, 2024
@janhec
Copy link
Author

janhec commented Jul 14, 2024 via email

@agiudiceandrea
Copy link
Contributor

agiudiceandrea commented Jul 14, 2024

@janhec, have you tried the very expression I've indicated in my previous comment name LIKE '%/_' ESCAPE '/'?

Just to be clear, are you referring to the "Query Builder" dialog available either through Layer -> Filter or Layer Properties -> Source -> Provider Feature Filter -> Query Builder?

image

@janhec
Copy link
Author

janhec commented Jul 14, 2024 via email

@janhec
Copy link
Author

janhec commented Jul 14, 2024 via email

@janhec
Copy link
Author

janhec commented Jul 14, 2024 via email

@agiudiceandrea
Copy link
Contributor

@janhec, see OSGeo/gdal#10416.

I think this issue report can be closed, since there is no issue in QGIS itself.

@janhec
Copy link
Author

janhec commented Jul 14, 2024

Yes, it can.

@janhec janhec closed this as completed Jul 14, 2024
@agiudiceandrea
Copy link
Contributor

@janhec see OSGeo/gdal#10425.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Data Provider Related to specific vector, raster or mesh data providers Feedback Waiting on the submitter for answers
Projects
None yet
Development

No branches or pull requests

2 participants