-
Notifications
You must be signed in to change notification settings - Fork 17
feat: Implement IPC RecordBatch body buffer compression #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
6ea12df
Define commpression registry
Djjanks 89436d8
Implement RecordBatch body decompression
Djjanks 4373d90
Export compressionType, Codec and compressionRegistry
Djjanks f1d2d31
Avoid copying decompressed data to contiguous ArrayBuffer
Djjanks 2146c61
Refactor: Move vector decompressed data reader to CompressedVectorLoader
Djjanks 0e79563
Refactor _CompressionRegistry
Djjanks 0cd7be2
Add codec encode validators
Djjanks e203bfa
Implement BodyCompression encode/decode to metadata message
Djjanks a060b93
Implement compression to RecordBatch writer
Djjanks ff32696
Refactor padding calculation for compressed Record Batch
Djjanks ae6fd40
Refactor RecordBatch writing to handle compression inline
Djjanks 81aa0b0
Add ZSTD validation
Djjanks e983b78
Fix buffer size calculation in _writeBodyBuffers
Djjanks b60969c
fix(ipc/writer): prevent compression with writeLegacyIpcFormat=true
Djjanks 4041d27
test(ipc/writer): add compression test to stream writer
Djjanks 2750ede
License header
Djjanks eb07473
feat(ipc/writer): add options to RecordBatchFileWriter constructor
Djjanks 1e43814
fix(ipc/writer): handle dictionary batch correctly when compression i…
Djjanks 0f1a11b
test(ipc/writer): add compression test to file writer
Djjanks 2363dc3
feat: implement dictionary compmression to writer and decompression t…
Djjanks efa38fe
feat: add Codec type to top-level export
Djjanks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| // 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. | ||
|
|
||
| export const LENGTH_NO_COMPRESSED_DATA = -1; | ||
| export const COMPRESS_LENGTH_PREFIX = 8; |
This file contains hidden or 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,46 @@ | ||
| // 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. | ||
|
|
||
| import { CompressionType } from '../../fb/compression-type.js'; | ||
| import { compressionValidators } from './validators.js'; | ||
|
|
||
| export interface Codec { | ||
| encode?(data: Uint8Array): Uint8Array; | ||
| decode?(data: Uint8Array): Uint8Array; | ||
| } | ||
|
|
||
| class _CompressionRegistry { | ||
| protected declare registry: { [key in CompressionType]?: Codec }; | ||
|
|
||
| constructor() { | ||
| this.registry = {}; | ||
| } | ||
|
|
||
| set(compression: CompressionType, codec: Codec) { | ||
| if (codec?.encode && typeof codec.encode === 'function' && !compressionValidators[compression].isValidCodecEncode(codec)) { | ||
| throw new Error(`Encoder for ${CompressionType[compression]} is not valid.`); | ||
| } | ||
| this.registry[compression] = codec; | ||
| } | ||
|
|
||
| get(compression: CompressionType): Codec | null { | ||
| return this.registry?.[compression] || null; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| export const compressionRegistry = new _CompressionRegistry(); |
This file contains hidden or 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,92 @@ | ||
| // 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. | ||
|
|
||
| import type { Codec } from './registry.ts'; | ||
| import { CompressionType } from '../../fb/compression-type.js'; | ||
|
|
||
| export interface CompressionValidator { | ||
| isValidCodecEncode(codec: Codec): boolean; | ||
| } | ||
|
|
||
| class Lz4FrameValidator implements CompressionValidator { | ||
| private readonly LZ4_FRAME_MAGIC = new Uint8Array([4, 34, 77, 24]); | ||
| private readonly MIN_HEADER_LENGTH = 7; // 4 (magic) + 2 (FLG + BD) + 1 (header checksum) = 7 min bytes | ||
|
|
||
| isValidCodecEncode(codec: Codec): boolean { | ||
| const testData = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); | ||
| const compressed = codec.encode!(testData); | ||
| return this._isValidCompressed(compressed); | ||
| } | ||
|
|
||
| private _isValidCompressed(buffer: Uint8Array): boolean { | ||
| return ( | ||
| this._hasMinimumLength(buffer) && | ||
| this._hasValidMagicNumber(buffer) && | ||
| this._hasValidVersion(buffer) | ||
| ); | ||
| } | ||
|
|
||
| private _hasMinimumLength(buffer: Uint8Array): boolean { | ||
| return buffer.length >= this.MIN_HEADER_LENGTH; | ||
| } | ||
|
|
||
| private _hasValidMagicNumber(buffer: Uint8Array): boolean { | ||
| return this.LZ4_FRAME_MAGIC.every( | ||
| (byte, i) => buffer[i] === byte | ||
| ); | ||
| } | ||
|
|
||
| private _hasValidVersion(buffer: Uint8Array): boolean { | ||
| const flg = buffer[4]; | ||
| const versionBits = (flg & 0xC0) >> 6; | ||
| return versionBits === 1; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class ZstdValidator implements CompressionValidator { | ||
| private readonly ZSTD_MAGIC = new Uint8Array([40, 181, 47, 253]); | ||
| private readonly MIN_HEADER_LENGTH = 6; // 4 (magic) + 2 (min Frame_Header) = 6 min bytes | ||
|
|
||
| isValidCodecEncode(codec: Codec): boolean { | ||
| const testData = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); | ||
| const compressed = codec.encode!(testData); | ||
| return this._isValidCompressed(compressed); | ||
| } | ||
|
|
||
| private _isValidCompressed(buffer: Uint8Array): boolean { | ||
| return ( | ||
| this._hasMinimumLength(buffer) && | ||
| this._hasValidMagicNumber(buffer) | ||
| ); | ||
| } | ||
|
|
||
| private _hasMinimumLength(buffer: Uint8Array): boolean { | ||
| return buffer.length >= this.MIN_HEADER_LENGTH; | ||
| } | ||
|
|
||
| private _hasValidMagicNumber(buffer: Uint8Array): boolean { | ||
| return this.ZSTD_MAGIC.every( | ||
| (byte, i) => buffer[i] === byte | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export const compressionValidators: Record<CompressionType, CompressionValidator> = { | ||
| [CompressionType.LZ4_FRAME]: new Lz4FrameValidator(), | ||
| [CompressionType.ZSTD]: new ZstdValidator(), | ||
| }; | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since many libraries use the raw LZ4 format instead of the framed format, I decided to add validation for the
encodefunction. This ensures that Arrow files compressed with LZ4 can be correctly read in other languages. Initially, I considered comparing compressed and decompressed buffers, but due to optional metadata flags, this might not be reliable. Instead, I validate that theencodefunction generates a correct metadata header. I'm unsure if similar validation is needed fordecodesince users should notice if their data decompresses incorrectly.