Skip to content

Commit

Permalink
OpenFileGDB: error out explicitly when attempting to create an index …
Browse files Browse the repository at this point in the history
…on multiple columns

Refs OSGeo#10590
  • Loading branch information
rouault committed Aug 12, 2024
1 parent 6339b46 commit 0ca6104
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
45 changes: 24 additions & 21 deletions autotest/ogr/ogr_openfilegdb_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,7 @@ def test_ogr_openfilegdb_write_spatial_index(
###############################################################################


@gdaltest.enable_exceptions()
def test_ogr_openfilegdb_write_attribute_index(tmp_vsimem):

dirname = tmp_vsimem / "out.gdb"
Expand Down Expand Up @@ -1589,31 +1590,35 @@ def test_ogr_openfilegdb_write_attribute_index(tmp_vsimem):
f = None

# Errors of index creation
with gdal.quiet_errors():
gdal.ErrorReset()
with pytest.raises(
Exception, match="Invalid index name: cannot be greater than 16 characters"
):
ds.ExecuteSQL("CREATE INDEX this_name_is_wayyyyy_tooo_long ON test(int16)")
assert gdal.GetLastErrorMsg() != ""

gdal.ErrorReset()
with pytest.raises(Exception, match="Invalid layer name: non_existing_layer"):
ds.ExecuteSQL("CREATE INDEX idx_int16 ON non_existing_layer(int16)")
assert gdal.GetLastErrorMsg() != ""

gdal.ErrorReset()
with pytest.raises(Exception, match="Cannot find field invalid_field"):
ds.ExecuteSQL("CREATE INDEX invalid_field ON test(invalid_field)")
assert gdal.GetLastErrorMsg() != ""

# Reserved keyword
gdal.ErrorReset()
with pytest.raises(
Exception, match="Invalid index name: must not be a reserved keyword"
):
ds.ExecuteSQL("CREATE INDEX SELECT ON test(int16)")
assert gdal.GetLastErrorMsg() != ""

gdal.ErrorReset()
with pytest.raises(Exception, match="Invalid index name: must start with a letter"):
ds.ExecuteSQL("CREATE INDEX _starting_by_ ON test(int16)")
assert gdal.GetLastErrorMsg() != ""

gdal.ErrorReset()
with pytest.raises(
Exception,
match="Invalid index name: must contain only alpha numeric character or _",
):
ds.ExecuteSQL("CREATE INDEX a&b ON test(int16)")
assert gdal.GetLastErrorMsg() != ""

with pytest.raises(
Exception, match="Creation of multiple-column indices is not supported"
):
ds.ExecuteSQL("CREATE INDEX index_on_two_cols ON test(int16, int32)")

# Create indexes
gdal.ErrorReset()
Expand All @@ -1631,20 +1636,18 @@ def test_ogr_openfilegdb_write_attribute_index(tmp_vsimem):
fld_defn = ogr.FieldDefn("unindexed", ogr.OFTString)
assert lyr.CreateField(fld_defn) == ogr.OGRERR_NONE

with gdal.quiet_errors():
with pytest.raises(Exception, match="An index with same name already exists"):
# Re-using an index name
gdal.ErrorReset()
ds.ExecuteSQL("CREATE INDEX idx_int16 ON test(unindexed)")
assert gdal.GetLastErrorMsg() != ""

with pytest.raises(Exception, match="Field int16 has already a registered index"):
# Trying to index twice a field
gdal.ErrorReset()
ds.ExecuteSQL("CREATE INDEX int16_again ON test(int16)")
assert gdal.GetLastErrorMsg() != ""

gdal.ErrorReset()
with pytest.raises(
Exception, match="Field lower_str has already a registered index"
):
ds.ExecuteSQL("CREATE INDEX lower_str_again ON test(lower_str)")
assert gdal.GetLastErrorMsg() != ""

ds = None

Expand Down
7 changes: 7 additions & 0 deletions ogr/ogrsf_frmts/openfilegdb/ogropenfilegdbdatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,13 @@ OGRLayer *OGROpenFileGDBDataSource::ExecuteSQL(const char *pszSQLCommand,
const std::string osLayerName = afterOn.substr(0, nOpenParPos);
const std::string osExpression = afterOn.substr(
nOpenParPos + 1, afterOn.size() - nOpenParPos - 2);
if (osExpression.find(',') != std::string::npos)
{
CPLError(
CE_Failure, CPLE_NotSupported,
"Creation of multiple-column indices is not supported");
return nullptr;
}
auto poLayer = GetLayerByName(osLayerName.c_str());
if (poLayer)
{
Expand Down

0 comments on commit 0ca6104

Please sign in to comment.