2024-05-22 09:34:42 +00:00
|
|
|
import { bitWidth, byteStreamSplit, readRleBitPackedHybrid } from './encoding.js'
|
2024-05-01 03:28:50 +00:00
|
|
|
import { readPlain } from './plain.js'
|
2024-05-22 09:34:42 +00:00
|
|
|
import { getMaxDefinitionLevel, getMaxRepetitionLevel } from './schema.js'
|
2024-05-26 13:00:20 +00:00
|
|
|
import { snappyUncompress } from './snappy.js'
|
2024-01-07 23:33:24 +00:00
|
|
|
|
|
|
|
|
/**
|
2024-05-24 07:19:02 +00:00
|
|
|
* Read a data page from uncompressed reader.
|
2024-04-18 07:02:29 +00:00
|
|
|
*
|
2024-02-26 18:32:53 +00:00
|
|
|
* @typedef {import("./types.d.ts").DataPage} DataPage
|
2024-01-07 23:33:24 +00:00
|
|
|
* @typedef {import("./types.d.ts").ColumnMetaData} ColumnMetaData
|
|
|
|
|
* @typedef {import("./types.d.ts").DataPageHeader} DataPageHeader
|
2024-04-30 00:38:26 +00:00
|
|
|
* @typedef {import("./types.d.ts").SchemaTree} SchemaTree
|
2024-05-02 06:23:50 +00:00
|
|
|
* @typedef {import("./types.d.ts").DecodedArray} DecodedArray
|
2024-01-07 23:33:24 +00:00
|
|
|
* @param {Uint8Array} bytes raw page data (should already be decompressed)
|
|
|
|
|
* @param {DataPageHeader} daph data page header
|
2024-04-30 01:45:29 +00:00
|
|
|
* @param {SchemaTree[]} schemaPath
|
|
|
|
|
* @param {ColumnMetaData} columnMetadata
|
2024-01-20 21:52:36 +00:00
|
|
|
* @returns {DataPage} definition levels, repetition levels, and array of values
|
2024-01-07 23:33:24 +00:00
|
|
|
*/
|
2024-05-20 09:53:07 +00:00
|
|
|
export function readDataPage(bytes, daph, schemaPath, { type }) {
|
2024-04-17 07:48:33 +00:00
|
|
|
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
|
|
|
const reader = { view, offset: 0 }
|
2024-05-02 06:23:50 +00:00
|
|
|
/** @type {DecodedArray} */
|
2024-05-20 09:53:07 +00:00
|
|
|
let dataPage
|
2024-01-07 23:33:24 +00:00
|
|
|
|
2024-05-11 01:50:12 +00:00
|
|
|
// repetition and definition levels
|
2024-04-30 00:38:26 +00:00
|
|
|
const repetitionLevels = readRepetitionLevels(reader, daph, schemaPath)
|
2024-06-08 02:30:30 +00:00
|
|
|
// assert(!repetitionLevels.length || repetitionLevels.length === daph.num_values)
|
2024-05-02 06:23:50 +00:00
|
|
|
const { definitionLevels, numNulls } = readDefinitionLevels(reader, daph, schemaPath)
|
2024-06-08 02:30:30 +00:00
|
|
|
// assert(!definitionLevels.length || definitionLevels.length === daph.num_values)
|
2024-01-07 23:33:24 +00:00
|
|
|
|
|
|
|
|
// read values based on encoding
|
2024-02-26 18:32:53 +00:00
|
|
|
const nValues = daph.num_values - numNulls
|
2024-02-27 18:33:17 +00:00
|
|
|
if (daph.encoding === 'PLAIN') {
|
2024-05-13 04:11:57 +00:00
|
|
|
const { type_length } = schemaPath[schemaPath.length - 1].element
|
2024-05-20 09:53:07 +00:00
|
|
|
dataPage = readPlain(reader, type, nValues, type_length)
|
2024-02-12 04:43:54 +00:00
|
|
|
} else if (
|
2024-02-27 18:33:17 +00:00
|
|
|
daph.encoding === 'PLAIN_DICTIONARY' ||
|
|
|
|
|
daph.encoding === 'RLE_DICTIONARY' ||
|
|
|
|
|
daph.encoding === 'RLE'
|
2024-02-12 04:43:54 +00:00
|
|
|
) {
|
2024-05-20 09:53:07 +00:00
|
|
|
const bitWidth = type === 'BOOLEAN' ? 1 : view.getUint8(reader.offset++)
|
2024-01-07 23:33:24 +00:00
|
|
|
if (bitWidth) {
|
2024-05-02 06:23:50 +00:00
|
|
|
dataPage = new Array(nValues)
|
|
|
|
|
readRleBitPackedHybrid(reader, bitWidth, view.byteLength - reader.offset, dataPage)
|
2024-01-07 23:33:24 +00:00
|
|
|
} else {
|
2024-05-20 09:53:07 +00:00
|
|
|
dataPage = new Uint8Array(nValues) // nValue zeroes
|
2024-01-07 23:33:24 +00:00
|
|
|
}
|
2024-05-20 09:53:07 +00:00
|
|
|
} else if (daph.encoding === 'BYTE_STREAM_SPLIT') {
|
2024-05-21 07:15:27 +00:00
|
|
|
const { type_length } = schemaPath[schemaPath.length - 1].element
|
|
|
|
|
dataPage = byteStreamSplit(reader, nValues, type, type_length)
|
2024-01-07 23:33:24 +00:00
|
|
|
} else {
|
|
|
|
|
throw new Error(`parquet unsupported encoding: ${daph.encoding}`)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 06:23:50 +00:00
|
|
|
return { definitionLevels, repetitionLevels, dataPage }
|
2024-01-07 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {Uint8Array} bytes raw page data
|
2024-05-11 01:50:12 +00:00
|
|
|
* @param {import("./types.d.ts").DictionaryPageHeader} diph dictionary page header
|
2024-04-30 01:45:29 +00:00
|
|
|
* @param {ColumnMetaData} columnMetadata
|
2024-05-13 04:11:57 +00:00
|
|
|
* @param {number | undefined} typeLength - type_length from schema
|
2024-05-23 06:45:02 +00:00
|
|
|
* @returns {DecodedArray}
|
2024-01-07 23:33:24 +00:00
|
|
|
*/
|
2024-05-13 04:11:57 +00:00
|
|
|
export function readDictionaryPage(bytes, diph, columnMetadata, typeLength) {
|
2024-04-17 07:48:33 +00:00
|
|
|
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
|
|
|
const reader = { view, offset: 0 }
|
2024-05-13 04:11:57 +00:00
|
|
|
return readPlain(reader, columnMetadata.type, diph.num_values, typeLength)
|
2024-01-07 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-04-17 07:48:33 +00:00
|
|
|
* @typedef {import("./types.d.ts").DataReader} DataReader
|
|
|
|
|
* @param {DataReader} reader data view for the page
|
2024-01-07 23:33:24 +00:00
|
|
|
* @param {DataPageHeader} daph data page header
|
2024-04-30 01:45:29 +00:00
|
|
|
* @param {SchemaTree[]} schemaPath
|
2024-04-17 07:48:33 +00:00
|
|
|
* @returns {any[]} repetition levels and number of bytes read
|
2024-01-07 23:33:24 +00:00
|
|
|
*/
|
2024-04-30 00:38:26 +00:00
|
|
|
function readRepetitionLevels(reader, daph, schemaPath) {
|
|
|
|
|
if (schemaPath.length > 1) {
|
|
|
|
|
const maxRepetitionLevel = getMaxRepetitionLevel(schemaPath)
|
2024-02-24 18:11:04 +00:00
|
|
|
if (maxRepetitionLevel) {
|
2024-04-30 21:40:18 +00:00
|
|
|
const values = new Array(daph.num_values)
|
2024-05-22 09:34:42 +00:00
|
|
|
readRleBitPackedHybrid(reader, bitWidth(maxRepetitionLevel), 0, values)
|
2024-04-30 21:40:18 +00:00
|
|
|
return values
|
2024-01-07 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-17 07:48:33 +00:00
|
|
|
return []
|
2024-01-07 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-04-17 07:48:33 +00:00
|
|
|
* @param {DataReader} reader data view for the page
|
2024-01-07 23:33:24 +00:00
|
|
|
* @param {DataPageHeader} daph data page header
|
2024-04-30 01:45:29 +00:00
|
|
|
* @param {SchemaTree[]} schemaPath
|
2024-05-02 06:23:50 +00:00
|
|
|
* @returns {{ definitionLevels: number[], numNulls: number }} definition levels
|
2024-01-07 23:33:24 +00:00
|
|
|
*/
|
2024-04-30 00:38:26 +00:00
|
|
|
function readDefinitionLevels(reader, daph, schemaPath) {
|
2024-05-22 09:34:42 +00:00
|
|
|
const maxDefinitionLevel = getMaxDefinitionLevel(schemaPath)
|
|
|
|
|
if (!maxDefinitionLevel) return { definitionLevels: [], numNulls: 0 }
|
2024-01-20 21:52:36 +00:00
|
|
|
|
2024-05-22 09:34:42 +00:00
|
|
|
const definitionLevels = new Array(daph.num_values)
|
|
|
|
|
readRleBitPackedHybrid(reader, bitWidth(maxDefinitionLevel), 0, definitionLevels)
|
2024-01-20 21:52:36 +00:00
|
|
|
|
2024-05-22 09:34:42 +00:00
|
|
|
// count nulls
|
|
|
|
|
let numNulls = daph.num_values
|
|
|
|
|
for (const def of definitionLevels) {
|
|
|
|
|
if (def === maxDefinitionLevel) numNulls--
|
2024-01-07 23:33:24 +00:00
|
|
|
}
|
2024-05-22 09:34:42 +00:00
|
|
|
if (numNulls === 0) definitionLevels.length = 0
|
|
|
|
|
|
|
|
|
|
return { definitionLevels, numNulls }
|
2024-01-07 23:33:24 +00:00
|
|
|
}
|
2024-05-26 13:00:20 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {Uint8Array} compressedBytes
|
|
|
|
|
* @param {number} uncompressed_page_size
|
|
|
|
|
* @param {import('./types.js').CompressionCodec} codec
|
|
|
|
|
* @param {import('./types.js').Compressors | undefined} compressors
|
|
|
|
|
* @returns {Uint8Array}
|
|
|
|
|
*/
|
|
|
|
|
export function decompressPage(compressedBytes, uncompressed_page_size, codec, compressors) {
|
|
|
|
|
/** @type {Uint8Array} */
|
|
|
|
|
let page
|
|
|
|
|
const customDecompressor = compressors?.[codec]
|
|
|
|
|
if (codec === 'UNCOMPRESSED') {
|
|
|
|
|
page = compressedBytes
|
|
|
|
|
} else if (customDecompressor) {
|
|
|
|
|
page = customDecompressor(compressedBytes, uncompressed_page_size)
|
|
|
|
|
} else if (codec === 'SNAPPY') {
|
|
|
|
|
page = new Uint8Array(uncompressed_page_size)
|
|
|
|
|
snappyUncompress(compressedBytes, page)
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`parquet unsupported compression codec: ${codec}`)
|
|
|
|
|
}
|
|
|
|
|
if (page?.length !== uncompressed_page_size) {
|
|
|
|
|
throw new Error(`parquet decompressed page length ${page?.length} does not match header ${uncompressed_page_size}`)
|
|
|
|
|
}
|
|
|
|
|
return page
|
|
|
|
|
}
|