@housekit/orm 0.1.47 → 0.1.49

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.
Files changed (79) hide show
  1. package/README.md +120 -5
  2. package/dist/builders/delete.js +112 -0
  3. package/dist/builders/insert.d.ts +0 -91
  4. package/dist/builders/insert.js +393 -0
  5. package/dist/builders/prepared.d.ts +1 -2
  6. package/dist/builders/prepared.js +30 -0
  7. package/dist/builders/select.d.ts +0 -161
  8. package/dist/builders/select.js +562 -0
  9. package/dist/builders/select.types.js +1 -0
  10. package/dist/builders/update.js +136 -0
  11. package/dist/client.d.ts +0 -6
  12. package/dist/client.js +140 -0
  13. package/dist/codegen/zod.js +107 -0
  14. package/dist/column.d.ts +1 -25
  15. package/dist/column.js +133 -0
  16. package/dist/compiler.d.ts +0 -7
  17. package/dist/compiler.js +513 -0
  18. package/dist/core.js +6 -0
  19. package/dist/data-types.d.ts +0 -61
  20. package/dist/data-types.js +127 -0
  21. package/dist/dictionary.d.ts +0 -149
  22. package/dist/dictionary.js +158 -0
  23. package/dist/engines.d.ts +0 -385
  24. package/dist/engines.js +292 -0
  25. package/dist/expressions.d.ts +0 -10
  26. package/dist/expressions.js +268 -0
  27. package/dist/external.d.ts +0 -112
  28. package/dist/external.js +224 -0
  29. package/dist/index.d.ts +0 -51
  30. package/dist/index.js +139 -6853
  31. package/dist/logger.js +36 -0
  32. package/dist/materialized-views.d.ts +0 -188
  33. package/dist/materialized-views.js +380 -0
  34. package/dist/metadata.js +59 -0
  35. package/dist/modules/aggregates.d.ts +0 -164
  36. package/dist/modules/aggregates.js +121 -0
  37. package/dist/modules/array.d.ts +0 -98
  38. package/dist/modules/array.js +71 -0
  39. package/dist/modules/conditional.d.ts +0 -84
  40. package/dist/modules/conditional.js +138 -0
  41. package/dist/modules/conversion.d.ts +0 -147
  42. package/dist/modules/conversion.js +109 -0
  43. package/dist/modules/geo.d.ts +0 -164
  44. package/dist/modules/geo.js +112 -0
  45. package/dist/modules/hash.js +4 -0
  46. package/dist/modules/index.js +12 -0
  47. package/dist/modules/json.d.ts +0 -106
  48. package/dist/modules/json.js +76 -0
  49. package/dist/modules/math.d.ts +0 -16
  50. package/dist/modules/math.js +16 -0
  51. package/dist/modules/string.d.ts +0 -136
  52. package/dist/modules/string.js +89 -0
  53. package/dist/modules/time.d.ts +0 -123
  54. package/dist/modules/time.js +91 -0
  55. package/dist/modules/types.d.ts +0 -133
  56. package/dist/modules/types.js +114 -0
  57. package/dist/modules/window.js +140 -0
  58. package/dist/relational.d.ts +0 -82
  59. package/dist/relational.js +290 -0
  60. package/dist/relations.js +21 -0
  61. package/dist/schema-builder.d.ts +0 -90
  62. package/dist/schema-builder.js +140 -0
  63. package/dist/table.d.ts +0 -42
  64. package/dist/table.js +406 -0
  65. package/dist/utils/background-batcher.js +75 -0
  66. package/dist/utils/batch-transform.js +51 -0
  67. package/dist/utils/binary-reader.d.ts +0 -6
  68. package/dist/utils/binary-reader.js +334 -0
  69. package/dist/utils/binary-serializer.d.ts +0 -125
  70. package/dist/utils/binary-serializer.js +637 -0
  71. package/dist/utils/binary-worker-code.js +1 -0
  72. package/dist/utils/binary-worker-pool.d.ts +0 -34
  73. package/dist/utils/binary-worker-pool.js +206 -0
  74. package/dist/utils/binary-worker.d.ts +0 -11
  75. package/dist/utils/binary-worker.js +63 -0
  76. package/dist/utils/insert-processing.d.ts +0 -2
  77. package/dist/utils/insert-processing.js +163 -0
  78. package/dist/utils/lru-cache.js +30 -0
  79. package/package.json +68 -3
