code: public static ColumnarBatch toBatch(
        StructType schema, MemoryMode memMode, Iterator<Row> row) {
      int capacity = 4 * 1024;
      WritableColumnVector[] columnVectors;
      if (memMode == MemoryMode.OFF_HEAP) {
        columnVectors = OffHeapColumnVector.allocateColumns(capacity, schema);
      } else {
        columnVectors = OnHeapColumnVector.allocateColumns(capacity, schema);
      }
  
      int n = 0;
      while (row.hasNext()) {
        Row r = row.next();
        for (int i = 0; i < schema.fields().length; i++) {
          appendValue(columnVectors[i], schema.fields()[i].dataType(), r, i);
        }
        n++;
      }
      ColumnarBatch batch = new ColumnarBatch(columnVectors);
      batch.setNumRows(n);
      return batch;
    }
docs: Converts an iterator of rows into a single ColumnBatch.