hyparquet/src/column.js

120 lines
5.0 KiB
JavaScript
Raw Normal View History

2024-05-18 02:41:40 +00:00
import { assembleLists } from './assemble.js'
import { convertWithDictionary } from './convert.js'
import { decompressPage, readDataPage, readDictionaryPage } from './datapage.js'
2024-02-24 18:11:04 +00:00
import { readDataPageV2 } from './datapageV2.js'
2024-01-08 01:04:05 +00:00
import { parquetHeader } from './header.js'
2024-06-08 02:30:30 +00:00
import { getMaxDefinitionLevel } from './schema.js'
2024-04-07 16:33:57 +00:00
import { concat } from './utils.js'
2024-01-08 01:04:05 +00:00
/**
* Parse column data from a buffer.
2024-01-08 01:04:05 +00:00
*
2024-05-23 05:24:54 +00:00
* @typedef {import('./types.js').ColumnMetaData} ColumnMetaData
* @typedef {import('./types.js').DecodedArray} DecodedArray
2024-05-23 05:24:54 +00:00
* @param {import('./types.js').DataReader} reader
2024-06-08 02:30:30 +00:00
* @param {number} rowLimit maximum number of rows to read
2024-01-08 01:04:05 +00:00
* @param {ColumnMetaData} columnMetadata column metadata
2024-05-23 05:24:54 +00:00
* @param {import('./types.js').SchemaTree[]} schemaPath schema path for the column
* @param {import('./hyparquet.js').ParquetReadOptions} options read options
2024-05-14 09:19:37 +00:00
* @returns {any[]} array of values
2024-01-08 01:04:05 +00:00
*/
2024-06-08 02:30:30 +00:00
export function readColumn(reader, rowLimit, columnMetadata, schemaPath, { compressors, utf8 }) {
2024-05-23 05:24:54 +00:00
const { element } = schemaPath[schemaPath.length - 1]
/** @type {DecodedArray | undefined} */
2024-01-08 01:04:05 +00:00
let dictionary = undefined
/** @type {any[]} */
2024-04-07 16:33:57 +00:00
const rowData = []
2024-04-30 00:38:26 +00:00
2024-06-08 02:30:30 +00:00
while (rowData.length < rowLimit) {
2024-01-08 01:04:05 +00:00
// parse column header
2024-05-01 07:55:16 +00:00
const header = parquetHeader(reader)
2024-05-23 05:24:54 +00:00
// assert(header.compressed_page_size !== undefined)
2024-01-08 01:04:05 +00:00
// read compressed_page_size bytes starting at offset
2024-05-14 09:19:37 +00:00
const compressedBytes = new Uint8Array(
2024-05-23 05:24:54 +00:00
reader.view.buffer, reader.view.byteOffset + reader.offset, header.compressed_page_size
2024-02-09 21:44:35 +00:00
)
2024-01-08 01:04:05 +00:00
// parse page data by type
2024-05-06 00:51:31 +00:00
/** @type {DecodedArray} */
let values
2024-04-18 07:02:29 +00:00
if (header.type === 'DATA_PAGE') {
2024-01-08 01:04:05 +00:00
const daph = header.data_page_header
2024-01-13 00:28:37 +00:00
if (!daph) throw new Error('parquet data page header is undefined')
2024-01-08 01:04:05 +00:00
2024-05-23 05:24:54 +00:00
const page = decompressPage(compressedBytes, Number(header.uncompressed_page_size), columnMetadata.codec, compressors)
2024-05-02 06:23:50 +00:00
const { definitionLevels, repetitionLevels, dataPage } = readDataPage(page, daph, schemaPath, columnMetadata)
2024-06-08 02:30:30 +00:00
// assert(!daph.statistics?.null_count || daph.statistics.null_count === BigInt(daph.num_values - dataPage.length))
2024-01-08 01:04:05 +00:00
// convert types, dereference dictionary, and assemble lists
values = convertWithDictionary(dataPage, dictionary, element, daph.encoding, utf8)
2024-05-18 02:41:40 +00:00
if (repetitionLevels.length || definitionLevels?.length) {
2024-04-30 00:38:26 +00:00
const maxDefinitionLevel = getMaxDefinitionLevel(schemaPath)
2024-05-18 05:44:03 +00:00
const repetitionPath = schemaPath.map(({ element }) => element.repetition_type)
2024-06-08 02:30:30 +00:00
assembleLists(
rowData, definitionLevels, repetitionLevels, values, repetitionPath, maxDefinitionLevel
2024-02-27 03:33:38 +00:00
)
2024-01-08 01:04:05 +00:00
} else {
2024-05-18 02:41:40 +00:00
// wrap nested flat data by depth
for (let i = 2; i < schemaPath.length; i++) {
if (schemaPath[i].element.repetition_type !== 'REQUIRED') {
values = Array.from(values, e => [e])
2024-05-18 02:41:40 +00:00
}
}
2024-06-08 02:30:30 +00:00
concat(rowData, values)
2024-01-08 01:04:05 +00:00
}
2024-04-18 07:02:29 +00:00
} else if (header.type === 'DATA_PAGE_V2') {
2024-02-24 18:11:04 +00:00
const daph2 = header.data_page_header_v2
if (!daph2) throw new Error('parquet data page header v2 is undefined')
2024-05-02 06:23:50 +00:00
const { definitionLevels, repetitionLevels, dataPage } = readDataPageV2(
2024-04-30 00:38:26 +00:00
compressedBytes, header, schemaPath, columnMetadata, compressors
2024-02-24 18:11:04 +00:00
)
// convert types, dereference dictionary, and assemble lists
values = convertWithDictionary(dataPage, dictionary, element, daph2.encoding, utf8)
2024-05-18 02:41:40 +00:00
if (repetitionLevels.length || definitionLevels?.length) {
const maxDefinitionLevel = getMaxDefinitionLevel(schemaPath)
2024-05-18 05:44:03 +00:00
const repetitionPath = schemaPath.map(({ element }) => element.repetition_type)
2024-06-08 02:30:30 +00:00
assembleLists(
rowData, definitionLevels, repetitionLevels, values, repetitionPath, maxDefinitionLevel
2024-05-06 00:51:31 +00:00
)
2024-06-08 02:30:30 +00:00
} else {
concat(rowData, values)
2024-02-24 18:11:04 +00:00
}
2024-05-18 02:41:40 +00:00
} else if (header.type === 'DICTIONARY_PAGE') {
const diph = header.dictionary_page_header
if (!diph) throw new Error('parquet dictionary page header is undefined')
const page = decompressPage(
compressedBytes, Number(header.uncompressed_page_size), columnMetadata.codec, compressors
)
dictionary = readDictionaryPage(page, diph, columnMetadata, element.type_length)
2024-01-08 01:04:05 +00:00
} else {
throw new Error(`parquet unsupported page type: ${header.type}`)
}
2024-05-01 07:55:16 +00:00
reader.offset += header.compressed_page_size
2024-01-08 01:04:05 +00:00
}
2024-06-08 02:30:30 +00:00
if (rowData.length < rowLimit) {
throw new Error(`parquet row data length ${rowData.length} does not match row group limit ${rowLimit}}`)
2024-01-14 19:14:04 +00:00
}
2024-06-26 00:41:47 +00:00
if (rowData.length > rowLimit) {
rowData.length = rowLimit // truncate to row limit
}
2024-01-14 19:14:04 +00:00
return rowData
}
/**
* Find the start byte offset for a column chunk.
*
* @param {ColumnMetaData} columnMetadata
* @returns {[bigint, bigint]} byte offset range
2024-01-14 19:14:04 +00:00
*/
export function getColumnRange({ dictionary_page_offset, data_page_offset, total_compressed_size }) {
2024-01-14 19:14:04 +00:00
let columnOffset = dictionary_page_offset
if (!columnOffset || data_page_offset < columnOffset) {
2024-01-14 19:14:04 +00:00
columnOffset = data_page_offset
}
return [columnOffset, columnOffset + total_compressed_size]
2024-01-08 01:04:05 +00:00
}