Program: 171038
Project: 171038
Buggy source:
@Override @GraphTransaction public AtlasSearchResult searchUsingBasicQuery(String query,String typeName,String classification,String attrName,String attrValuePrefix,boolean excludeDeletedEntities,int limit,int offset) throws AtlasBaseException {
  AtlasSearchResult ret=new AtlasSearchResult(AtlasQueryType.BASIC);
  if (LOG.isDebugEnabled()) {
    LOG.debug("String_Node_Str",query,typeName,classification);
  }
  final QueryParams params=validateSearchParams(limit,offset);
  Set<String> typeNames=null;
  Set<String> classificationNames=null;
  String attrQualifiedName=null;
  if (StringUtils.isNotEmpty(typeName)) {
    AtlasEntityType entityType=typeRegistry.getEntityTypeByName(typeName);
    if (entityType == null) {
      throw new AtlasBaseException(UNKNOWN_TYPENAME,typeName);
    }
    typeNames=entityType.getTypeAndAllSubTypes();
    ret.setType(typeName);
  }
  if (StringUtils.isNotEmpty(classification)) {
    AtlasClassificationType classificationType=typeRegistry.getClassificationTypeByName(classification);
    if (classificationType == null) {
      throw new AtlasBaseException(CLASSIFICATION_NOT_FOUND,classification);
    }
    classificationNames=classificationType.getTypeAndAllSubTypes();
    ret.setClassification(classification);
  }
  boolean isAttributeSearch=StringUtils.isNotEmpty(attrName) || StringUtils.isNotEmpty(attrValuePrefix);
  boolean isGuidPrefixSearch=false;
  if (isAttributeSearch) {
    AtlasEntityType entityType=typeRegistry.getEntityTypeByName(typeName);
    ret.setQueryType(AtlasQueryType.ATTRIBUTE);
    if (entityType != null) {
      AtlasAttribute attribute=null;
      if (StringUtils.isNotEmpty(attrName)) {
        attribute=entityType.getAttribute(attrName);
        if (attribute == null) {
          throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_ATTRIBUTE,attrName,typeName);
        }
      }
 else {
        final List<String> defaultAttrNames=new ArrayList<>(Arrays.asList("String_Node_Str","String_Node_Str"));
        Iterator<String> iter=defaultAttrNames.iterator();
        while (iter.hasNext() && attribute == null) {
          attrName=iter.next();
          attribute=entityType.getAttribute(attrName);
        }
      }
      if (attribute == null) {
        isGuidPrefixSearch=true;
        query=null;
      }
 else {
        attrQualifiedName=attribute.getQualifiedName();
        String attrQuery=String.format("String_Node_Str",attrName,attrValuePrefix.replaceAll("String_Node_Str","String_Node_Str"));
        query=StringUtils.isEmpty(query) ? attrQuery : String.format("String_Node_Str",query,attrQuery);
      }
    }
    if (LOG.isDebugEnabled()) {
      LOG.debug("String_Node_Str",attrName,attrValuePrefix);
    }
  }
  if (StringUtils.isNotEmpty(query)) {
    final String idxQuery=getQueryForFullTextSearch(query,typeName,classification);
    final int startIdx=params.offset();
    final int resultSize=params.limit();
    int resultIdx=0;
    for (int indexQueryOffset=0; ; indexQueryOffset+=getMaxResultSetSize()) {
      final Iterator<Result<?,?>> qryResult=graph.indexQuery(Constants.FULLTEXT_INDEX,idxQuery,indexQueryOffset).vertices();
      if (LOG.isDebugEnabled()) {
        LOG.debug("String_Node_Str" + idxQuery + "String_Node_Str"+ indexQueryOffset);
      }
      if (!qryResult.hasNext()) {
        break;
      }
      while (qryResult.hasNext()) {
        AtlasVertex<?,?> vertex=qryResult.next().getVertex();
        String vertexTypeName=GraphHelper.getTypeName(vertex);
        if (StringUtils.isEmpty(vertexTypeName) || StringUtils.isEmpty(GraphHelper.getGuid(vertex))) {
          continue;
        }
        if (typeNames != null && !typeNames.contains(vertexTypeName)) {
          continue;
        }
        if (classificationNames != null) {
          List<String> traitNames=GraphHelper.getTraitNames(vertex);
          if (CollectionUtils.isEmpty(traitNames) || !CollectionUtils.containsAny(classificationNames,traitNames)) {
            continue;
          }
        }
        if (isAttributeSearch) {
          String vertexAttrValue=vertex.getProperty(attrQualifiedName,String.class);
          if (StringUtils.isNotEmpty(vertexAttrValue) && !vertexAttrValue.startsWith(attrValuePrefix)) {
            continue;
          }
        }
        if (skipDeletedEntities(excludeDeletedEntities,vertex)) {
          continue;
        }
        resultIdx++;
        if (resultIdx <= startIdx) {
          continue;
        }
        AtlasEntityHeader header=entityRetriever.toAtlasEntityHeader(vertex);
        ret.addEntity(header);
        if (ret.getEntities().size() == resultSize) {
          break;
        }
      }
      if (ret.getEntities() != null && ret.getEntities().size() == resultSize) {
        break;
      }
    }
  }
 else {
    final Map<String,Object> bindings=new HashMap<>();
    String basicQuery="String_Node_Str";
    if (classificationNames != null) {
      bindings.put("String_Node_Str",classificationNames);
      basicQuery+=gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_CLASSIFICATION_FILTER);
    }
    if (typeNames != null) {
      bindings.put("String_Node_Str",typeNames);
      basicQuery+=gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_TYPE_FILTER);
    }
    if (excludeDeletedEntities) {
      bindings.put("String_Node_Str",ACTIVE.toString());
      basicQuery+=gremlinQueryProvider.getQuery(BASIC_SEARCH_STATE_FILTER);
    }
    if (isGuidPrefixSearch) {
      bindings.put("String_Node_Str",attrValuePrefix + "String_Node_Str");
      basicQuery+=gremlinQueryProvider.getQuery(AtlasGremlinQuery.GUID_PREFIX_FILTER);
    }
    bindings.put("String_Node_Str",params.offset());
    bindings.put("String_Node_Str",params.offset() + params.limit());
    basicQuery+=gremlinQueryProvider.getQuery(TO_RANGE_LIST);
    ScriptEngine scriptEngine=graph.getGremlinScriptEngine();
    try {
      Object result=graph.executeGremlinScript(scriptEngine,bindings,basicQuery,false);
      if (result instanceof List && CollectionUtils.isNotEmpty((List)result)) {
        List queryResult=(List)result;
        Object firstElement=queryResult.get(0);
        if (firstElement instanceof AtlasVertex) {
          for (          Object element : queryResult) {
            if (element instanceof AtlasVertex) {
              ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex)element));
            }
 else {
              LOG.warn("String_Node_Str",basicQuery,element);
            }
          }
        }
      }
    }
 catch (    ScriptException e) {
      throw new AtlasBaseException(DISCOVERY_QUERY_FAILED,basicQuery);
    }
 finally {
      graph.releaseGremlinScriptEngine(scriptEngine);
    }
  }
  return ret;
}

