-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetParquetColumn.ts
More file actions
65 lines (58 loc) · 2.25 KB
/
getParquetColumn.ts
File metadata and controls
65 lines (58 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { ColumnData, ParquetReadOptions, parquetRead } from 'hyparquet'
type GetColumnOptions = Omit<ParquetReadOptions, 'columns' | 'rowStart' | 'rowEnd' | 'onChunk' | 'onComplete'> & {column: string}
export async function getParquetColumn({ metadata, file, column, compressors }: GetColumnOptions): Promise<unknown[]> {
const numRows = Number(metadata?.num_rows)
if (isNaN(numRows)) {
throw new Error('metadata.num_rows is undefined')
}
if (numRows === 0) {
return []
}
const lastError: {error?: Error} = {}
const values: unknown[] = Array(numRows).fill(undefined)
const ranges: [number, number][] = []
function onChunk({ columnName, columnData, rowStart, rowEnd }: ColumnData) {
if (columnName !== column) {
lastError.error = new Error(`unexpected column name ${columnName}`)
}
for (let i = rowStart; i < rowEnd; i++) {
values[i] = columnData[i - rowStart]
}
ranges.push([rowStart, rowEnd])
}
// this awaits all the promises. When it returns, all the data should have already been sent using onChunk
await parquetRead({ metadata, file, columns: [column], compressors, onChunk })
// Do some checks before returning the data
// check for errors
if (lastError.error !== undefined) {
throw lastError.error
}
// check for missing data (should be faster than checking for undefined values in the array)
const sortedRanges = ranges.sort((a, b) => a[0] - b[0])
for (let i = 0; i < sortedRanges.length - 1; i++) {
const range = sortedRanges[i]
const nextRange = sortedRanges[i + 1]
if (!range || !nextRange) {
throw new Error('The ranges should not be undefined')
}
if (range[1] !== nextRange[0]) {
throw new Error(`missing data between rows ${range[1]} and ${nextRange[0]}`)
}
}
const firstRange = sortedRanges[0]
if (!firstRange) {
throw new Error('The first range should not be undefined')
}
if (firstRange[0] !== 0) {
throw new Error(`missing data before row ${firstRange[0]}`)
}
const lastRange = sortedRanges[sortedRanges.length - 1]
if (!lastRange) {
throw new Error('The last range should not be undefined')
}
if (lastRange[1] !== numRows) {
throw new Error(`missing data after row ${lastRange[1]}`)
}
// return the values
return values
}