Program: 112964
Project: 112964
Buggy source:
public static DistributableQuery createOptimalQuery(final String cql,final GeotoolsFeatureDataAdapter adapter,final CompareOperation geoCompareOp,final PrimaryIndex index) throws CQLException {
  final Filter cqlFilter=CQL.toFilter(cql);
  final ExtractAttributesFilter attributesVisitor=new ExtractAttributesFilter();
  final Object obj=cqlFilter.accept(attributesVisitor,null);
  final Collection<String> attrs;
  if ((obj != null) && (obj instanceof Collection)) {
    attrs=(Collection<String>)obj;
  }
 else {
    attrs=new ArrayList<String>();
  }
  final boolean isSpatial=index == null ? false : hasAtLeastSpatial(index);
  final boolean isTemporal=index == null ? false : hasTime(index) && adapter.hasTemporalConstraints();
  if (isSpatial) {
    final String geomName=adapter.getType().getGeometryDescriptor().getLocalName();
    attrs.remove(geomName);
  }
  if (isTemporal) {
    final TimeDescriptors timeDescriptors=adapter.getTimeDescriptors();
    if (timeDescriptors != null) {
      final AttributeDescriptor timeDesc=timeDescriptors.getTime();
      if (timeDesc != null) {
        attrs.remove(timeDesc.getLocalName());
      }
      final AttributeDescriptor startDesc=timeDescriptors.getStartRange();
      if (startDesc != null) {
        attrs.remove(startDesc.getLocalName());
      }
      final AttributeDescriptor endDesc=timeDescriptors.getEndRange();
      if (endDesc != null) {
        attrs.remove(endDesc.getLocalName());
      }
    }
  }
  DistributableQuery baseQuery=null;
  final Geometry geometry=ExtractGeometryFilterVisitor.getConstraints(cqlFilter,adapter.getType().getCoordinateReferenceSystem());
  final TemporalConstraintsSet timeConstraintSet=new ExtractTimeFilterVisitor(adapter.getTimeDescriptors()).getConstraints(cqlFilter);
  if (geometry != null) {
    Constraints constraints=GeometryUtils.basicConstraintsFromGeometry(geometry);
    if ((timeConstraintSet != null) && !timeConstraintSet.isEmpty()) {
      final TemporalConstraints temporalConstraints=QueryIndexHelper.getTemporalConstraintsForDescriptors(adapter.getTimeDescriptors(),timeConstraintSet);
      final Constraints timeConstraints=SpatialTemporalQuery.createConstraints(temporalConstraints,false);
      constraints=constraints.merge(timeConstraints);
    }
    baseQuery=new SpatialQuery(constraints,geometry,geoCompareOp);
  }
 else   if ((timeConstraintSet != null) && !timeConstraintSet.isEmpty()) {
    final TemporalConstraints temporalConstraints=QueryIndexHelper.getTemporalConstraintsForDescriptors(adapter.getTimeDescriptors(),timeConstraintSet);
    baseQuery=new TemporalQuery(temporalConstraints);
  }
  if (attrs.isEmpty()) {
    return baseQuery;
  }
 else {
    return new CQLQuery(baseQuery,cqlFilter,adapter);
  }
}

Fixed version (ground truth):
public static DistributableQuery createOptimalQuery(final Filter cqlFilter,final GeotoolsFeatureDataAdapter adapter,final CompareOperation geoCompareOp,final PrimaryIndex index){
  final ExtractAttributesFilter attributesVisitor=new ExtractAttributesFilter();
  final Object obj=cqlFilter.accept(attributesVisitor,null);
  final Collection<String> attrs;
  if ((obj != null) && (obj instanceof Collection)) {
    attrs=(Collection<String>)obj;
  }
 else {
    attrs=new ArrayList<String>();
  }
  final boolean isSpatial=index == null ? false : hasAtLeastSpatial(index);
  final boolean isTemporal=index == null ? false : hasTime(index) && adapter.hasTemporalConstraints();
  if (isSpatial) {
    final String geomName=adapter.getType().getGeometryDescriptor().getLocalName();
    attrs.remove(geomName);
  }
  if (isTemporal) {
    final TimeDescriptors timeDescriptors=adapter.getTimeDescriptors();
    if (timeDescriptors != null) {
      final AttributeDescriptor timeDesc=timeDescriptors.getTime();
      if (timeDesc != null) {
        attrs.remove(timeDesc.getLocalName());
      }
      final AttributeDescriptor startDesc=timeDescriptors.getStartRange();
      if (startDesc != null) {
        attrs.remove(startDesc.getLocalName());
      }
      final AttributeDescriptor endDesc=timeDescriptors.getEndRange();
      if (endDesc != null) {
        attrs.remove(endDesc.getLocalName());
      }
    }
  }
  DistributableQuery baseQuery=null;
  final Geometry geometry=ExtractGeometryFilterVisitor.getConstraints(cqlFilter,adapter.getType().getCoordinateReferenceSystem());
  final TemporalConstraintsSet timeConstraintSet=new ExtractTimeFilterVisitor(adapter.getTimeDescriptors()).getConstraints(cqlFilter);
  if (geometry != null) {
    Constraints constraints=GeometryUtils.basicConstraintsFromGeometry(geometry);
    if ((timeConstraintSet != null) && !timeConstraintSet.isEmpty()) {
      final TemporalConstraints temporalConstraints=QueryIndexHelper.getTemporalConstraintsForDescriptors(adapter.getTimeDescriptors(),timeConstraintSet);
      final Constraints timeConstraints=SpatialTemporalQuery.createConstraints(temporalConstraints,false);
      constraints=constraints.merge(timeConstraints);
    }
    baseQuery=new SpatialQuery(constraints,geometry,geoCompareOp);
  }
 else   if ((timeConstraintSet != null) && !timeConstraintSet.isEmpty()) {
    final TemporalConstraints temporalConstraints=QueryIndexHelper.getTemporalConstraintsForDescriptors(adapter.getTimeDescriptors(),timeConstraintSet);
    baseQuery=new TemporalQuery(temporalConstraints);
  }
  if (attrs.isEmpty()) {
    return baseQuery;
  }
 else {
    return new CQLQuery(baseQuery,cqlFilter,adapter);
  }
}