@@ -0,0 +1,637 @@
1
+ const hasNativeUUID = typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function';
2
+ let uuidv4Fn = null;
3
+ function generateUUIDv4() {
4
+ if (hasNativeUUID)
5
+ return crypto.randomUUID();
6
+ if (uuidv4Fn)
7
+ return uuidv4Fn();
8
+ const uuid = require('uuid');
9
+ uuidv4Fn = uuid.v4;
10
+ return uuidv4Fn();
11
+ }
12
+ const WRITER_POOL_SIZE = 8;
13
+ const writerPool = [];
14
+ let poolInitialized = false;
15
+ function initWriterPool() {
16
+ if (poolInitialized)
17
+ return;
18
+ for (let i = 0; i < WRITER_POOL_SIZE; i++) {
19
+ writerPool.push(new BinaryWriter(64 * 1024));
20
+ }
21
+ poolInitialized = true;
22
+ }
23
+ export function acquireWriter() {
24
+ initWriterPool();
25
+ const writer = writerPool.pop();
26
+ if (writer) {
27
+ writer.reset();
28
+ return writer;
29
+ }
30
+ return new BinaryWriter(64 * 1024);
31
+ }
32
+ export function releaseWriter(writer) {
33
+ if (writerPool.length < WRITER_POOL_SIZE) {
34
+ writer.reset();
35
+ writerPool.push(writer);
36
+ }
37
+ }
38
+ export class BinaryWriter {
39
+ buffer;
40
+ offset = 0;
41
+ constructor(initialSize = 4096) {
42
+ this.buffer = Buffer.allocUnsafe(initialSize);
43
+ }
44
+ ensureCapacity(bytes) {
45
+ const required = this.offset + bytes;
46
+ if (required > this.buffer.length) {
47
+ const newSize = Math.max(this.buffer.length * 2, required);
48
+ const newBuffer = Buffer.allocUnsafe(newSize);
49
+ this.buffer.copy(newBuffer, 0, 0, this.offset);
50
+ this.buffer = newBuffer;
51
+ }
52
+ }
53
+ reset() {
54
+ this.offset = 0;
55
+ }
56
+ getBuffer() {
57
+ return this.buffer.subarray(0, this.offset);
58
+ }
59
+ toBuffer() {
60
+ return Buffer.from(this.buffer.subarray(0, this.offset));
61
+ }
62
+ writeInt8(value) {
63
+ this.ensureCapacity(1);
64
+ this.buffer.writeInt8(value, this.offset);
65
+ this.offset += 1;
66
+ }
67
+ writeUInt8(value) {
68
+ this.ensureCapacity(1);
69
+ this.buffer.writeUInt8(value, this.offset);
70
+ this.offset += 1;
71
+ }
72
+ writeInt16(value) {
73
+ this.ensureCapacity(2);
74
+ this.buffer.writeInt16LE(value, this.offset);
75
+ this.offset += 2;
76
+ }
77
+ writeUInt16(value) {
78
+ this.ensureCapacity(2);
79
+ this.buffer.writeUInt16LE(value, this.offset);
80
+ this.offset += 2;
81
+ }
82
+ writeInt32(value) {
83
+ this.ensureCapacity(4);
84
+ this.buffer.writeInt32LE(value, this.offset);
85
+ this.offset += 4;
86
+ }
87
+ writeUInt32(value) {
88
+ this.ensureCapacity(4);
89
+ this.buffer.writeUInt32LE(value, this.offset);
90
+ this.offset += 4;
91
+ }
92
+ writeInt64(value) {
93
+ this.ensureCapacity(8);
94
+ this.buffer.writeBigInt64LE(BigInt(value), this.offset);
95
+ this.offset += 8;
96
+ }
97
+ writeUInt64(value) {
98
+ this.ensureCapacity(8);
99
+ this.buffer.writeBigUInt64LE(BigInt(value), this.offset);
100
+ this.offset += 8;
101
+ }
102
+ writeInt128(value) {
103
+ this.ensureCapacity(16);
104
+ const low = value & BigInt('0xFFFFFFFFFFFFFFFF');
105
+ const high = value >> BigInt(64);
106
+ this.buffer.writeBigUInt64LE(low, this.offset);
107
+ this.buffer.writeBigInt64LE(high, this.offset + 8);
108
+ this.offset += 16;
109
+ }
110
+ writeUInt128(value) {
111
+ this.ensureCapacity(16);
112
+ const low = value & BigInt('0xFFFFFFFFFFFFFFFF');
113
+ const high = value >> BigInt(64);
114
+ this.buffer.writeBigUInt64LE(low, this.offset);
115
+ this.buffer.writeBigUInt64LE(high, this.offset + 8);
116
+ this.offset += 16;
117
+ }
118
+ writeInt256(value) {
119
+ this.ensureCapacity(32);
120
+ for (let i = 0; i < 4; i++) {
121
+ const word = value & BigInt('0xFFFFFFFFFFFFFFFF');
122
+ this.buffer.writeBigUInt64LE(word, this.offset + i * 8);
123
+ value >>= BigInt(64);
124
+ }
125
+ this.offset += 32;
126
+ }
127
+ writeUInt256(value) {
128
+ this.writeInt256(value);
129
+ }
130
+ writeFloat32(value) {
131
+ this.ensureCapacity(4);
132
+ this.buffer.writeFloatLE(value, this.offset);
133
+ this.offset += 4;
134
+ }
135
+ writeFloat64(value) {
136
+ this.ensureCapacity(8);
137
+ this.buffer.writeDoubleLE(value, this.offset);
138
+ this.offset += 8;
139
+ }
140
+ writeVarInt(value) {
141
+ if (value < 0x80) {
142
+ this.ensureCapacity(1);
143
+ this.buffer[this.offset++] = value;
144
+ return;
145
+ }
146
+ if (value < 0x4000) {
147
+ this.ensureCapacity(2);
148
+ this.buffer[this.offset++] = (value & 0x7f) | 0x80;
149
+ this.buffer[this.offset++] = value >> 7;
150
+ return;
151
+ }
152
+ if (value < 0x200000) {
153
+ this.ensureCapacity(3);
154
+ this.buffer[this.offset++] = (value & 0x7f) | 0x80;
155
+ this.buffer[this.offset++] = ((value >> 7) & 0x7f) | 0x80;
156
+ this.buffer[this.offset++] = value >> 14;
157
+ return;
158
+ }
159
+ this.ensureCapacity(10);
160
+ while (value >= 0x80) {
161
+ this.buffer[this.offset++] = (value & 0x7f) | 0x80;
162
+ value >>>= 7;
163
+ }
164
+ this.buffer[this.offset++] = value;
165
+ }
166
+ writeString(value) {
167
+ const strBuffer = Buffer.from(value, 'utf-8');
168
+ this.writeVarInt(strBuffer.length);
169
+ this.ensureCapacity(strBuffer.length);
170
+ strBuffer.copy(this.buffer, this.offset);
171
+ this.offset += strBuffer.length;
172
+ }
173
+ writeFixedString(value, length) {
174
+ this.ensureCapacity(length);
175
+ const strBuffer = Buffer.from(value, 'utf-8');
176
+ const copyLen = Math.min(strBuffer.length, length);
177
+ strBuffer.copy(this.buffer, this.offset, 0, copyLen);
178
+ if (copyLen < length) {
179
+ this.buffer.fill(0, this.offset + copyLen, this.offset + length);
180
+ }
181
+ this.offset += length;
182
+ }
183
+ writeBytes(data) {
184
+ this.ensureCapacity(data.length);
185
+ data.copy(this.buffer, this.offset);
186
+ this.offset += data.length;
187
+ }
188
+ writeUUID(value) {
189
+ this.ensureCapacity(16);
190
+ const hex = value.replace(/-/g, '');
191
+ if (hex.length !== 32) {
192
+ throw new Error(`Invalid UUID: ${value}`);
193
+ }
194
+ const bytes = Buffer.from(hex, 'hex');
195
+ for (let i = 7; i >= 0; i--) {
196
+ this.buffer[this.offset++] = bytes[i];
197
+ }
198
+ for (let i = 15; i >= 8; i--) {
199
+ this.buffer[this.offset++] = bytes[i];
200
+ }
201
+ }
202
+ writeDate(value) {
203
+ const days = typeof value === 'number'
204
+ ? value
205
+ : Math.floor(value.getTime() / (1000 * 60 * 60 * 24));
206
+ this.writeUInt16(days);
207
+ }
208
+ writeDate32(value) {
209
+ const days = typeof value === 'number'
210
+ ? value
211
+ : Math.floor(value.getTime() / (1000 * 60 * 60 * 24));
212
+ this.writeInt32(days);
213
+ }
214
+ writeDateTime(value) {
215
+ const seconds = typeof value === 'number'
216
+ ? value
217
+ : Math.floor(value.getTime() / 1000);
218
+ this.writeUInt32(seconds);
219
+ }
220
+ writeDateTime64(value, precision = 3) {
221
+ const multiplier = Math.pow(10, precision);
222
+ const ticks = typeof value === 'number'
223
+ ? BigInt(Math.round(value * multiplier))
224
+ : BigInt(Math.round(value.getTime() * multiplier / 1000));
225
+ this.writeInt64(ticks);
226
+ }
227
+ writeNullable(isNull) {
228
+ this.writeUInt8(isNull ? 1 : 0);
229
+ }
230
+ writeArrayLength(length) {
231
+ this.writeUInt64(length);
232
+ }
233
+ writeDecimal32(value, scale) {
234
+ const scaled = Math.round(value * Math.pow(10, scale));
235
+ this.writeInt32(scaled);
236
+ }
237
+ writeDecimal64(value, scale) {
238
+ const scaled = BigInt(Math.round(value * Math.pow(10, scale)));
239
+ this.writeInt64(scaled);
240
+ }
241
+ writeDecimal128(value, scale) {
242
+ let scaled;
243
+ if (typeof value === 'bigint') {
244
+ scaled = value * BigInt(Math.pow(10, scale));
245
+ }
246
+ else {
247
+ scaled = BigInt(Math.round(value * Math.pow(10, scale)));
248
+ }
249
+ this.writeInt128(scaled);
250
+ }
251
+ writeBool(value) {
252
+ this.writeUInt8(value ? 1 : 0);
253
+ }
254
+ writeIPv4(value) {
255
+ if (typeof value === 'number') {
256
+ this.writeUInt32(value);
257
+ return;
258
+ }
259
+ const parts = value.split('.').map(Number);
260
+ if (parts.length !== 4) {
261
+ throw new Error(`Invalid IPv4: ${value}`);
262
+ }
263
+ const num = (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3];
264
+ this.writeUInt32(num >>> 0);
265
+ }
266
+ writeIPv6(value) {
267
+ if (Buffer.isBuffer(value)) {
268
+ if (value.length !== 16) {
269
+ throw new Error('IPv6 must be 16 bytes');
270
+ }
271
+ this.writeBytes(value);
272
+ return;
273
+ }
274
+ const expanded = this.expandIPv6(value);
275
+ const parts = expanded.split(':');
276
+ this.ensureCapacity(16);
277
+ for (const part of parts) {
278
+ const num = parseInt(part, 16);
279
+ this.buffer.writeUInt16BE(num, this.offset);
280
+ this.offset += 2;
281
+ }
282
+ }
283
+ expandIPv6(ip) {
284
+ const parts = ip.split('::');
285
+ if (parts.length === 1) {
286
+ return ip;
287
+ }
288
+ const left = parts[0] ? parts[0].split(':') : [];
289
+ const right = parts[1] ? parts[1].split(':') : [];
290
+ const missing = 8 - left.length - right.length;
291
+ const middle = Array(missing).fill('0000');
292
+ return [...left, ...middle, ...right].map(p => p.padStart(4, '0')).join(':');
293
+ }
294
+ writeEnum8(value) {
295
+ this.writeInt8(value);
296
+ }
297
+ writeEnum16(value) {
298
+ this.writeInt16(value);
299
+ }
300
+ }
301
+ function parseGenericTypes(typeString) {
302
+ const args = [];
303
+ let current = '';
304
+ let parenDepth = 0;
305
+ for (let i = 0; i < typeString.length; i++) {
306
+ const char = typeString[i];
307
+ if (char === ',' && parenDepth === 0) {
308
+ args.push(current.trim());
309
+ current = '';
310
+ }
311
+ else {
312
+ if (char === '(')
313
+ parenDepth++;
314
+ if (char === ')')
315
+ parenDepth--;
316
+ current += char;
317
+ }
318
+ }
319
+ if (current.trim()) {
320
+ args.push(current.trim());
321
+ }
322
+ return args;
323
+ }
324
+ export function createBinaryEncoder(clickhouseType, isNullable = false) {
325
+ const type = clickhouseType.toLowerCase().trim();
326
+ const nullableMatch = type.match(/^nullable\((.+)\)$/);
327
+ if (nullableMatch) {
328
+ const innerEncoder = createBinaryEncoder(nullableMatch[1], false);
329
+ return (writer, value) => {
330
+ if (value === null || value === undefined) {
331
+ writer.writeNullable(true);
332
+ }
333
+ else {
334
+ writer.writeNullable(false);
335
+ innerEncoder(writer, value);
336
+ }
337
+ };
338
+ }
339
+ const arrayMatch = type.match(/^array\((.+)\)$/);
340
+ if (arrayMatch) {
341
+ const elementEncoder = createBinaryEncoder(arrayMatch[1], false);
342
+ return (writer, value) => {
343
+ const arr = Array.isArray(value) ? value : [];
344
+ writer.writeArrayLength(arr.length);
345
+ for (const item of arr) {
346
+ elementEncoder(writer, item);
347
+ }
348
+ };
349
+ }
350
+ const mapMatch = type.match(/^map\((.+)\)$/);
351
+ if (mapMatch) {
352
+ const [keyType, valueType] = parseGenericTypes(mapMatch[1]);
353
+ if (!keyType || !valueType)
354
+ throw new Error(`Invalid Map type: ${type}`);
355
+ const keyEncoder = createBinaryEncoder(keyType);
356
+ const valueEncoder = createBinaryEncoder(valueType);
357
+ return (writer, value) => {
358
+ let entries;
359
+ if (value instanceof Map) {
360
+ entries = Array.from(value.entries());
361
+ }
362
+ else if (typeof value === 'object' && value !== null) {
363
+ entries = Object.entries(value);
364
+ }
365
+ else {
366
+ entries = [];
367
+ }
368
+ writer.writeUInt64(entries.length);
369
+ for (const [k, v] of entries) {
370
+ keyEncoder(writer, k);
371
+ valueEncoder(writer, v);
372
+ }
373
+ };
374
+ }
375
+ const tupleMatch = type.match(/^tuple\((.+)\)$/);
376
+ if (tupleMatch) {
377
+ const types = parseGenericTypes(tupleMatch[1]);
378
+ const encoders = types.map(t => createBinaryEncoder(t));
379
+ return (writer, value) => {
380
+ const arr = Array.isArray(value) ? value : [];
381
+ for (let i = 0; i < encoders.length; i++) {
382
+ encoders[i](writer, arr[i]);
383
+ }
384
+ };
385
+ }
386
+ const nestedMatch = type.match(/^nested\((.+)\)$/);
387
+ if (nestedMatch) {
388
+ const fields = parseGenericTypes(nestedMatch[1]);
389
+ const types = fields.map(f => {
390
+ const parts = f.trim().split(/\s+/);
391
+ if (parts.length < 2)
392
+ return 'String';
393
+ return parts.slice(1).join(' ');
394
+ });
395
+ const tupleType = `Tuple(${types.join(', ')})`;
396
+ const tupleEncoder = createBinaryEncoder(tupleType);
397
+ return (writer, value) => {
398
+ const arr = Array.isArray(value) ? value : [];
399
+ writer.writeArrayLength(arr.length);
400
+ for (const item of arr) {
401
+ let tupleValue = item;
402
+ if (!Array.isArray(item) && typeof item === 'object') {
403
+ tupleValue = fields.map(f => {
404
+ const name = f.trim().split(/\s+/)[0];
405
+ return item[name];
406
+ });
407
+ }
408
+ tupleEncoder(writer, tupleValue);
409
+ }
410
+ };
411
+ }
412
+ const lcMatch = type.match(/^lowcardinality\((.+)\)$/);
413
+ if (lcMatch) {
414
+ return createBinaryEncoder(lcMatch[1]);
415
+ }
416
+ const fixedStringMatch = type.match(/^fixedstring\((\d+)\)$/);
417
+ if (fixedStringMatch) {
418
+ const length = parseInt(fixedStringMatch[1], 10);
419
+ return (writer, value) => {
420
+ writer.writeFixedString(String(value ?? ''), length);
421
+ };
422
+ }
423
+ const decimalMatch = type.match(/^decimal(?:32|64|128)?\((\d+),\s*(\d+)\)$/);
424
+ if (decimalMatch) {
425
+ const scale = parseInt(decimalMatch[2], 10);
426
+ if (type.includes('decimal128')) {
427
+ return (writer, value) => writer.writeDecimal128(value, scale);
428
+ }
429
+ if (type.includes('decimal64')) {
430
+ return (writer, value) => writer.writeDecimal64(value, scale);
431
+ }
432
+ return (writer, value) => writer.writeDecimal32(value, scale);
433
+ }
434
+ const dt64Match = type.match(/^datetime64\((\d+)/);
435
+ if (dt64Match) {
436
+ const precision = parseInt(dt64Match[1], 10);
437
+ return (writer, value) => writer.writeDateTime64(value, precision);
438
+ }
439
+ const enum8Match = type.match(/^enum8\(/);
440
+ if (enum8Match) {
441
+ return (writer, value) => writer.writeEnum8(Number(value));
442
+ }
443
+ const enum16Match = type.match(/^enum16\(/);
444
+ if (enum16Match) {
445
+ return (writer, value) => writer.writeEnum16(Number(value));
446
+ }
447
+ const baseEncoder = getBaseTypeEncoder(type);
448
+ if (isNullable) {
449
+ return (writer, value) => {
450
+ if (value === null || value === undefined) {
451
+ writer.writeNullable(true);
452
+ }
453
+ else {
454
+ writer.writeNullable(false);
455
+ baseEncoder(writer, value);
456
+ }
457
+ };
458
+ }
459
+ if (type === 'uuid') {
460
+ return (writer, value) => {
461
+ if (value === undefined || value === null) {
462
+ baseEncoder(writer, generateUUIDv4());
463
+ }
464
+ else {
465
+ baseEncoder(writer, value);
466
+ }
467
+ };
468
+ }
469
+ return baseEncoder;
470
+ }
471
+ function getBaseTypeEncoder(type) {
472
+ if (type === 'int8')
473
+ return (w, v) => w.writeInt8(Number(v));
474
+ if (type === 'uint8')
475
+ return (w, v) => w.writeUInt8(Number(v));
476
+ if (type === 'int16')
477
+ return (w, v) => w.writeInt16(Number(v));
478
+ if (type === 'uint16')
479
+ return (w, v) => w.writeUInt16(Number(v));
480
+ if (type === 'int32')
481
+ return (w, v) => w.writeInt32(Number(v));
482
+ if (type === 'uint32')
483
+ return (w, v) => w.writeUInt32(Number(v));
484
+ if (type === 'int64')
485
+ return (w, v) => w.writeInt64(v);
486
+ if (type === 'uint64')
487
+ return (w, v) => w.writeUInt64(v);
488
+ if (type === 'int128')
489
+ return (w, v) => w.writeInt128(BigInt(v));
490
+ if (type === 'uint128')
491
+ return (w, v) => w.writeUInt128(BigInt(v));
492
+ if (type === 'int256')
493
+ return (w, v) => w.writeInt256(BigInt(v));
494
+ if (type === 'uint256')
495
+ return (w, v) => w.writeUInt256(BigInt(v));
496
+ if (type === 'float32')
497
+ return (w, v) => w.writeFloat32(Number(v));
498
+ if (type === 'float64')
499
+ return (w, v) => w.writeFloat64(Number(v));
500
+ if (type === 'string')
501
+ return (w, v) => w.writeString(String(v ?? ''));
502
+ if (type === 'uuid')
503
+ return (w, v) => w.writeUUID(String(v));
504
+ if (type === 'date')
505
+ return (w, v) => w.writeDate(v);
506
+ if (type === 'date32')
507
+ return (w, v) => w.writeDate32(v);
508
+ if (type === 'datetime')
509
+ return (w, v) => w.writeDateTime(v);
510
+ if (type.startsWith('datetime64'))
511
+ return (w, v) => w.writeDateTime64(v, 3);
512
+ if (type === 'bool' || type === 'boolean')
513
+ return (w, v) => w.writeBool(Boolean(v));
514
+ if (type === 'ipv4')
515
+ return (w, v) => w.writeIPv4(v);
516
+ if (type === 'ipv6')
517
+ return (w, v) => w.writeIPv6(v);
518
+ return (w, v) => w.writeString(String(v ?? ''));
519
+ }
520
+ export function buildBinaryConfig(columns) {
521
+ const keyMapping = new Map();
522
+ const encoders = [];
523
+ const columnInfos = [];
524
+ columns.forEach((col, index) => {
525
+ keyMapping.set(col.propKey, index);
526
+ keyMapping.set(col.name, index);
527
+ encoders.push(createBinaryEncoder(col.type, col.isNull));
528
+ columnInfos.push({
529
+ name: col.name,
530
+ type: col.type,
531
+ isNullable: col.isNull,
532
+ });
533
+ });
534
+ return { columns: columnInfos, keyMapping, encoders };
535
+ }
536
+ export function serializeRowBinary(row, config, writer = new BinaryWriter()) {
537
+ writer.reset();
538
+ for (let i = 0; i < config.columns.length; i++) {
539
+ const col = config.columns[i];
540
+ const value = row[col.name];
541
+ config.encoders[i](writer, value);
542
+ }
543
+ return writer.toBuffer();
544
+ }
545
+ export function serializeRowsBinary(rows, config) {
546
+ const estimatedRowSize = config.columns.length * 16;
547
+ const writer = new BinaryWriter(rows.length * estimatedRowSize);
548
+ for (const row of rows) {
549
+ for (let i = 0; i < config.columns.length; i++) {
550
+ const col = config.columns[i];
551
+ const value = row[col.name];
552
+ config.encoders[i](writer, value);
553
+ }
554
+ }
555
+ return writer.toBuffer();
556
+ }
557
+ export function createAccessor(propKey, columnName) {
558
+ if (propKey === columnName) {
559
+ return (row) => row[propKey];
560
+ }
561
+ return (row) => {
562
+ const v = row[propKey];
563
+ return v !== undefined ? v : row[columnName];
564
+ };
565
+ }
566
+ export function buildOptimizedBinaryConfig(columns, options) {
567
+ const encoders = [];
568
+ const accessors = [];
569
+ const columnInfos = [];
570
+ for (const col of columns) {
571
+ encoders.push(createBinaryEncoder(col.type, col.isNull));
572
+ accessors.push(createAccessor(col.propKey, col.name));
573
+ columnInfos.push({
574
+ name: col.name,
575
+ type: col.type,
576
+ isNullable: col.isNull,
577
+ });
578
+ }
579
+ return {
580
+ columns: columnInfos,
581
+ encoders,
582
+ accessors,
583
+ skipValidation: options?.skipValidation
584
+ };
585
+ }
586
+ export function serializeRowsOptimized(rows, config) {
587
+ const writer = acquireWriter();
588
+ try {
589
+ const { encoders, accessors } = config;
590
+ const colCount = encoders.length;
591
+ for (let r = 0; r < rows.length; r++) {
592
+ const row = rows[r];
593
+ for (let c = 0; c < colCount; c++) {
594
+ encoders[c](writer, accessors[c](row));
595
+ }
596
+ }
597
+ return writer.toBuffer();
598
+ }
599
+ finally {
600
+ releaseWriter(writer);
601
+ }
602
+ }
603
+ export function isNumericHeavySchema(columns) {
604
+ const numericTypes = ['int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32',
605
+ 'int64', 'uint64', 'float32', 'float64'];
606
+ let numericCount = 0;
607
+ for (const col of columns) {
608
+ const type = col.type.toLowerCase();
609
+ if (numericTypes.some(t => type === t || type.startsWith(t))) {
610
+ numericCount++;
611
+ }
612
+ }
613
+ return numericCount > columns.length / 2;
614
+ }
615
+ export function serializeNumericBatch(rows, config, numericIndices) {
616
+ const rowCount = rows.length;
617
+ const numericCount = numericIndices.length;
618
+ const numericBuffer = new Float64Array(rowCount * numericCount);
619
+ const otherData = [];
620
+ for (let r = 0; r < rowCount; r++) {
621
+ const row = rows[r];
622
+ const otherRow = [];
623
+ let numIdx = 0;
624
+ for (let c = 0; c < config.accessors.length; c++) {
625
+ const value = config.accessors[c](row);
626
+ if (numericIndices.includes(c)) {
627
+ numericBuffer[r * numericCount + numIdx] = Number(value) || 0;
628
+ numIdx++;
629
+ }
630
+ else {
631
+ otherRow.push(value);
632
+ }
633
+ }
634
+ otherData.push(otherRow);
635
+ }
636
+ return { numericBuffer: numericBuffer.buffer, otherData };
637
+ }
@@ -0,0 +1 @@
1
+ export const binaryWorkerCode = "var{defineProperty:i,getOwnPropertyNames:g0,getOwnPropertyDescriptor:M0}=Object,h0=Object.prototype.hasOwnProperty;var m0=new WeakMap,b0=(f)=>{var x=m0.get(f),U;if(x)return x;if(x=i({},\"__esModule\",{value:!0}),f&&typeof f===\"object\"||typeof f===\"function\")g0(f).map((m)=>!h0.call(x,m)&&i(x,m,{get:()=>f[m],enumerable:!(U=M0(f,m))||U.enumerable}));return m0.set(f,x),x};var o0=(f,x)=>{for(var U in x)i(f,U,{get:x[U],enumerable:!0,configurable:!0,set:(m)=>x[U]=()=>m})};var L=(f,x)=>()=>(f&&(x=f(f=0)),x);var A0=\"ffffffff-ffff-ffff-ffff-ffffffffffff\";var D0=\"00000000-0000-0000-0000-000000000000\";var S0;var V0=L(()=>{S0=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/i});function B0(f){return typeof f===\"string\"&&S0.test(f)}var Z;var C=L(()=>{V0();Z=B0});function p0(f){if(!Z(f))throw TypeError(\"Invalid UUID\");let x;return Uint8Array.of((x=parseInt(f.slice(0,8),16))>>>24,x>>>16&255,x>>>8&255,x&255,(x=parseInt(f.slice(9,13),16))>>>8,x&255,(x=parseInt(f.slice(14,18),16))>>>8,x&255,(x=parseInt(f.slice(19,23),16))>>>8,x&255,(x=parseInt(f.slice(24,36),16))/1099511627776&255,x/4294967296&255,x>>>24&255,x>>>16&255,x>>>8&255,x&255)}var N;var F=L(()=>{C();N=p0});function X(f,x=0){return(_[f[x+0]]+_[f[x+1]]+_[f[x+2]]+_[f[x+3]]+\"-\"+_[f[x+4]]+_[f[x+5]]+\"-\"+_[f[x+6]]+_[f[x+7]]+\"-\"+_[f[x+8]]+_[f[x+9]]+\"-\"+_[f[x+10]]+_[f[x+11]]+_[f[x+12]]+_[f[x+13]]+_[f[x+14]]+_[f[x+15]]).toLowerCase()}function y0(f,x=0){let U=X(f,x);if(!Z(U))throw TypeError(\"Stringified UUID is invalid\");return U}var _,R0;var Q=L(()=>{C();_=[];for(let f=0;f<256;++f)_.push((f+256).toString(16).slice(1));R0=y0});import{randomFillSync as i0}from\"node:crypto\";function Y(){if(M>h.length-16)i0(h),M=0;return h.slice(M,M+=16)}var h,M;var b=L(()=>{h=new Uint8Array(256),M=h.length});function c0(f,x,U){let m,A=f?._v6??!1;if(f){let D=Object.keys(f);if(D.length===1&&D[0]===\"_v6\")f=void 0}if(f)m=$0(f.random??f.rng?.()??Y(),f.msecs,f.nsecs,f.clockseq,f.node,x,U);else{let D=Date.now(),R=Y();w0(K,D,R),m=$0(R,K.msecs,K.nsecs,A?void 0:K.clockseq,A?void 0:K.node,x,U)}return x??X(m)}function w0(f,x,U){if(f.msecs??=-1/0,f.nsecs??=0,x===f.msecs){if(f.nsecs++,f.nsecs>=1e4)f.node=void 0,f.nsecs=0}else if(x>f.msecs)f.nsecs=0;else if(x<f.msecs)f.node=void 0;if(!f.node)f.node=U.slice(10,16),f.node[0]|=1,f.clockseq=(U[8]<<8|U[9])&16383;return f.msecs=x,f}function $0(f,x,U,m,A,D,R=0){if(f.length<16)throw Error(\"Random bytes length must be >= 16\");if(!D)D=new Uint8Array(16),R=0;else if(R<0||R+16>D.length)throw RangeError(`UUID byte range ${R}:${R+15} is out of buffer bounds`);x??=Date.now(),U??=0,m??=(f[8]<<8|f[9])&16383,A??=f.slice(10,16),x+=12219292800000;let q=((x&268435455)*1e4+U)%4294967296;D[R++]=q>>>24&255,D[R++]=q>>>16&255,D[R++]=q>>>8&255,D[R++]=q&255;let H=x/4294967296*1e4&268435455;D[R++]=H>>>8&255,D[R++]=H&255,D[R++]=H>>>24&15|16,D[R++]=H>>>16&255,D[R++]=m>>>8|128,D[R++]=m&255;for(let n=0;n<6;++n)D[R++]=A[n];return D}var K,o;var w=L(()=>{b();Q();K={};o=c0});function z(f){let x=typeof f===\"string\"?N(f):f,U=r0(x);return typeof f===\"string\"?X(U):U}function r0(f){return Uint8Array.of((f[6]&15)<<4|f[7]>>4&15,(f[7]&15)<<4|(f[4]&240)>>4,(f[4]&15)<<4|(f[5]&240)>>4,(f[5]&15)<<4|(f[0]&240)>>4,(f[0]&15)<<4|(f[1]&240)>>4,(f[1]&15)<<4|(f[2]&240)>>4,96|f[2]&15,f[3],f[8],f[9],f[10],f[11],f[12],f[13],f[14],f[15])}var r=L(()=>{F();Q()});import{createHash as t0}from\"node:crypto\";function s0(f){if(Array.isArray(f))f=Buffer.from(f);else if(typeof f===\"string\")f=Buffer.from(f,\"utf8\");return t0(\"md5\").update(f).digest()}var L0;var H0=L(()=>{L0=s0});function u0(f){f=unescape(encodeURIComponent(f));let x=new Uint8Array(f.length);for(let U=0;U<f.length;++U)x[U]=f.charCodeAt(U);return x}function P(f,x,U,m,A,D){let R=typeof U===\"string\"?u0(U):U,q=typeof m===\"string\"?N(m):m;if(typeof m===\"string\")m=N(m);if(m?.length!==16)throw TypeError(\"Namespace must be array-like (16 iterable integer values, 0-255)\");let H=new Uint8Array(16+R.length);if(H.set(q),H.set(R,q.length),H=x(H),H[6]=H[6]&15|f,H[8]=H[8]&63|128,A){D=D||0;for(let n=0;n<16;++n)A[D+n]=H[n];return A}return X(H)}var B=\"6ba7b810-9dad-11d1-80b4-00c04fd430c8\",p=\"6ba7b811-9dad-11d1-80b4-00c04fd430c8\";var t=L(()=>{F();Q()});function s(f,x,U,m){return P(48,L0,f,x,U,m)}var _0;var I0=L(()=>{H0();t();s.DNS=B;s.URL=p;_0=s});import{randomUUID as l0}from\"node:crypto\";var u;var X0=L(()=>{u={randomUUID:l0}});function v0(f,x,U){f=f||{};let m=f.random??f.rng?.()??Y();if(m.length<16)throw Error(\"Random bytes length must be >= 16\");if(m[6]=m[6]&15|64,m[8]=m[8]&63|128,x){if(U=U||0,U<0||U+16>x.length)throw RangeError(`UUID byte range ${U}:${U+15} is out of buffer bounds`);for(let A=0;A<16;++A)x[U+A]=m[A];return x}return X(m)}function a0(f,x,U){if(u.randomUUID&&!x&&!f)return u.randomUUID();return v0(f,x,U)}var j0;var n0=L(()=>{X0();b();Q();j0=a0});import{createHash as e0}from\"node:crypto\";function f1(f){if(Array.isArray(f))f=Buffer.from(f);else if(typeof f===\"string\")f=Buffer.from(f,\"utf8\");return e0(\"sha1\").update(f).digest()}var q0;var J0=L(()=>{q0=f1});function l(f,x,U,m){return P(80,q0,f,x,U,m)}var N0;var Q0=L(()=>{J0();t();l.DNS=B;l.URL=p;N0=l});function x1(f,x,U){f??={},U??=0;let m=o({...f,_v6:!0},new Uint8Array(16));if(m=z(m),x){for(let A=0;A<16;A++)x[U+A]=m[A];return x}return X(m)}var Y0;var T0=L(()=>{Q();w();r();Y0=x1});function v(f){let x=typeof f===\"string\"?N(f):f,U=U1(x);return typeof f===\"string\"?X(U):U}function U1(f){return Uint8Array.of((f[3]&15)<<4|f[4]>>4&15,(f[4]&15)<<4|(f[5]&240)>>4,(f[5]&15)<<4|f[6]&15,f[7],(f[1]&15)<<4|(f[2]&240)>>4,(f[2]&15)<<4|(f[3]&240)>>4,16|(f[0]&240)>>4,(f[0]&15)<<4|(f[1]&240)>>4,f[8],f[9],f[10],f[11],f[12],f[13],f[14],f[15])}var G0=L(()=>{F();Q()});function m1(f,x,U){let m;if(f)m=Z0(f.random??f.rng?.()??Y(),f.msecs,f.seq,x,U);else{let A=Date.now(),D=Y();A1(a,A,D),m=Z0(D,a.msecs,a.seq,x,U)}return x??X(m)}function A1(f,x,U){if(f.msecs??=-1/0,f.seq??=0,x>f.msecs)f.seq=U[6]<<23|U[7]<<16|U[8]<<8|U[9],f.msecs=x;else if(f.seq=f.seq+1|0,f.seq===0)f.msecs++;return f}function Z0(f,x,U,m,A=0){if(f.length<16)throw Error(\"Random bytes length must be >= 16\");if(!m)m=new Uint8Array(16),A=0;else if(A<0||A+16>m.length)throw RangeError(`UUID byte range ${A}:${A+15} is out of buffer bounds`);return x??=Date.now(),U??=f[6]*127<<24|f[7]<<16|f[8]<<8|f[9],m[A++]=x/1099511627776&255,m[A++]=x/4294967296&255,m[A++]=x/16777216&255,m[A++]=x/65536&255,m[A++]=x/256&255,m[A++]=x&255,m[A++]=112|U>>>28&15,m[A++]=U>>>20&255,m[A++]=128|U>>>14&63,m[A++]=U>>>6&255,m[A++]=U<<2&255|f[10]&3,m[A++]=f[11],m[A++]=f[12],m[A++]=f[13],m[A++]=f[14],m[A++]=f[15],m}var a,O0;var C0=L(()=>{b();Q();a={};O0=m1});function D1(f){if(!Z(f))throw TypeError(\"Invalid UUID\");return parseInt(f.slice(14,15),16)}var F0;var K0=L(()=>{C();F0=D1});var z0={};o0(z0,{version:()=>F0,validate:()=>Z,v7:()=>O0,v6ToV1:()=>v,v6:()=>Y0,v5:()=>N0,v4:()=>j0,v3:()=>_0,v1ToV6:()=>z,v1:()=>o,stringify:()=>R0,parse:()=>N,NIL:()=>D0,MAX:()=>A0});var P0=L(()=>{F();Q();w();r();I0();n0();Q0();T0();G0();C0();C();K0()});import{parentPort as W,workerData as k0}from\"worker_threads\";var S1=typeof crypto<\"u\"&&typeof crypto.randomUUID===\"function\",y=null;function V1(){if(S1)return crypto.randomUUID();if(y)return y();return y=(P0(),b0(z0)).v4,y()}class f0{buffer;offset=0;constructor(f=4096){this.buffer=Buffer.allocUnsafe(f)}ensureCapacity(f){let x=this.offset+f;if(x>this.buffer.length){let U=Math.max(this.buffer.length*2,x),m=Buffer.allocUnsafe(U);this.buffer.copy(m,0,0,this.offset),this.buffer=m}}reset(){this.offset=0}getBuffer(){return this.buffer.subarray(0,this.offset)}toBuffer(){return Buffer.from(this.buffer.subarray(0,this.offset))}writeInt8(f){this.ensureCapacity(1),this.buffer.writeInt8(f,this.offset),this.offset+=1}writeUInt8(f){this.ensureCapacity(1),this.buffer.writeUInt8(f,this.offset),this.offset+=1}writeInt16(f){this.ensureCapacity(2),this.buffer.writeInt16LE(f,this.offset),this.offset+=2}writeUInt16(f){this.ensureCapacity(2),this.buffer.writeUInt16LE(f,this.offset),this.offset+=2}writeInt32(f){this.ensureCapacity(4),this.buffer.writeInt32LE(f,this.offset),this.offset+=4}writeUInt32(f){this.ensureCapacity(4),this.buffer.writeUInt32LE(f,this.offset),this.offset+=4}writeInt64(f){this.ensureCapacity(8),this.buffer.writeBigInt64LE(BigInt(f),this.offset),this.offset+=8}writeUInt64(f){this.ensureCapacity(8),this.buffer.writeBigUInt64LE(BigInt(f),this.offset),this.offset+=8}writeInt128(f){this.ensureCapacity(16);let x=f&BigInt(\"0xFFFFFFFFFFFFFFFF\"),U=f>>BigInt(64);this.buffer.writeBigUInt64LE(x,this.offset),this.buffer.writeBigInt64LE(U,this.offset+8),this.offset+=16}writeUInt128(f){this.ensureCapacity(16);let x=f&BigInt(\"0xFFFFFFFFFFFFFFFF\"),U=f>>BigInt(64);this.buffer.writeBigUInt64LE(x,this.offset),this.buffer.writeBigUInt64LE(U,this.offset+8),this.offset+=16}writeInt256(f){this.ensureCapacity(32);for(let x=0;x<4;x++){let U=f&BigInt(\"0xFFFFFFFFFFFFFFFF\");this.buffer.writeBigUInt64LE(U,this.offset+x*8),f>>=BigInt(64)}this.offset+=32}writeUInt256(f){this.writeInt256(f)}writeFloat32(f){this.ensureCapacity(4),this.buffer.writeFloatLE(f,this.offset),this.offset+=4}writeFloat64(f){this.ensureCapacity(8),this.buffer.writeDoubleLE(f,this.offset),this.offset+=8}writeVarInt(f){if(f<128){this.ensureCapacity(1),this.buffer[this.offset++]=f;return}if(f<16384){this.ensureCapacity(2),this.buffer[this.offset++]=f&127|128,this.buffer[this.offset++]=f>>7;return}if(f<2097152){this.ensureCapacity(3),this.buffer[this.offset++]=f&127|128,this.buffer[this.offset++]=f>>7&127|128,this.buffer[this.offset++]=f>>14;return}this.ensureCapacity(10);while(f>=128)this.buffer[this.offset++]=f&127|128,f>>>=7;this.buffer[this.offset++]=f}writeString(f){let x=Buffer.from(f,\"utf-8\");this.writeVarInt(x.length),this.ensureCapacity(x.length),x.copy(this.buffer,this.offset),this.offset+=x.length}writeFixedString(f,x){this.ensureCapacity(x);let U=Buffer.from(f,\"utf-8\"),m=Math.min(U.length,x);if(U.copy(this.buffer,this.offset,0,m),m<x)this.buffer.fill(0,this.offset+m,this.offset+x);this.offset+=x}writeBytes(f){this.ensureCapacity(f.length),f.copy(this.buffer,this.offset),this.offset+=f.length}writeUUID(f){this.ensureCapacity(16);let x=f.replace(/-/g,\"\");if(x.length!==32)throw Error(`Invalid UUID: ${f}`);let U=Buffer.from(x,\"hex\");for(let m=7;m>=0;m--)this.buffer[this.offset++]=U[m];for(let m=15;m>=8;m--)this.buffer[this.offset++]=U[m]}writeDate(f){let x=typeof f===\"number\"?f:Math.floor(f.getTime()/86400000);this.writeUInt16(x)}writeDate32(f){let x=typeof f===\"number\"?f:Math.floor(f.getTime()/86400000);this.writeInt32(x)}writeDateTime(f){let x=typeof f===\"number\"?f:Math.floor(f.getTime()/1000);this.writeUInt32(x)}writeDateTime64(f,x=3){let U=Math.pow(10,x),m=typeof f===\"number\"?BigInt(Math.round(f*U)):BigInt(Math.round(f.getTime()*U/1000));this.writeInt64(m)}writeNullable(f){this.writeUInt8(f?1:0)}writeArrayLength(f){this.writeUInt64(f)}writeDecimal32(f,x){let U=Math.round(f*Math.pow(10,x));this.writeInt32(U)}writeDecimal64(f,x){let U=BigInt(Math.round(f*Math.pow(10,x)));this.writeInt64(U)}writeDecimal128(f,x){let U;if(typeof f===\"bigint\")U=f*BigInt(Math.pow(10,x));else U=BigInt(Math.round(f*Math.pow(10,x)));this.writeInt128(U)}writeBool(f){this.writeUInt8(f?1:0)}writeIPv4(f){if(typeof f===\"number\"){this.writeUInt32(f);return}let x=f.split(\".\").map(Number);if(x.length!==4)throw Error(`Invalid IPv4: ${f}`);let U=x[0]<<24|x[1]<<16|x[2]<<8|x[3];this.writeUInt32(U>>>0)}writeIPv6(f){if(Buffer.isBuffer(f)){if(f.length!==16)throw Error(\"IPv6 must be 16 bytes\");this.writeBytes(f);return}let U=this.expandIPv6(f).split(\":\");this.ensureCapacity(16);for(let m of U){let A=parseInt(m,16);this.buffer.writeUInt16BE(A,this.offset),this.offset+=2}}expandIPv6(f){let x=f.split(\"::\");if(x.length===1)return f;let U=x[0]?x[0].split(\":\"):[],m=x[1]?x[1].split(\":\"):[],A=8-U.length-m.length,D=Array(A).fill(\"0000\");return[...U,...D,...m].map((R)=>R.padStart(4,\"0\")).join(\":\")}writeEnum8(f){this.writeInt8(f)}writeEnum16(f){this.writeInt16(f)}}function e(f){let x=[],U=\"\",m=0;for(let A=0;A<f.length;A++){let D=f[A];if(D===\",\"&&m===0)x.push(U.trim()),U=\"\";else{if(D===\"(\")m++;if(D===\")\")m--;U+=D}}if(U.trim())x.push(U.trim());return x}function T(f,x=!1){let U=f.toLowerCase().trim(),m=U.match(/^nullable\\((.+)\\)$/);if(m){let V=T(m[1],!1);return(S,$)=>{if($===null||$===void 0)S.writeNullable(!0);else S.writeNullable(!1),V(S,$)}}let A=U.match(/^array\\((.+)\\)$/);if(A){let V=T(A[1],!1);return(S,$)=>{let J=Array.isArray($)?$:[];S.writeArrayLength(J.length);for(let j of J)V(S,j)}}let D=U.match(/^map\\((.+)\\)$/);if(D){let[V,S]=e(D[1]);if(!V||!S)throw Error(`Invalid Map type: ${U}`);let $=T(V),J=T(S);return(j,I)=>{let G;if(I instanceof Map)G=Array.from(I.entries());else if(typeof I===\"object\"&&I!==null)G=Object.entries(I);else G=[];j.writeUInt64(G.length);for(let[O,g]of G)$(j,O),J(j,g)}}let R=U.match(/^tuple\\((.+)\\)$/);if(R){let S=e(R[1]).map(($)=>T($));return($,J)=>{let j=Array.isArray(J)?J:[];for(let I=0;I<S.length;I++)S[I]($,j[I])}}let q=U.match(/^nested\\((.+)\\)$/);if(q){let V=e(q[1]),$=`Tuple(${V.map((j)=>{let I=j.trim().split(/\\s+/);if(I.length<2)return\"String\";return I.slice(1).join(\" \")}).join(\", \")})`,J=T($);return(j,I)=>{let G=Array.isArray(I)?I:[];j.writeArrayLength(G.length);for(let O of G){let g=O;if(!Array.isArray(O)&&typeof O===\"object\")g=V.map((W0)=>{let d0=W0.trim().split(/\\s+/)[0];return O[d0]});J(j,g)}}}let H=U.match(/^lowcardinality\\((.+)\\)$/);if(H)return T(H[1]);let n=U.match(/^fixedstring\\((\\d+)\\)$/);if(n){let V=parseInt(n[1],10);return(S,$)=>{S.writeFixedString(String($??\"\"),V)}}let x0=U.match(/^decimal(?:32|64|128)?\\((\\d+),\\s*(\\d+)\\)$/);if(x0){let V=parseInt(x0[2],10);if(U.includes(\"decimal128\"))return(S,$)=>S.writeDecimal128($,V);if(U.includes(\"decimal64\"))return(S,$)=>S.writeDecimal64($,V);return(S,$)=>S.writeDecimal32($,V)}let U0=U.match(/^datetime64\\((\\d+)/);if(U0){let V=parseInt(U0[1],10);return(S,$)=>S.writeDateTime64($,V)}if(U.match(/^enum8\\(/))return(V,S)=>V.writeEnum8(Number(S));if(U.match(/^enum16\\(/))return(V,S)=>V.writeEnum16(Number(S));let d=R1(U);if(x)return(V,S)=>{if(S===null||S===void 0)V.writeNullable(!0);else V.writeNullable(!1),d(V,S)};if(U===\"uuid\")return(V,S)=>{if(S===void 0||S===null)d(V,V1());else d(V,S)};return d}function R1(f){if(f===\"int8\")return(x,U)=>x.writeInt8(Number(U));if(f===\"uint8\")return(x,U)=>x.writeUInt8(Number(U));if(f===\"int16\")return(x,U)=>x.writeInt16(Number(U));if(f===\"uint16\")return(x,U)=>x.writeUInt16(Number(U));if(f===\"int32\")return(x,U)=>x.writeInt32(Number(U));if(f===\"uint32\")return(x,U)=>x.writeUInt32(Number(U));if(f===\"int64\")return(x,U)=>x.writeInt64(U);if(f===\"uint64\")return(x,U)=>x.writeUInt64(U);if(f===\"int128\")return(x,U)=>x.writeInt128(BigInt(U));if(f===\"uint128\")return(x,U)=>x.writeUInt128(BigInt(U));if(f===\"int256\")return(x,U)=>x.writeInt256(BigInt(U));if(f===\"uint256\")return(x,U)=>x.writeUInt256(BigInt(U));if(f===\"float32\")return(x,U)=>x.writeFloat32(Number(U));if(f===\"float64\")return(x,U)=>x.writeFloat64(Number(U));if(f===\"string\")return(x,U)=>x.writeString(String(U??\"\"));if(f===\"uuid\")return(x,U)=>x.writeUUID(String(U));if(f===\"date\")return(x,U)=>x.writeDate(U);if(f===\"date32\")return(x,U)=>x.writeDate32(U);if(f===\"datetime\")return(x,U)=>x.writeDateTime(U);if(f.startsWith(\"datetime64\"))return(x,U)=>x.writeDateTime64(U,3);if(f===\"bool\"||f===\"boolean\")return(x,U)=>x.writeBool(Boolean(U));if(f===\"ipv4\")return(x,U)=>x.writeIPv4(U);if(f===\"ipv6\")return(x,U)=>x.writeIPv6(U);return(x,U)=>x.writeString(String(U??\"\"))}var k=null,E=null;function $1(f,x){if(!k||!E)throw Error(\"Worker not configured\");E.reset();for(let U of f)for(let m=0;m<k.columns.length;m++){let A=k.columns[m],D=U[A.name];k.encoders[m](E,D)}return E.toBuffer()}function E0(f){try{switch(f.type){case\"configure\":{let x=f.columns.map((U)=>T(U.type,U.isNullable));k={columns:f.columns,keyMapping:new Map,encoders:x},E=new f0(1048576),W?.postMessage({type:\"ready\"});break}case\"serialize\":{let x=$1(f.rows,f.batchId),U=new ArrayBuffer(x.length);new Uint8Array(U).set(x),W?.postMessage({type:\"result\",batchId:f.batchId,buffer:Buffer.from(U),rowCount:f.rows.length},[U]);break}case\"shutdown\":process.exit(0)}}catch(x){W?.postMessage({type:\"error\",batchId:f.batchId,error:x instanceof Error?x.message:String(x)})}}if(W){if(W.on(\"message\",E0),k0?.columns)E0({type:\"configure\",columns:k0.columns})}\n";