Skip to content

Commit f5fb00a

Browse files
committed
extract functions for clarity
1 parent 02eef1d commit f5fb00a

File tree

1 file changed

+76
-68
lines changed

1 file changed

+76
-68
lines changed

src/lib/tableProvider.ts

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -107,80 +107,88 @@ export function parquetDataFrame(from: AsyncBufferFrom, metadata: FileMetaData):
107107
return sortIndex
108108
}
109109

110+
function sortedRows({ start, end, orderBy }: { start: number, end: number, orderBy: OrderBy}) {
111+
const numRows = end - start
112+
const wrapped = new Array(numRows).fill(null).map(() => resolvableRow(header))
113+
114+
getSortIndex(orderBy).then(indices => {
115+
// Compute row groups to fetch
116+
for (const index of indices.slice(start, end)) {
117+
const groupIndex = groupEnds.findIndex(end => index < end)
118+
fetchRowGroup(groupIndex)
119+
}
120+
121+
// Re-assemble data in sorted order into wrapped
122+
for (let i = start; i < end; i++) {
123+
const index = indices[i]
124+
if (index === undefined) {
125+
throw new Error(`index ${i} not found in indices`)
126+
}
127+
const row = data[index]
128+
if (row === undefined) {
129+
throw new Error('Row not fetched')
130+
}
131+
const { cells } = row
132+
const wrappedRow = wrapped[i - start]
133+
if (wrappedRow === undefined) {
134+
throw new Error(`Wrapped row missing at index ${i - start}`)
135+
}
136+
wrappedRow.index.resolve(index)
137+
for (const key of header) {
138+
const cell = cells[key]
139+
if (cell) {
140+
// TODO(SL): should we remove this check? It makes sense only if header change
141+
// but if so, I guess we will have more issues
142+
cell
143+
.then((value: unknown) => {
144+
const wrappedCell = wrappedRow.cells[key]
145+
if (wrappedCell === undefined) {
146+
throw new Error(`Wrapped cell not found for column ${key}`)
147+
}
148+
wrappedCell.resolve(value)
149+
})
150+
.catch((error: unknown) => {
151+
console.error('Error resolving sorted row', error)
152+
})
153+
}
154+
}
155+
}
156+
}).catch((error: unknown) => {
157+
console.error(
158+
'Error fetching sort index or resolving sorted rows',
159+
error
160+
)
161+
})
162+
163+
return wrapped
164+
}
165+
166+
function unsortedRows({ start, end }: { start: number, end: number }) {
167+
for (let i = 0; i < groups.length; i++) {
168+
const groupStart = groupEnds[i - 1] ?? 0
169+
const groupEnd = groupEnds[i]
170+
if (groupEnd === undefined) {
171+
throw new Error(`Missing group end at index ${i}`)
172+
}
173+
if (start < groupEnd && end > groupStart) {
174+
fetchRowGroup(i)
175+
}
176+
}
177+
const wrapped = data.slice(start, end)
178+
if (wrapped.some(row => row === undefined)) {
179+
throw new Error('Row not fetched')
180+
}
181+
return wrapped as ResolvableRow[]
182+
}
183+
110184
return {
111185
header,
112186
numRows: Number(metadata.num_rows),
113187
rows({ start, end, orderBy }: { start: number, end: number, orderBy?: OrderBy}) {
114188
if (orderBy?.length) {
115-
const numRows = end - start
116-
const wrapped = new Array(numRows).fill(null).map(() => resolvableRow(header))
117-
118-
getSortIndex(orderBy).then(indices => {
119-
// Compute row groups to fetch
120-
for (const index of indices.slice(start, end)) {
121-
const groupIndex = groupEnds.findIndex(end => index < end)
122-
fetchRowGroup(groupIndex)
123-
}
124-
125-
// Re-assemble data in sorted order into wrapped
126-
for (let i = start; i < end; i++) {
127-
const index = indices[i]
128-
if (index === undefined) {
129-
throw new Error(`index ${i} not found in indices`)
130-
}
131-
const row = data[index]
132-
if (row === undefined) {
133-
throw new Error('Row not fetched')
134-
}
135-
const { cells } = row
136-
const wrappedRow = wrapped[i - start]
137-
if (wrappedRow === undefined) {
138-
throw new Error(`Wrapped row missing at index ${i - start}`)
139-
}
140-
wrappedRow.index.resolve(index)
141-
for (const key of header) {
142-
const cell = cells[key]
143-
if (cell) {
144-
// TODO(SL): should we remove this check? It makes sense only if header change
145-
// but if so, I guess we will have more issues
146-
cell
147-
.then((value: unknown) => {
148-
const wrappedCell = wrappedRow.cells[key]
149-
if (wrappedCell === undefined) {
150-
throw new Error(`Wrapped cell not found for column ${key}`)
151-
}
152-
wrappedCell.resolve(value)
153-
})
154-
.catch((error: unknown) => {
155-
console.error('Error resolving sorted row', error)
156-
})
157-
}
158-
}
159-
}
160-
}).catch((error: unknown) => {
161-
console.error(
162-
'Error fetching sort index or resolving sorted rows',
163-
error
164-
)
165-
})
166-
167-
return wrapped
189+
return sortedRows({ start, end, orderBy })
168190
} else {
169-
for (let i = 0; i < groups.length; i++) {
170-
const groupStart = groupEnds[i - 1] ?? 0
171-
const groupEnd = groupEnds[i]
172-
if (groupEnd === undefined) {
173-
throw new Error(`Missing group end at index ${i}`)
174-
}
175-
if (start < groupEnd && end > groupStart) {
176-
fetchRowGroup(i)
177-
}
178-
}
179-
const wrapped = data.slice(start, end)
180-
if (wrapped.some(row => row === undefined)) {
181-
throw new Error('Row not fetched')
182-
}
183-
return wrapped as ResolvableRow[]
191+
return unsortedRows({ start, end })
184192
}
185193
},
186194
sortable: true,

0 commit comments

Comments
 (0)