[processing] Fix crash in extract by attribute when field name does not exist

Fixes #19531
This commit is contained in:
Nyall Dawson 2018-08-03 19:30:06 +10:00
parent 9e0860637d
commit 4acef1efbf
5 changed files with 23 additions and 1 deletions

View File

@ -130,6 +130,9 @@ class SelectByAttribute(QgisAlgorithm):
fields = layer.fields() fields = layer.fields()
idx = layer.fields().lookupField(fieldName) idx = layer.fields().lookupField(fieldName)
if idx < 0:
raise QgsProcessingException(self.tr("Field '{}' was not found in layer").format(fieldName))
fieldType = fields[idx].type() fieldType = fields[idx].type()
if fieldType != QVariant.String and operator in self.STRING_OPERATORS: if fieldType != QVariant.String and operator in self.STRING_OPERATORS:

View File

@ -119,6 +119,9 @@ class AlgorithmsTest(object):
exec(('\n'.join(defs['expectedFailure'][:-1])), globals(), locals()) exec(('\n'.join(defs['expectedFailure'][:-1])), globals(), locals())
expectFailure = eval(defs['expectedFailure'][-1]) expectFailure = eval(defs['expectedFailure'][-1])
if 'expectedException' in defs:
expectFailure = True
# ignore user setting for invalid geometry handling # ignore user setting for invalid geometry handling
context = QgsProcessingContext() context = QgsProcessingContext()
context.setProject(QgsProject.instance()) context.setProject(QgsProject.instance())

View File

@ -2086,6 +2086,20 @@ tests:
name: expected/extract_by_attribute_not_null.gml name: expected/extract_by_attribute_not_null.gml
type: vector type: vector
- algorithm: native:extractbyattribute
name: Extract by attribute (bad field)
params:
FIELD: notafield
INPUT:
name: polys.gml
type: vector
OPERATOR: '9'
results:
OUTPUT:
name: expected/failure.gml
type: vector
expectedException: true
- algorithm: native:extractbyattribute - algorithm: native:extractbyattribute
name: Extract by attribute (starts with) name: Extract by attribute (starts with)
params: params:

View File

@ -101,8 +101,10 @@ QVariantMap QgsExtractByAttributeAlgorithm::processAlgorithm( const QVariantMap
std::unique_ptr< QgsFeatureSink > nonMatchingSink( parameterAsSink( parameters, QStringLiteral( "FAIL_OUTPUT" ), context, nonMatchingSinkId, source->fields(), std::unique_ptr< QgsFeatureSink > nonMatchingSink( parameterAsSink( parameters, QStringLiteral( "FAIL_OUTPUT" ), context, nonMatchingSinkId, source->fields(),
source->wkbType(), source->sourceCrs() ) ); source->wkbType(), source->sourceCrs() ) );
int idx = source->fields().lookupField( fieldName ); int idx = source->fields().lookupField( fieldName );
if ( idx < 0 )
throw QgsProcessingException( QObject::tr( "Field '%1' was not found in INPUT source" ).arg( fieldName ) );
QVariant::Type fieldType = source->fields().at( idx ).type(); QVariant::Type fieldType = source->fields().at( idx ).type();
if ( fieldType != QVariant::String && ( op == BeginsWith || op == Contains || op == DoesNotContain ) ) if ( fieldType != QVariant::String && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )