Skip to content

Commit

Permalink
ExecuteSQL() OGRSQL dialect: error out if SetSpatialFilter() fails (f…
Browse files Browse the repository at this point in the history
…ixes #9623)
  • Loading branch information
rouault committed Apr 5, 2024
1 parent b677b38 commit cc162dd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
16 changes: 16 additions & 0 deletions autotest/ogr/ogr_sql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,3 +1863,19 @@ def test_ogr_sql_ilike_utf8():

lyr.SetAttributeFilter("'éven' ILIKE '%xen'")
assert lyr.GetFeatureCount() == 0


###############################################################################
# Test error on setting a spatial filter during ExecuteSQL


@gdaltest.enable_exceptions()
def test_ogr_sql_test_execute_sql_error_on_spatial_filter_mem_layer():

ds = ogr.GetDriverByName("Memory").CreateDataSource("")
ds.CreateLayer("test", geom_type=ogr.wkbNone)
geom = ogr.CreateGeometryFromWkt("POLYGON((0 0,0 1,1 1,1 0,0 0))")
with pytest.raises(
Exception, match="Cannot set spatial filter: no geometry field present in layer"
):
ds.ExecuteSQL("SELECT 1 FROM test", spatialFilter=geom)
14 changes: 9 additions & 5 deletions gcore/gdaldataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6972,23 +6972,27 @@ OGRLayer *GDALDataset::BuildLayerFromSelectInfo(
swq_select *psSelectInfo, OGRGeometry *poSpatialFilter,
const char *pszDialect, swq_select_parse_options *poSelectParseOptions)
{
OGRGenSQLResultsLayer *poResults = nullptr;
std::unique_ptr<OGRGenSQLResultsLayer> poResults;
GDALSQLParseInfo *psParseInfo =
BuildParseInfo(psSelectInfo, poSelectParseOptions);

if (psParseInfo)
{
poResults =
new OGRGenSQLResultsLayer(this, psSelectInfo, poSpatialFilter,
psParseInfo->pszWHERE, pszDialect);
const auto nErrorCounter = CPLGetErrorCounter();
poResults = std::make_unique<OGRGenSQLResultsLayer>(
this, psSelectInfo, poSpatialFilter, psParseInfo->pszWHERE,
pszDialect);
if (CPLGetErrorCounter() > nErrorCounter &&
CPLGetLastErrorType() != CE_None)
poResults.reset();
}
else
{
delete psSelectInfo;
}
DestroyParseInfo(psParseInfo);

return poResults;
return poResults.release();
}

/************************************************************************/
Expand Down
7 changes: 7 additions & 0 deletions ogr/ogrsf_frmts/generic/ogrlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,9 @@ bool OGRLayer::ValidateGeometryFieldIndexForSetSpatialFilter(
void OGRLayer::SetSpatialFilter(OGRGeometry *poGeomIn)

{
if (poGeomIn && !ValidateGeometryFieldIndexForSetSpatialFilter(0, poGeomIn))
return;

m_iGeomFieldFilter = 0;
if (InstallFilter(poGeomIn))
ResetReading();
Expand All @@ -1492,6 +1495,10 @@ void OGRLayer::SetSpatialFilter(int iGeomField, OGRGeometry *poGeomIn)
{
if (iGeomField == 0)
{
if (poGeomIn &&
!ValidateGeometryFieldIndexForSetSpatialFilter(0, poGeomIn))
return;

m_iGeomFieldFilter = iGeomField;
SetSpatialFilter(poGeomIn);
}
Expand Down

0 comments on commit cc162dd

Please sign in to comment.