Program: 31211
Project: 31211
Buggy source:
private RecordSource selectColumns0(final RecordSource recordSource,QueryModel model) throws ParserException {
  final ObjList<QueryColumn> columns=model.getColumns();
  final CharSequenceIntHashMap columnNameHistogram=model.getColumnNameHistogram();
  final RecordMetadata meta=recordSource.getMetadata();
  this.outerVirtualColumns.clear();
  this.aggregators.clear();
  this.selectedColumns.clear();
  this.selectedColumnAliases.clear();
  this.groupKeyColumns.clear();
  this.aggregateColumnSequence=0;
  this.analyticColumns.clear();
  ObjList<VirtualColumn> virtualColumns=null;
  try {
    for (int i=0, k=columns.size(); i < k; i++) {
      final QueryColumn qc=columns.getQuick(i);
      final ExprNode node=qc.getAst();
      final boolean analytic=qc instanceof AnalyticColumn;
      if (!analytic && node.type == ExprNode.LITERAL) {
        if (meta.getColumnIndexQuiet(node.token) == -1) {
          throw QueryError.invalidColumn(node.position,node.token);
        }
        if (columnNameHistogram.get(node.token) > 0) {
          throw QueryError.ambiguousColumn(node.position);
        }
        selectedColumns.add(node.token);
        addAlias(node.position,qc.getAlias() == null ? node.token : qc.getAlias());
        groupKeyColumns.add(node.token);
        continue;
      }
      if (qc.getAlias() == null) {
        qc.of(createAlias(aggregateColumnSequence++),node);
      }
      selectedColumns.add(qc.getAlias());
      addAlias(node.position,qc.getAlias());
      if (!analytic && node.type == ExprNode.FUNCTION && FunctionFactories.isAggregate(node.token)) {
        aggregators.add(qc);
        continue;
      }
      if (node.type == ExprNode.OPERATION || node.type == ExprNode.FUNCTION) {
        int beforeSplit=aggregators.size();
        splitAggregates(node,aggregators);
        if (beforeSplit < aggregators.size()) {
          outerVirtualColumns.add(qc);
          continue;
        }
      }
      if (analytic) {
        if (qc.getAst().type != ExprNode.FUNCTION) {
          throw QueryError.$(qc.getAst().position,"String_Node_Str");
        }
        if (aggregators.size() > 0) {
          throw QueryError.$(qc.getAst().position,"String_Node_Str");
        }
        AnalyticColumn ac=(AnalyticColumn)qc;
        analyticColumns.add(ac);
      }
 else {
        if (virtualColumns == null) {
          virtualColumns=new ObjList<>();
        }
        VirtualColumn vc=virtualColumnBuilder.createVirtualColumn(model,qc.getAst(),recordSource.getMetadata());
        vc.setName(qc.getAlias());
        virtualColumns.add(vc);
        groupKeyColumns.add(qc.getAlias());
      }
    }
  }
 catch (  ParserException e) {
    Misc.free(recordSource);
    throw e;
  }
  RecordSource rs=recordSource;
  if (virtualColumns != null) {
    rs=new VirtualColumnRecordSource(rs,virtualColumns);
  }
  if (aggregators.size() > 0) {
    rs=compileAggregates(rs,model);
  }
 else {
    ExprNode sampleBy=model.getSampleBy();
    if (sampleBy != null) {
      Misc.free(rs);
      throw QueryError.$(sampleBy.position,"String_Node_Str");
    }
  }
  if (outerVirtualColumns.size() > 0) {
    rs=compileOuterVirtualColumns(rs,model);
  }
  if (analyticColumns.size() > 0) {
    rs=compileAnalytic(rs,model);
  }
  if (selectedColumns.size() > 0) {
    rs=new SelectedColumnsRecordSource(rs,selectedColumns,selectedColumnAliases);
  }
  return rs;
}

