-
Notifications
You must be signed in to change notification settings - Fork 3
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
Open
Djjanks
wants to merge
20
commits into
apache:main
Choose a base branch
from
Djjanks:feature/arrow-compression
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
80f47ca
Define commpression registry
Djjanks 43905c8
Implement RecordBatch body decompression
Djjanks 8eb03e7
Export compressionType, Codec and compressionRegistry
Djjanks f932ebc
Avoid copying decompressed data to contiguous ArrayBuffer
Djjanks 7bcd611
Refactor: Move vector decompressed data reader to CompressedVectorLoader
Djjanks b6c2753
Refactor _CompressionRegistry
Djjanks a05e7c0
Add codec encode validators
Djjanks 7d2aa05
Implement BodyCompression encode/decode to metadata message
Djjanks 7900afb
Implement compression to RecordBatch writer
Djjanks d1cabbf
Refactor padding calculation for compressed Record Batch
Djjanks 76b1e30
Refactor RecordBatch writing to handle compression inline
Djjanks deb0b18
Add ZSTD validation
Djjanks 01b2ab8
Fix buffer size calculation in _writeBodyBuffers
Djjanks d02694a
fix(ipc/writer): prevent compression with writeLegacyIpcFormat=true
Djjanks c82914f
test(ipc/writer): add compression test to stream writer
Djjanks ff01e9f
Merge branch 'main' into feature/arrow-compression
Djjanks 33789f9
License header
Djjanks 0487c44
feat(ipc/writer): add options to RecordBatchFileWriter constructor
Djjanks 56845c2
fix(ipc/writer): handle dictionary batch correctly when compression i…
Djjanks 0ca03f2
test(ipc/writer): add compression test to file writer
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.
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
encode
function. 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 theencode
function generates a correct metadata header. I'm unsure if similar validation is needed fordecode
since users should notice if their data decompresses incorrectly.