Skip to content

Commit a4ec8ed

Browse files
committed
refactor: deduplicate QMCB dtype constants — r2-spill.ts imports from columnar.ts
DTYPE_F64/I64/UTF8/BOOL/F32VEC/NULL and QMCB_MAGIC were defined independently in both files with identical values. If either drifted, spill encode/decode would silently corrupt. Now single source of truth in columnar.ts.
1 parent 53ff289 commit a4ec8ed

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

src/columnar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import type { Row } from "./types.js";
1818
const textDecoder = new TextDecoder();
1919
const textEncoder = new TextEncoder();
2020

21-
const QMCB_MAGIC = 0x42434D51; // "QMCB" little-endian
21+
export const QMCB_MAGIC = 0x42434D51; // "QMCB" little-endian
2222

23-
// QMCB dtype tags (extends r2-spill.ts)
23+
// QMCB dtype tags (shared with r2-spill.ts)
2424
export const DTYPE_F64 = 0;
2525
export const DTYPE_I64 = 1;
2626
export const DTYPE_UTF8 = 2;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export {
4545
wasmResultToQMCB, encodeColumnarBatch, decodeColumnarBatch,
4646
columnarBatchToRows, columnarKWayMerge, concatColumnarBatches,
4747
sliceColumnarBatch, concatQMCBBatches, readColumnValue,
48-
DTYPE_F64, DTYPE_I64, DTYPE_UTF8, DTYPE_BOOL, DTYPE_F32VEC,
48+
QMCB_MAGIC, DTYPE_F64, DTYPE_I64, DTYPE_UTF8, DTYPE_BOOL, DTYPE_F32VEC,
4949
DTYPE_NULL, DTYPE_I32, DTYPE_F32,
5050
} from "./columnar.js";
5151
export type { ColumnarColumn, ColumnarBatch } from "./columnar.js";

src/r2-spill.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,15 @@
3131

3232
import type { Row } from "./types.js";
3333
import { withRetry, withTimeout } from "./coalesce.js";
34+
import {
35+
QMCB_MAGIC,
36+
DTYPE_F64, DTYPE_I64, DTYPE_UTF8, DTYPE_BOOL, DTYPE_F32VEC, DTYPE_NULL,
37+
} from "./columnar.js";
3438

3539
const R2_SPILL_TIMEOUT_MS = 10_000;
36-
const MAGIC = 0x42434D51; // "QMCB" little-endian
3740
const encoder = new TextEncoder();
3841
const decoder = new TextDecoder();
3942

40-
// Dtype tags
41-
const DTYPE_F64 = 0;
42-
const DTYPE_I64 = 1;
43-
const DTYPE_UTF8 = 2;
44-
const DTYPE_BOOL = 3;
45-
const DTYPE_F32VEC = 4;
46-
const DTYPE_NULL = 5;
47-
4843
/** Generic spill backend for sort/join operators. */
4944
export interface SpillBackend {
5045
/** Write a sorted run of rows. Returns a spill ID for later streaming. */
@@ -80,7 +75,7 @@ export function encodeColumnarRun(rows: Row[]): ArrayBuffer {
8075
// Empty run: just header
8176
const buf = new ArrayBuffer(10);
8277
const view = new DataView(buf);
83-
view.setUint32(0, MAGIC, true);
78+
view.setUint32(0, QMCB_MAGIC, true);
8479
view.setUint32(4, 0, true);
8580
view.setUint16(8, 0, true);
8681
return buf;
@@ -200,7 +195,7 @@ export function encodeColumnarRun(rows: Row[]): ArrayBuffer {
200195
let offset = 0;
201196

202197
// Header
203-
view.setUint32(offset, MAGIC, true); offset += 4;
198+
view.setUint32(offset, QMCB_MAGIC, true); offset += 4;
204199
view.setUint32(offset, rowCount, true); offset += 4;
205200
view.setUint16(offset, columnCount, true); offset += 2;
206201

@@ -315,7 +310,7 @@ export function* decodeColumnarRun(buf: ArrayBuffer): Generator<Row> {
315310
const bytes = new Uint8Array(buf);
316311

317312
const magic = view.getUint32(0, true);
318-
if (magic !== MAGIC) throw new Error("Invalid spill file: bad magic");
313+
if (magic !== QMCB_MAGIC) throw new Error("Invalid spill file: bad magic");
319314

320315
const rowCount = view.getUint32(4, true);
321316
const columnCount = view.getUint16(8, true);

0 commit comments

Comments
 (0)