}
}
public void mergeAndSpill() throws SchemaChangeException {
logger.debug("Copier allocator current allocation {}", copierAllocator.getAllocatedMemory());
VectorContainer outputContainer = new VectorContainer();
List<BatchGroup> batchGroupList = Lists.newArrayList();
int batchCount = batchGroups.size();
for (int i = 0; i < batchCount / 2; i++) {
if (batchGroups.size() == 0) {
break;
}
if (batchGroups.peekLast().getSv2() == null) {
break;
}
BatchGroup batch = batchGroups.pollLast();
batchGroupList.add(batch);
long bufferSize = getBufferSize(batch);
totalSizeInMemory -= bufferSize;
}
if (batchGroupList.size() == 0) {
return;
}
int estimatedRecordSize = 0;
for (VectorWrapper w : batchGroups.get(0)) {
try {
estimatedRecordSize += TypeHelper.getSize(w.getField().getType());
} catch (UnsupportedOperationException e) {
estimatedRecordSize += 50;
}
}
int targetRecordCount = Math.max(1, 250 * 1000 / estimatedRecordSize);
VectorContainer hyperBatch = constructHyperBatch(batchGroupList);
createCopier(hyperBatch, batchGroupList, outputContainer);
int count = copier.next(targetRecordCount);
assert count > 0;
VectorContainer c1 = VectorContainer.getTransferClone(outputContainer);
c1.buildSchema(BatchSchema.SelectionVectorMode.NONE);
c1.setRecordCount(count);
String outputFile = String.format(Utilities.getFileNameForQueryFragment(context, dirs.next(), "spill" + uid + "_" + spillCount++));
BatchGroup newGroup = new BatchGroup(c1, fs, outputFile, oContext.getAllocator());
try {