aloneguid / parquet-dotnet

Fully managed Apache Parquet implementation
https://aloneguid.github.io/parquet-dotnet/
MIT License
542 stars 140 forks source link

Feature request: add API for 2 step group writting #484

Open i-sinister opened 4 months ago

i-sinister commented 4 months ago

Issue description

I have to save a large dataset with many groups each having 20M rows and currently it takes too much time. I think it would improve overall speed if appending a group would be 'pipelined' by separating into 2 steps:

  1. preparing data to write
  2. appending to the file

Current parquet-dotnet implementation already uses 2 separate memory streams while saving content of the column (DataColumnWriter.WriteColumnAsync):

  1. copy data from column array into memory stream
  2. copy memory stream into byte array and compress into another byte array
  3. append compressed byte array to the parquet "file"

Whole process could be 'pipelined':

  1. application starts a thread that prepares serialized compressed data and metadata for columns
  2. application starts another thread that appends prepared column data to the parquet file:

Something like (BlockingCollection is an overkill here ofcourse):

using (var extendedGroupWriter = parquetWriter.CreateExtendedGroupWriter(...))
{
  var queue = new BlockingCollection();
  var prepareTask = Task.Factory.StartNew(
    () =>
    {
       foreach (var partition in GetLargePartitionedDataSet())
       {
         var preparedgroup = extendedGroupWriter
           .StartGroupPreparation()
           // serializing and compressing data for field1
           .Append(field1, partition.Select(...))
          // serializing and compressing data for field2
           .Append(field2, partition.Select(...))
          // serializing and compressing data for field3
           .Append(field3, partition.Select(...))
           .PrepareGroupData();
         queue.Add(preparedGroup);
       }
       queue.CompleteAdding();
    });
  var writeTask = Task.Factory.StartNew(
    () =>
    {
      while (!queue.IsCompleted)
      {
      using var preparedGroup = queue.Take(cancellationToken);
          parquetWriter.WritePreparedGroup(preparedGroup);
      }
    });
  await writeTask;
}

Or serialization and compression could be separate steps and compression could be done directly into the target stead, without using extra memory stream.

These same approach could be used on the column level:

  1. first thread prepares data for the column
  2. second thread appends column data as soon as it is ready