alp-codec 0.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- alp_codec-0.8.0/.github/workflows/ci.yml +26 -0
- alp_codec-0.8.0/.github/workflows/release.yml +60 -0
- alp_codec-0.8.0/.gitignore +18 -0
- alp_codec-0.8.0/LICENSE +189 -0
- alp_codec-0.8.0/PKG-INFO +86 -0
- alp_codec-0.8.0/README.md +55 -0
- alp_codec-0.8.0/alp/__init__.py +121 -0
- alp_codec-0.8.0/alp/_bit_packing.py +101 -0
- alp_codec-0.8.0/alp/_constants.py +40 -0
- alp_codec-0.8.0/alp/_encoder.py +102 -0
- alp_codec-0.8.0/pyproject.toml +52 -0
- alp_codec-0.8.0/tests/__init__.py +0 -0
- alp_codec-0.8.0/tests/test_alp.py +162 -0
- alp_codec-0.8.0/tests/test_smoke.py +8 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
matrix:
|
|
13
|
+
python-version: ["3.11", "3.12", "3.13"]
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: actions/setup-python@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: ${{ matrix.python-version }}
|
|
19
|
+
- name: Install
|
|
20
|
+
run: |
|
|
21
|
+
python -m pip install --upgrade pip
|
|
22
|
+
pip install -e ".[dev]"
|
|
23
|
+
- name: Lint
|
|
24
|
+
run: ruff check .
|
|
25
|
+
- name: Test
|
|
26
|
+
run: pytest -q
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
# Triggered by pushing a SemVer-shaped tag (e.g. 0.8.0, 0.8.1rc1).
|
|
4
|
+
# Builds the wheel + sdist and uploads to PyPI via OIDC trusted publishing.
|
|
5
|
+
# No API tokens involved — auth is via the Sigstore identity of this workflow,
|
|
6
|
+
# which PyPI is configured to trust for the alp-codec project.
|
|
7
|
+
on:
|
|
8
|
+
push:
|
|
9
|
+
tags:
|
|
10
|
+
- "[0-9]+.[0-9]+.[0-9]+"
|
|
11
|
+
- "[0-9]+.[0-9]+.[0-9]+a[0-9]+"
|
|
12
|
+
- "[0-9]+.[0-9]+.[0-9]+b[0-9]+"
|
|
13
|
+
- "[0-9]+.[0-9]+.[0-9]+rc[0-9]+"
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
build:
|
|
17
|
+
name: Build sdist + wheel
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v4
|
|
21
|
+
with:
|
|
22
|
+
fetch-depth: 0
|
|
23
|
+
|
|
24
|
+
- uses: actions/setup-python@v5
|
|
25
|
+
with:
|
|
26
|
+
python-version: "3.12"
|
|
27
|
+
|
|
28
|
+
- name: Install build tooling
|
|
29
|
+
run: |
|
|
30
|
+
python -m pip install --upgrade pip
|
|
31
|
+
python -m pip install build
|
|
32
|
+
|
|
33
|
+
- name: Build distributions
|
|
34
|
+
run: python -m build
|
|
35
|
+
|
|
36
|
+
- name: Sanity-check distributions
|
|
37
|
+
run: |
|
|
38
|
+
python -m pip install --upgrade twine
|
|
39
|
+
python -m twine check dist/*
|
|
40
|
+
|
|
41
|
+
- uses: actions/upload-artifact@v4
|
|
42
|
+
with:
|
|
43
|
+
name: distributions
|
|
44
|
+
path: dist/
|
|
45
|
+
if-no-files-found: error
|
|
46
|
+
|
|
47
|
+
publish:
|
|
48
|
+
name: Publish to PyPI
|
|
49
|
+
needs: build
|
|
50
|
+
runs-on: ubuntu-latest
|
|
51
|
+
permissions:
|
|
52
|
+
# Required for OIDC-based trusted publishing to PyPI.
|
|
53
|
+
id-token: write
|
|
54
|
+
steps:
|
|
55
|
+
- uses: actions/download-artifact@v4
|
|
56
|
+
with:
|
|
57
|
+
name: distributions
|
|
58
|
+
path: dist/
|
|
59
|
+
|
|
60
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
alp_codec-0.8.0/LICENSE
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean any work of authorship, including
|
|
48
|
+
the original version of the Work and any modifications or additions
|
|
49
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
50
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
51
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
52
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
53
|
+
means any form of electronic, verbal, or written communication sent
|
|
54
|
+
to the Licensor or its representatives, including but not limited to
|
|
55
|
+
communication on electronic mailing lists, source code control systems,
|
|
56
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
57
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
58
|
+
excluding communication that is conspicuously marked or otherwise
|
|
59
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
60
|
+
|
|
61
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
62
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
63
|
+
subsequently incorporated within the Work.
|
|
64
|
+
|
|
65
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
66
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
67
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
68
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
69
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
70
|
+
Work and such Derivative Works in Source or Object form.
|
|
71
|
+
|
|
72
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
73
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
74
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
75
|
+
(except as stated in this section) patent license to make, have made,
|
|
76
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
77
|
+
where such license applies only to those patent claims licensable
|
|
78
|
+
by such Contributor that are necessarily infringed by their
|
|
79
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
80
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
81
|
+
institute patent litigation against any entity (including a
|
|
82
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
83
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
84
|
+
or contributory patent infringement, then any patent licenses
|
|
85
|
+
granted to You under this License for that Work shall terminate
|
|
86
|
+
as of the date such litigation is filed.
|
|
87
|
+
|
|
88
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
89
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
90
|
+
modifications, and in Source or Object form, provided that You
|
|
91
|
+
meet the following conditions:
|
|
92
|
+
|
|
93
|
+
(a) You must give any other recipients of the Work or
|
|
94
|
+
Derivative Works a copy of this License; and
|
|
95
|
+
|
|
96
|
+
(b) You must cause any modified files to carry prominent notices
|
|
97
|
+
stating that You changed the files; and
|
|
98
|
+
|
|
99
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
100
|
+
that You distribute, all copyright, patent, trademark, and
|
|
101
|
+
attribution notices from the Source form of the Work,
|
|
102
|
+
excluding those notices that do not pertain to any part of
|
|
103
|
+
the Derivative Works; and
|
|
104
|
+
|
|
105
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
106
|
+
distribution, then any Derivative Works that You distribute must
|
|
107
|
+
include a readable copy of the attribution notices contained
|
|
108
|
+
within such NOTICE file, excluding any notices that do not
|
|
109
|
+
pertain to any part of the Derivative Works, in at least one
|
|
110
|
+
of the following places: within a NOTICE text file distributed
|
|
111
|
+
as part of the Derivative Works; within the Source form or
|
|
112
|
+
documentation, if provided along with the Derivative Works; or,
|
|
113
|
+
within a display generated by the Derivative Works, if and
|
|
114
|
+
wherever such third-party notices normally appear. The contents
|
|
115
|
+
of the NOTICE file are for informational purposes only and
|
|
116
|
+
do not modify the License. You may add Your own attribution
|
|
117
|
+
notices within Derivative Works that You distribute, alongside
|
|
118
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
119
|
+
that such additional attribution notices cannot be construed
|
|
120
|
+
as modifying the License.
|
|
121
|
+
|
|
122
|
+
You may add Your own copyright statement to Your modifications and
|
|
123
|
+
may provide additional or different license terms and conditions
|
|
124
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
125
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
126
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
127
|
+
the conditions stated in this License.
|
|
128
|
+
|
|
129
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
130
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
131
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
132
|
+
this License, without any additional terms or conditions.
|
|
133
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
134
|
+
the terms of any separate license agreement you may have executed
|
|
135
|
+
with Licensor regarding such Contributions.
|
|
136
|
+
|
|
137
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
138
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
139
|
+
except as required for reasonable and customary use in describing the
|
|
140
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
141
|
+
|
|
142
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
143
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
144
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
145
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
146
|
+
implied, including, without limitation, any warranties or conditions
|
|
147
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
148
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
149
|
+
appropriateness of using or redistributing the Work and assume any
|
|
150
|
+
risks associated with Your exercise of permissions under this License.
|
|
151
|
+
|
|
152
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
153
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
154
|
+
unless required by applicable law (such as deliberate and grossly
|
|
155
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
156
|
+
liable to You for damages, including any direct, indirect, special,
|
|
157
|
+
incidental, or consequential damages of any character arising as a
|
|
158
|
+
result of this License or out of the use or inability to use the
|
|
159
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
160
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
161
|
+
other commercial damages or losses), even if such Contributor
|
|
162
|
+
has been advised of the possibility of such damages.
|
|
163
|
+
|
|
164
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
165
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
166
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
167
|
+
or other liability obligations and/or rights consistent with this
|
|
168
|
+
License. However, in accepting such obligations, You may act only
|
|
169
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
170
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
171
|
+
defend, and hold each Contributor harmless for any liability
|
|
172
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
173
|
+
of your accepting any such warranty or additional liability.
|
|
174
|
+
|
|
175
|
+
END OF TERMS AND CONDITIONS
|
|
176
|
+
|
|
177
|
+
Copyright 2026 Wualabs LTD
|
|
178
|
+
|
|
179
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
180
|
+
you may not use this file except in compliance with the License.
|
|
181
|
+
You may obtain a copy of the License at
|
|
182
|
+
|
|
183
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
184
|
+
|
|
185
|
+
Unless required by applicable law or agreed to in writing, software
|
|
186
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
187
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
188
|
+
See the License for the specific language governing permissions and
|
|
189
|
+
limitations under the License.
|
alp_codec-0.8.0/PKG-INFO
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: alp-codec
|
|
3
|
+
Version: 0.8.0
|
|
4
|
+
Summary: Pure Python implementation of ALP (Adaptive Lossless floating-Point) compression — port of QTSurfer/alp-java.
|
|
5
|
+
Project-URL: Homepage, https://github.com/QTSurfer/alp-py
|
|
6
|
+
Project-URL: Documentation, https://github.com/QTSurfer/alp-py#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/QTSurfer/alp-py
|
|
8
|
+
Project-URL: Issues, https://github.com/QTSurfer/alp-py/issues
|
|
9
|
+
Project-URL: Reference (Java), https://github.com/QTSurfer/alp-java
|
|
10
|
+
Project-URL: Paper, https://dl.acm.org/doi/10.1145/3626717
|
|
11
|
+
Author: Wualabs LTD
|
|
12
|
+
License: Apache-2.0
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Keywords: alp,codec,columnar,compression,floating-point,time-series
|
|
15
|
+
Classifier: Development Status :: 3 - Alpha
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Intended Audience :: Science/Research
|
|
18
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering
|
|
23
|
+
Classifier: Topic :: System :: Archiving :: Compression
|
|
24
|
+
Requires-Python: >=3.11
|
|
25
|
+
Requires-Dist: numpy>=1.26
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
28
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# alp-py
|
|
33
|
+
|
|
34
|
+
[](https://github.com/QTSurfer/alp-py/actions/workflows/ci.yml)
|
|
35
|
+
[](https://pypi.org/project/alp-codec/)
|
|
36
|
+
[](LICENSE)
|
|
37
|
+
|
|
38
|
+
Pure Python implementation of the **ALP** (Adaptive Lossless floating-Point) compression algorithm.
|
|
39
|
+
|
|
40
|
+
Based on: Afroozeh & Boncz, *"ALP: Adaptive Lossless floating-Point Compression"*, ACM SIGMOD 2024.
|
|
41
|
+
([paper](https://dl.acm.org/doi/10.1145/3626717) · [pdf](https://ir.cwi.nl/pub/33334/33334.pdf))
|
|
42
|
+
|
|
43
|
+
Python port of the [QTSurfer/alp-java](https://github.com/QTSurfer/alp-java) reference implementation. Bit-exact byte-level compatibility with `alp-java` — files written by either implementation can be read by the other.
|
|
44
|
+
|
|
45
|
+
## Status
|
|
46
|
+
|
|
47
|
+
🚧 **Pre-release** — repository scaffold while we port the codec from Java. The first published release will be `0.8.0` to align with `alp-java`.
|
|
48
|
+
|
|
49
|
+
## Install
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install alp-codec
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Usage (planned API)
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import numpy as np
|
|
59
|
+
from alp import encode, decode
|
|
60
|
+
|
|
61
|
+
values = np.array([65007.28, 65007.31, 65007.30, 65007.32], dtype=np.float64)
|
|
62
|
+
|
|
63
|
+
# Encode to bytes
|
|
64
|
+
blob = encode(values)
|
|
65
|
+
|
|
66
|
+
# Decode back to a NumPy float64 array
|
|
67
|
+
recovered = decode(blob)
|
|
68
|
+
assert np.array_equal(values, recovered)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Why ALP
|
|
72
|
+
|
|
73
|
+
ALP applies **semantic compression** to decimal floating-point arrays. For a typical financial price column at 2 decimal places it reaches **3-4 bits per value** — about an order of magnitude better than Gorilla XOR or block-compressed PLAIN encodings.
|
|
74
|
+
|
|
75
|
+
The algorithm picks an `(e, f)` pair per block such that `value × 10^e × 2^-f` is exactly representable as a small `int64`, encodes the integers via Frame-Of-Reference + bit-packing, and stores any non-conforming values as outliers with a position list.
|
|
76
|
+
|
|
77
|
+
## Reference implementations
|
|
78
|
+
|
|
79
|
+
| Language | Repo | Status |
|
|
80
|
+
|----------|------|--------|
|
|
81
|
+
| Java | [QTSurfer/alp-java](https://github.com/QTSurfer/alp-java) | Reference |
|
|
82
|
+
| Python | [QTSurfer/alp-py](https://github.com/QTSurfer/alp-py) | This repo |
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
Copyright 2026 Wualabs LTD. Apache License 2.0 — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# alp-py
|
|
2
|
+
|
|
3
|
+
[](https://github.com/QTSurfer/alp-py/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/alp-codec/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
Pure Python implementation of the **ALP** (Adaptive Lossless floating-Point) compression algorithm.
|
|
8
|
+
|
|
9
|
+
Based on: Afroozeh & Boncz, *"ALP: Adaptive Lossless floating-Point Compression"*, ACM SIGMOD 2024.
|
|
10
|
+
([paper](https://dl.acm.org/doi/10.1145/3626717) · [pdf](https://ir.cwi.nl/pub/33334/33334.pdf))
|
|
11
|
+
|
|
12
|
+
Python port of the [QTSurfer/alp-java](https://github.com/QTSurfer/alp-java) reference implementation. Bit-exact byte-level compatibility with `alp-java` — files written by either implementation can be read by the other.
|
|
13
|
+
|
|
14
|
+
## Status
|
|
15
|
+
|
|
16
|
+
🚧 **Pre-release** — repository scaffold while we port the codec from Java. The first published release will be `0.8.0` to align with `alp-java`.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install alp-codec
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage (planned API)
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
import numpy as np
|
|
28
|
+
from alp import encode, decode
|
|
29
|
+
|
|
30
|
+
values = np.array([65007.28, 65007.31, 65007.30, 65007.32], dtype=np.float64)
|
|
31
|
+
|
|
32
|
+
# Encode to bytes
|
|
33
|
+
blob = encode(values)
|
|
34
|
+
|
|
35
|
+
# Decode back to a NumPy float64 array
|
|
36
|
+
recovered = decode(blob)
|
|
37
|
+
assert np.array_equal(values, recovered)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Why ALP
|
|
41
|
+
|
|
42
|
+
ALP applies **semantic compression** to decimal floating-point arrays. For a typical financial price column at 2 decimal places it reaches **3-4 bits per value** — about an order of magnitude better than Gorilla XOR or block-compressed PLAIN encodings.
|
|
43
|
+
|
|
44
|
+
The algorithm picks an `(e, f)` pair per block such that `value × 10^e × 2^-f` is exactly representable as a small `int64`, encodes the integers via Frame-Of-Reference + bit-packing, and stores any non-conforming values as outliers with a position list.
|
|
45
|
+
|
|
46
|
+
## Reference implementations
|
|
47
|
+
|
|
48
|
+
| Language | Repo | Status |
|
|
49
|
+
|----------|------|--------|
|
|
50
|
+
| Java | [QTSurfer/alp-java](https://github.com/QTSurfer/alp-java) | Reference |
|
|
51
|
+
| Python | [QTSurfer/alp-py](https://github.com/QTSurfer/alp-py) | This repo |
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
Copyright 2026 Wualabs LTD. Apache License 2.0 — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"""ALP (Adaptive Lossless floating-Point) compression — Python port.
|
|
2
|
+
|
|
3
|
+
Public API::
|
|
4
|
+
|
|
5
|
+
from alp import encode, decode
|
|
6
|
+
|
|
7
|
+
Both work in-memory on a numpy float64 array. The wire format matches
|
|
8
|
+
``com.wualabs.qtsurfer.alp.AlpCompressor`` byte for byte.
|
|
9
|
+
|
|
10
|
+
Wire format::
|
|
11
|
+
|
|
12
|
+
[int32 BE] total count
|
|
13
|
+
For each VECTOR_SIZE-sized chunk:
|
|
14
|
+
[uint8] e (exponent)
|
|
15
|
+
[uint8] f (factor)
|
|
16
|
+
[uint8] bitWidth
|
|
17
|
+
[int64 BE] frame (FOR base)
|
|
18
|
+
[uint16 BE] exceptionCount
|
|
19
|
+
[uint16 BE] packedLength
|
|
20
|
+
[packedLength bytes] bit-packed deltas (LE within bytes)
|
|
21
|
+
For each exception:
|
|
22
|
+
[uint16 BE] index
|
|
23
|
+
[int64 BE] original IEEE 754 bits
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
import io
|
|
29
|
+
import struct
|
|
30
|
+
|
|
31
|
+
import numpy as np
|
|
32
|
+
|
|
33
|
+
from . import _bit_packing as bp
|
|
34
|
+
from . import _encoder
|
|
35
|
+
from ._constants import VECTOR_SIZE
|
|
36
|
+
|
|
37
|
+
__version__ = "0.8.0"
|
|
38
|
+
|
|
39
|
+
_SAMPLE_SIZE = 64
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def encode(values: np.ndarray | list[float]) -> bytes:
|
|
43
|
+
"""Compress a sequence of doubles into the ALP wire format."""
|
|
44
|
+
arr = np.ascontiguousarray(np.asarray(values, dtype=np.float64))
|
|
45
|
+
count = arr.size
|
|
46
|
+
|
|
47
|
+
out = io.BytesIO()
|
|
48
|
+
out.write(struct.pack(">i", count))
|
|
49
|
+
|
|
50
|
+
offset = 0
|
|
51
|
+
while offset < count:
|
|
52
|
+
vector_size = min(VECTOR_SIZE, count - offset)
|
|
53
|
+
chunk = arr[offset : offset + vector_size]
|
|
54
|
+
e, f = _encoder.find_best_exponents(chunk, vector_size, _SAMPLE_SIZE)
|
|
55
|
+
encoded, exception_indices, exception_bits = _encoder.encode_vector(
|
|
56
|
+
chunk, vector_size, e, f
|
|
57
|
+
)
|
|
58
|
+
frame = bp.find_frame(encoded, vector_size)
|
|
59
|
+
bit_width = bp.compute_bit_width(encoded, vector_size, frame)
|
|
60
|
+
packed = bp.pack(encoded, vector_size, frame, bit_width)
|
|
61
|
+
|
|
62
|
+
out.write(struct.pack(">B", e))
|
|
63
|
+
out.write(struct.pack(">B", f))
|
|
64
|
+
out.write(struct.pack(">B", bit_width))
|
|
65
|
+
out.write(struct.pack(">q", frame))
|
|
66
|
+
out.write(struct.pack(">H", len(exception_indices)))
|
|
67
|
+
out.write(struct.pack(">H", len(packed)))
|
|
68
|
+
out.write(packed)
|
|
69
|
+
for idx, bits in zip(exception_indices, exception_bits, strict=False):
|
|
70
|
+
out.write(struct.pack(">H", int(idx)))
|
|
71
|
+
out.write(struct.pack(">q", int(bits)))
|
|
72
|
+
|
|
73
|
+
offset += vector_size
|
|
74
|
+
|
|
75
|
+
return out.getvalue()
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def decode(data: bytes) -> np.ndarray:
|
|
79
|
+
"""Decompress ALP-encoded bytes back into a numpy float64 array."""
|
|
80
|
+
if len(data) < 4:
|
|
81
|
+
raise ValueError(f"truncated ALP blob: missing 4-byte count header (got {len(data)} bytes)")
|
|
82
|
+
|
|
83
|
+
in_buf = io.BytesIO(data)
|
|
84
|
+
(count,) = struct.unpack(">i", in_buf.read(4))
|
|
85
|
+
out = np.empty(count, dtype=np.float64)
|
|
86
|
+
|
|
87
|
+
offset = 0
|
|
88
|
+
while offset < count:
|
|
89
|
+
vector_size = min(VECTOR_SIZE, count - offset)
|
|
90
|
+
header = in_buf.read(3 + 8 + 2 + 2)
|
|
91
|
+
if len(header) != 15:
|
|
92
|
+
raise ValueError("truncated ALP vector header")
|
|
93
|
+
e = header[0]
|
|
94
|
+
f = header[1]
|
|
95
|
+
bit_width = header[2]
|
|
96
|
+
(frame,) = struct.unpack(">q", header[3:11])
|
|
97
|
+
(exception_count,) = struct.unpack(">H", header[11:13])
|
|
98
|
+
(packed_length,) = struct.unpack(">H", header[13:15])
|
|
99
|
+
|
|
100
|
+
packed = in_buf.read(packed_length)
|
|
101
|
+
if len(packed) != packed_length:
|
|
102
|
+
raise ValueError(
|
|
103
|
+
f"truncated ALP packed body: expected {packed_length} bytes, got {len(packed)}"
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
encoded = bp.unpack(packed, vector_size, frame, bit_width)
|
|
107
|
+
for i in range(vector_size):
|
|
108
|
+
out[offset + i] = _encoder.decode_value(int(encoded[i]), e, f)
|
|
109
|
+
|
|
110
|
+
for _ in range(exception_count):
|
|
111
|
+
tail = in_buf.read(2 + 8)
|
|
112
|
+
if len(tail) != 10:
|
|
113
|
+
raise ValueError("truncated ALP exception entry")
|
|
114
|
+
(idx,) = struct.unpack(">H", tail[:2])
|
|
115
|
+
(orig_bits_signed,) = struct.unpack(">q", tail[2:10])
|
|
116
|
+
unsigned = orig_bits_signed & ((1 << 64) - 1)
|
|
117
|
+
out[offset + idx] = struct.unpack("<d", unsigned.to_bytes(8, "little", signed=False))[0]
|
|
118
|
+
|
|
119
|
+
offset += vector_size
|
|
120
|
+
|
|
121
|
+
return out
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""Fixed-width bit-packing with Frame-Of-Reference subtraction.
|
|
2
|
+
|
|
3
|
+
Mirrors :class:`com.wualabs.qtsurfer.alp.BitPacking`. The Java side packs
|
|
4
|
+
deltas as little-endian within each window of 8 bytes; this module
|
|
5
|
+
reproduces that layout byte-for-byte.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
_GROUP_SIZE = 32
|
|
13
|
+
_MASK64 = (1 << 64) - 1
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def find_frame(values: np.ndarray, count: int) -> int:
|
|
17
|
+
"""Return the minimum integer in ``values[:count]`` (Frame Of Reference base)."""
|
|
18
|
+
if count == 0:
|
|
19
|
+
return 0
|
|
20
|
+
return int(values[:count].min())
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def compute_bit_width(values: np.ndarray, count: int, frame: int) -> int:
|
|
24
|
+
"""Bits needed to represent ``max(values) - frame`` (0 if all values are equal)."""
|
|
25
|
+
if count == 0:
|
|
26
|
+
return 0
|
|
27
|
+
deltas = np.asarray(values[:count], dtype=np.int64) - np.int64(frame)
|
|
28
|
+
max_delta = int(deltas.max())
|
|
29
|
+
if max_delta == 0:
|
|
30
|
+
return 0
|
|
31
|
+
return max_delta.bit_length()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def pack(values: np.ndarray, count: int, frame: int, bit_width: int) -> bytes:
|
|
35
|
+
"""Pack ``values - frame`` into a tightly-packed little-endian bit stream."""
|
|
36
|
+
if bit_width == 0:
|
|
37
|
+
return b""
|
|
38
|
+
|
|
39
|
+
total_bits = count * bit_width
|
|
40
|
+
total_bytes = (total_bits + 7) // 8
|
|
41
|
+
buf = bytearray(total_bytes)
|
|
42
|
+
|
|
43
|
+
mask = _MASK64 if bit_width == 64 else (1 << bit_width) - 1
|
|
44
|
+
bit_pos = 0
|
|
45
|
+
for i in range(count):
|
|
46
|
+
delta = (int(values[i]) - frame) & mask
|
|
47
|
+
byte_idx = bit_pos // 8
|
|
48
|
+
bit_offset = bit_pos % 8
|
|
49
|
+
# Write up to 9 bytes so any bitWidth ≤ 56 fits in one shifted word.
|
|
50
|
+
shifted = delta << bit_offset
|
|
51
|
+
bytes_needed = (bit_offset + bit_width + 7) // 8
|
|
52
|
+
for b in range(bytes_needed):
|
|
53
|
+
if byte_idx + b >= len(buf):
|
|
54
|
+
break
|
|
55
|
+
buf[byte_idx + b] |= (shifted >> (b * 8)) & 0xFF
|
|
56
|
+
bit_pos += bit_width
|
|
57
|
+
|
|
58
|
+
return bytes(buf)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def unpack(packed: bytes, count: int, frame: int, bit_width: int) -> np.ndarray:
|
|
62
|
+
"""Inverse of :func:`pack`. Returns an ``int64`` numpy array."""
|
|
63
|
+
out = np.empty(count, dtype=np.int64)
|
|
64
|
+
if bit_width == 0:
|
|
65
|
+
out.fill(frame)
|
|
66
|
+
return out
|
|
67
|
+
|
|
68
|
+
mask = _MASK64 if bit_width == 64 else (1 << bit_width) - 1
|
|
69
|
+
full_groups = count // _GROUP_SIZE
|
|
70
|
+
remainder = count % _GROUP_SIZE
|
|
71
|
+
|
|
72
|
+
bit_pos = 0
|
|
73
|
+
out_idx = 0
|
|
74
|
+
for _ in range(full_groups):
|
|
75
|
+
for i in range(_GROUP_SIZE):
|
|
76
|
+
byte_idx = bit_pos // 8
|
|
77
|
+
bit_offset = bit_pos % 8
|
|
78
|
+
bytes_avail = min(8, len(packed) - byte_idx)
|
|
79
|
+
raw = 0
|
|
80
|
+
for b in range(bytes_avail):
|
|
81
|
+
raw |= packed[byte_idx + b] << (b * 8)
|
|
82
|
+
out[out_idx + i] = frame + ((raw >> bit_offset) & mask)
|
|
83
|
+
bit_pos += bit_width
|
|
84
|
+
out_idx += _GROUP_SIZE
|
|
85
|
+
|
|
86
|
+
for i in range(remainder):
|
|
87
|
+
byte_idx = bit_pos // 8
|
|
88
|
+
bit_offset = bit_pos % 8
|
|
89
|
+
result = 0
|
|
90
|
+
bits_read = 0
|
|
91
|
+
while bits_read < bit_width:
|
|
92
|
+
bits_to_read = min(8 - bit_offset, bit_width - bits_read)
|
|
93
|
+
byte_mask = (1 << bits_to_read) - 1
|
|
94
|
+
result |= ((packed[byte_idx] >> bit_offset) & byte_mask) << bits_read
|
|
95
|
+
bits_read += bits_to_read
|
|
96
|
+
bit_offset = 0
|
|
97
|
+
byte_idx += 1
|
|
98
|
+
out[out_idx + i] = frame + result
|
|
99
|
+
bit_pos += bit_width
|
|
100
|
+
|
|
101
|
+
return out
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Constants for the ALP (Adaptive Lossless floating-Point) algorithm.
|
|
2
|
+
|
|
3
|
+
Reference: Afroozeh & Boncz, *"ALP: Adaptive Lossless floating-Point
|
|
4
|
+
Compression"*, ACM SIGMOD 2024 (DOI: 10.1145/3626717).
|
|
5
|
+
|
|
6
|
+
Mirrors :class:`com.wualabs.qtsurfer.alp.AlpConstants` byte-for-byte —
|
|
7
|
+
any change here affects the wire format and must be made in lock-step
|
|
8
|
+
with ``alp-java``.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
# Vector size for ALP encoding (matches DuckDB / columnar convention).
|
|
14
|
+
VECTOR_SIZE = 1024
|
|
15
|
+
|
|
16
|
+
# Maximum exponent e (multiply by 10^e to convert to integer).
|
|
17
|
+
MAX_EXPONENT = 18
|
|
18
|
+
|
|
19
|
+
# Maximum factor f (divide by 10^f to keep within 64-bit range).
|
|
20
|
+
MAX_FACTOR = 10
|
|
21
|
+
|
|
22
|
+
# Maximum fraction of values allowed as exceptions (currently informational —
|
|
23
|
+
# AlpCompressor in Java doesn't actually fall back to ALP-RD/raw when this
|
|
24
|
+
# is exceeded; it just stores all the exceptions). Kept here for parity.
|
|
25
|
+
MAX_EXCEPTION_FRACTION = 0.05
|
|
26
|
+
|
|
27
|
+
# Powers of 10 as Python ints (POW10_LONG in Java).
|
|
28
|
+
POW10_LONG: tuple[int, ...] = tuple(10**i for i in range(MAX_EXPONENT + 1))
|
|
29
|
+
|
|
30
|
+
# Powers of 10 as floats (POW10_DOUBLE in Java).
|
|
31
|
+
POW10_DOUBLE: tuple[float, ...] = tuple(float(p) for p in POW10_LONG)
|
|
32
|
+
|
|
33
|
+
# Reciprocals of powers of 10 (1/10^f) for the factor range.
|
|
34
|
+
INV_POW10: tuple[float, ...] = tuple(
|
|
35
|
+
1.0 / POW10_DOUBLE[i] if i > 0 else 1.0
|
|
36
|
+
for i in range(MAX_FACTOR + 1)
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
# Magic number for branchless rounding (= 2^51 + 2^52).
|
|
40
|
+
ENCODE_MAGIC = 6755399441055744.0
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""ALP per-value encoder/decoder + parameter search.
|
|
2
|
+
|
|
3
|
+
Mirror of :class:`com.wualabs.qtsurfer.alp.AlpEncoder`.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import struct
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
from ._constants import (
|
|
13
|
+
ENCODE_MAGIC,
|
|
14
|
+
INV_POW10,
|
|
15
|
+
MAX_EXPONENT,
|
|
16
|
+
MAX_FACTOR,
|
|
17
|
+
POW10_DOUBLE,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _bits(value: float) -> int:
|
|
22
|
+
"""Return the raw IEEE 754 bits of a float64 as an unsigned 64-bit int."""
|
|
23
|
+
return int.from_bytes(struct.pack("<d", value), "little", signed=False)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def encode_value(value: float, e: int, f: int) -> int:
|
|
27
|
+
"""Encode one double via ``round(value * 10^e / 10^f)``.
|
|
28
|
+
|
|
29
|
+
Uses the same branchless ``+ MAGIC - MAGIC`` rounding trick as Java's
|
|
30
|
+
Math (effectively round-half-to-even on a typical x86 build with
|
|
31
|
+
default rounding mode). The Python ``int(...)`` truncation here
|
|
32
|
+
matches Java's ``(long) (...)`` cast behaviour after the magic add.
|
|
33
|
+
"""
|
|
34
|
+
scaled = value * POW10_DOUBLE[e] * INV_POW10[f]
|
|
35
|
+
rounded = scaled + ENCODE_MAGIC - ENCODE_MAGIC
|
|
36
|
+
# Java casts a double to long via truncation, which agrees with
|
|
37
|
+
# the rounded value here as long as it fits in [-2^63, 2^63 - 1].
|
|
38
|
+
return int(rounded)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def decode_value(encoded: int, e: int, f: int) -> float:
|
|
42
|
+
return float(encoded) * POW10_DOUBLE[f] / POW10_DOUBLE[e]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def find_best_exponents(values: np.ndarray, count: int, sample_size: int) -> tuple[int, int]:
|
|
46
|
+
"""Pick the (e, f) pair with the fewest round-trip exceptions over a sample."""
|
|
47
|
+
n = min(count, sample_size)
|
|
48
|
+
best_e = 0
|
|
49
|
+
best_f = 0
|
|
50
|
+
best_exceptions = (1 << 31) - 1 # Integer.MAX_VALUE
|
|
51
|
+
for e in range(MAX_EXPONENT + 1):
|
|
52
|
+
for f in range(min(e, MAX_FACTOR) + 1):
|
|
53
|
+
exceptions = 0
|
|
54
|
+
for i in range(n):
|
|
55
|
+
v = float(values[i])
|
|
56
|
+
enc = encode_value(v, e, f)
|
|
57
|
+
dec = decode_value(enc, e, f)
|
|
58
|
+
if _bits(dec) != _bits(v):
|
|
59
|
+
exceptions += 1
|
|
60
|
+
if exceptions < best_exceptions:
|
|
61
|
+
best_exceptions = exceptions
|
|
62
|
+
best_e = e
|
|
63
|
+
best_f = f
|
|
64
|
+
if exceptions == 0:
|
|
65
|
+
return best_e, best_f
|
|
66
|
+
return best_e, best_f
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def encode_vector(
|
|
70
|
+
values: np.ndarray,
|
|
71
|
+
count: int,
|
|
72
|
+
e: int,
|
|
73
|
+
f: int,
|
|
74
|
+
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
75
|
+
"""Encode ``count`` values with parameters ``(e, f)``.
|
|
76
|
+
|
|
77
|
+
Returns ``(encoded, exception_indices, exception_value_bits)``:
|
|
78
|
+
|
|
79
|
+
- ``encoded``: int64 array of length ``count`` with the FOR-encodable
|
|
80
|
+
integer for every position (including exception positions).
|
|
81
|
+
- ``exception_indices``: int array of positions that didn't round-trip.
|
|
82
|
+
- ``exception_value_bits``: int64 array of the original IEEE 754 bits
|
|
83
|
+
for those exception positions, in the same order.
|
|
84
|
+
"""
|
|
85
|
+
encoded = np.empty(count, dtype=np.int64)
|
|
86
|
+
indices: list[int] = []
|
|
87
|
+
bits: list[int] = []
|
|
88
|
+
for i in range(count):
|
|
89
|
+
v = float(values[i])
|
|
90
|
+
enc = encode_value(v, e, f)
|
|
91
|
+
encoded[i] = enc
|
|
92
|
+
if _bits(decode_value(enc, e, f)) != _bits(v):
|
|
93
|
+
indices.append(i)
|
|
94
|
+
# Store the raw IEEE bits as signed int64 so numpy can hold them.
|
|
95
|
+
unsigned = _bits(v)
|
|
96
|
+
signed = unsigned - (1 << 64) if unsigned >= (1 << 63) else unsigned
|
|
97
|
+
bits.append(signed)
|
|
98
|
+
return (
|
|
99
|
+
encoded,
|
|
100
|
+
np.asarray(indices, dtype=np.int32),
|
|
101
|
+
np.asarray(bits, dtype=np.int64),
|
|
102
|
+
)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "alp-codec"
|
|
7
|
+
version = "0.8.0"
|
|
8
|
+
description = "Pure Python implementation of ALP (Adaptive Lossless floating-Point) compression — port of QTSurfer/alp-java."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "Apache-2.0" }
|
|
11
|
+
authors = [{ name = "Wualabs LTD" }]
|
|
12
|
+
requires-python = ">=3.11"
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Intended Audience :: Science/Research",
|
|
17
|
+
"License :: OSI Approved :: Apache Software License",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Topic :: Scientific/Engineering",
|
|
22
|
+
"Topic :: System :: Archiving :: Compression",
|
|
23
|
+
]
|
|
24
|
+
dependencies = [
|
|
25
|
+
"numpy>=1.26",
|
|
26
|
+
]
|
|
27
|
+
keywords = ["alp", "compression", "floating-point", "time-series", "columnar", "codec"]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/QTSurfer/alp-py"
|
|
31
|
+
Documentation = "https://github.com/QTSurfer/alp-py#readme"
|
|
32
|
+
Repository = "https://github.com/QTSurfer/alp-py"
|
|
33
|
+
Issues = "https://github.com/QTSurfer/alp-py/issues"
|
|
34
|
+
"Reference (Java)" = "https://github.com/QTSurfer/alp-java"
|
|
35
|
+
Paper = "https://dl.acm.org/doi/10.1145/3626717"
|
|
36
|
+
|
|
37
|
+
[project.optional-dependencies]
|
|
38
|
+
dev = [
|
|
39
|
+
"pytest>=8.0",
|
|
40
|
+
"ruff>=0.6",
|
|
41
|
+
"mypy>=1.10",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
[tool.hatch.build.targets.wheel]
|
|
45
|
+
packages = ["alp"]
|
|
46
|
+
|
|
47
|
+
[tool.ruff]
|
|
48
|
+
line-length = 100
|
|
49
|
+
target-version = "py311"
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
testpaths = ["tests"]
|
|
File without changes
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""ALP codec tests — round-trips, edge cases, and Java byte-equality."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import struct
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
from alp import decode, encode
|
|
11
|
+
from alp._bit_packing import compute_bit_width, find_frame, pack, unpack
|
|
12
|
+
from alp._encoder import decode_value, encode_value, find_best_exponents
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _bits_equal(actual: np.ndarray, expected: list[float]) -> None:
|
|
16
|
+
assert len(actual) == len(expected)
|
|
17
|
+
for i, (a, e) in enumerate(zip(actual, expected, strict=False)):
|
|
18
|
+
assert struct.pack("<d", float(a)) == struct.pack("<d", float(e)), (
|
|
19
|
+
f"row {i}: {a!r} != {e!r}"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# --- High-level encode/decode round-trips -----------------------------------
|
|
24
|
+
|
|
25
|
+
def test_empty_roundtrip() -> None:
|
|
26
|
+
blob = encode([])
|
|
27
|
+
# 4-byte count header only
|
|
28
|
+
assert blob == b"\x00\x00\x00\x00"
|
|
29
|
+
assert decode(blob).size == 0
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_prices_2dp_roundtrip() -> None:
|
|
33
|
+
values = [65007.28, 65007.31, 65007.30, 65007.32, 65007.32]
|
|
34
|
+
decoded = decode(encode(values))
|
|
35
|
+
np.testing.assert_array_equal(decoded, values)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_integers_roundtrip() -> None:
|
|
39
|
+
values = [1.0, 2.0, 3.0, 4.0, 5.0]
|
|
40
|
+
decoded = decode(encode(values))
|
|
41
|
+
np.testing.assert_array_equal(decoded, values)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_identical_values_roundtrip() -> None:
|
|
45
|
+
values = [3.14, 3.14, 3.14, 3.14]
|
|
46
|
+
decoded = decode(encode(values))
|
|
47
|
+
np.testing.assert_array_equal(decoded, values)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_mixed_values_roundtrip() -> None:
|
|
51
|
+
values = [-12.5, -12.6, 0.0, 100.0, 1.234]
|
|
52
|
+
decoded = decode(encode(values))
|
|
53
|
+
np.testing.assert_array_equal(decoded, values)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_decimal_fractions_roundtrip() -> None:
|
|
57
|
+
"""0.1, 0.2, ... aren't representable in binary; ALP must mark them as
|
|
58
|
+
exceptions at e=0, but for any e≥1 the values round-trip."""
|
|
59
|
+
values = [0.1, 0.2, 0.3, 0.4, 0.5]
|
|
60
|
+
decoded = decode(encode(values))
|
|
61
|
+
np.testing.assert_array_equal(decoded, values)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_two_vectors_roundtrip() -> None:
|
|
65
|
+
"""Boundary case: more than VECTOR_SIZE rows so encode produces 2 chunks."""
|
|
66
|
+
n = 1500
|
|
67
|
+
rng = np.random.default_rng(42)
|
|
68
|
+
values = (rng.integers(0, 100_000, size=n).astype(np.float64) / 100.0).tolist()
|
|
69
|
+
decoded = decode(encode(values))
|
|
70
|
+
np.testing.assert_array_equal(decoded, values)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
# --- Per-value encoder / decoder primitives ---------------------------------
|
|
74
|
+
|
|
75
|
+
def test_encode_decode_value_roundtrip_for_2dp() -> None:
|
|
76
|
+
for v in [65007.28, 0.5, 100.0, -42.5]:
|
|
77
|
+
e, f = find_best_exponents(np.asarray([v]), 1, sample_size=4)
|
|
78
|
+
enc = encode_value(v, e, f)
|
|
79
|
+
dec = decode_value(enc, e, f)
|
|
80
|
+
assert struct.pack("<d", dec) == struct.pack("<d", v)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
# --- Bit-packing primitive --------------------------------------------------
|
|
84
|
+
|
|
85
|
+
def test_bit_packing_roundtrip_random_widths() -> None:
|
|
86
|
+
rng = np.random.default_rng(7)
|
|
87
|
+
for bit_width in (1, 5, 8, 13, 24, 33, 60, 64):
|
|
88
|
+
if bit_width >= 64:
|
|
89
|
+
high = 1 << 32
|
|
90
|
+
else:
|
|
91
|
+
high = (1 << bit_width) - 1
|
|
92
|
+
n = 100
|
|
93
|
+
deltas = rng.integers(0, high + 1, size=n, dtype=np.int64)
|
|
94
|
+
frame = -42
|
|
95
|
+
values = deltas + frame
|
|
96
|
+
packed = pack(values, n, frame, bit_width)
|
|
97
|
+
unpacked = unpack(packed, n, frame, bit_width)
|
|
98
|
+
np.testing.assert_array_equal(unpacked, values)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def test_bit_packing_zero_width() -> None:
|
|
102
|
+
values = np.array([7, 7, 7, 7], dtype=np.int64)
|
|
103
|
+
frame = find_frame(values, len(values))
|
|
104
|
+
bit_width = compute_bit_width(values, len(values), frame)
|
|
105
|
+
assert bit_width == 0
|
|
106
|
+
packed = pack(values, len(values), frame, bit_width)
|
|
107
|
+
assert packed == b""
|
|
108
|
+
unpacked = unpack(packed, len(values), frame, bit_width)
|
|
109
|
+
np.testing.assert_array_equal(unpacked, values)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
# --- Cross-implementation: bytes generated by alp-java AlpCompressor --------
|
|
113
|
+
|
|
114
|
+
JAVA_FIXTURES = {
|
|
115
|
+
"ints": (
|
|
116
|
+
[1.0, 2.0, 3.0, 4.0, 5.0],
|
|
117
|
+
"000000050000030000000000000001000000028846",
|
|
118
|
+
),
|
|
119
|
+
"prices_2dp": (
|
|
120
|
+
[65007.28, 65007.31, 65007.30, 65007.32, 65007.32],
|
|
121
|
+
"000000050200030000000000633178000000029848",
|
|
122
|
+
),
|
|
123
|
+
"identical": (
|
|
124
|
+
[3.14, 3.14, 3.14, 3.14],
|
|
125
|
+
"00000004020000000000000000013a00000000",
|
|
126
|
+
),
|
|
127
|
+
"decimal_fractions": (
|
|
128
|
+
[0.1, 0.2, 0.3, 0.4, 0.5],
|
|
129
|
+
"000000050100030000000000000001000000028846",
|
|
130
|
+
),
|
|
131
|
+
"mixed": (
|
|
132
|
+
[-12.5, -12.6, 0.0, 100.0, 1.234],
|
|
133
|
+
"00000005030011ffffffffffffcec80000000b64000000e0c4c0bead6003",
|
|
134
|
+
),
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def test_python_encode_matches_java_byte_for_byte() -> None:
|
|
139
|
+
for label, (values, expected_hex) in JAVA_FIXTURES.items():
|
|
140
|
+
produced = encode(values).hex()
|
|
141
|
+
assert produced == expected_hex, (
|
|
142
|
+
f"{label}: python produced {produced}, java produced {expected_hex}"
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def test_python_decode_of_java_bytes_matches_input() -> None:
|
|
147
|
+
for label, (values, hex_blob) in JAVA_FIXTURES.items():
|
|
148
|
+
decoded = decode(bytes.fromhex(hex_blob))
|
|
149
|
+
_bits_equal(decoded, values)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
# --- Truncation handling -----------------------------------------------------
|
|
153
|
+
|
|
154
|
+
def test_decode_rejects_truncated_count_header() -> None:
|
|
155
|
+
with pytest.raises(ValueError, match="missing 4-byte count"):
|
|
156
|
+
decode(b"\x00\x00")
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def test_decode_rejects_truncated_vector_header() -> None:
|
|
160
|
+
blob = b"\x00\x00\x00\x05\x00\x00\x00" # claims 5 values but only 3 header bytes
|
|
161
|
+
with pytest.raises(ValueError, match="truncated ALP vector header"):
|
|
162
|
+
decode(blob)
|