code: func (fs *FileSorter) externalSort() (*comparableRow, error) {
  	if !fs.fetched {
  		if len(fs.buf) > 0 {
  			err := fs.flushToFile()
  			if err != nil {
  				return nil, errors.Trace(err)
  			}
  		}
  
  		heap.Init(fs.rowHeap)
  		if fs.rowHeap.err != nil {
  			return nil, errors.Trace(fs.rowHeap.err)
  		}
  
  		err := fs.openAllFiles()
  		if err != nil {
  			return nil, errors.Trace(err)
  		}
  
  		for id := range fs.fds {
  			row, err := fs.fetchNextRow(id)
  			if err != nil {
  				return nil, errors.Trace(err)
  			}
  			if row == nil {
  				return nil, errors.New("file is empty")
  			}
  
  			im := &item{
  				index: id,
  				value: row,
  			}
  
  			heap.Push(fs.rowHeap, im)
  			if fs.rowHeap.err != nil {
  				return nil, errors.Trace(fs.rowHeap.err)
  			}
  		}
  
  		fs.fetched = true
  	}
  
  	if fs.rowHeap.Len() > 0 {
  		im := heap.Pop(fs.rowHeap).(*item)
  		if fs.rowHeap.err != nil {
  			return nil, errors.Trace(fs.rowHeap.err)
  		}
  
  		row, err := fs.fetchNextRow(im.index)
  		if err != nil {
  			return nil, errors.Trace(err)
  		}
  		if row != nil {
  			im := &item{
  				index: im.index,
  				value: row,
  			}
  
  			heap.Push(fs.rowHeap, im)
  			if fs.rowHeap.err != nil {
  				return nil, errors.Trace(fs.rowHeap.err)
  			}
  		}
  
  		return im.value, nil
  	}
  
  	return nil, nil
  }
docs: // Perform external file sort.