Source:
protected QueryExpression itrArrayMatchExpression(ArrayMatchExpression q, Path context) { LOGGER.debug("Rewriting {}",q); Path arrayName=context.isEmpty()?q.getArray():new Path(context,q.getArray()); ResolvedFieldInfo arrayDescendant=null; for(ResolvedFieldInfo rfi:conjunct.getFieldInfo()) { Path p=rfi.getFieldName(); if(arrayName.matchingPrefix(p)) { arrayDescendant=rfi; break; } } if(arrayDescendant==null) throw new RuntimeException("Unknown field:"+arrayName); LOGGER.debug("Array descendant:{}",arrayDescendant); CompositeMetadata fmd=arrayDescendant.getFieldEntityCompositeMetadata(); while(fmd!=null&&fmd.getEntityPath().numSegments()>arrayName.numSegments()) fmd=fmd.getParent(); CompositeMetadata arrayMd=fmd; LOGGER.debug("Array metadata:{}",(arrayMd == null ? null : arrayMd.getName())); Path absoluteArray; if(conjunct.isRequestQuery()) { absoluteArray=arrayName; } else { absoluteArray=(arrayMd!=null&&arrayMd.getParent()!=null)?new Path(arrayMd.getEntityPath(),new Path(Path.ANYPATH,arrayName)):arrayName; } LOGGER.debug("Absolute array:{}",absoluteArray); if(arrayMd==thisMd&&!absoluteArray.equals(thisMd.getEntityPath())) { Path arrayPath = null; if(arrayMd!=null&&arrayMd.getParent()==null) { arrayPath=absoluteArray; } else if (arrayMd!=null) { arrayPath=absoluteArray.suffix(-(arrayMd.getEntityPath().numSegments()+1)); } QueryExpression newq = iterate(q.getElemMatch(), new Path(new Path(context, arrayPath), Path.ANYPATH)); return new ArrayMatchExpression(arrayPath,newq); } else { int delta=thisMd.getEntityPath().numSegments() - absoluteArray.numSegments(); relativeSuffixLength+=delta; QueryExpression ret=iterate(q.getElemMatch(),context); relativeSuffixLength-=delta; return ret; } }

Target:
protected QueryExpression itrArrayMatchExpression(ArrayMatchExpression q, Path context) { LOGGER.debug("Rewriting {}",q); Path arrayName=context.isEmpty()?q.getArray():new Path(context,q.getArray()); ResolvedFieldInfo arrayDescendant=null; for(ResolvedFieldInfo rfi:conjunct.getFieldInfo()) { Path p=rfi.getFieldName(); if(arrayName.matchingPrefix(p)) { arrayDescendant=rfi; break; } } if(arrayDescendant==null) throw new RuntimeException("Unknown field:"+arrayName); LOGGER.debug("Array descendant:{}",arrayDescendant); CompositeMetadata fmd=arrayDescendant.getFieldEntityCompositeMetadata(); while(fmd!=null&&fmd.getEntityPath().numSegments()>arrayName.numSegments()) fmd=fmd.getParent(); CompositeMetadata arrayMd=fmd; LOGGER.debug("Array metadata:{}",(arrayMd == null ? null : arrayMd.getName())); Path absoluteArray; if(conjunct.isRequestQuery()) { absoluteArray=arrayName; } else { absoluteArray=(arrayMd!=null&&arrayMd.getParent()!=null)?new Path(arrayMd.getEntityPath(),new Path(Path.ANYPATH,arrayName)):arrayName; } LOGGER.debug("Absolute array:{}",absoluteArray); if(arrayMd==thisMd&&!absoluteArray.equals(thisMd.getEntityPath())) { Path arrayPath = null; if(arrayMd.getParent()==null) { arrayPath=absoluteArray; } else { arrayPath=absoluteArray.suffix(-(arrayMd.getEntityPath().numSegments()+1)); } QueryExpression newq = iterate(q.getElemMatch(), new Path(new Path(context, arrayPath), Path.ANYPATH)); return new ArrayMatchExpression(arrayPath,newq); } else { int delta=thisMd.getEntityPath().numSegments() - absoluteArray.numSegments(); relativeSuffixLength+=delta; QueryExpression ret=iterate(q.getElemMatch(),context); relativeSuffixLength-=delta; return ret; } }