Fixed version (ground truth):
@Override @GraphTransaction public AtlasSearchResult searchUsingBasicQuery(String query,String typeName,String classification,String attrName,String attrValuePrefix,boolean excludeDeletedEntities,int limit,int offset) throws AtlasBaseException {
  AtlasSearchResult ret=new AtlasSearchResult(AtlasQueryType.BASIC);
  if (LOG.isDebugEnabled()) {
    LOG.debug("String_Node_Str",query,typeName,classification);
  }
  final QueryParams params=QueryParams.getNormalizedParams(limit,offset);
  Set<String> typeNames=null;
  Set<String> classificationNames=null;
  String attrQualifiedName=null;
  if (StringUtils.isNotEmpty(typeName)) {
    AtlasEntityType entityType=typeRegistry.getEntityTypeByName(typeName);
    if (entityType == null) {
      throw new AtlasBaseException(UNKNOWN_TYPENAME,typeName);
    }
    typeNames=entityType.getTypeAndAllSubTypes();
    ret.setType(typeName);
  }
  if (StringUtils.isNotEmpty(classification)) {
    AtlasClassificationType classificationType=typeRegistry.getClassificationTypeByName(classification);
    if (classificationType == null) {
      throw new AtlasBaseException(CLASSIFICATION_NOT_FOUND,classification);
    }
    classificationNames=classificationType.getTypeAndAllSubTypes();
    ret.setClassification(classification);
  }
  boolean isAttributeSearch=StringUtils.isNotEmpty(attrName) || StringUtils.isNotEmpty(attrValuePrefix);
  boolean isGuidPrefixSearch=false;
  if (isAttributeSearch) {
    AtlasEntityType entityType=typeRegistry.getEntityTypeByName(typeName);
    ret.setQueryType(AtlasQueryType.ATTRIBUTE);
    if (entityType != null) {
      AtlasAttribute attribute=null;
      if (StringUtils.isNotEmpty(attrName)) {
        attribute=entityType.getAttribute(attrName);
        if (attribute == null) {
          throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_ATTRIBUTE,attrName,typeName);
        }
      }
 else {
        final List<String> defaultAttrNames=new ArrayList<>(Arrays.asList("String_Node_Str","String_Node_Str"));
        Iterator<String> iter=defaultAttrNames.iterator();
        while (iter.hasNext() && attribute == null) {
          attrName=iter.next();
          attribute=entityType.getAttribute(attrName);
        }
      }
      if (attribute == null) {
        isGuidPrefixSearch=true;
        query=null;
      }
 else {
        attrQualifiedName=attribute.getQualifiedName();
        String attrQuery=String.format("String_Node_Str",attrName,attrValuePrefix.replaceAll("String_Node_Str","String_Node_Str"));
        query=StringUtils.isEmpty(query) ? attrQuery : String.format("String_Node_Str",query,attrQuery);
      }
    }
    if (LOG.isDebugEnabled()) {
      LOG.debug("String_Node_Str",attrName,attrValuePrefix);
    }
  }
  if (StringUtils.isNotEmpty(query)) {
    final String idxQuery=getQueryForFullTextSearch(query,typeName,classification);
    final int startIdx=params.offset();
    final int resultSize=params.limit();
    int resultIdx=0;
    for (int indexQueryOffset=0; ; indexQueryOffset+=getMaxResultSetSize()) {
      final Iterator<Result<?,?>> qryResult=graph.indexQuery(Constants.FULLTEXT_INDEX,idxQuery,indexQueryOffset).vertices();
      if (LOG.isDebugEnabled()) {
        LOG.debug("String_Node_Str" + idxQuery + "String_Node_Str"+ indexQueryOffset);
      }
      if (!qryResult.hasNext()) {
        break;
      }
      while (qryResult.hasNext()) {
        AtlasVertex<?,?> vertex=qryResult.next().getVertex();
        String vertexTypeName=GraphHelper.getTypeName(vertex);
        if (StringUtils.isEmpty(vertexTypeName) || StringUtils.isEmpty(GraphHelper.getGuid(vertex))) {
          continue;
        }
        if (typeNames != null && !typeNames.contains(vertexTypeName)) {
          continue;
        }
        if (classificationNames != null) {
          List<String> traitNames=GraphHelper.getTraitNames(vertex);
          if (CollectionUtils.isEmpty(traitNames) || !CollectionUtils.containsAny(classificationNames,traitNames)) {
            continue;
          }
        }
        if (isAttributeSearch) {
          String vertexAttrValue=vertex.getProperty(attrQualifiedName,String.class);
          if (StringUtils.isNotEmpty(vertexAttrValue) && !vertexAttrValue.startsWith(attrValuePrefix)) {
            continue;
          }
        }
        if (skipDeletedEntities(excludeDeletedEntities,vertex)) {
          continue;
        }
        resultIdx++;
        if (resultIdx <= startIdx) {
          continue;
        }
        AtlasEntityHeader header=entityRetriever.toAtlasEntityHeader(vertex);
        ret.addEntity(header);
        if (ret.getEntities().size() == resultSize) {
          break;
        }
      }
      if (ret.getEntities() != null && ret.getEntities().size() == resultSize) {
        break;
      }
    }
  }
 else {
    final Map<String,Object> bindings=new HashMap<>();
    String basicQuery="String_Node_Str";
    if (classificationNames != null) {
      bindings.put("String_Node_Str",classificationNames);
      basicQuery+=gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_CLASSIFICATION_FILTER);
    }
    if (typeNames != null) {
      bindings.put("String_Node_Str",typeNames);
      basicQuery+=gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_TYPE_FILTER);
    }
    if (excludeDeletedEntities) {
      bindings.put("String_Node_Str",ACTIVE.toString());
      basicQuery+=gremlinQueryProvider.getQuery(BASIC_SEARCH_STATE_FILTER);
    }
    if (isGuidPrefixSearch) {
      bindings.put("String_Node_Str",attrValuePrefix + "String_Node_Str");
      basicQuery+=gremlinQueryProvider.getQuery(AtlasGremlinQuery.GUID_PREFIX_FILTER);
    }
    bindings.put("String_Node_Str",params.offset());
    bindings.put("String_Node_Str",params.offset() + params.limit());
    basicQuery+=gremlinQueryProvider.getQuery(TO_RANGE_LIST);
    ScriptEngine scriptEngine=graph.getGremlinScriptEngine();
    try {
      Object result=graph.executeGremlinScript(scriptEngine,bindings,basicQuery,false);
      if (result instanceof List && CollectionUtils.isNotEmpty((List)result)) {
        List queryResult=(List)result;
        Object firstElement=queryResult.get(0);
        if (firstElement instanceof AtlasVertex) {
          for (          Object element : queryResult) {
            if (element instanceof AtlasVertex) {
              ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex)element));
            }
 else {
              LOG.warn("String_Node_Str",basicQuery,element);
            }
          }
        }
      }
    }
 catch (    ScriptException e) {
      throw new AtlasBaseException(DISCOVERY_QUERY_FAILED,basicQuery);
    }
 finally {
      graph.releaseGremlinScriptEngine(scriptEngine);
    }
  }
  return ret;
}