Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
PostGIS backend: translate insensitive equality comparison as Postgre…
… SQL lower(foo::text) = lower('bar').

Fixes a performance issue dating from 7.0

The wfs_filter_postgis.map wfs_filter_postgis_property_is_equal_case_insensitive
test case exercices this code path.
  • Loading branch information
rouault committed Nov 22, 2019
1 parent 0af5737 commit 7f3cebc
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions mappostgis.c
Expand Up @@ -4036,6 +4036,8 @@ int msPostGISLayerTranslateFilter(layerObj *layer, expressionObj *filter, char *
msFree(snippet);
msFree(stresc);
} else if(filter->type == MS_EXPRESSION) {
int ieq_expected = MS_FALSE;

if(msPostGISParseData(layer) != MS_SUCCESS) return MS_FAILURE;

if(layer->debug >= 2) msDebug("msPostGISLayerTranslateFilter. String: %s.\n", filter->string);
Expand All @@ -4050,7 +4052,9 @@ int msPostGISLayerTranslateFilter(layerObj *layer, expressionObj *filter, char *
*/
if(node->token == MS_TOKEN_BINDING_TIME) {
bindingToken = node->token;
} else if(node->token == MS_TOKEN_COMPARISON_EQ || node->token == MS_TOKEN_COMPARISON_NE ||
} else if(node->token == MS_TOKEN_COMPARISON_EQ ||
node->token == MS_TOKEN_COMPARISON_IEQ ||
node->token == MS_TOKEN_COMPARISON_NE ||
node->token == MS_TOKEN_COMPARISON_GT || node->token == MS_TOKEN_COMPARISON_GE ||
node->token == MS_TOKEN_COMPARISON_LT || node->token == MS_TOKEN_COMPARISON_LE ||
node->token == MS_TOKEN_COMPARISON_IN) {
Expand Down Expand Up @@ -4101,7 +4105,10 @@ int msPostGISLayerTranslateFilter(layerObj *layer, expressionObj *filter, char *

msFreeCharArray(strings, nstrings);
} else {
strtmpl = "'%s'";
if(comparisonToken == MS_TOKEN_COMPARISON_IEQ)
strtmpl = "lower('%s')";
else
strtmpl = "'%s'";
stresc = msPostGISEscapeSQLParam(layer, node->tokenval.strval);
snippet = (char *) msSmallMalloc(strlen(strtmpl) + strlen(stresc));
sprintf(snippet, strtmpl, stresc);
Expand Down Expand Up @@ -4149,7 +4156,11 @@ int msPostGISLayerTranslateFilter(layerObj *layer, expressionObj *filter, char *
case MS_TOKEN_BINDING_DOUBLE:
case MS_TOKEN_BINDING_INTEGER:
case MS_TOKEN_BINDING_STRING:
if(node->token == MS_TOKEN_BINDING_STRING || node->next->token == MS_TOKEN_COMPARISON_RE || node->next->token == MS_TOKEN_COMPARISON_IRE)
if (node->token == MS_TOKEN_BINDING_STRING && node->next->token == MS_TOKEN_COMPARISON_IEQ ) {
strtmpl = "lower(%s::text)";
ieq_expected = MS_TRUE;
}
else if(node->token == MS_TOKEN_BINDING_STRING || node->next->token == MS_TOKEN_COMPARISON_RE || node->next->token == MS_TOKEN_COMPARISON_IRE)
strtmpl = "%s::text"; /* explicit cast necessary for certain operators */
else
strtmpl = "%s";
Expand Down Expand Up @@ -4195,8 +4206,19 @@ int msPostGISLayerTranslateFilter(layerObj *layer, expressionObj *filter, char *
native_string = msStringConcatenate(native_string, msExpressionTokenToString(node->token));
break;

/* unsupported tokens */
case MS_TOKEN_COMPARISON_IEQ:
if( ieq_expected )
{
native_string = msStringConcatenate(native_string, "=");
ieq_expected = MS_FALSE;
}
else
{
goto cleanup;
}
break;

/* unsupported tokens */
case MS_TOKEN_COMPARISON_BEYOND:
case MS_TOKEN_FUNCTION_TOSTRING:
case MS_TOKEN_FUNCTION_ROUND:
Expand Down

0 comments on commit 7f3cebc

Please sign in to comment.