Fixed version (ground truth):
private RecordSource selectColumns0(final RecordSource recordSource,QueryModel model) throws ParserException {
  final ObjList<QueryColumn> columns=model.getColumns();
  final CharSequenceIntHashMap columnNameHistogram=model.getColumnNameHistogram();
  final RecordMetadata meta=recordSource.getMetadata();
  this.outerVirtualColumns.clear();
  this.innerVirtualColumn.clear();
  this.aggregators.clear();
  this.selectedColumns.clear();
  this.selectedColumnAliases.clear();
  this.groupKeyColumns.clear();
  this.aggregateColumnSequence=0;
  this.analyticColumns.clear();
  RecordSource rs=recordSource;
  try {
    for (int i=0, k=columns.size(); i < k; i++) {
      final QueryColumn qc=columns.getQuick(i);
      final ExprNode node=qc.getAst();
      final boolean analytic=qc instanceof AnalyticColumn;
      if (!analytic && node.type == ExprNode.LITERAL) {
        if (meta.getColumnIndexQuiet(node.token) == -1) {
          throw QueryError.invalidColumn(node.position,node.token);
        }
        if (columnNameHistogram.get(node.token) > 0) {
          throw QueryError.ambiguousColumn(node.position);
        }
        selectedColumns.add(node.token);
        addAlias(node.position,qc.getName());
        groupKeyColumns.add(node.token);
        continue;
      }
      if (qc.getAlias() == null) {
        qc.of(createAlias(aggregateColumnSequence++),node.position,node);
      }
      selectedColumns.add(qc.getAlias());
      addAlias(node.position,qc.getAlias());
      if (!analytic && node.type == ExprNode.FUNCTION && FunctionFactories.isAggregate(node.token)) {
        aggregators.add(qc);
        continue;
      }
      if (node.type == ExprNode.OPERATION || node.type == ExprNode.FUNCTION) {
        int beforeSplit=aggregators.size();
        splitAggregates(node,aggregators);
        if (beforeSplit < aggregators.size()) {
          outerVirtualColumns.add(qc);
          continue;
        }
      }
      if (analytic) {
        if (qc.getAst().type != ExprNode.FUNCTION) {
          throw QueryError.$(qc.getAst().position,"String_Node_Str");
        }
        if (aggregators.size() > 0) {
          throw QueryError.$(qc.getAst().position,"String_Node_Str");
        }
        AnalyticColumn ac=(AnalyticColumn)qc;
        analyticColumns.add(ac);
      }
 else {
        innerVirtualColumn.add(qc);
      }
    }
    if (innerVirtualColumn.size() > 0) {
      ObjList<VirtualColumn> virtualColumns=new ObjList<>();
      final String timestampName;
      if (model.getSampleBy() != null) {
        timestampName=meta.getColumnName(getTimestampIndex(model,model.getTimestamp(),meta));
      }
 else {
        timestampName=null;
      }
      for (int i=0, n=innerVirtualColumn.size(); i < n; i++) {
        QueryColumn qc=innerVirtualColumn.getQuick(i);
        if (Chars.equalsNc(qc.getAlias(),timestampName)) {
          throw QueryError.$(qc.getAliasPosition() == -1 ? qc.getAst().position : qc.getAliasPosition(),"String_Node_Str");
        }
        VirtualColumn vc=virtualColumnBuilder.createVirtualColumn(model,qc.getAst(),meta);
        vc.setName(qc.getAlias());
        virtualColumns.add(vc);
        groupKeyColumns.add(qc.getAlias());
      }
      rs=new VirtualColumnRecordSource(rs,virtualColumns);
    }
    if (aggregators.size() > 0) {
      rs=compileAggregates(rs,model);
    }
 else {
      ExprNode sampleBy=model.getSampleBy();
      if (sampleBy != null) {
        throw QueryError.$(sampleBy.position,"String_Node_Str");
      }
    }
    if (outerVirtualColumns.size() > 0) {
      rs=compileOuterVirtualColumns(rs,model);
    }
    if (analyticColumns.size() > 0) {
      rs=compileAnalytic(rs,model);
    }
    if (selectedColumns.size() > 0) {
      rs=new SelectedColumnsRecordSource(rs,selectedColumns,selectedColumnAliases);
    }
    return rs;
  }
 catch (  ParserException e) {
    Misc.free(rs);
    throw e;
  }
}