|
| 1 | +/** |
| 2 | + * @import {Writer} from 'hyparquet-writer/src/types.js' |
| 3 | + * @import {AvroRecord, AvroType} from '../src/types.js' |
| 4 | + */ |
| 5 | + |
| 6 | +import { ByteWriter } from 'hyparquet-writer' |
| 7 | + |
| 8 | +/** |
| 9 | + * @param {Object} options |
| 10 | + * @param {Writer} options.writer |
| 11 | + * @param {AvroRecord} options.schema |
| 12 | + * @param {Record<string, any>[]} options.records |
| 13 | + * @param {number} [options.blockSize] |
| 14 | + */ |
| 15 | +export function avroWrite({ writer, schema, records, blockSize = 512 }) { |
| 16 | + writer.appendUint32(0x016a624f) // Obj\x01 |
| 17 | + |
| 18 | + const meta = { |
| 19 | + 'avro.schema': typeof schema === 'string' ? schema : JSON.stringify(schema), |
| 20 | + 'avro.codec': 'null', |
| 21 | + } |
| 22 | + appendZigZag(writer, Object.keys(meta).length) |
| 23 | + for (const [key, value] of Object.entries(meta)) { |
| 24 | + const kb = new TextEncoder().encode(key) |
| 25 | + appendZigZag(writer, kb.length) |
| 26 | + writer.appendBytes(kb) |
| 27 | + const vb = new TextEncoder().encode(value) |
| 28 | + appendZigZag(writer, vb.length) |
| 29 | + writer.appendBytes(vb) |
| 30 | + } |
| 31 | + writer.appendVarInt(0) |
| 32 | + |
| 33 | + const sync = new Uint8Array(16) |
| 34 | + for (let i = 0; i < 16; i++) sync[i] = Math.random() * 256 | 0 |
| 35 | + writer.appendBytes(sync) |
| 36 | + |
| 37 | + for (let i = 0; i < records.length; i += blockSize) { |
| 38 | + const block = records.slice(i, i + blockSize) |
| 39 | + appendZigZag(writer, block.length) // record count |
| 40 | + const blockWriter = new ByteWriter() |
| 41 | + for (const record of block) { |
| 42 | + for (const { name, type } of schema.fields) { |
| 43 | + writeType(blockWriter, type, record[name]) |
| 44 | + } |
| 45 | + } |
| 46 | + appendZigZag(writer, blockWriter.offset) // block size |
| 47 | + writer.appendBuffer(blockWriter.getBuffer()) |
| 48 | + writer.appendBytes(sync) |
| 49 | + } |
| 50 | + |
| 51 | + if (writer.finish) writer.finish() |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @param {Writer} writer |
| 56 | + * @param {AvroType} schema |
| 57 | + * @param {*} value |
| 58 | + */ |
| 59 | +function writeType(writer, schema, value) { |
| 60 | + console.log('writeType', schema, value) |
| 61 | + if (Array.isArray(schema)) { |
| 62 | + // find matching union branch |
| 63 | + const idx = schema.findIndex(s => { |
| 64 | + if (Array.isArray(s)) throw new Error('nested unions not supported') |
| 65 | + |
| 66 | + // normalise branch to a tag string we can test against |
| 67 | + const tag = typeof s === 'string' ? s : 'logicalType' in s ? s.logicalType : s.type |
| 68 | + |
| 69 | + if (value === null) return tag === 'null' |
| 70 | + if (tag === 'boolean') return typeof value === 'boolean' |
| 71 | + if (tag === 'int') return typeof value === 'number' && Number.isInteger(value) |
| 72 | + if (tag === 'long') return typeof value === 'bigint' || typeof value === 'number' |
| 73 | + if (tag === 'float' || tag === 'double') return typeof value === 'number' |
| 74 | + if (tag === 'string') return typeof value === 'string' |
| 75 | + if (tag === 'bytes') return value instanceof Uint8Array |
| 76 | + if (tag === 'record') return typeof value === 'object' && value !== null |
| 77 | + if (tag === 'array') return Array.isArray(value) |
| 78 | + return false |
| 79 | + }) |
| 80 | + |
| 81 | + if (idx === -1) throw new Error('union branch not found') |
| 82 | + writer.appendVarInt(idx) |
| 83 | + writeType(writer, schema[idx], value) |
| 84 | + } else if (typeof schema === 'string') { |
| 85 | + // primitive type |
| 86 | + if (schema === 'null') { |
| 87 | + // no-op |
| 88 | + } else if (schema === 'boolean') { |
| 89 | + writer.appendUint8(value ? 1 : 0) |
| 90 | + } else if (schema === 'int') { |
| 91 | + appendZigZag(writer, value) |
| 92 | + } else if (schema === 'long') { |
| 93 | + if (typeof value === 'bigint') { |
| 94 | + appendZigZag64(writer, value) |
| 95 | + } else { |
| 96 | + appendZigZag(writer, value) |
| 97 | + } |
| 98 | + } else if (schema === 'float') { |
| 99 | + writer.appendFloat32(value) |
| 100 | + } else if (schema === 'double') { |
| 101 | + writer.appendFloat64(value) |
| 102 | + } else if (schema === 'bytes') { |
| 103 | + const b = value instanceof Uint8Array ? value : new TextEncoder().encode(value) |
| 104 | + appendZigZag(writer, b.length) |
| 105 | + writer.appendBytes(b) |
| 106 | + |
| 107 | + } else if (schema === 'string') { |
| 108 | + const b = new TextEncoder().encode(String(value)) |
| 109 | + appendZigZag(writer, b.length) |
| 110 | + writer.appendBytes(b) |
| 111 | + } |
| 112 | + } else if ('logicalType' in schema) { |
| 113 | + if (schema.logicalType === 'date') { |
| 114 | + appendZigZag(writer, value instanceof Date ? Math.floor(value.getTime() / 86400000) : value) |
| 115 | + } else if (schema.logicalType === 'timestamp-millis') { |
| 116 | + appendZigZag64(writer, value instanceof Date ? BigInt(value.getTime()) : BigInt(value)) |
| 117 | + } else if (schema.logicalType === 'timestamp-micros') { |
| 118 | + appendZigZag64( |
| 119 | + writer, |
| 120 | + value instanceof Date ? BigInt(value.getTime()) * 1000n : BigInt(value) |
| 121 | + ) |
| 122 | + } else if (schema.logicalType === 'decimal') { |
| 123 | + const scale = 'scale' in schema ? schema.scale ?? 0 : 0 |
| 124 | + let u |
| 125 | + if (typeof value === 'bigint') { |
| 126 | + u = value |
| 127 | + } else if (typeof value === 'number') { |
| 128 | + u = BigInt(Math.round(value * 10 ** scale)) |
| 129 | + } else { |
| 130 | + throw new Error('decimal value must be bigint or number') |
| 131 | + } |
| 132 | + const b = bigIntToBytes(u) |
| 133 | + writer.appendVarInt(b.length) |
| 134 | + writer.appendBytes(b) |
| 135 | + } else { |
| 136 | + throw new Error(`unknown logical type ${schema.logicalType}`) |
| 137 | + } |
| 138 | + } else if (schema.type === 'record') { |
| 139 | + for (const f of schema.fields) { |
| 140 | + writeType(writer, f.type, value[f.name]) |
| 141 | + } |
| 142 | + } else if (schema.type === 'array') { |
| 143 | + if (value.length) { |
| 144 | + writer.appendVarInt(value.length) |
| 145 | + for (const it of value) writeType(writer, schema.items, it) |
| 146 | + } |
| 147 | + writer.appendVarInt(0) |
| 148 | + } else { |
| 149 | + throw new Error(`unknown schema type ${JSON.stringify(schema)}`) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * @param {Writer} writer |
| 155 | + * @param {number} v |
| 156 | + */ |
| 157 | +function appendZigZag(writer, v) { |
| 158 | + writer.appendVarInt(v << 1 ^ v >> 31) |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * @param {Writer} writer |
| 163 | + * @param {bigint} v |
| 164 | + */ |
| 165 | +function appendZigZag64(writer, v) { |
| 166 | + writer.appendVarBigInt(v << 1n ^ v >> 63n) |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * Convert a signed BigInt into two’s-complement big-endian bytes. |
| 171 | + * @param {bigint} value |
| 172 | + * @returns {Uint8Array} |
| 173 | + */ |
| 174 | +function bigIntToBytes(value) { |
| 175 | + const neg = value < 0n |
| 176 | + let abs = neg ? -value : value |
| 177 | + const out = [] |
| 178 | + while (abs > 0n) { out.unshift(Number(abs & 0xffn)); abs >>= 8n } |
| 179 | + if (out.length === 0) out.push(0) |
| 180 | + |
| 181 | + if (neg) { |
| 182 | + for (let i = 0; i < out.length; i++) out[i] ^= 0xff |
| 183 | + for (let i = out.length - 1; i >= 0; i--) { |
| 184 | + out[i] = out[i] + 1 & 0xff |
| 185 | + if (out[i]) break |
| 186 | + } |
| 187 | + if ((out[0] & 0x80) === 0) out.unshift(0xff) |
| 188 | + } else if ((out[0] & 0x80) !== 0) { |
| 189 | + out.unshift(0) |
| 190 | + } |
| 191 | + |
| 192 | + return Uint8Array.from(out) |
| 193 | +} |
0 commit comments