Skip to content

Commit

Permalink
OGRSQL: fix incorrect interaction of LIMIT clause and SetNextByIndex() (
Browse files Browse the repository at this point in the history
fixes #8585)
  • Loading branch information
rouault authored and github-actions[bot] committed Oct 21, 2023
1 parent afca35b commit 625624c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
33 changes: 33 additions & 0 deletions autotest/ogr/ogr_sql_rfc28.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,39 @@ def test_ogr_rfc28_47():
gdaltest.ds.ReleaseResultSet(lyr)
assert tr

with data_ds.ExecuteSQL("SELECT * FROM POLY LIMIT 1") as lyr:
assert lyr.SetNextByIndex(1) == ogr.OGRERR_FAILURE
assert lyr.GetNextFeature() is None

with data_ds.ExecuteSQL("SELECT * FROM POLY LIMIT 1 OFFSET 1") as lyr:
assert lyr.SetNextByIndex(1) == ogr.OGRERR_FAILURE
assert lyr.GetNextFeature() is None

with data_ds.ExecuteSQL("SELECT * FROM POLY LIMIT 2 OFFSET 1") as lyr:
assert lyr.SetNextByIndex(1) == ogr.OGRERR_NONE
f = lyr.GetNextFeature()
assert f["EAS_ID"] == 171
assert lyr.GetNextFeature() is None

with data_ds.ExecuteSQL("SELECT * FROM POLY LIMIT 2 OFFSET 1") as lyr:
assert lyr.SetNextByIndex(1) == ogr.OGRERR_NONE
assert lyr.SetNextByIndex(1) == ogr.OGRERR_NONE
f = lyr.GetNextFeature()
assert f["EAS_ID"] == 171
assert lyr.GetNextFeature() is None

with data_ds.ExecuteSQL("SELECT * FROM POLY LIMIT 1 OFFSET 1") as lyr:
assert lyr.SetNextByIndex((1 << 63) - 1) == ogr.OGRERR_FAILURE
assert lyr.GetNextFeature() is None

with data_ds.ExecuteSQL("SELECT * FROM POLY OFFSET 1") as lyr:
assert lyr.SetNextByIndex((1 << 63) - 1) == ogr.OGRERR_FAILURE
assert lyr.GetNextFeature() is None

with data_ds.ExecuteSQL("SELECT * FROM POLY") as lyr:
assert lyr.SetNextByIndex((1 << 63) - 1) == ogr.OGRERR_FAILURE
assert lyr.GetNextFeature() is None


###############################################################################
# Test date/datetime comparisons (#6810)
Expand Down
23 changes: 21 additions & 2 deletions ogr/ogrsf_frmts/generic/ogr_gensql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "ogr_api.h"
#include "cpl_time.h"
#include <algorithm>
#include <limits>
#include <vector>

//! @cond Doxygen_Suppress
Expand Down Expand Up @@ -635,6 +636,7 @@ void OGRGenSQLResultsLayer::ResetReading()

nNextIndexFID = psSelectInfo->offset;
nIteratedFeatures = -1;
m_bEOF = false;
}

/************************************************************************/
Expand All @@ -652,10 +654,22 @@ OGRErr OGRGenSQLResultsLayer::SetNextByIndex(GIntBig nIndex)

swq_select *psSelectInfo = static_cast<swq_select *>(pSelectInfo);

nIteratedFeatures = 0;
if (psSelectInfo->limit >= 0)
{
nIteratedFeatures = nIndex;
if (nIteratedFeatures >= psSelectInfo->limit)
{
return OGRERR_FAILURE;
}
}

CreateOrderByIndex();

if (nIndex > std::numeric_limits<GIntBig>::max() - psSelectInfo->offset)
{
m_bEOF = true;
return OGRERR_FAILURE;
}
if (psSelectInfo->query_mode == SWQM_SUMMARY_RECORD ||
psSelectInfo->query_mode == SWQM_DISTINCT_LIST ||
panFIDIndex != nullptr)
Expand All @@ -665,7 +679,10 @@ OGRErr OGRGenSQLResultsLayer::SetNextByIndex(GIntBig nIndex)
}
else
{
return poSrcLayer->SetNextByIndex(nIndex + psSelectInfo->offset);
OGRErr eErr = poSrcLayer->SetNextByIndex(nIndex + psSelectInfo->offset);
if (eErr != OGRERR_NONE)
m_bEOF = true;
return eErr;
}
}

Expand Down Expand Up @@ -1589,6 +1606,8 @@ OGRFeature *OGRGenSQLResultsLayer::GetNextFeature()
{
swq_select *psSelectInfo = static_cast<swq_select *>(pSelectInfo);

if (m_bEOF)
return nullptr;
if (psSelectInfo->limit >= 0 &&
(nIteratedFeatures < 0 ? 0 : nIteratedFeatures) >= psSelectInfo->limit)
return nullptr;
Expand Down
1 change: 1 addition & 0 deletions ogr/ogrsf_frmts/generic/ogr_gensql.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class OGRGenSQLResultsLayer final : public OGRLayer

std::string m_osInitialWHERE{};
bool m_bForwardWhereToSourceLayer = true;
bool m_bEOF = false;

OGRLayer **papoTableLayers;

Expand Down

0 comments on commit 625624c

Please sign in to comment.