forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apacheGH-32240: [C#] Support decompression of IPC format buffers (apa…
…che#33603) # Which issue does this PR close? Closes apache#32240 / https://issues.apache.org/jira/browse/ARROW-16921 # What changes are included in this PR? This PR implements decompression support for Arrow IPC format files and streams in the dotnet/C# library. The main concern raised in the above Jira issue was that we don't want to add new NuGet package dependencies to support decompression formats that won't be needed by most users, so a default `CompressionProvider` implementation has been added that uses reflection to use the `ZstdNet` package for ZSTD decompression and `K4os.Compression.LZ4.Streams` and `CommunityToolkit.HighPerformance` for LZ4 Frame support if they are available. The `netstandard1.3` target has decompression support disabled due to some reflection functionality being missing, and neither `ZstdNet` or `K4os.Compression.LZ4.Streams` support `netstandard1.3`. The `ArrowFileReader` and `ArrowStreamReader` constructors accept an `ICompressionProvider` parameter to allow users to provide their own compression provider if they want to use different dependencies. ### Alternatives to consider An alternative approach that could be considered instead of reflection is to use these extra dependencies as build time dependencies but not make them dependencies of the NuGet package. I tested this out in adamreeve@4544afd and it seems to work reasonably well too but required bumping the version of `System.Runtime.CompilerServices.Unsafe` under the `netstandard2.0` and `netcoreapp3.1` targets. This reduces all the reflection boilerplate but seems pretty hacky as now Apache.Arrow.dll depends on these extra dlls and we rely on the dotnet runtime behaviour of not trying to load them until they're used. So I think the reflection approach is better. Another alternative would be to move decompression support into a separate NuGet package (eg. `Apache.Arrow.Compression`) that depends on `Apache.Arrow` and has an implementation of `ICompressionProvider` that users can pass in to the `ArrowFileReader` constructor, or maybe has a way to register itself with the `Apache.Arrow` package so it only needs to be configured once. That would seem cleaner to me but I'm not sure how much work it would be to set up a whole new package. # Are these changes tested? Yes, new unit tests have been added. Test files have been created with a Python script that is included in the PR due to only decompression support being added and not compression support. # Are there any user-facing changes? Yes, this implements a new feature but in a backwards compatible way. * Closes: apache#32240 Authored-by: Adam Reeve <adreeve@gmail.com> Signed-off-by: Will Jones <willjones127@gmail.com>
- Loading branch information
Showing
22 changed files
with
546 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace Apache.Arrow.Ipc | ||
{ | ||
public enum CompressionCodecType | ||
{ | ||
Lz4Frame, | ||
Zstd, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Buffers.Binary; | ||
using Apache.Arrow.Memory; | ||
|
||
namespace Apache.Arrow.Ipc | ||
{ | ||
/// <summary> | ||
/// Creates Arrow buffers from compressed data | ||
/// </summary> | ||
internal sealed class DecompressingBufferCreator : IBufferCreator | ||
{ | ||
private readonly ICompressionCodec _compressionCodec; | ||
private readonly MemoryAllocator _allocator; | ||
|
||
public DecompressingBufferCreator(ICompressionCodec compressionCodec, MemoryAllocator allocator) | ||
{ | ||
_compressionCodec = compressionCodec; | ||
_allocator = allocator; | ||
} | ||
|
||
public ArrowBuffer CreateBuffer(ReadOnlyMemory<byte> source) | ||
{ | ||
// See the BodyCompressionMethod enum in format/Message.fbs | ||
// for documentation on the Buffer compression method used here. | ||
|
||
if (source.Length < 8) | ||
{ | ||
throw new Exception($"Invalid compressed data buffer size ({source.Length}), expected at least 8 bytes"); | ||
} | ||
|
||
// First 8 bytes give the uncompressed data length | ||
var uncompressedLength = BinaryPrimitives.ReadInt64LittleEndian(source.Span.Slice(0, 8)); | ||
if (uncompressedLength == -1) | ||
{ | ||
// The buffer is not actually compressed | ||
return new ArrowBuffer(source.Slice(8)); | ||
} | ||
|
||
var outputData = _allocator.Allocate(Convert.ToInt32(uncompressedLength)); | ||
var decompressedLength = _compressionCodec.Decompress(source.Slice(8), outputData.Memory); | ||
if (decompressedLength != uncompressedLength) | ||
{ | ||
throw new Exception($"Expected to decompress {uncompressedLength} bytes, but got {decompressedLength} bytes"); | ||
} | ||
|
||
return new ArrowBuffer(outputData); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_compressionCodec.Dispose(); | ||
} | ||
} | ||
} |
Oops, something went wrong.