From 9a5ac8874beafb9f49e18f618771a34962862452 Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Mon, 18 Nov 2024 13:47:21 +0100 Subject: [PATCH 01/11] copy files from hightable and remove dependency (it was including React in the bundle) --- packages/utils/package.json | 1 - packages/utils/src/dataframe.ts | 162 ++++++++++++++++++++++++++ packages/utils/src/rowCache.ts | 50 ++++++++ packages/utils/src/tableProvider.ts | 2 +- packages/utils/test/dataframe.test.ts | 140 ++++++++++++++++++++++ packages/utils/test/rowCache.test.ts | 94 +++++++++++++++ 6 files changed, 447 insertions(+), 2 deletions(-) create mode 100644 packages/utils/src/dataframe.ts create mode 100644 packages/utils/src/rowCache.ts create mode 100644 packages/utils/test/dataframe.test.ts create mode 100644 packages/utils/test/rowCache.test.ts diff --git a/packages/utils/package.json b/packages/utils/package.json index 8243095e..c8017b3d 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -36,7 +36,6 @@ "test": "vitest run" }, "dependencies": { - "hightable": "0.6.3", "hyparquet": "1.5.0", "hyparquet-compressors": "0.1.4" }, diff --git a/packages/utils/src/dataframe.ts b/packages/utils/src/dataframe.ts new file mode 100644 index 00000000..48ae777e --- /dev/null +++ b/packages/utils/src/dataframe.ts @@ -0,0 +1,162 @@ +type WrappedPromise = Promise & { + resolved?: T + rejected?: Error +} + +/** + * A row where each cell is a promise. + * The promise must be wrapped with `wrapPromise` so that HighTable can render + * the state synchronously. + */ +export type AsyncRow = Record> + +/** + * A row where each cell is a resolved value. + */ +export type Row = Record + +/** + * Streamable row data + */ +export interface DataFrame { + header: string[] + numRows: number + // Rows are 0-indexed, excludes the header, end is exclusive + rows(start: number, end: number, orderBy?: string): AsyncRow[] | Promise + sortable?: boolean +} + +export function resolvableRow(header: string[]): { [key: string]: ResolvablePromise } { + return Object.fromEntries(header.map(key => [key, resolvablePromise()])) +} + +/** + * Helper method to wrap future rows into AsyncRows. + * Helpful when you want to define a DataFrame with simple async fetching of rows. + * This function turns future data into a "grid" of wrapped promises. + */ +export function asyncRows(rows: AsyncRow[] | Promise, numRows: number, header: string[]): AsyncRow[] { + if (Array.isArray(rows)) return rows + // Make grid of resolvable promises + const wrapped = new Array(numRows).fill(null).map(_ => resolvableRow(header)) + rows.then(rows => { + if (rows.length !== numRows) { + console.warn(`Expected ${numRows} rows, got ${rows.length}`) + } + for (let i = 0; i < rows.length; i++) { + const row = rows[i] + for (const key of header) { + wrapped[i][key].resolve(row[key]) + } + } + }).catch(error => { + // Reject all promises on error + for (let i = 0; i < numRows; i++) { + for (const key of header) { + wrapped[i][key].reject(error) + } + } + }) + return wrapped +} + +/** + * Wrap a promise to save the resolved value and error. + * Note: you can't await on a WrappedPromise, you must use then. + */ +export function wrapPromise(promise: Promise | T): WrappedPromise { + if (!(promise instanceof Promise)) { + promise = Promise.resolve(promise) + } + const wrapped: WrappedPromise = promise.then(resolved => { + wrapped.resolved = resolved + return resolved + }).catch(rejected => { + wrapped.rejected = rejected + throw rejected + }) + return wrapped +} + +export type ResolvablePromise = Promise & { + resolve: (value: T) => void + reject: (error: Error) => void +} + +/** + * Create a promise that can be resolved or rejected later. + */ +export function resolvablePromise(): ResolvablePromise { + let resolve: (value: T) => void + let reject: (error: Error) => void + const promise = wrapPromise(new Promise((res, rej) => { + resolve = res + reject = rej + })) as ResolvablePromise + promise.resolve = resolve! + promise.reject = reject! + return promise +} + +/** + * Wraps a DataFrame to make it sortable. + * Requires fetching all rows to sort. + */ +export function sortableDataFrame(data: DataFrame): DataFrame { + if (data.sortable) return data // already sortable + // Fetch all rows and add __index__ column + let all: Promise + return { + ...data, + rows(start: number, end: number, orderBy?: string): AsyncRow[] | Promise { + if (orderBy) { + if (!data.header.includes(orderBy)) { + throw new Error(`Invalid orderBy field: ${orderBy}`) + } + if (!all) { + // Fetch all rows and add __index__ column + all = awaitRows(data.rows(0, data.numRows)) + .then(rows => rows.map((row, i) => ({ __index__: i, ...row }))) + } + const sorted = all.then(all => { + return all.sort((a, b) => { + if (a[orderBy] < b[orderBy]) return -1 + if (a[orderBy] > b[orderBy]) return 1 + return 0 + }).slice(start, end) + }) + return sorted + } else { + return data.rows(start, end) + } + }, + sortable: true, + } +} + +/** + * Await all promises in an AsyncRow and return resolved row. + */ +export function awaitRow(row: AsyncRow): Promise { + return Promise.all(Object.values(row)) + .then(values => Object.fromEntries(Object.keys(row).map((key, i) => [key, values[i]]))) +} + +/** + * Await all promises in list of AsyncRows and return resolved rows. + */ +export function awaitRows(rows: AsyncRow[] | Promise): Promise { + if (rows instanceof Promise) return rows + return Promise.all(rows.map(awaitRow)) +} + +export function arrayDataFrame(data: Row[]): DataFrame { + if (!data.length) return { header: [], numRows: 0, rows: () => Promise.resolve([]) } + return { + header: Object.keys(data[0]), + numRows: data.length, + rows(start: number, end: number): Promise { + return Promise.resolve(data.slice(start, end)) + }, + } +} diff --git a/packages/utils/src/rowCache.ts b/packages/utils/src/rowCache.ts new file mode 100644 index 00000000..516dc703 --- /dev/null +++ b/packages/utils/src/rowCache.ts @@ -0,0 +1,50 @@ +import { AsyncRow, DataFrame, asyncRows } from './dataframe.js' + +/** + * Wrap a dataframe with cached rows. + * + * @param df async dataframe to wrap + */ +export function rowCache(df: DataFrame): DataFrame { + // Row cache is stored as a sorted array of RowGroups, per sort order + const caches: {[key: string]: AsyncRow[]} = {} + + let hits = 0 + let misses = 0 + + return { + ...df, + rows(start: number, end: number, orderBy?: string): AsyncRow[] { + // Cache per sort order + const cache = caches[orderBy || ''] ||= new Array(df.numRows) + const n = hits + misses + if (n && !(n % 10)) { + console.log(`Cache hits: ${hits} / ${hits + misses} (${(100 * hits / (hits + misses)).toFixed(1)}%)`) + } + + // Fetch missing rows in contiguous blocks + let blockStart: number | undefined + let hasCacheMiss = false + for (let i = start; i <= end; i++) { + if (i < end && !cache[i]) { + if (blockStart === undefined) { + blockStart = i + } + } else if (blockStart !== undefined) { + const blockEnd = i + const numRows = blockEnd - blockStart + const futureRows = asyncRows(df.rows(blockStart, blockEnd, orderBy), numRows, df.header) + for (let j = 0; j < blockEnd - blockStart; j++) { + cache[blockStart + j] = futureRows[j] + } + blockStart = undefined + hasCacheMiss = true + } + } + if (hasCacheMiss) misses++ + else hits++ + + return cache.slice(start, end) + }, + } +} diff --git a/packages/utils/src/tableProvider.ts b/packages/utils/src/tableProvider.ts index 5791a853..d67742ea 100644 --- a/packages/utils/src/tableProvider.ts +++ b/packages/utils/src/tableProvider.ts @@ -1,5 +1,5 @@ -import { DataFrame, ResolvablePromise, resolvablePromise } from 'hightable' import { FileMetaData, parquetSchema } from 'hyparquet' +import { DataFrame, ResolvablePromise, resolvablePromise } from './dataframe.js' import { parquetQueryWorker, parquetSortIndexWorker } from './workers/parquetWorkerClient.js' import type { AsyncBufferFrom } from './workers/types.d.ts' diff --git a/packages/utils/test/dataframe.test.ts b/packages/utils/test/dataframe.test.ts new file mode 100644 index 00000000..2ed8ceba --- /dev/null +++ b/packages/utils/test/dataframe.test.ts @@ -0,0 +1,140 @@ +import { describe, expect, it } from 'vitest' +import { + DataFrame, Row, arrayDataFrame, awaitRows, resolvablePromise, sortableDataFrame, wrapPromise, +} from '../src/dataframe.js' + +function wrapObject(obj: Record): Row { + return Object.fromEntries( + Object.entries(obj).map(([key, value]) => [key, wrapPromise(value)]) + ) +} + +describe('resolvablePromise', () => { + it('should resolve with a value', async () => { + const promise = resolvablePromise() + promise.resolve(42) + expect(await promise).toBe(42) + }) + it('should reject with an error', async () => { + const promise = resolvablePromise() + promise.reject(new Error('Failed')) + await expect(promise).rejects.toThrow('Failed') + }) +}) + +describe('awaitRows', () => { + it('should resolve with a row', async () => { + const row = wrapObject({ id: 1, name: 'Alice', age: 30, __index__: 0 }) + const result = await awaitRows([row]) + expect(result).toEqual([{ id: 1, name: 'Alice', age: 30, __index__: 0 }]) + }) +}) + +describe('sortableDataFrame', () => { + const data = [ + { id: 3, name: 'Charlie', age: 25 }, + { id: 1, name: 'Alice', age: 30 }, + { id: 2, name: 'Bob', age: 20 }, + { id: 4, name: 'Dani', age: 20 }, + ] + + const dataFrame: DataFrame = { + header: ['id', 'name', 'age'], + numRows: data.length, + rows(start: number, end: number): Row[] { + // Return the slice of data between start and end indices + return data.slice(start, end).map(wrapObject) + }, + sortable: false, + } + + const sortableDf = sortableDataFrame(dataFrame) + + it('should set sortable to true', () => { + expect(sortableDf.sortable).toBe(true) + }) + + it('should preserve header and numRows', () => { + expect(sortableDf.header).toEqual(dataFrame.header) + expect(sortableDf.numRows).toBe(dataFrame.numRows) + }) + + it('should return unsorted data when orderBy is not provided', async () => { + const rows = await awaitRows(sortableDf.rows(0, 3)) + expect(rows).toEqual([ + { id: 3, name: 'Charlie', age: 25 }, + { id: 1, name: 'Alice', age: 30 }, + { id: 2, name: 'Bob', age: 20 }, + ]) + }) + + it('should return data sorted by column "age"', async () => { + const rows = await awaitRows(sortableDf.rows(0, 4, 'age')) + expect(rows).toEqual([ + { id: 2, name: 'Bob', age: 20, __index__: 2 }, + { id: 4, name: 'Dani', age: 20, __index__: 3 }, + { id: 3, name: 'Charlie', age: 25, __index__: 0 }, + { id: 1, name: 'Alice', age: 30, __index__: 1 }, + ]) + }) + + it('should slice the sorted data correctly', async () => { + const rows = await awaitRows(sortableDf.rows(1, 3, 'id')) + expect(rows).toEqual([ + { id: 2, name: 'Bob', age: 20, __index__: 2 }, + { id: 3, name: 'Charlie', age: 25, __index__: 0 }, + ]) + }) + + it('returns self if already sortable', () => { + const sortableDf2 = sortableDataFrame(sortableDf) + expect(sortableDf2).toBe(sortableDf) + }) + + it('should throw for invalid orderBy field', () => { + expect(() => sortableDf.rows(0, 3, 'invalid')) + .toThrowError('Invalid orderBy field: invalid') + }) +}) + +describe('arrayDataFrame', () => { + const testData = [ + { id: 1, name: 'Alice', age: 30 }, + { id: 2, name: 'Bob', age: 25 }, + { id: 3, name: 'Charlie', age: 35 }, + ] + + it('should create a DataFrame with correct header and numRows', () => { + const df = arrayDataFrame(testData) + expect(df.header).toEqual(['id', 'name', 'age']) + expect(df.numRows).toBe(3) + }) + + it('should handle empty data array', () => { + const df = arrayDataFrame([]) + expect(df.header).toEqual([]) + expect(df.numRows).toBe(0) + expect(df.rows(0, 1)).resolves.toEqual([]) + }) + + it('should return correct rows for given range', async () => { + const df = arrayDataFrame(testData) + const rows = await df.rows(0, 2) + expect(rows).toEqual([ + { id: 1, name: 'Alice', age: 30 }, + { id: 2, name: 'Bob', age: 25 }, + ]) + }) + + it('should handle start index equal to end index', async () => { + const df = arrayDataFrame(testData) + const rows = await df.rows(1, 1) + expect(rows).toEqual([]) + }) + + it('should return all rows when end index exceeds array length', async () => { + const df = arrayDataFrame(testData) + const rows = await df.rows(0, 10) + expect(rows).toEqual(testData) + }) +}) diff --git a/packages/utils/test/rowCache.test.ts b/packages/utils/test/rowCache.test.ts new file mode 100644 index 00000000..2c5bbee3 --- /dev/null +++ b/packages/utils/test/rowCache.test.ts @@ -0,0 +1,94 @@ +import { describe, expect, it, vi } from 'vitest' +import { Row, awaitRows } from '../src/dataframe.js' +import { rowCache } from '../src/rowCache.js' + +// Mock DataFrame +function makeDf() { + return { + header: ['id'], + numRows: 10, + rows: vi.fn((start: number, end: number): Row[] => { + return new Array(end - start).fill(null) + .map((_, index) => ({ id: start + index })) + }), + } +} + +describe('rowCache', () => { + it('should fetch uncached rows', async () => { + const df = makeDf() + const dfCached = rowCache(df) + const rows = await awaitRows(dfCached.rows(0, 3)) + expect(rows).toEqual([{ id: 0 }, { id: 1 }, { id: 2 }]) + expect(df.rows).toHaveBeenCalledTimes(1) + expect(df.rows).toHaveBeenCalledWith(0, 3, undefined) + }) + + it('should return cached rows', async () => { + const df = makeDf() + const dfCached = rowCache(df) + + // Initial fetch to cache rows + const rowsPre = await awaitRows(dfCached.rows(3, 6)) + expect(rowsPre).toEqual([{ id: 3 }, { id: 4 }, { id: 5 }]) + expect(df.rows).toHaveBeenCalledTimes(1) + expect(df.rows).toHaveBeenCalledWith(3, 6, undefined) + + // Subsequent fetch should use cache + const rowsPost = await awaitRows(dfCached.rows(3, 6)) + expect(rowsPost).toEqual([{ id: 3 }, { id: 4 }, { id: 5 }]) + expect(df.rows).toHaveBeenCalledTimes(1) + }) + + it('should handle adjacent cached blocks', async () => { + const df = makeDf() + const dfCached = rowCache(df) + + // Cache first block + dfCached.rows(0, 3) + expect(df.rows).toHaveBeenCalledTimes(1) + // Cache adjacent block + dfCached.rows(3, 6) + expect(df.rows).toHaveBeenCalledTimes(2) + // Fetch combined block + const adjacentRows = await awaitRows(dfCached.rows(0, 6)) + + expect(adjacentRows).toEqual([ + { id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }, + ]) + expect(df.rows).toHaveBeenCalledTimes(2) + }) + + it('should handle a gap in cached blocks', async () => { + const df = makeDf() + const dfCached = rowCache(df) + + // Cache first block + dfCached.rows(0, 2) + // Cache second block + dfCached.rows(4, 6) + // Fetch combined block + const gapRows = await awaitRows(dfCached.rows(0, 6)) + expect(gapRows).toEqual([ + { id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }, + ]) + expect(df.rows).toHaveBeenCalledTimes(3) + expect(df.rows).toHaveBeenCalledWith(2, 4, undefined) + }) + + it('should handle overlapping cached blocks', async () => { + const df = makeDf() + const dfCached = rowCache(df) + + // Cache first block + dfCached.rows(6, 9) + + // Fetch overlapping block + const overlappingRows = await awaitRows(dfCached.rows(8, 11)) + expect(overlappingRows).toEqual([ + { id: 8 }, { id: 9 }, { id: 10 }, + ]) + expect(df.rows).toHaveBeenCalledTimes(2) + expect(df.rows).toHaveBeenCalledWith(9, 11, undefined) + }) +}) From ae9521c7bba5b3d2b6ba89f8e64e5afcba44cf53 Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Mon, 18 Nov 2024 13:58:21 +0100 Subject: [PATCH 02/11] fix types --- packages/utils/src/rowCache.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/rowCache.ts b/packages/utils/src/rowCache.ts index 516dc703..1f5ebbbc 100644 --- a/packages/utils/src/rowCache.ts +++ b/packages/utils/src/rowCache.ts @@ -7,7 +7,7 @@ import { AsyncRow, DataFrame, asyncRows } from './dataframe.js' */ export function rowCache(df: DataFrame): DataFrame { // Row cache is stored as a sorted array of RowGroups, per sort order - const caches: {[key: string]: AsyncRow[]} = {} + const caches: Record = {} let hits = 0 let misses = 0 @@ -16,7 +16,7 @@ export function rowCache(df: DataFrame): DataFrame { ...df, rows(start: number, end: number, orderBy?: string): AsyncRow[] { // Cache per sort order - const cache = caches[orderBy || ''] ||= new Array(df.numRows) + const cache = caches[orderBy ?? ''] ??= new Array(df.numRows) const n = hits + misses if (n && !(n % 10)) { console.log(`Cache hits: ${hits} / ${hits + misses} (${(100 * hits / (hits + misses)).toFixed(1)}%)`) From 3cb159a7731a677da4b65ad47e00e8bd3cc47358 Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Mon, 18 Nov 2024 14:15:28 +0100 Subject: [PATCH 03/11] fix types --- packages/utils/src/dataframe.ts | 23 ++++++++++++----------- packages/utils/src/index.ts | 2 +- packages/utils/src/workers/types.ts | 3 +-- packages/utils/test/dataframe.test.ts | 15 +++++++++------ 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/utils/src/dataframe.ts b/packages/utils/src/dataframe.ts index 48ae777e..5d4cca91 100644 --- a/packages/utils/src/dataframe.ts +++ b/packages/utils/src/dataframe.ts @@ -8,12 +8,12 @@ type WrappedPromise = Promise & { * The promise must be wrapped with `wrapPromise` so that HighTable can render * the state synchronously. */ -export type AsyncRow = Record> +export type AsyncRow = Record> /** * A row where each cell is a resolved value. */ -export type Row = Record +export type Row = Record /** * Streamable row data @@ -26,8 +26,8 @@ export interface DataFrame { sortable?: boolean } -export function resolvableRow(header: string[]): { [key: string]: ResolvablePromise } { - return Object.fromEntries(header.map(key => [key, resolvablePromise()])) +export function resolvableRow(header: string[]): Record> { + return Object.fromEntries(header.map(key => [key, resolvablePromise()])) } /** @@ -38,7 +38,7 @@ export function resolvableRow(header: string[]): { [key: string]: ResolvableProm export function asyncRows(rows: AsyncRow[] | Promise, numRows: number, header: string[]): AsyncRow[] { if (Array.isArray(rows)) return rows // Make grid of resolvable promises - const wrapped = new Array(numRows).fill(null).map(_ => resolvableRow(header)) + const wrapped = new Array(numRows).fill(null).map(() => resolvableRow(header)) rows.then(rows => { if (rows.length !== numRows) { console.warn(`Expected ${numRows} rows, got ${rows.length}`) @@ -49,11 +49,12 @@ export function asyncRows(rows: AsyncRow[] | Promise, numRows: number, he wrapped[i][key].resolve(row[key]) } } - }).catch(error => { + }).catch((error: unknown) => { + const rejected = error instanceof Error ? error : new Error(String(error)) // Reject all promises on error for (let i = 0; i < numRows; i++) { for (const key of header) { - wrapped[i][key].reject(error) + wrapped[i][key].reject(rejected) } } }) @@ -71,9 +72,9 @@ export function wrapPromise(promise: Promise | T): WrappedPromise { const wrapped: WrappedPromise = promise.then(resolved => { wrapped.resolved = resolved return resolved - }).catch(rejected => { - wrapped.rejected = rejected - throw rejected + }).catch((error: unknown) => { + wrapped.rejected = error instanceof Error ? error : new Error(String(error)) + throw error }) return wrapped } @@ -105,7 +106,7 @@ export function resolvablePromise(): ResolvablePromise { export function sortableDataFrame(data: DataFrame): DataFrame { if (data.sortable) return data // already sortable // Fetch all rows and add __index__ column - let all: Promise + let all: Promise | undefined return { ...data, rows(start: number, end: number, orderBy?: string): AsyncRow[] | Promise { diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 239674ea..fef55b72 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -5,5 +5,5 @@ export type { FileKey, FolderKey, ParsedKey, UrlKey } from './key.js' export { parquetDataFrame } from './tableProvider.js' export { asyncBufferFrom, cn } from './utils.js' export { parquetQueryWorker } from './workers/parquetWorkerClient.js' -export type { AsyncBufferFrom, Row } from './workers/types.js' +export type { AsyncBufferFrom } from './workers/types.js' diff --git a/packages/utils/src/workers/types.ts b/packages/utils/src/workers/types.ts index 6915993c..fabd377d 100644 --- a/packages/utils/src/workers/types.ts +++ b/packages/utils/src/workers/types.ts @@ -1,4 +1,5 @@ import { ColumnData, FileMetaData, ParquetReadOptions } from 'hyparquet' +import type { Row } from '../dataframe.js' // Serializable constructors for AsyncBuffers interface AsyncBufferFromFile { @@ -17,8 +18,6 @@ export interface ParquetReadWorkerOptions extends Omit ; interface Message { queryId: number diff --git a/packages/utils/test/dataframe.test.ts b/packages/utils/test/dataframe.test.ts index 2ed8ceba..986aa165 100644 --- a/packages/utils/test/dataframe.test.ts +++ b/packages/utils/test/dataframe.test.ts @@ -1,11 +1,13 @@ import { describe, expect, it } from 'vitest' import { - DataFrame, Row, arrayDataFrame, awaitRows, resolvablePromise, sortableDataFrame, wrapPromise, + AsyncRow, + DataFrame, + arrayDataFrame, awaitRows, resolvablePromise, sortableDataFrame, wrapPromise, } from '../src/dataframe.js' -function wrapObject(obj: Record): Row { +function wrapObject(obj: Record): AsyncRow { return Object.fromEntries( - Object.entries(obj).map(([key, value]) => [key, wrapPromise(value)]) + Object.entries(obj).map(([key, value]) => [key, wrapPromise(value)]), ) } @@ -41,7 +43,7 @@ describe('sortableDataFrame', () => { const dataFrame: DataFrame = { header: ['id', 'name', 'age'], numRows: data.length, - rows(start: number, end: number): Row[] { + rows(start: number, end: number): AsyncRow[] { // Return the slice of data between start and end indices return data.slice(start, end).map(wrapObject) }, @@ -110,11 +112,12 @@ describe('arrayDataFrame', () => { expect(df.numRows).toBe(3) }) - it('should handle empty data array', () => { + it('should handle empty data array', async () => { const df = arrayDataFrame([]) expect(df.header).toEqual([]) expect(df.numRows).toBe(0) - expect(df.rows(0, 1)).resolves.toEqual([]) + const rows = await df.rows(0, 1) + expect(rows).toEqual([]) }) it('should return correct rows for given range', async () => { From 9bfde6c8f75d77673a7601ce52826707cf7ee013 Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Mon, 18 Nov 2024 14:18:17 +0100 Subject: [PATCH 04/11] fix types --- packages/utils/test/rowCache.test.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/utils/test/rowCache.test.ts b/packages/utils/test/rowCache.test.ts index 2c5bbee3..4ef63cea 100644 --- a/packages/utils/test/rowCache.test.ts +++ b/packages/utils/test/rowCache.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it, vi } from 'vitest' -import { Row, awaitRows } from '../src/dataframe.js' +import { AsyncRow, awaitRows } from '../src/dataframe.js' import { rowCache } from '../src/rowCache.js' // Mock DataFrame @@ -7,9 +7,9 @@ function makeDf() { return { header: ['id'], numRows: 10, - rows: vi.fn((start: number, end: number): Row[] => { + rows: vi.fn((start: number, end: number): AsyncRow[] => { return new Array(end - start).fill(null) - .map((_, index) => ({ id: start + index })) + .map((_, index) => ({ id: Promise.resolve(start + index) })) }), } } @@ -45,10 +45,10 @@ describe('rowCache', () => { const dfCached = rowCache(df) // Cache first block - dfCached.rows(0, 3) + await dfCached.rows(0, 3) expect(df.rows).toHaveBeenCalledTimes(1) // Cache adjacent block - dfCached.rows(3, 6) + await dfCached.rows(3, 6) expect(df.rows).toHaveBeenCalledTimes(2) // Fetch combined block const adjacentRows = await awaitRows(dfCached.rows(0, 6)) @@ -64,9 +64,9 @@ describe('rowCache', () => { const dfCached = rowCache(df) // Cache first block - dfCached.rows(0, 2) + await dfCached.rows(0, 2) // Cache second block - dfCached.rows(4, 6) + await dfCached.rows(4, 6) // Fetch combined block const gapRows = await awaitRows(dfCached.rows(0, 6)) expect(gapRows).toEqual([ @@ -81,7 +81,7 @@ describe('rowCache', () => { const dfCached = rowCache(df) // Cache first block - dfCached.rows(6, 9) + await dfCached.rows(6, 9) // Fetch overlapping block const overlappingRows = await awaitRows(dfCached.rows(8, 11)) From f4794a98a4dea49e6d8ca2de0a693ee0ed31e345 Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Mon, 18 Nov 2024 14:20:56 +0100 Subject: [PATCH 05/11] refactor --- packages/utils/src/dataframe.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/utils/src/dataframe.ts b/packages/utils/src/dataframe.ts index 5d4cca91..c2bfbc5b 100644 --- a/packages/utils/src/dataframe.ts +++ b/packages/utils/src/dataframe.ts @@ -138,9 +138,9 @@ export function sortableDataFrame(data: DataFrame): DataFrame { /** * Await all promises in an AsyncRow and return resolved row. */ -export function awaitRow(row: AsyncRow): Promise { - return Promise.all(Object.values(row)) - .then(values => Object.fromEntries(Object.keys(row).map((key, i) => [key, values[i]]))) +export async function awaitRow(row: AsyncRow): Promise { + const values = await Promise.all(Object.values(row)) + return Object.fromEntries(Object.keys(row).map((key, i) => [key, values[i]])) } /** From 3f4d9c886d8b6a08629f78aab5db64551245528b Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Mon, 18 Nov 2024 15:27:39 +0100 Subject: [PATCH 06/11] use Promise.withResolvers --- packages/utils/eslint.config.js | 4 ++-- packages/utils/src/dataframe.ts | 23 ++++++++++++----------- packages/utils/tsconfig.json | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/packages/utils/eslint.config.js b/packages/utils/eslint.config.js index 7e636c78..222b3993 100644 --- a/packages/utils/eslint.config.js +++ b/packages/utils/eslint.config.js @@ -9,7 +9,7 @@ export default tseslint.config( extends: [js.configs.recommended, ...tseslint.configs.strictTypeChecked, ...tseslint.configs.stylisticTypeChecked], files: ['**/*.{ts,js}'], languageOptions: { - ecmaVersion: 2020, + ecmaVersion: globals.esnext, globals: globals.browser, parserOptions: { project: ['./tsconfig.json', './tsconfig.eslint.json'], @@ -26,7 +26,7 @@ export default tseslint.config( { files: ['test/**/*.{ts}', '*.{js,ts}'], languageOptions: { - ecmaVersion: 2020, + ecmaVersion: globals.esnext, globals: { ...globals.node, ...globals.browser, diff --git a/packages/utils/src/dataframe.ts b/packages/utils/src/dataframe.ts index c2bfbc5b..41ed43ba 100644 --- a/packages/utils/src/dataframe.ts +++ b/packages/utils/src/dataframe.ts @@ -85,18 +85,19 @@ export type ResolvablePromise = Promise & { } /** - * Create a promise that can be resolved or rejected later. + * Create a promise that can be resolved or rejected later using the resolve and reject methods. + * It's also a wrapped promise, with resolved and rejected properties. */ -export function resolvablePromise(): ResolvablePromise { - let resolve: (value: T) => void - let reject: (error: Error) => void - const promise = wrapPromise(new Promise((res, rej) => { - resolve = res - reject = rej - })) as ResolvablePromise - promise.resolve = resolve! - promise.reject = reject! - return promise +export function resolvablePromise(): ResolvablePromise & WrappedPromise { + const promise = Promise.withResolvers() + const wrapped = Object.assign( + wrapPromise(promise.promise), + { + resolve: promise.resolve, + reject : promise.reject, + }, + ) + return wrapped } /** diff --git a/packages/utils/tsconfig.json b/packages/utils/tsconfig.json index c3e5ca28..97c94b26 100644 --- a/packages/utils/tsconfig.json +++ b/packages/utils/tsconfig.json @@ -3,7 +3,7 @@ "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.tsbuildinfo", "target": "ES2020", "useDefineForClassFields": true, - "lib": ["ES2020", "DOM", "DOM.Iterable"], + "lib": ["ESNext", "DOM", "DOM.Iterable"], "module": "NodeNext", "skipLibCheck": true, From adde308e5b4399cd1bd5aeb3c100f8f1a3f89e7e Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Mon, 18 Nov 2024 15:38:03 +0100 Subject: [PATCH 07/11] fix types, and export functions and types --- packages/utils/src/index.ts | 3 +++ packages/utils/src/tableProvider.ts | 1 - packages/utils/src/workers/parquetWorkerClient.ts | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index fef55b72..c92b5cab 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,7 +1,10 @@ +export { arrayDataFrame, asyncRows, awaitRow, awaitRows, resolvablePromise, resolvableRow, sortableDataFrame, wrapPromise } from './dataframe.js' +export type { AsyncRow, DataFrame, ResolvablePromise, Row } from './dataframe.js' export { contentTypes, formatFileSize, getFileDate, getFileDateShort, getFileSize, imageTypes, listFiles, parseFileSize } from './files.js' export type { FileMetadata } from './files.js' export { parseKey } from './key.js' export type { FileKey, FolderKey, ParsedKey, UrlKey } from './key.js' +export { rowCache } from './rowCache.js' export { parquetDataFrame } from './tableProvider.js' export { asyncBufferFrom, cn } from './utils.js' export { parquetQueryWorker } from './workers/parquetWorkerClient.js' diff --git a/packages/utils/src/tableProvider.ts b/packages/utils/src/tableProvider.ts index d67742ea..24faf9bd 100644 --- a/packages/utils/src/tableProvider.ts +++ b/packages/utils/src/tableProvider.ts @@ -26,7 +26,6 @@ export function parquetDataFrame(from: AsyncBufferFrom, metadata: FileMetaData): for (let i = rowStart; i < rowEnd; i++) { data[i] = Object.fromEntries( header.map((key) => [key, resolvablePromise()]), - // ^ for type: resolvableRow uses any, not unknown ) } parquetQueryWorker({ from, metadata, rowStart, rowEnd }) diff --git a/packages/utils/src/workers/parquetWorkerClient.ts b/packages/utils/src/workers/parquetWorkerClient.ts index 66053ced..29998cb5 100644 --- a/packages/utils/src/workers/parquetWorkerClient.ts +++ b/packages/utils/src/workers/parquetWorkerClient.ts @@ -2,7 +2,8 @@ import ParquetWorker from './parquetWorker?worker&inline' /// ^ the worker is bundled with the main thread code (inline) which is easier for users to import /// (no need to copy the worker file to the right place) import { ColumnData } from 'hyparquet' -import type { ParquetMessage, ParquetReadWorkerOptions, ParquetSortIndexOptions, Row, SortParquetMessage } from './types.js' +import type { Row } from '../dataframe.js' +import type { ParquetMessage, ParquetReadWorkerOptions, ParquetSortIndexOptions, SortParquetMessage } from './types.js' let worker: Worker | undefined let nextQueryId = 0 From dc60f8670c4b82f814ca10387f28b1ec86741783 Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Mon, 18 Nov 2024 15:41:50 +0100 Subject: [PATCH 08/11] disable typescript for unknowns comparison --- packages/utils/src/dataframe.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/dataframe.ts b/packages/utils/src/dataframe.ts index 41ed43ba..7c6c5042 100644 --- a/packages/utils/src/dataframe.ts +++ b/packages/utils/src/dataframe.ts @@ -122,8 +122,13 @@ export function sortableDataFrame(data: DataFrame): DataFrame { } const sorted = all.then(all => { return all.sort((a, b) => { - if (a[orderBy] < b[orderBy]) return -1 - if (a[orderBy] > b[orderBy]) return 1 + /// TODO(SL): rewrite the function, handling every case + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const valueA: any = a[orderBy] + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const valueB: any = b[orderBy] + if (valueA < valueB) return -1 + if (valueA > valueB) return 1 return 0 }).slice(start, end) }) From 2862db1bd5966d8944d3e5bb736499165500d98b Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Mon, 18 Nov 2024 15:57:54 +0100 Subject: [PATCH 09/11] use stable node version --- .github/workflows/ci_packages_utils.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/ci_packages_utils.yml b/.github/workflows/ci_packages_utils.yml index 244e9448..e4c1cc29 100644 --- a/.github/workflows/ci_packages_utils.yml +++ b/.github/workflows/ci_packages_utils.yml @@ -14,6 +14,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run lint @@ -21,6 +24,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: tsc @@ -28,6 +34,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run coverage @@ -35,5 +44,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build From 34db646461d3994240929f763f3eb801afb4fc98 Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Mon, 18 Nov 2024 16:03:44 +0100 Subject: [PATCH 10/11] import from utils, not hightable --- .github/workflows/ci_apps_cli.yml | 12 ++++++++++++ .github/workflows/ci_apps_hightable_demo.yml | 12 ++++++++++++ .github/workflows/ci_apps_hyparquet_demo.yml | 12 ++++++++++++ .github/workflows/ci_packages_components.yml | 12 ++++++++++++ packages/components/src/viewers/ParquetView.tsx | 4 ++-- 5 files changed, 50 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci_apps_cli.yml b/.github/workflows/ci_apps_cli.yml index aa3e0d30..faa57375 100644 --- a/.github/workflows/ci_apps_cli.yml +++ b/.github/workflows/ci_apps_cli.yml @@ -15,6 +15,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils @@ -29,6 +32,9 @@ jobs: working-directory: ./ steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils @@ -41,6 +47,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils @@ -55,6 +64,9 @@ jobs: working-directory: ./ steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils diff --git a/.github/workflows/ci_apps_hightable_demo.yml b/.github/workflows/ci_apps_hightable_demo.yml index 3703d078..01d7faa1 100644 --- a/.github/workflows/ci_apps_hightable_demo.yml +++ b/.github/workflows/ci_apps_hightable_demo.yml @@ -14,6 +14,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run lint @@ -21,6 +24,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: tsc @@ -28,6 +34,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run coverage @@ -35,5 +44,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build \ No newline at end of file diff --git a/.github/workflows/ci_apps_hyparquet_demo.yml b/.github/workflows/ci_apps_hyparquet_demo.yml index fa0f3ff1..e8bc73c7 100644 --- a/.github/workflows/ci_apps_hyparquet_demo.yml +++ b/.github/workflows/ci_apps_hyparquet_demo.yml @@ -14,6 +14,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils @@ -23,6 +26,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils @@ -32,6 +38,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils @@ -41,6 +50,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils diff --git a/.github/workflows/ci_packages_components.yml b/.github/workflows/ci_packages_components.yml index 5a44a632..0cd317dd 100644 --- a/.github/workflows/ci_packages_components.yml +++ b/.github/workflows/ci_packages_components.yml @@ -14,6 +14,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils @@ -23,6 +26,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils @@ -32,6 +38,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils @@ -41,6 +50,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 - run: npm i - run: npm run build working-directory: ./packages/utils diff --git a/packages/components/src/viewers/ParquetView.tsx b/packages/components/src/viewers/ParquetView.tsx index c9270ca2..778a5ee4 100644 --- a/packages/components/src/viewers/ParquetView.tsx +++ b/packages/components/src/viewers/ParquetView.tsx @@ -1,5 +1,5 @@ -import { FileKey, UrlKey, parquetDataFrame } from '@hyparam/utils' -import HighTable, { DataFrame, rowCache } from 'hightable' +import { DataFrame, FileKey, UrlKey, parquetDataFrame, rowCache } from '@hyparam/utils' +import HighTable from 'hightable' import { asyncBufferFromUrl, parquetMetadataAsync } from 'hyparquet' import React, { useCallback, useEffect, useState } from 'react' import { Spinner } from '../Layout.js' From 61ab9adc5ac51972a027829a589584380e4945a5 Mon Sep 17 00:00:00 2001 From: Sylvain Lesage Date: Mon, 18 Nov 2024 16:06:10 +0100 Subject: [PATCH 11/11] import from utils, not hightable --- apps/hyparquet-demo/src/App.tsx | 3 +-- apps/hyparquet-demo/src/Page.tsx | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/hyparquet-demo/src/App.tsx b/apps/hyparquet-demo/src/App.tsx index 4b500baa..80c28b98 100644 --- a/apps/hyparquet-demo/src/App.tsx +++ b/apps/hyparquet-demo/src/App.tsx @@ -2,8 +2,7 @@ import { ReactNode } from 'react' import Page, { PageProps } from './Page.js' import Welcome from './Welcome.js' -import { AsyncBufferFrom, Row, asyncBufferFrom, parquetQueryWorker } from '@hyparam/utils' -import { DataFrame, rowCache } from 'hightable' +import { AsyncBufferFrom, DataFrame, Row, asyncBufferFrom, parquetQueryWorker, rowCache } from '@hyparam/utils' import { FileMetaData, byteLengthFromUrl, parquetMetadataAsync, parquetSchema } from 'hyparquet' import { useCallback, useEffect, useState } from 'react' import Dropzone from './Dropzone.js' diff --git a/apps/hyparquet-demo/src/Page.tsx b/apps/hyparquet-demo/src/Page.tsx index 1d3735e7..60cff72a 100644 --- a/apps/hyparquet-demo/src/Page.tsx +++ b/apps/hyparquet-demo/src/Page.tsx @@ -1,4 +1,5 @@ -import HighTable, { DataFrame } from 'hightable' +import { DataFrame } from '@hyparam/utils' +import HighTable from 'hightable' import { FileMetaData } from 'hyparquet' import { ReactNode, useState } from 'react' import Dropdown from './Dropdown.js'