hyparquet-writer/src/unconvert.js

127 lines
4.3 KiB
JavaScript
Raw Normal View History

2025-04-11 10:13:35 +00:00
const dayMillis = 86400000 // 1 day in milliseconds
/**
* Convert from rich to primitive types.
*
* @import {DecodedArray, SchemaElement} from 'hyparquet'
2025-04-11 23:26:07 +00:00
* @param {SchemaElement} element
* @param {DecodedArray} values
* @returns {DecodedArray}
*/
2025-04-11 23:26:07 +00:00
export function unconvert(element, values) {
const ctype = element.converted_type
2025-04-11 10:13:35 +00:00
if (ctype === 'DECIMAL') {
2025-04-11 23:26:07 +00:00
const scale = element.scale || 0
2025-04-11 10:13:35 +00:00
const factor = 10 ** scale
return values.map(v => {
if (v === null || v === undefined) return v
if (typeof v !== 'number') throw new Error('DECIMAL must be a number')
2025-04-11 23:26:07 +00:00
return unconvertDecimal(element, BigInt(Math.round(v * factor)))
2025-04-11 10:13:35 +00:00
})
}
if (ctype === 'DATE') {
return values.map(v => v.getTime())
}
2025-04-11 08:41:56 +00:00
if (ctype === 'TIMESTAMP_MILLIS') {
return Array.from(values).map(v => BigInt(v.getTime()))
}
if (ctype === 'TIMESTAMP_MICROS') {
return Array.from(values).map(v => BigInt(v.getTime() * 1000))
}
if (ctype === 'JSON') {
if (!Array.isArray(values)) throw new Error('JSON must be an array')
const encoder = new TextEncoder()
return values.map(v => encoder.encode(JSON.stringify(v)))
}
if (ctype === 'UTF8') {
if (!Array.isArray(values)) throw new Error('strings must be an array')
const encoder = new TextEncoder()
return values.map(v => encoder.encode(v))
}
return values
}
2025-04-03 20:21:57 +00:00
/**
* Uncovert from rich type to byte array for metadata statistics.
*
* @param {import('hyparquet/src/types.js').MinMaxType | undefined} value
2025-04-11 23:26:07 +00:00
* @param {SchemaElement} element
2025-04-03 20:21:57 +00:00
* @returns {Uint8Array | undefined}
*/
2025-04-11 23:26:07 +00:00
export function unconvertMetadata(value, element) {
2025-04-03 20:21:57 +00:00
if (value === undefined || value === null) return undefined
2025-04-11 23:26:07 +00:00
const { type, converted_type } = element
2025-04-03 20:21:57 +00:00
if (type === 'BOOLEAN') return new Uint8Array([value ? 1 : 0])
if (type === 'BYTE_ARRAY' || type === 'FIXED_LEN_BYTE_ARRAY') {
// truncate byte arrays to 16 bytes for statistics
if (value instanceof Uint8Array) return value.slice(0, 16)
return new TextEncoder().encode(value.toString().slice(0, 16))
}
if (type === 'FLOAT' && typeof value === 'number') {
const buffer = new ArrayBuffer(4)
new DataView(buffer).setFloat32(0, value, true)
return new Uint8Array(buffer)
}
if (type === 'DOUBLE' && typeof value === 'number') {
const buffer = new ArrayBuffer(8)
new DataView(buffer).setFloat64(0, value, true)
return new Uint8Array(buffer)
}
if (type === 'INT32' && typeof value === 'number') {
const buffer = new ArrayBuffer(4)
new DataView(buffer).setInt32(0, value, true)
return new Uint8Array(buffer)
}
if (type === 'INT64' && typeof value === 'bigint') {
const buffer = new ArrayBuffer(8)
new DataView(buffer).setBigInt64(0, value, true)
return new Uint8Array(buffer)
}
2025-04-11 10:13:35 +00:00
if (type === 'INT32' && converted_type === 'DATE' && value instanceof Date) {
const buffer = new ArrayBuffer(8)
new DataView(buffer).setInt32(0, Math.floor(value.getTime() / dayMillis), true)
return new Uint8Array(buffer)
}
2025-04-03 20:21:57 +00:00
if (type === 'INT64' && converted_type === 'TIMESTAMP_MILLIS' && value instanceof Date) {
const buffer = new ArrayBuffer(8)
new DataView(buffer).setBigInt64(0, BigInt(value.getTime()), true)
return new Uint8Array(buffer)
}
throw new Error(`unsupported type for statistics: ${type} with value ${value}`)
}
2025-04-11 10:13:35 +00:00
/**
2025-04-11 23:26:07 +00:00
* @param {SchemaElement} element
2025-04-11 10:13:35 +00:00
* @param {bigint} value
2025-04-11 23:26:07 +00:00
* @returns {number | bigint | Uint8Array}
2025-04-11 10:13:35 +00:00
*/
2025-04-11 23:26:07 +00:00
export function unconvertDecimal({ type, type_length }, value) {
if (type === 'INT32') return Number(value)
if (type === 'INT64') return value
if (type === 'FIXED_LEN_BYTE_ARRAY' && !type_length) {
throw new Error('fixed length byte array type_length is required')
}
if (!type_length && !value) return new Uint8Array()
2025-04-11 10:13:35 +00:00
2025-04-11 23:26:07 +00:00
const bytes = []
2025-04-11 10:13:35 +00:00
while (true) {
// extract the lowest 8 bits
2025-04-11 23:26:07 +00:00
const byte = Number(value & 0xffn)
2025-04-11 10:13:35 +00:00
bytes.unshift(byte)
2025-04-11 23:26:07 +00:00
value >>= 8n
2025-04-11 10:13:35 +00:00
2025-04-11 23:26:07 +00:00
if (type_length) {
if (bytes.length >= type_length) break // fixed length
} else {
// for nonnegative: stop when top byte has signBit = 0 AND shifted value == 0n
// for negative: stop when top byte has signBit = 1 AND shifted value == -1n
const signBit = byte & 0x80
if (!signBit && value === 0n || signBit && value === -1n) {
break
}
2025-04-11 10:13:35 +00:00
}
}
return new Uint8Array(bytes)
}