Program: 64283
Project: 64283
Buggy source:
/** 
 * INTERNAL: Check if new expression need to be created for sub queries and re-normalized.
 */
protected Expression checkJoinForSubSelectWithParent(ExpressionNormalizer normalizer){
  SQLSelectStatement statement=normalizer.getStatement();
  if (!isClonedForSubQuery && statement.isSubSelect() && statement.getParentStatement().getBuilder().equals(getBuilder())) {
    if (baseExpression.isQueryKeyExpression()) {
      QueryKeyExpression baseQueryKeyExpression=(QueryKeyExpression)baseExpression;
      if (baseQueryKeyExpression.hasBeenNormalized()) {
        return null;
      }
      DatabaseMapping mapping=baseQueryKeyExpression.getMapping();
      if (mapping != null && mapping.isOneToOneMapping()) {
        if (statement.getOptimizedClonedExpressions().containsKey(this)) {
          return statement.getOptimizedClonedExpressions().get(this);
        }
        QueryKeyExpression clonedBaseExpression=null;
        if (baseQueryKeyExpression.getBaseExpression().isQueryKeyExpression()) {
          DatabaseMapping basebaseExprMapping=((QueryKeyExpression)baseQueryKeyExpression.getBaseExpression()).getMapping();
          if (basebaseExprMapping != null && basebaseExprMapping.isOneToOneMapping()) {
            clonedBaseExpression=(QueryKeyExpression)baseQueryKeyExpression.normalize(normalizer);
          }
        }
        if (clonedBaseExpression == null) {
          clonedBaseExpression=new QueryKeyExpression(baseQueryKeyExpression.getName(),baseQueryKeyExpression.getBaseExpression());
          clonedBaseExpression.shouldQueryToManyRelationship=baseQueryKeyExpression.shouldQueryToManyRelationship;
          clonedBaseExpression.shouldUseOuterJoin=baseQueryKeyExpression.shouldUseOuterJoin;
          clonedBaseExpression.hasQueryKey=baseQueryKeyExpression.hasQueryKey;
          clonedBaseExpression.hasMapping=baseQueryKeyExpression.hasMapping;
          clonedBaseExpression.isClonedForSubQuery=true;
          clonedBaseExpression=(QueryKeyExpression)clonedBaseExpression.normalize(normalizer);
          statement.addOptimizedClonedExpressions(baseQueryKeyExpression,clonedBaseExpression);
        }
        QueryKeyExpression clonedExpression=new QueryKeyExpression(name,clonedBaseExpression);
        clonedExpression.shouldQueryToManyRelationship=this.shouldQueryToManyRelationship;
        clonedExpression.shouldUseOuterJoin=this.shouldUseOuterJoin;
        clonedExpression.hasQueryKey=this.hasQueryKey;
        clonedExpression.hasMapping=this.hasMapping;
        clonedExpression.isClonedForSubQuery=true;
        clonedExpression=(QueryKeyExpression)clonedExpression.normalize(normalizer);
        statement.addOptimizedClonedExpressions(this,clonedExpression);
        return clonedExpression;
      }
    }
  }
  return null;
}

Fixed version (ground truth):
/** 
 * INTERNAL: Check if new expression need to be created for sub queries and re-normalized.
 */
protected Expression checkJoinForSubSelectWithParent(ExpressionNormalizer normalizer,Expression base,List<Expression> foreignKeyJoinPointer){
  SQLSelectStatement statement=normalizer.getStatement();
  if (!isClonedForSubQuery && statement.isSubSelect() && statement.getParentStatement().getBuilder().equals(getBuilder())) {
    if (baseExpression.isQueryKeyExpression()) {
      QueryKeyExpression baseQueryKeyExpression=(QueryKeyExpression)baseExpression;
      DatabaseMapping mapping=baseQueryKeyExpression.getMapping();
      if (mapping != null && mapping.isOneToOneMapping()) {
        if (statement.getOptimizedClonedExpressions().containsKey(this)) {
          return statement.getOptimizedClonedExpressions().get(this);
        }
        QueryKeyExpression clonedBaseExpression=null;
        if (baseQueryKeyExpression.hasBeenNormalized()) {
          clonedBaseExpression=(QueryKeyExpression)baseQueryKeyExpression.normalize(normalizer);
        }
        if (clonedBaseExpression == null && baseQueryKeyExpression.getBaseExpression().isQueryKeyExpression()) {
          DatabaseMapping basebaseExprMapping=((QueryKeyExpression)baseQueryKeyExpression.getBaseExpression()).getMapping();
          if (basebaseExprMapping != null && basebaseExprMapping.isOneToOneMapping()) {
            clonedBaseExpression=(QueryKeyExpression)baseQueryKeyExpression.normalize(normalizer);
          }
        }
        if (clonedBaseExpression == null) {
          clonedBaseExpression=new QueryKeyExpression(baseQueryKeyExpression.getName(),baseQueryKeyExpression.getBaseExpression());
          clonedBaseExpression.shouldQueryToManyRelationship=baseQueryKeyExpression.shouldQueryToManyRelationship;
          clonedBaseExpression.shouldUseOuterJoin=baseQueryKeyExpression.shouldUseOuterJoin;
          clonedBaseExpression.hasQueryKey=baseQueryKeyExpression.hasQueryKey;
          clonedBaseExpression.hasMapping=baseQueryKeyExpression.hasMapping;
          clonedBaseExpression.isAttributeExpression=baseQueryKeyExpression.isAttributeExpression;
          clonedBaseExpression.isClonedForSubQuery=true;
          clonedBaseExpression=(QueryKeyExpression)clonedBaseExpression.normalize(normalizer);
          statement.addOptimizedClonedExpressions(baseQueryKeyExpression,clonedBaseExpression);
        }
        QueryKeyExpression clonedExpression=new QueryKeyExpression(name,clonedBaseExpression);
        clonedExpression.shouldQueryToManyRelationship=this.shouldQueryToManyRelationship;
        clonedExpression.shouldUseOuterJoin=this.shouldUseOuterJoin;
        clonedExpression.hasQueryKey=this.hasQueryKey;
        clonedExpression.hasMapping=this.hasMapping;
        clonedExpression.isAttributeExpression=this.isAttributeExpression;
        clonedExpression.isClonedForSubQuery=true;
        if (base == this) {
          clonedExpression=(QueryKeyExpression)clonedExpression.normalize(normalizer,clonedExpression,foreignKeyJoinPointer);
        }
 else {
          clonedExpression=(QueryKeyExpression)clonedExpression.normalize(normalizer,base,foreignKeyJoinPointer);
        }
        statement.addOptimizedClonedExpressions(this,clonedExpression);
        return clonedExpression;
      }
    }
  }
  return null;
}