jsonstat-io 0.1.0
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.
- package/LICENSE +201 -0
- package/README.md +200 -0
- package/dist/arrow/index.cjs +60 -0
- package/dist/arrow/index.cjs.map +1 -0
- package/dist/arrow/index.d.cts +125 -0
- package/dist/arrow/index.d.ts +125 -0
- package/dist/arrow/index.js +3 -0
- package/dist/arrow/index.js.map +1 -0
- package/dist/arrowToCube-BdRWZ5oG.d.cts +29 -0
- package/dist/arrowToCube-Mjr79ZHM.d.ts +29 -0
- package/dist/chunk-2HY7NRKX.js +259 -0
- package/dist/chunk-2HY7NRKX.js.map +1 -0
- package/dist/chunk-62NIY7ML.js +191 -0
- package/dist/chunk-62NIY7ML.js.map +1 -0
- package/dist/chunk-AXUZF6O5.cjs +352 -0
- package/dist/chunk-AXUZF6O5.cjs.map +1 -0
- package/dist/chunk-EFRDFQOT.js +335 -0
- package/dist/chunk-EFRDFQOT.js.map +1 -0
- package/dist/chunk-GDKDYLZ4.cjs +263 -0
- package/dist/chunk-GDKDYLZ4.cjs.map +1 -0
- package/dist/chunk-IDTQZG4B.js +432 -0
- package/dist/chunk-IDTQZG4B.js.map +1 -0
- package/dist/chunk-KFIKGBIJ.cjs +200 -0
- package/dist/chunk-KFIKGBIJ.cjs.map +1 -0
- package/dist/chunk-L65CRXTV.js +64 -0
- package/dist/chunk-L65CRXTV.js.map +1 -0
- package/dist/chunk-PHLCWTHB.cjs +446 -0
- package/dist/chunk-PHLCWTHB.cjs.map +1 -0
- package/dist/chunk-VQCS4LVD.cjs +166 -0
- package/dist/chunk-VQCS4LVD.cjs.map +1 -0
- package/dist/chunk-VYS7DPB3.js +162 -0
- package/dist/chunk-VYS7DPB3.js.map +1 -0
- package/dist/chunk-ZEDTWRDN.cjs +70 -0
- package/dist/chunk-ZEDTWRDN.cjs.map +1 -0
- package/dist/cli/index.cjs +337 -0
- package/dist/cli/index.cjs.map +1 -0
- package/dist/cli/index.d.cts +90 -0
- package/dist/cli/index.d.ts +90 -0
- package/dist/cli/index.js +333 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cubeBuilder-BEGA4C5Y.js +4 -0
- package/dist/cubeBuilder-BEGA4C5Y.js.map +1 -0
- package/dist/cubeBuilder-ZU76VVEX.cjs +25 -0
- package/dist/cubeBuilder-ZU76VVEX.cjs.map +1 -0
- package/dist/cubeReader-7QVUF3TI.js +4 -0
- package/dist/cubeReader-7QVUF3TI.js.map +1 -0
- package/dist/cubeReader-UN2BHQWK.cjs +21 -0
- package/dist/cubeReader-UN2BHQWK.cjs.map +1 -0
- package/dist/index.cjs +172 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +445 -0
- package/dist/index.d.ts +445 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/ir-BArfNxo-.d.cts +143 -0
- package/dist/ir-Jae2ymov.d.ts +143 -0
- package/dist/jsonstat-BX4mBLci.d.cts +179 -0
- package/dist/jsonstat-BX4mBLci.d.ts +179 -0
- package/dist/sources/csv.cjs +40 -0
- package/dist/sources/csv.cjs.map +1 -0
- package/dist/sources/csv.d.cts +112 -0
- package/dist/sources/csv.d.ts +112 -0
- package/dist/sources/csv.js +3 -0
- package/dist/sources/csv.js.map +1 -0
- package/dist/sources/csvw.cjs +236 -0
- package/dist/sources/csvw.cjs.map +1 -0
- package/dist/sources/csvw.d.cts +143 -0
- package/dist/sources/csvw.d.ts +143 -0
- package/dist/sources/csvw.js +230 -0
- package/dist/sources/csvw.js.map +1 -0
- package/dist/sources/duckdb.cjs +121 -0
- package/dist/sources/duckdb.cjs.map +1 -0
- package/dist/sources/duckdb.d.cts +111 -0
- package/dist/sources/duckdb.d.ts +111 -0
- package/dist/sources/duckdb.js +115 -0
- package/dist/sources/duckdb.js.map +1 -0
- package/dist/sources/parquet.cjs +117 -0
- package/dist/sources/parquet.cjs.map +1 -0
- package/dist/sources/parquet.d.cts +83 -0
- package/dist/sources/parquet.d.ts +83 -0
- package/dist/sources/parquet.js +111 -0
- package/dist/sources/parquet.js.map +1 -0
- package/dist/sources/polars.cjs +94 -0
- package/dist/sources/polars.cjs.map +1 -0
- package/dist/sources/polars.d.cts +91 -0
- package/dist/sources/polars.d.ts +91 -0
- package/dist/sources/polars.js +87 -0
- package/dist/sources/polars.js.map +1 -0
- package/docs/api.md +322 -0
- package/docs/architecture.md +140 -0
- package/docs/cli.md +159 -0
- package/docs/formats/arrow.md +111 -0
- package/docs/formats/csv.md +115 -0
- package/docs/formats/csvw.md +152 -0
- package/docs/formats/duckdb.md +151 -0
- package/docs/formats/parquet.md +132 -0
- package/docs/formats/polars.md +111 -0
- package/docs/mapping.md +136 -0
- package/package.json +119 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Xavier Badosa
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
200
|
+
implied. See the License for the specific language governing
|
|
201
|
+
permissions and limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
# jsonstat-io
|
|
2
|
+
|
|
3
|
+
> Convert between [JSON-stat 2.0](https://jsonstat.org/) cubes and the columnar stack — Apache Arrow, Parquet, DuckDB, Polars, CSVW, CSV. An Arrow-bridged, bidirectional interop layer for the lakehouse ecosystem.
|
|
4
|
+
|
|
5
|
+
`jsonstat-io` bridges the **columnar stack** — Arrow, Parquet, DuckDB, Polars — and the **JSON-stat statistical format** in a single, dependency-light package. Because Parquet, DuckDB, and Polars all emit Apache Arrow tables natively, one conversion path serves them all in **both directions**:
|
|
6
|
+
|
|
7
|
+
- **Import:** columnar → JSON-stat (`importToDataset`)
|
|
8
|
+
- **Export:** JSON-stat → columnar (`exportDataset`)
|
|
9
|
+
|
|
10
|
+
This is the *Arrow-hub* insight: **N sources → one Arrow hub → JSON-stat**, and back. The same `arrowToCube` / `cubeToArrow` pair powers every binary format.
|
|
11
|
+
|
|
12
|
+
- **Bidirectional.** Import and export are both first-class, fully-tested directions sharing one round-trip-safe IR.
|
|
13
|
+
- **Pure-TS core.** The cube engine is pure TypeScript, with a documented seam to swap in a Rust/Wasm accelerator later without changing the public API.
|
|
14
|
+
- **Isomorphic.** Works in Node (≥18) and the browser. Heavy format engines (Parquet/DuckDB/Polars) are optional peer dependencies, imported lazily so browser bundles stay lean.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install jsonstat-io
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The only hard runtime dependencies are [`apache-arrow`](https://arrow.apache.org/docs/js/) and [`commander`](https://github.com/tj/commander.js) (CLI only). Format-specific engines are **optional peers** — install only what you use:
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
npm install parquet-wasm # Parquet (browser + Node)
|
|
26
|
+
npm install duckdb-async # DuckDB (Node)
|
|
27
|
+
npm install @duckdb/duckdb-wasm # DuckDB (browser)
|
|
28
|
+
npm install nodejs-polars # Polars (Node only)
|
|
29
|
+
npm install jsonstat-validator # optional output validation
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Quick start
|
|
33
|
+
|
|
34
|
+
### Import — columnar → JSON-stat (Node or browser)
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { importToDataset } from "jsonstat-io";
|
|
38
|
+
|
|
39
|
+
// Auto-detects Parquet from magic bytes → Arrow hub → JSON-stat dataset.
|
|
40
|
+
const dataset = await importToDataset("./sales.parquet");
|
|
41
|
+
console.log(JSON.stringify(dataset, null, 2));
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
One call handles files, URLs, stdin (`"-"`), `Uint8Array`, and `Blob`:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
await importToDataset("https://example.com/data.arrow"); // URL
|
|
48
|
+
await importToDataset(bytes); // Uint8Array
|
|
49
|
+
await importToDataset("./report.csv", { from: "csv" }); // force CSV
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Export — JSON-stat → columnar (Node or browser)
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { exportDataset } from "jsonstat-io";
|
|
56
|
+
|
|
57
|
+
// JSON-stat dataset → Arrow Table
|
|
58
|
+
const table = await exportDataset(dataset, { to: "arrow" });
|
|
59
|
+
|
|
60
|
+
// → Parquet bytes (needs parquet-wasm)
|
|
61
|
+
const parquetBytes = await exportDataset(dataset, { to: "parquet" });
|
|
62
|
+
|
|
63
|
+
// → CSV text + CSVW metadata
|
|
64
|
+
const { csv, metadata } = await exportDataset(dataset, { to: "csvw" });
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### CLI
|
|
68
|
+
|
|
69
|
+
The `--to` flag drives direction: `jsonstat` (default) imports; `arrow|parquet|csv|csvw` exports.
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
# IMPORT: file → JSON-stat, written to stdout
|
|
73
|
+
npx jsonstat-io ./sales.parquet
|
|
74
|
+
|
|
75
|
+
# Write to a file, with a dataset label
|
|
76
|
+
npx jsonstat-io ./sales.parquet -o sales.jsonstat.json --label "Sales 2024"
|
|
77
|
+
|
|
78
|
+
# Pipe CSV on stdin, assign roles explicitly
|
|
79
|
+
cat data.csv | npx jsonstat-io - --measure amount --role time=year,geo=country
|
|
80
|
+
|
|
81
|
+
# EXPORT: JSON-stat → Parquet
|
|
82
|
+
npx jsonstat-io ./sales.jsonstat.json --to parquet -o sales.parquet
|
|
83
|
+
|
|
84
|
+
# JSON-stat → CSV (+ sibling -metadata.json)
|
|
85
|
+
npx jsonstat-io ./sales.jsonstat.json --to csv -o sales.csv
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
See [`docs/cli.md`](./docs/cli.md) for the full CLI reference.
|
|
89
|
+
|
|
90
|
+
## Why Arrow as the hub?
|
|
91
|
+
|
|
92
|
+
| Format | Arrow-native? | Import adapter | Export adapter |
|
|
93
|
+
|----------|:-------------:|--------------------------------------|--------------------------------------|
|
|
94
|
+
| Parquet | ✅ `parquet-wasm` | [`/parquet`](./docs/formats/parquet.md) | [`/parquet`](./docs/formats/parquet.md) |
|
|
95
|
+
| DuckDB | ✅ `.arrow()` | [`/duckdb`](./docs/formats/duckdb.md) | [`/duckdb`](./docs/formats/duckdb.md) |
|
|
96
|
+
| Polars | ✅ `toArrow()` | [`/polars`](./docs/formats/polars.md) | [`/polars`](./docs/formats/polars.md) |
|
|
97
|
+
| Arrow IPC| ✅ (it *is* Arrow)| [`/arrow`](./docs/formats/arrow.md) | [`/arrow`](./docs/formats/arrow.md) |
|
|
98
|
+
| CSVW | ❌ → IR directly | [`/csvw`](./docs/formats/csvw.md) | [`/csvw`](./docs/formats/csvw.md) |
|
|
99
|
+
| Plain CSV| ❌ → IR directly | [`/csv`](./docs/formats/csv.md) | [`/csv`](./docs/formats/csv.md) |
|
|
100
|
+
|
|
101
|
+
Every Arrow-producing format funnels through **one** `arrowToCube` / `cubeToArrow` pair. This means the JSON-stat mapping logic (dimensions, roles, sparse/dense, status) is implemented and tested exactly once per direction, then reused. Adding a new Arrow-native format is a ~30-line adapter.
|
|
102
|
+
|
|
103
|
+
## How it works
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
IMPORT (columnar → JSON-stat)
|
|
107
|
+
┌──────────┐ ┌─────────────┐ ┌──────────┐ ┌──────────────┐
|
|
108
|
+
│ Parquet │──▶│ parquet-wasm│──▶│ │ │ │
|
|
109
|
+
│ DuckDB │──▶│ .arrow() │──▶│ Arrow │──▶│ arrowToCube │──┐
|
|
110
|
+
│ Polars │──▶│ toArrow() │──▶│ Table │ │ (the hub) │ │
|
|
111
|
+
│ Arrow IPC│──▶│ tableFromIPC│──▶│ │ │ │ │
|
|
112
|
+
└──────────┘ └─────────────┘ └──────────┘ └──────────────┘ │
|
|
113
|
+
▼
|
|
114
|
+
┌──────────┐ ┌─────────────┐ ┌────────────────┐
|
|
115
|
+
│ CSVW │──▶│ csvwToCube │─────────────────────────▶│ Observations │
|
|
116
|
+
│ CSV │──▶│ csvToCube │─────────────────────────▶│ IR (tidy long) │
|
|
117
|
+
│ JSON-stat│──▶│ readDataset │─────────────────────────▶│ (round-trip) │
|
|
118
|
+
└──────────┘ └─────────────┘ └───────┬────────┘
|
|
119
|
+
│ buildDataset
|
|
120
|
+
▼
|
|
121
|
+
┌───────────────┐
|
|
122
|
+
│ JSON-stat 2.0 │
|
|
123
|
+
│ Dataset │
|
|
124
|
+
└───────────────┘
|
|
125
|
+
|
|
126
|
+
EXPORT (JSON-stat → columnar)
|
|
127
|
+
JSON-stat Dataset ──▶ readDataset ──▶ Observations IR ──┬─▶ cubeToArrow ──▶ Arrow / Parquet / DuckDB / Polars
|
|
128
|
+
├─▶ cubeToCsv ──▶ CSV text
|
|
129
|
+
└─▶ cubeToCsvw ──▶ CSV + CSVW metadata
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
The central intermediate representation is the **Observations IR** ([`Observations`](./src/model/ir.ts)): a tidy long table with dimension columns, exactly one measure column, and an optional status column. Every source produces it; [`buildDataset`](./src/core/cubeBuilder.ts) scatters it into the row-major JSON-stat cube, and [`readDataset`](./src/core/cubeReader.ts) flattens a cube back into the IR for export.
|
|
133
|
+
|
|
134
|
+
## Architecture at a glance
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
src/
|
|
138
|
+
├── model/ # Pure types: JSON-stat + Observations IR (zero runtime)
|
|
139
|
+
├── core/ # The engine: strides math, cube builder, cube reader
|
|
140
|
+
│ ├── strides.ts # Row-major stride arithmetic (flatPosition, multiIndex)
|
|
141
|
+
│ ├── cubeBuilder.ts # Observations IR → JSON-stat Dataset (import)
|
|
142
|
+
│ └── cubeReader.ts # JSON-stat Dataset → Observations IR (export)
|
|
143
|
+
├── arrow/ # The Arrow hub (bidirectional)
|
|
144
|
+
│ ├── schemaMeta.ts # jsonstat.* metadata key contract
|
|
145
|
+
│ ├── arrowToCube.ts # Arrow Table → Observations IR (import)
|
|
146
|
+
│ └── arrowFromCube.ts# Observations IR → Arrow Table (export)
|
|
147
|
+
├── sources/ # Per-format adapters (optional peers, lazy, bidirectional)
|
|
148
|
+
│ ├── parquet.ts duckdb.ts polars.ts csvw.ts csv.ts
|
|
149
|
+
├── sink/ # serialize.ts — JSON-stat → canonical JSON string/bytes
|
|
150
|
+
├── util/ # detect.ts (format sniffing), fetch.ts (loading), density.ts
|
|
151
|
+
├── cli/ # args.ts (parsing) + index.ts (commander entry)
|
|
152
|
+
└── index.ts # Public API barrel + importToDataset / exportDataset
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
See [`docs/architecture.md`](./docs/architecture.md) for the layered design rationale, [`docs/mapping.md`](./docs/mapping.md) for the spec-fidelity mapping table, and [`docs/api.md`](./docs/api.md) for the full API reference.
|
|
156
|
+
|
|
157
|
+
## Spec fidelity
|
|
158
|
+
|
|
159
|
+
`jsonstat-io` preserves the full JSON-stat 2.0 model, not just values, in both directions:
|
|
160
|
+
|
|
161
|
+
- **Roles** (`time`, `geo`, `metric`) — from Arrow schema metadata or explicit options.
|
|
162
|
+
- **Category labels, units, coordinates, child hierarchies** — round-tripped via `jsonstat.*` metadata keys.
|
|
163
|
+
- **Dense vs sparse value forms** — auto-decided by null ratio, or forced via options.
|
|
164
|
+
- **Status** (string / array / object forms) — emitted per-row, deduplicated when uniform.
|
|
165
|
+
- **Canonical key ordering** — the serializer reorders top-level keys to the canonical order for diff-stable output.
|
|
166
|
+
|
|
167
|
+
See [`docs/mapping.md`](./docs/mapping.md) for the complete fidelity table.
|
|
168
|
+
|
|
169
|
+
## Node vs Browser
|
|
170
|
+
|
|
171
|
+
| Capability | Node ≥18 | Browser |
|
|
172
|
+
|-------------------------|:--------:|:-------:|
|
|
173
|
+
| Arrow IPC | ✅ | ✅ |
|
|
174
|
+
| Parquet (`parquet-wasm`)| ✅ | ✅ |
|
|
175
|
+
| DuckDB (wasm) | ✅ | ✅ |
|
|
176
|
+
| DuckDB (native) | ✅ | — |
|
|
177
|
+
| Polars | ✅ | — |
|
|
178
|
+
| CSVW / CSV | ✅ | ✅ |
|
|
179
|
+
| CLI | ✅ | — |
|
|
180
|
+
| File paths / stdin | ✅ | — |
|
|
181
|
+
|
|
182
|
+
In the browser, pass `Uint8Array` or `Blob` directly; the library never touches `node:fs`.
|
|
183
|
+
|
|
184
|
+
## Documented seams
|
|
185
|
+
|
|
186
|
+
1. **Rust/Wasm accelerator:** The pure-TS stride math and value scattering in `core/` is the performance-critical path. The public API (`buildDataset`, `arrowToCube`, `exportDataset`) is stable; an accelerator can replace the internals behind the same signatures.
|
|
187
|
+
|
|
188
|
+
## Testing
|
|
189
|
+
|
|
190
|
+
159 tests cover the stride math, cube builder/reader, Arrow hub round-trips, JSON-stat round-trips, export round-trips (Arrow, CSV, CSVW, Parquet), format detection, density decisions, serialization, and CLI argument parsing:
|
|
191
|
+
|
|
192
|
+
```sh
|
|
193
|
+
npm test # vitest run
|
|
194
|
+
npm run typecheck # tsc --noEmit
|
|
195
|
+
npm run build # tsup (ESM + CJS + .d.ts)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## License
|
|
199
|
+
|
|
200
|
+
Apache-2.0 © Xavier Badosa
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkPHLCWTHB_cjs = require('../chunk-PHLCWTHB.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "ArrowConversionError", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () { return chunkPHLCWTHB_cjs.ArrowConversionError; }
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(exports, "META_PREFIX", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return chunkPHLCWTHB_cjs.META_PREFIX; }
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(exports, "arrowToCube", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return chunkPHLCWTHB_cjs.arrowToCube; }
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports, "arrowToDataset", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return chunkPHLCWTHB_cjs.arrowToDataset; }
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports, "buildFieldMeta", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function () { return chunkPHLCWTHB_cjs.buildFieldMeta; }
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(exports, "buildSchemaMeta", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () { return chunkPHLCWTHB_cjs.buildSchemaMeta; }
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, "cubeToArrow", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () { return chunkPHLCWTHB_cjs.cubeToArrow; }
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(exports, "getFieldMeta", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: function () { return chunkPHLCWTHB_cjs.getFieldMeta; }
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(exports, "getFieldMetaJson", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
get: function () { return chunkPHLCWTHB_cjs.getFieldMetaJson; }
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(exports, "getFieldRole", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function () { return chunkPHLCWTHB_cjs.getFieldRole; }
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(exports, "isMeasureField", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
get: function () { return chunkPHLCWTHB_cjs.isMeasureField; }
|
|
50
|
+
});
|
|
51
|
+
Object.defineProperty(exports, "isStatusField", {
|
|
52
|
+
enumerable: true,
|
|
53
|
+
get: function () { return chunkPHLCWTHB_cjs.isStatusField; }
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(exports, "readSchemaMeta", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
get: function () { return chunkPHLCWTHB_cjs.readSchemaMeta; }
|
|
58
|
+
});
|
|
59
|
+
//# sourceMappingURL=index.cjs.map
|
|
60
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
export { a as ArrowConversionError, A as ArrowToCubeOptions, b as arrowToCube, c as arrowToDataset } from '../arrowToCube-BdRWZ5oG.cjs';
|
|
2
|
+
import { Table, Field, Schema } from 'apache-arrow';
|
|
3
|
+
import { O as Observations, R as RoleMap, D as DatasetMeta } from '../ir-BArfNxo-.cjs';
|
|
4
|
+
import '../jsonstat-BX4mBLci.cjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* arrowFromCube — the inverse of [`arrowToCube`](./arrowToCube.ts): converts the
|
|
8
|
+
* [`Observations`](../model/ir.ts) IR into an Apache Arrow [`Table`].
|
|
9
|
+
*
|
|
10
|
+
* ## Role
|
|
11
|
+
*
|
|
12
|
+
* This is the **Phase-2 export seam**: the forward path from JSON-stat to the
|
|
13
|
+
* columnar lakehouse stack. Export = [`readDataset`](../core/cubeReader.ts)
|
|
14
|
+
* (JSON-stat → IR) → `cubeToArrow` (IR → Arrow) → Arrow-to-Parquet/DuckDB/
|
|
15
|
+
* Polars/CSV writers (which Arrow ecosystems provide natively).
|
|
16
|
+
*
|
|
17
|
+
* It is shipped in v0.1 (alongside [`cubeReader`](../core/cubeReader.ts)) for
|
|
18
|
+
* two reasons: (1) the round-trip tests use it to assert that JSON-stat → IR →
|
|
19
|
+
* Arrow → IR → JSON-stat is stable; (2) proving the IR is bidirectional now
|
|
20
|
+
* de-risks Phase 2 to "just write the sink writers".
|
|
21
|
+
*
|
|
22
|
+
* ## Mapping
|
|
23
|
+
*
|
|
24
|
+
* - Each dimension → a dictionary-encoded Utf8 column (dictionary = the
|
|
25
|
+
* resolved category list), annotated with `jsonstat.*` field metadata so the
|
|
26
|
+
* round-trip preserves labels/units/roles/hierarchies.
|
|
27
|
+
* - The measure → a Float64 column (nulls preserved), marked
|
|
28
|
+
* `jsonstat.measure=true`.
|
|
29
|
+
* - The status column (if any) → a Utf8 column marked `jsonstat.status=true`.
|
|
30
|
+
* - Dataset-level metadata (label/source/updated/extension/roles/valueForm) is
|
|
31
|
+
* written to the schema metadata.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Convert the [`Observations`](../model/ir.ts) IR into an Arrow [`Table`].
|
|
36
|
+
*
|
|
37
|
+
* Each dimension becomes a dictionary column; the measure becomes a Float64
|
|
38
|
+
* column; the optional status becomes a Utf8 column. All `jsonstat.*`
|
|
39
|
+
* metadata is attached so the result round-trips through [`arrowToCube`](./arrowToCube.ts).
|
|
40
|
+
*
|
|
41
|
+
* @throws Error if a dimension column referenced by the model is missing.
|
|
42
|
+
*/
|
|
43
|
+
declare function cubeToArrow(obs: Observations): Table;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Arrow schema metadata contract for JSON-stat.
|
|
47
|
+
*
|
|
48
|
+
* Producers (Parquet/DuckDB/Polars/CSVW adapters, or hand-built Arrow tables)
|
|
49
|
+
* annotate Arrow [`Field`](https://arrow.apache.org/docs/js/) metadata with
|
|
50
|
+
* `jsonstat.*` keys so that [`arrowToCube`](./arrowToCube.ts) can reconstruct
|
|
51
|
+
* the full JSON-stat model — not just the raw values. Without these keys the
|
|
52
|
+
* converter falls back to heuristic inference (dictionary columns = dimensions,
|
|
53
|
+
* first numeric column = measure); with them, the mapping is exact and
|
|
54
|
+
* lossless, which is what the round-trip tests require.
|
|
55
|
+
*
|
|
56
|
+
* ## Metadata key reference
|
|
57
|
+
*
|
|
58
|
+
* Field-level keys (on `Field.metadata`):
|
|
59
|
+
*
|
|
60
|
+
* | Key | Value | Meaning |
|
|
61
|
+
* |------------------------------|----------------------------------------|---------|
|
|
62
|
+
* | `jsonstat.role` | `"time"` / `"geo"` / `"metric"` | Dimension role |
|
|
63
|
+
* | `jsonstat.measure` | `"true"` | Marks this column as the measure (overrides type heuristics) |
|
|
64
|
+
* | `jsonstat.status` | `"true"` | Marks this column as the per-row status column |
|
|
65
|
+
* | `jsonstat.label` | string | Dimension label |
|
|
66
|
+
* | `jsonstat.categoryLabels` | JSON object: `{id: label}` | Category labels |
|
|
67
|
+
* | `jsonstat.categoryUnits` | JSON object: `{id: {decimals,...}}` | Per-category unit metadata (metric role) |
|
|
68
|
+
* | `jsonstat.categoryCoords` | JSON object: `{id: [lon,lat]}` | Per-category coordinates (geo role) |
|
|
69
|
+
* | `jsonstat.categoryChild` | JSON object: `{parent: [child,...]}` | Hierarchy |
|
|
70
|
+
* | `jsonstat.categoryOrder` | JSON array: `[id,...]` | Explicit category ordering (e.g. chronological) |
|
|
71
|
+
*
|
|
72
|
+
* Schema-level keys (on `Schema.metadata`):
|
|
73
|
+
*
|
|
74
|
+
* | Key | Value | Meaning |
|
|
75
|
+
* |------------------------------|----------------------------------------|---------|
|
|
76
|
+
* | `jsonstat.label` | string | Dataset label |
|
|
77
|
+
* | `jsonstat.source` | string | Dataset source |
|
|
78
|
+
* | `jsonstat.updated` | ISO 8601 string | Dataset update time |
|
|
79
|
+
* | `jsonstat.extension` | JSON object | Provider extension data |
|
|
80
|
+
* | `jsonstat.valueForm` | `"auto"`/`"dense"`/`"sparse"` | Value emission hint |
|
|
81
|
+
*
|
|
82
|
+
* See [docs/mapping.md](../../docs/mapping.md) for the full fidelity table.
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
/** Metadata namespace prefix used for all JSON-stat keys. */
|
|
86
|
+
declare const META_PREFIX = "jsonstat.";
|
|
87
|
+
/** Read a single string metadata value from a field. */
|
|
88
|
+
declare function getFieldMeta(field: Field, key: string): string | undefined;
|
|
89
|
+
/** Read and JSON-parse a field metadata value, with a fallback. */
|
|
90
|
+
declare function getFieldMetaJson<T>(field: Field, key: string): T | undefined;
|
|
91
|
+
/** Is this field marked as the measure column? */
|
|
92
|
+
declare function isMeasureField(field: Field): boolean;
|
|
93
|
+
/** Is this field marked as the status column? */
|
|
94
|
+
declare function isStatusField(field: Field): boolean;
|
|
95
|
+
/** Read the dimension role assigned to a field, if any. */
|
|
96
|
+
declare function getFieldRole(field: Field): "time" | "geo" | "metric" | undefined;
|
|
97
|
+
/** Build a metadata Map for a field from JSON-stat-oriented hints. */
|
|
98
|
+
declare function buildFieldMeta(hints: {
|
|
99
|
+
role?: "time" | "geo" | "metric";
|
|
100
|
+
measure?: boolean;
|
|
101
|
+
status?: boolean;
|
|
102
|
+
label?: string;
|
|
103
|
+
categoryLabels?: Record<string, string>;
|
|
104
|
+
categoryUnits?: Record<string, unknown>;
|
|
105
|
+
categoryCoords?: Record<string, [number, number]>;
|
|
106
|
+
categoryChild?: Record<string, string[]>;
|
|
107
|
+
categoryOrder?: string[];
|
|
108
|
+
}): Map<string, string>;
|
|
109
|
+
/** Read dataset-level metadata from a schema. */
|
|
110
|
+
declare function readSchemaMeta(schema: Schema): {
|
|
111
|
+
meta?: DatasetMeta;
|
|
112
|
+
roles?: RoleMap;
|
|
113
|
+
valueForm?: "auto" | "dense" | "sparse";
|
|
114
|
+
};
|
|
115
|
+
/** Build a schema-level metadata Map from dataset hints. */
|
|
116
|
+
declare function buildSchemaMeta(hints: {
|
|
117
|
+
label?: string;
|
|
118
|
+
source?: string;
|
|
119
|
+
updated?: string;
|
|
120
|
+
extension?: Record<string, unknown>;
|
|
121
|
+
roles?: RoleMap;
|
|
122
|
+
valueForm?: "auto" | "dense" | "sparse";
|
|
123
|
+
}): Map<string, string>;
|
|
124
|
+
|
|
125
|
+
export { META_PREFIX, buildFieldMeta, buildSchemaMeta, cubeToArrow, getFieldMeta, getFieldMetaJson, getFieldRole, isMeasureField, isStatusField, readSchemaMeta };
|